


'''simple docstring'''





import math
from typing import Dict, Iterable, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    is_torch_available,
    is_torch_tensor,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_torch_available():
    import torch

if is_vision_available():
    import PIL


__lowerCamelCase												=						logging.get_logger(__name__)


def        a__     (					UpperCamelCase_						:      np.ndarray,   UpperCamelCase_						:      Union[int, Iterable[int]],   UpperCamelCase_						:      bool,   UpperCamelCase_						:      int						):
   def constraint_to_multiple_of(UpperCamelCase_						:      int,   UpperCamelCase_						:      List[Any],   UpperCamelCase_						:      int=0,   UpperCamelCase_						:      Tuple=None						):
      UpperCAmelCase__	:List[Any]      =      round(val / multiple						) * multiple

      if max_val is not None and x > max_val:
         UpperCAmelCase__	:Union[str, Any]      =      math.floor(val / multiple						) * multiple

      if x < min_val:
         UpperCAmelCase__	:Dict      =      math.ceil(val / multiple						) * multiple

      return x

   UpperCAmelCase__	:List[Any]      =      (output_size, output_size) if isinstance(UpperCamelCase_,   UpperCamelCase_						) else output_size

   UpperCAmelCase__   ,						UpperCAmelCase__	:List[str]      =      get_image_size(UpperCamelCase_						)
   UpperCAmelCase__   ,						UpperCAmelCase__	:List[str]      =      output_size

   # determine new height and width
   UpperCAmelCase__	:Any      =      output_height / input_height
   UpperCAmelCase__	:List[Any]      =      output_width / input_width

   if keep_aspect_ratio:
      # scale as little as possible
      if abs(1 - scale_width						) < abs(1 - scale_height						):
         # fit width
         UpperCAmelCase__	:Optional[int]      =      scale_width
      else:
         # fit height
         UpperCAmelCase__	:int      =      scale_height

   UpperCAmelCase__	:Tuple      =      constraint_to_multiple_of(scale_height * input_height,   multiple=UpperCamelCase_						)
   UpperCAmelCase__	:int      =      constraint_to_multiple_of(scale_width * input_width,   multiple=UpperCamelCase_						)

   return (new_height, new_width)




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					["pixel_values"]


 def __init__(      self							:	Tuple			,       __lowerCamelCase							:	bool = True			,       __lowerCamelCase							:	Dict[str, int] = None			,       __lowerCamelCase							:	PILImageResampling = PILImageResampling.BILINEAR			,       __lowerCamelCase							:	bool = False			,       __lowerCamelCase							:	int = 1			,       __lowerCamelCase							:	bool = True			,       __lowerCamelCase							:	Union[int, float] = 1 / 2_5_5			,       __lowerCamelCase							:	bool = True			,       __lowerCamelCase							:	Optional[Union[float, List[float]]] = None			,       __lowerCamelCase							:	Optional[Union[float, List[float]]] = None			,       **__lowerCamelCase							:	int			,       ):
    super().__init__(**__lowerCamelCase )
    UpperCAmelCase__	:Any      =      size if size is not None else {'''height''': 3_8_4, '''width''': 3_8_4}
    UpperCAmelCase__	:Optional[int]      =      get_size_dict(__lowerCamelCase )
    UpperCAmelCase__	:Optional[Any]      =      do_resize
    UpperCAmelCase__	:Any      =      size
    UpperCAmelCase__	:List[Any]      =      keep_aspect_ratio
    UpperCAmelCase__	:Dict      =      ensure_multiple_of
    UpperCAmelCase__	:Any      =      resample
    UpperCAmelCase__	:int      =      do_rescale
    UpperCAmelCase__	:List[Any]      =      rescale_factor
    UpperCAmelCase__	:Optional[int]      =      do_normalize
    UpperCAmelCase__	:int      =      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
    UpperCAmelCase__	:List[str]      =      image_std if image_std is not None else IMAGENET_STANDARD_STD


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	np.ndarray			,       __lowerCamelCase							:	Dict[str, int]			,       __lowerCamelCase							:	bool = False			,       __lowerCamelCase							:	int = 1			,       __lowerCamelCase							:	PILImageResampling = PILImageResampling.BICUBIC			,       __lowerCamelCase							:	Optional[Union[str, ChannelDimension]] = None			,       **__lowerCamelCase							:	Optional[int]			,       ):
    UpperCAmelCase__	:List[Any]      =      get_size_dict(__lowerCamelCase )
    if "height" not in size or "width" not in size:
       raise ValueError(f'''The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}''' )
    UpperCAmelCase__	:List[str]      =      get_resize_output_image_size(
        __lowerCamelCase			,       output_size=(size['''height'''], size['''width'''])			,       keep_aspect_ratio=__lowerCamelCase			,       multiple=__lowerCamelCase			,       )
    return resize(__lowerCamelCase			,       size=__lowerCamelCase			,       resample=__lowerCamelCase			,       data_format=__lowerCamelCase			,       **__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	np.ndarray			,       __lowerCamelCase							:	Union[int, float]			,       __lowerCamelCase							:	Optional[Union[str, ChannelDimension]] = None			,       **__lowerCamelCase							:	Dict			,       ):
    return rescale(__lowerCamelCase			,       scale=__lowerCamelCase			,       data_format=__lowerCamelCase			,       **__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	np.ndarray			,       __lowerCamelCase							:	Union[float, List[float]]			,       __lowerCamelCase							:	Union[float, List[float]]			,       __lowerCamelCase							:	Optional[Union[str, ChannelDimension]] = None			,       **__lowerCamelCase							:	str			,       ):
    return normalize(__lowerCamelCase			,       mean=__lowerCamelCase			,       std=__lowerCamelCase			,       data_format=__lowerCamelCase			,       **__lowerCamelCase )


 def        __SCREAMING_SNAKE_CASE					(      self							:	Optional[int]			,       __lowerCamelCase							:	ImageInput			,       __lowerCamelCase							:	bool = None			,       __lowerCamelCase							:	int = None			,       __lowerCamelCase							:	bool = None			,       __lowerCamelCase							:	int = None			,       __lowerCamelCase							:	PILImageResampling = None			,       __lowerCamelCase							:	bool = None			,       __lowerCamelCase							:	float = None			,       __lowerCamelCase							:	bool = None			,       __lowerCamelCase							:	Optional[Union[float, List[float]]] = None			,       __lowerCamelCase							:	Optional[Union[float, List[float]]] = None			,       __lowerCamelCase							:	Optional[Union[str, TensorType]] = None			,       __lowerCamelCase							:	ChannelDimension = ChannelDimension.FIRST			,       **__lowerCamelCase							:	List[Any]			,       ):
    UpperCAmelCase__	:Any      =      do_resize if do_resize is not None else self.do_resize
    UpperCAmelCase__	:Dict      =      size if size is not None else self.size
    UpperCAmelCase__	:Union[str, Any]      =      get_size_dict(__lowerCamelCase )
    UpperCAmelCase__	:Union[str, Any]      =      keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
    UpperCAmelCase__	:Optional[int]      =      ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
    UpperCAmelCase__	:List[Any]      =      resample if resample is not None else self.resample
    UpperCAmelCase__	:Any      =      do_rescale if do_rescale is not None else self.do_rescale
    UpperCAmelCase__	:List[Any]      =      rescale_factor if rescale_factor is not None else self.rescale_factor
    UpperCAmelCase__	:Optional[Any]      =      do_normalize if do_normalize is not None else self.do_normalize
    UpperCAmelCase__	:Optional[Any]      =      image_mean if image_mean is not None else self.image_mean
    UpperCAmelCase__	:Any      =      image_std if image_std is not None else self.image_std

    UpperCAmelCase__	:Any      =      make_list_of_images(__lowerCamelCase )

    if not valid_images(__lowerCamelCase ):
       raise ValueError(
           '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
           '''torch.Tensor, tf.Tensor or jax.ndarray.''' )

    if do_resize and size is None or resample is None:
       raise ValueError('''Size and resample must be specified if do_resize is True.''' )

    if do_rescale and rescale_factor is None:
       raise ValueError('''Rescale factor must be specified if do_rescale is True.''' )

    if do_normalize and (image_mean is None or image_std is None):
       raise ValueError('''Image mean and std must be specified if do_normalize is True.''' )

    # All transformations expect numpy arrays.
    UpperCAmelCase__	:Tuple      =      [to_numpy_array(__lowerCamelCase ) for image in images]

    if do_resize:
       UpperCAmelCase__	:Tuple      =      [self.resize(image=__lowerCamelCase			,       size=__lowerCamelCase			,       resample=__lowerCamelCase ) for image in images]

    if do_rescale:
       UpperCAmelCase__	:List[Any]      =      [self.rescale(image=__lowerCamelCase			,       scale=__lowerCamelCase ) for image in images]

    if do_normalize:
       UpperCAmelCase__	:List[str]      =      [self.normalize(image=__lowerCamelCase			,       mean=__lowerCamelCase			,       std=__lowerCamelCase ) for image in images]

    UpperCAmelCase__	:str      =      [to_channel_dimension_format(__lowerCamelCase			,       __lowerCamelCase ) for image in images]

    UpperCAmelCase__	:List[str]      =      {'''pixel_values''': images}
    return BatchFeature(data=__lowerCamelCase			,       tensor_type=__lowerCamelCase )





 def        __SCREAMING_SNAKE_CASE					(      self							:	Dict			,       __lowerCamelCase							:	Dict			,       __lowerCamelCase							:	List[Tuple] = None ):
    UpperCAmelCase__	:Dict      =      outputs.logits

    # Resize logits and compute semantic segmentation maps
    if target_sizes is not None:
       if len(__lowerCamelCase ) != len(__lowerCamelCase ):
          raise ValueError(
              '''Make sure that you pass in as many target sizes as the batch dimension of the logits''' )

       if is_torch_tensor(__lowerCamelCase ):
          UpperCAmelCase__	:int      =      target_sizes.numpy()

       UpperCAmelCase__	:Tuple      =      []

       for idx in range(len(__lowerCamelCase ) ):
          UpperCAmelCase__	:Optional[int]      =      torch.nn.functional.interpolate(
              logits[idx].unsqueeze(dim=0 )			,       size=target_sizes[idx]			,       mode='''bilinear'''			,       align_corners=__lowerCamelCase )
          UpperCAmelCase__	:Optional[Any]      =      resized_logits[0].argmax(dim=0 )
          semantic_segmentation.append(__lowerCamelCase )
    else:
       UpperCAmelCase__	:Any      =      logits.argmax(dim=1 )
       UpperCAmelCase__	:List[str]      =      [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0] )]

    return semantic_segmentation







'''simple docstring'''





from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCamelCase												=						logging.get_logger(__name__)

__lowerCamelCase												=						{
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}




class 				UpperCAmelCase			(			_snake_case						):
 UpperCAmelCase				=					"autoformer"
 UpperCAmelCase				=					{
     "hidden_size": "d_model",
     "num_attention_heads": "encoder_attention_heads",
     "num_hidden_layers": "encoder_layers",
 }


 def __init__(      self							:	List[str]			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	str = "student_t"			,       __lowerCamelCase							:	str = "nll"			,       __lowerCamelCase							:	int = 1			,       __lowerCamelCase							:	List[int] = [1, 2, 3, 4, 5, 6, 7]			,       __lowerCamelCase							:	bool = True			,       __lowerCamelCase							:	int = 0			,       __lowerCamelCase							:	int = 0			,       __lowerCamelCase							:	int = 0			,       __lowerCamelCase							:	int = 0			,       __lowerCamelCase							:	Optional[List[int]] = None			,       __lowerCamelCase							:	Optional[List[int]] = None			,       __lowerCamelCase							:	int = 6_4			,       __lowerCamelCase							:	int = 2			,       __lowerCamelCase							:	int = 2			,       __lowerCamelCase							:	int = 2			,       __lowerCamelCase							:	int = 2			,       __lowerCamelCase							:	int = 3_2			,       __lowerCamelCase							:	int = 3_2			,       __lowerCamelCase							:	str = "gelu"			,       __lowerCamelCase							:	float = 0.1			,       __lowerCamelCase							:	float = 0.1			,       __lowerCamelCase							:	float = 0.1			,       __lowerCamelCase							:	float = 0.1			,       __lowerCamelCase							:	float = 0.1			,       __lowerCamelCase							:	int = 1_0_0			,       __lowerCamelCase							:	float = 0.02			,       __lowerCamelCase							:	bool = True			,       __lowerCamelCase							:	Optional[int]=True			,       __lowerCamelCase							:	int = 1_0			,       __lowerCamelCase							:	int = 2_5			,       __lowerCamelCase							:	int = 3			,       **__lowerCamelCase							:	int			,       ):
    # time series specific configuration
    UpperCAmelCase__	:Any      =      prediction_length
    UpperCAmelCase__	:Optional[int]      =      context_length if context_length is not None else prediction_length
    UpperCAmelCase__	:int      =      distribution_output
    UpperCAmelCase__	:str      =      loss
    UpperCAmelCase__	:Tuple      =      input_size
    UpperCAmelCase__	:Optional[int]      =      num_time_features
    UpperCAmelCase__	:List[Any]      =      lags_sequence
    UpperCAmelCase__	:Any      =      scaling
    UpperCAmelCase__	:Union[str, Any]      =      num_dynamic_real_features
    UpperCAmelCase__	:Dict      =      num_static_real_features
    UpperCAmelCase__	:Optional[int]      =      num_static_categorical_features
    if cardinality is not None and num_static_categorical_features > 0:
       if len(__lowerCamelCase ) != num_static_categorical_features:
          raise ValueError(
              '''The cardinality should be a list of the same length as `num_static_categorical_features`''' )
       UpperCAmelCase__	:Any      =      cardinality
    else:
       UpperCAmelCase__	:Dict      =      [0]
    if embedding_dimension is not None and num_static_categorical_features > 0:
       if len(__lowerCamelCase ) != num_static_categorical_features:
          raise ValueError(
              '''The embedding dimension should be a list of the same length as `num_static_categorical_features`''' )
       UpperCAmelCase__	:List[str]      =      embedding_dimension
    else:
       UpperCAmelCase__	:int      =      [min(5_0			,       (cat + 1) // 2 ) for cat in self.cardinality]
    UpperCAmelCase__	:Optional[Any]      =      num_parallel_samples

    # Transformer architecture configuration
    UpperCAmelCase__	:Optional[Any]      =      input_size * len(self.lags_sequence ) + self._number_of_features
    UpperCAmelCase__	:Optional[Any]      =      d_model
    UpperCAmelCase__	:Any      =      encoder_attention_heads
    UpperCAmelCase__	:Any      =      decoder_attention_heads
    UpperCAmelCase__	:int      =      encoder_ffn_dim
    UpperCAmelCase__	:Union[str, Any]      =      decoder_ffn_dim
    UpperCAmelCase__	:Optional[Any]      =      encoder_layers
    UpperCAmelCase__	:str      =      decoder_layers

    UpperCAmelCase__	:Optional[Any]      =      dropout
    UpperCAmelCase__	:str      =      attention_dropout
    UpperCAmelCase__	:int      =      activation_dropout
    UpperCAmelCase__	:Optional[Any]      =      encoder_layerdrop
    UpperCAmelCase__	:Dict      =      decoder_layerdrop

    UpperCAmelCase__	:Union[str, Any]      =      activation_function
    UpperCAmelCase__	:List[Any]      =      init_std

    UpperCAmelCase__	:List[str]      =      use_cache

    # Autoformer
    UpperCAmelCase__	:List[str]      =      label_length
    UpperCAmelCase__	:Dict      =      moving_average
    UpperCAmelCase__	:str      =      autocorrelation_factor

    super().__init__(is_encoder_decoder=__lowerCamelCase			,       **__lowerCamelCase )





 @property
 def        __SCREAMING_SNAKE_CASE					(      self							:	int ):
    return (
        sum(self.embedding_dimension )
        + self.num_dynamic_real_features
        + self.num_time_features
        + self.num_static_real_features
        + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
    )



