


"""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__			(				SCREAMING_SNAKE_CASE    :    np.ndarray				,      SCREAMING_SNAKE_CASE    :    Union[int, Iterable[int]]				,      SCREAMING_SNAKE_CASE    :    bool				,      SCREAMING_SNAKE_CASE    :    int				):


  '''simple docstring'''







  def constraint_to_multiple_of(SCREAMING_SNAKE_CASE    :    List[Any]				,      SCREAMING_SNAKE_CASE    :    Any				,      SCREAMING_SNAKE_CASE    :    Union[str, Any]=0				,      SCREAMING_SNAKE_CASE    :    Optional[int]=None				):
    lowerCAmelCase     :     List[Any]								=							round(val / multiple				) * multiple

    if max_val is not None and x > max_val:
      lowerCAmelCase     :     int								=							math.floor(val / multiple				) * multiple

    if x < min_val:
      lowerCAmelCase     :     Optional[int]								=							math.ceil(val / multiple				) * multiple

    return x

  lowerCAmelCase     :     List[Any]								=							(output_size, output_size) if isinstance(SCREAMING_SNAKE_CASE				,      SCREAMING_SNAKE_CASE				) else output_size

  lowerCAmelCase						,  lowerCAmelCase     :     Tuple								=							get_image_size(SCREAMING_SNAKE_CASE				)
  lowerCAmelCase						,  lowerCAmelCase     :     Optional[Any]								=							output_size

  # determine new height and width
  lowerCAmelCase     :     Dict								=							output_height / input_height
  lowerCAmelCase     :     Tuple								=							output_width / input_width

  if keep_aspect_ratio:
    # scale as little as possible
    if abs(1 - scale_width				) < abs(1 - scale_height				):
      # fit width
      lowerCAmelCase     :     Dict								=							scale_width
    else:
      # fit height
      lowerCAmelCase     :     Dict								=							scale_height

  lowerCAmelCase     :     List[Any]								=							constraint_to_multiple_of(scale_height * input_height				,      multiple=SCREAMING_SNAKE_CASE				)
  lowerCAmelCase     :     Optional[Any]								=							constraint_to_multiple_of(scale_width * input_width				,      multiple=SCREAMING_SNAKE_CASE				)

  return (new_height, new_width)





class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				Any   =["pixel_values"]

       def __init__(						self		,     snake_case__ = True		,     snake_case__ = None		,     snake_case__ = PILImageResampling.BILINEAR		,     snake_case__ = False		,     snake_case__ = 1		,     snake_case__ = True		,     snake_case__ = 1 / 255		,     snake_case__ = True		,     snake_case__ = None		,     snake_case__ = None		,     **snake_case__		,     ):






         """simple docstring"""





         super().__init__(**snake_case__						)
         lowerCAmelCase     :     int								=							size if size is not None else {"height": 384, "width": 384}
         lowerCAmelCase     :     Tuple								=							get_size_dict(snake_case__						)
         lowerCAmelCase     :     int								=							do_resize
         lowerCAmelCase     :     Tuple								=							size
         lowerCAmelCase     :     Dict								=							keep_aspect_ratio
         lowerCAmelCase     :     List[Any]								=							ensure_multiple_of
         lowerCAmelCase     :     Optional[int]								=							resample
         lowerCAmelCase     :     Tuple								=							do_rescale
         lowerCAmelCase     :     int								=							rescale_factor
         lowerCAmelCase     :     List[Any]								=							do_normalize
         lowerCAmelCase     :     str								=							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         lowerCAmelCase     :     List[str]								=							image_std if image_std is not None else IMAGENET_STANDARD_STD

       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__ = False		,     snake_case__ = 1		,     snake_case__ = PILImageResampling.BICUBIC		,     snake_case__ = None		,     **snake_case__		,     ):






         """simple docstring"""





         lowerCAmelCase     :     int								=							get_size_dict(snake_case__						)
         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()}"""						)
         lowerCAmelCase     :     int								=							get_resize_output_image_size(
             snake_case__		,     output_size=(size["height"], size["width"])		,     keep_aspect_ratio=snake_case__		,     multiple=snake_case__		,     )
         return resize(snake_case__		,     size=snake_case__		,     resample=snake_case__		,     data_format=snake_case__		,     **snake_case__						)

       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__ = None		,     **snake_case__		,     ):






         """simple docstring"""





         return rescale(snake_case__		,     scale=snake_case__		,     data_format=snake_case__		,     **snake_case__						)

       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__		,     snake_case__ = None		,     **snake_case__		,     ):






         """simple docstring"""





         return normalize(snake_case__		,     mean=snake_case__		,     std=snake_case__		,     data_format=snake_case__		,     **snake_case__						)

       def        lowercase__							(						self		,     snake_case__		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = None		,     snake_case__ = ChannelDimension.FIRST		,     **snake_case__		,     ):






         """simple docstring"""





         lowerCAmelCase     :     Tuple								=							do_resize if do_resize is not None else self.do_resize
         lowerCAmelCase     :     Dict								=							size if size is not None else self.size
         lowerCAmelCase     :     Any								=							get_size_dict(snake_case__						)
         lowerCAmelCase     :     Any								=							keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
         lowerCAmelCase     :     List[Any]								=							ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
         lowerCAmelCase     :     Tuple								=							resample if resample is not None else self.resample
         lowerCAmelCase     :     List[Any]								=							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     :     Tuple								=							do_normalize if do_normalize is not None else self.do_normalize
         lowerCAmelCase     :     str								=							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(snake_case__						)

         if not valid_images(snake_case__						):
           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.
         lowerCAmelCase     :     Optional[Any]								=							[to_numpy_array(snake_case__						) for image in images]

         if do_resize:
           lowerCAmelCase     :     Dict								=							[self.resize(image=snake_case__		,     size=snake_case__		,     resample=snake_case__						) for image in images]

         if do_rescale:
           lowerCAmelCase     :     Optional[int]								=							[self.rescale(image=snake_case__		,     scale=snake_case__						) for image in images]

         if do_normalize:
           lowerCAmelCase     :     Optional[int]								=							[self.normalize(image=snake_case__		,     mean=snake_case__		,     std=snake_case__						) for image in images]

         lowerCAmelCase     :     Optional[Any]								=							[to_channel_dimension_format(snake_case__		,     snake_case__						) for image in images]

         lowerCAmelCase     :     Any								=							{"pixel_values": images}
         return BatchFeature(data=snake_case__		,     tensor_type=snake_case__						)







       def        lowercase__							(						self		,     snake_case__		,     snake_case__ = None						):






         """simple docstring"""





         lowerCAmelCase     :     Optional[int]								=							outputs.logits

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

           if is_torch_tensor(snake_case__						):
             lowerCAmelCase     :     Optional[Any]								=							target_sizes.numpy()

           lowerCAmelCase     :     Dict								=							[]

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

         return semantic_segmentation







"""simple docstring"""




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


UpperCAmelCase__      :    Optional[Any]    							= logging.get_logger(__name__)

UpperCAmelCase__      :    Dict    							= {
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[int]					       =   '''audio-spectrogram-transformer'''



				def __init__(self   ,       SCREAMING_SNAKE_CASE__=7_68   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=30_72   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=1E-12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10_24   ,       SCREAMING_SNAKE_CASE__=1_28   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Tuple:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       str           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       int           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       Tuple           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       Any           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       int           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       int           =   layer_norm_eps
											SCREAMING_SNAKE_CASE__						:       Dict           =   patch_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   qkv_bias
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   frequency_stride
											SCREAMING_SNAKE_CASE__						:       Any           =   time_stride
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   max_length
											SCREAMING_SNAKE_CASE__						:       Any           =   num_mel_bins




