


"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

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


__UpperCAmelCase    				=      logging.get_logger(__name__)


if is_vision_available():
   import PIL

class  lowercase_  ( a_						):
      __magic_name__     :							List[str]          =				["""pixel_values"""]







      def __init__(      self :							Tuple		,  _lowercase :							bool = True		,  _lowercase :							Dict[str, int] = None		,  _lowercase :							PILImageResampling = PILImageResampling.BILINEAR		,  _lowercase :							bool = True		,  _lowercase :							bool = False		,  _lowercase :							bool = True		,  _lowercase :							bool = True		,  _lowercase :							Union[int, float] = 1 / 2_5_5		,  _lowercase :							bool = True		,  _lowercase :							Optional[Union[float, List[float]]] = None		,  _lowercase :							Optional[Union[float, List[float]]] = None		,  **_lowercase :							Optional[int]		,  ):
        super().__init__(**_lowercase    )

        lowerCAmelCase__    :    List[str]								=					size if size is not None else {"height": 2_5_6_0, "width": 1_9_2_0}
        if isinstance(_lowercase		,  (tuple, list)    ):
          # The previous feature extractor size parameter was in (width, height) format
          lowerCAmelCase__    :    Dict								=					size[::-1]
        lowerCAmelCase__    :    List[str]								=					get_size_dict(_lowercase    )

        lowerCAmelCase__    :    List[str]								=					do_resize
        lowerCAmelCase__    :    Tuple								=					size
        lowerCAmelCase__    :    List[str]								=					resample
        lowerCAmelCase__    :    Tuple								=					do_thumbnail
        lowerCAmelCase__    :    Union[str, Any]								=					do_align_long_axis
        lowerCAmelCase__    :    Optional[int]								=					do_pad
        lowerCAmelCase__    :    Tuple								=					do_rescale
        lowerCAmelCase__    :    Dict								=					rescale_factor
        lowerCAmelCase__    :    Tuple								=					do_normalize
        lowerCAmelCase__    :    Any								=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
        lowerCAmelCase__    :    Optional[int]								=					image_std if image_std is not None else IMAGENET_STANDARD_STD







      def 				_lowerCAmelCase       (      self :							Union[str, Any]		,  _lowercase :							np.ndarray		,  _lowercase :							Dict[str, int]		,  _lowercase :							Optional[Union[str, ChannelDimension]] = None    ):
        lowerCAmelCase__				,		lowerCAmelCase__    :    Optional[int]								=					get_image_size(_lowercase    )
        lowerCAmelCase__				,		lowerCAmelCase__    :    Tuple								=					size["height"], size["width"]

        if (output_width < output_height and input_width > input_height) or (
            output_width > output_height and input_width < input_height
        ):
          lowerCAmelCase__    :    Optional[Any]								=					np.rotaa(_lowercase		,  3    )

        if data_format is not None:
          lowerCAmelCase__    :    List[Any]								=					to_channel_dimension_format(_lowercase		,  _lowercase    )

        return image







      def 				_lowerCAmelCase       (      self :							int		,  _lowercase :							np.ndarray		,  _lowercase :							Dict[str, int]		,  _lowercase :							bool = False		,  _lowercase :							Optional[Union[str, ChannelDimension]] = None		,  ):
        lowerCAmelCase__				,		lowerCAmelCase__    :    Any								=					size["height"], size["width"]
        lowerCAmelCase__				,		lowerCAmelCase__    :    Union[str, Any]								=					get_image_size(_lowercase    )

        lowerCAmelCase__    :    List[Any]								=					output_width - input_width
        lowerCAmelCase__    :    Tuple								=					output_height - input_height

        if random_padding:
          lowerCAmelCase__    :    int								=					np.random.randint(low=0		,  high=delta_height + 1    )
          lowerCAmelCase__    :    Tuple								=					np.random.randint(low=0		,  high=delta_width + 1    )
        else:
          lowerCAmelCase__    :    Optional[int]								=					delta_height // 2
          lowerCAmelCase__    :    Tuple								=					delta_width // 2

        lowerCAmelCase__    :    Dict								=					delta_height - pad_top
        lowerCAmelCase__    :    int								=					delta_width - pad_left

        lowerCAmelCase__    :    Union[str, Any]								=					((pad_top, pad_bottom), (pad_left, pad_right))
        return pad(_lowercase		,  _lowercase		,  data_format=_lowercase    )







      def 				_lowerCAmelCase       (      self :							Optional[int]		,  *_lowercase :							Optional[Any]		,  **_lowercase :							Optional[Any]    ):
        logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead."    )
        return self.pad_image(*_lowercase		,  **_lowercase    )







      def 				_lowerCAmelCase       (      self :							Any		,  _lowercase :							np.ndarray		,  _lowercase :							Dict[str, int]		,  _lowercase :							PILImageResampling = PILImageResampling.BICUBIC		,  _lowercase :							Optional[Union[str, ChannelDimension]] = None		,  **_lowercase :							Dict		,  ):
        lowerCAmelCase__				,		lowerCAmelCase__    :    int								=					get_image_size(_lowercase    )
        lowerCAmelCase__				,		lowerCAmelCase__    :    Union[str, Any]								=					size["height"], size["width"]

        # We always resize to the smallest of either the input or output size.
        lowerCAmelCase__    :    Any								=					min(_lowercase		,  _lowercase    )
        lowerCAmelCase__    :    Dict								=					min(_lowercase		,  _lowercase    )

        if height == input_height and width == input_width:
          return image

        if input_height > input_width:
          lowerCAmelCase__    :    Any								=					int(input_width * height / input_height    )
        elif input_width > input_height:
          lowerCAmelCase__    :    List[str]								=					int(input_height * width / input_width    )

        return resize(
            _lowercase		,  size=(height, width)		,  resample=_lowercase		,  reducing_gap=2.0		,  data_format=_lowercase		,  **_lowercase    )







      def 				_lowerCAmelCase       (      self :							Optional[Any]		,  _lowercase :							np.ndarray		,  _lowercase :							Dict[str, int]		,  _lowercase :							PILImageResampling = PILImageResampling.BICUBIC		,  _lowercase :							Optional[Union[str, ChannelDimension]] = None		,  **_lowercase :							int		,  ):
        lowerCAmelCase__    :    str								=					get_size_dict(_lowercase    )
        lowerCAmelCase__    :    List[Any]								=					min(size["height"]		,  size["width"]    )
        lowerCAmelCase__    :    List[str]								=					get_resize_output_image_size(_lowercase		,  size=_lowercase		,  default_to_square=_lowercase    )
        lowerCAmelCase__    :    Optional[Any]								=					resize(_lowercase		,  size=_lowercase		,  resample=_lowercase		,  data_format=_lowercase		,  **_lowercase    )
        return resized_image







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







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







      def 				_lowerCAmelCase       (      self :							Tuple		,  _lowercase :							ImageInput		,  _lowercase :							bool = None		,  _lowercase :							Dict[str, int] = None		,  _lowercase :							PILImageResampling = None		,  _lowercase :							bool = None		,  _lowercase :							bool = None		,  _lowercase :							bool = None		,  _lowercase :							bool = False		,  _lowercase :							bool = None		,  _lowercase :							float = None		,  _lowercase :							bool = None		,  _lowercase :							Optional[Union[float, List[float]]] = None		,  _lowercase :							Optional[Union[float, List[float]]] = None		,  _lowercase :							Optional[Union[str, TensorType]] = None		,  _lowercase :							Optional[ChannelDimension] = ChannelDimension.FIRST		,  **_lowercase :							Dict		,  ):
        lowerCAmelCase__    :    Union[str, Any]								=					do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase__    :    str								=					size if size is not None else self.size
        if isinstance(_lowercase		,  (tuple, list)    ):
          # Previous feature extractor had size in (width, height) format
          lowerCAmelCase__    :    int								=					size[::-1]
        lowerCAmelCase__    :    Union[str, Any]								=					get_size_dict(_lowercase    )
        lowerCAmelCase__    :    List[str]								=					resample if resample is not None else self.resample
        lowerCAmelCase__    :    Any								=					do_thumbnail if do_thumbnail is not None else self.do_thumbnail
        lowerCAmelCase__    :    int								=					do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
        lowerCAmelCase__    :    List[Any]								=					do_pad if do_pad is not None else self.do_pad
        lowerCAmelCase__    :    Dict								=					do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase__    :    str								=					rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase__    :    List[Any]								=					do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase__    :    Dict								=					image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase__    :    Tuple								=					image_std if image_std is not None else self.image_std

        lowerCAmelCase__    :    str								=					make_list_of_images(_lowercase    )

        if not valid_images(_lowercase    ):
          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:
          raise ValueError("Size 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_pad and size is None:
          raise ValueError("Size must be specified if do_pad 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.
        lowerCAmelCase__    :    Dict								=					[to_numpy_array(_lowercase    ) for image in images]

        if do_align_long_axis:
          lowerCAmelCase__    :    List[str]								=					[self.align_long_axis(_lowercase		,  size=_lowercase    ) for image in images]

        if do_resize:
          lowerCAmelCase__    :    Optional[int]								=					[self.resize(image=_lowercase		,  size=_lowercase		,  resample=_lowercase    ) for image in images]

        if do_thumbnail:
          lowerCAmelCase__    :    List[str]								=					[self.thumbnail(image=_lowercase		,  size=_lowercase    ) for image in images]

        if do_pad:
          lowerCAmelCase__    :    List[str]								=					[self.pad_image(image=_lowercase		,  size=_lowercase		,  random_padding=_lowercase    ) for image in images]

        if do_rescale:
          lowerCAmelCase__    :    Union[str, Any]								=					[self.rescale(image=_lowercase		,  scale=_lowercase    ) for image in images]

        if do_normalize:
          lowerCAmelCase__    :    int								=					[self.normalize(image=_lowercase		,  mean=_lowercase		,  std=_lowercase    ) for image in images]

        lowerCAmelCase__    :    List[str]								=					[to_channel_dimension_format(_lowercase		,  _lowercase    ) for image in images]

        lowerCAmelCase__    :    Optional[int]								=					{"pixel_values": images}
        return BatchFeature(data=_lowercase		,  tensor_type=_lowercase    )




"""simple docstring"""





import functools
import operator

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


__UpperCAmelCase    				=      logging.get_logger(__name__)

__UpperCAmelCase    				=      {
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class  lowercase_  ( a_						):
      __magic_name__     :							List[Any]          =				"""hubert"""







      def __init__(      self :							List[Any]		,  _lowercase :							Optional[int]=3_2		,  _lowercase :							Tuple=7_6_8		,  _lowercase :							List[Any]=1_2		,  _lowercase :							Dict=1_2		,  _lowercase :							Tuple=3_0_7_2		,  _lowercase :							str="gelu"		,  _lowercase :							Optional[int]=0.1		,  _lowercase :							int=0.1		,  _lowercase :							Tuple=0.1		,  _lowercase :							str=True		,  _lowercase :							Optional[Any]=0.0		,  _lowercase :							List[Any]=0.1		,  _lowercase :							Union[str, Any]=0.1		,  _lowercase :							Union[str, Any]=0.02		,  _lowercase :							List[Any]=1e-5		,  _lowercase :							List[Any]="group"		,  _lowercase :							Tuple="gelu"		,  _lowercase :							List[str]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)		,  _lowercase :							List[Any]=(5, 2, 2, 2, 2, 2, 2)		,  _lowercase :							Tuple=(1_0, 3, 3, 3, 3, 2, 2)		,  _lowercase :							Any=False		,  _lowercase :							str=1_2_8		,  _lowercase :							int=1_6		,  _lowercase :							str=False		,  _lowercase :							Union[str, Any]=True		,  _lowercase :							Optional[Any]=0.05		,  _lowercase :							List[Any]=1_0		,  _lowercase :							Dict=2		,  _lowercase :							Optional[int]=0.0		,  _lowercase :							List[str]=1_0		,  _lowercase :							List[str]=0		,  _lowercase :							Optional[Any]="sum"		,  _lowercase :							int=False		,  _lowercase :							Any=False		,  _lowercase :							Optional[Any]=2_5_6		,  _lowercase :							int=0		,  _lowercase :							Optional[int]=1		,  _lowercase :							List[Any]=2		,  **_lowercase :							Tuple		,  ):
        super().__init__(**_lowercase		,  pad_token_id=_lowercase		,  bos_token_id=_lowercase		,  eos_token_id=_lowercase    )
        lowerCAmelCase__    :    Optional[Any]								=					hidden_size
        lowerCAmelCase__    :    int								=					feat_extract_norm
        lowerCAmelCase__    :    Optional[Any]								=					feat_extract_activation
        lowerCAmelCase__    :    Optional[Any]								=					list(_lowercase    )
        lowerCAmelCase__    :    Union[str, Any]								=					list(_lowercase    )
        lowerCAmelCase__    :    Tuple								=					list(_lowercase    )
        lowerCAmelCase__    :    str								=					conv_bias
        lowerCAmelCase__    :    Optional[Any]								=					num_conv_pos_embeddings
        lowerCAmelCase__    :    List[str]								=					num_conv_pos_embedding_groups
        lowerCAmelCase__    :    List[Any]								=					len(self.conv_dim    )
        lowerCAmelCase__    :    Union[str, Any]								=					num_hidden_layers
        lowerCAmelCase__    :    str								=					intermediate_size
        lowerCAmelCase__    :    List[Any]								=					hidden_act
        lowerCAmelCase__    :    Optional[int]								=					num_attention_heads
        lowerCAmelCase__    :    Optional[int]								=					hidden_dropout
        lowerCAmelCase__    :    Dict								=					attention_dropout
        lowerCAmelCase__    :    Dict								=					activation_dropout
        lowerCAmelCase__    :    Optional[Any]								=					feat_proj_layer_norm
        lowerCAmelCase__    :    Union[str, Any]								=					feat_proj_dropout
        lowerCAmelCase__    :    Optional[Any]								=					final_dropout
        lowerCAmelCase__    :    Dict								=					layerdrop
        lowerCAmelCase__    :    Optional[int]								=					layer_norm_eps
        lowerCAmelCase__    :    str								=					initializer_range
        lowerCAmelCase__    :    int								=					vocab_size
        lowerCAmelCase__    :    int								=					do_stable_layer_norm
        lowerCAmelCase__    :    Union[str, Any]								=					use_weighted_layer_sum
        lowerCAmelCase__    :    Tuple								=					classifier_proj_size

        if (
            (len(self.conv_stride    ) != self.num_feat_extract_layers)
            or (len(self.conv_kernel    ) != self.num_feat_extract_layers)
            or (len(self.conv_dim    ) != self.num_feat_extract_layers)
        ):
          raise ValueError(
              "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
              " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
              f" {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,"
              f" `len(config.conv_kernel) = {len(self.conv_kernel    )}`."    )

        # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
        lowerCAmelCase__    :    int								=					apply_spec_augment
        lowerCAmelCase__    :    Tuple								=					mask_time_prob
        lowerCAmelCase__    :    int								=					mask_time_length
        lowerCAmelCase__    :    Dict								=					mask_time_min_masks
        lowerCAmelCase__    :    Union[str, Any]								=					mask_feature_prob
        lowerCAmelCase__    :    Dict								=					mask_feature_length
        lowerCAmelCase__    :    Tuple								=					mask_feature_min_masks

        # ctc loss
        lowerCAmelCase__    :    Tuple								=					ctc_loss_reduction
        lowerCAmelCase__    :    List[str]								=					ctc_zero_infinity







      @property
      def 				_lowerCAmelCase       (      self :							Dict    ):
        return functools.reduce(operator.mul		,  self.conv_stride		,  1    )
