



"""simple docstring"""
from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
       import PIL

if is_torch_available():
       import torch


UpperCAmelCase						:		Tuple			= logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase   ):
    lowercase__      =   ["pixel_values"]

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







      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      lowercase_								= size if size is not None else {"""shortest_edge""": 2_2_4}
      lowercase_								= get_size_dict(lowerCAmelCase_ ,  default_to_square=lowerCAmelCase_)
      lowercase_								= crop_size if crop_size is not None else {"""height""": 2_5_6, """width""": 2_5_6}
      lowercase_								= get_size_dict(lowerCAmelCase_ ,  param_name="""crop_size""")

      lowercase_								= do_resize
      lowercase_								= size
      lowercase_								= resample
      lowercase_								= do_rescale
      lowercase_								= rescale_factor
      lowercase_								= do_center_crop
      lowercase_								= crop_size
      lowercase_								= do_flip_channel_order

    def 					_UpperCAmelCase	(				self			:				Dict ,  lowerCAmelCase_			:				np.ndarray ,  lowerCAmelCase_			:				Dict[str, int] ,  lowerCAmelCase_			:				PILImageResampling = PIL.Image.BILINEAR ,  lowerCAmelCase_			:				Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase_			:				Optional[int] ,  ):







      """simple docstring"""







      lowercase_								= get_size_dict(lowerCAmelCase_ ,  default_to_square=lowerCAmelCase_)
      if "shortest_edge" not in size:
        raise ValueError(F'''The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}''')
      lowercase_								= get_resize_output_image_size(lowerCAmelCase_ ,  size=size["""shortest_edge"""] ,  default_to_square=lowerCAmelCase_)
      return resize(lowerCAmelCase_ ,  size=lowerCAmelCase_ ,  resample=lowerCAmelCase_ ,  data_format=lowerCAmelCase_ ,  **lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Any ,  lowerCAmelCase_			:				np.ndarray ,  lowerCAmelCase_			:				Dict[str, int] ,  lowerCAmelCase_			:				Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase_			:				Union[str, Any] ,  ):







      """simple docstring"""







      lowercase_								= 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()}''')
      return center_crop(lowerCAmelCase_ ,  size=(size["""height"""], size["""width"""]) ,  data_format=lowerCAmelCase_ ,  **lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				List[str] ,  lowerCAmelCase_			:				np.ndarray ,  lowerCAmelCase_			:				Union[int, float] ,  lowerCAmelCase_			:				Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase_			:				List[str] ,  ):







      """simple docstring"""







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

    def 					_UpperCAmelCase	(				self			:				List[str] ,  lowerCAmelCase_			:				np.ndarray ,  lowerCAmelCase_			:				Optional[Union[str, ChannelDimension]] = None):







      """simple docstring"""







      return flip_channel_order(lowerCAmelCase_ ,  data_format=lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Any ,  lowerCAmelCase_			:				ImageInput ,  lowerCAmelCase_			:				bool = None ,  lowerCAmelCase_			:				Dict[str, int] = None ,  lowerCAmelCase_			:				PILImageResampling = None ,  lowerCAmelCase_			:				bool = None ,  lowerCAmelCase_			:				float = None ,  lowerCAmelCase_			:				bool = None ,  lowerCAmelCase_			:				Dict[str, int] = None ,  lowerCAmelCase_			:				bool = None ,  lowerCAmelCase_			:				Optional[Union[str, TensorType]] = None ,  lowerCAmelCase_			:				ChannelDimension = ChannelDimension.FIRST ,  **lowerCAmelCase_			:				Optional[int] ,  ):







      """simple docstring"""







      lowercase_								= do_resize if do_resize is not None else self.do_resize
      lowercase_								= resample if resample is not None else self.resample
      lowercase_								= do_rescale if do_rescale is not None else self.do_rescale
      lowercase_								= rescale_factor if rescale_factor is not None else self.rescale_factor
      lowercase_								= do_center_crop if do_center_crop is not None else self.do_center_crop
      lowercase_								= (
          do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
      )

      lowercase_								= size if size is not None else self.size
      lowercase_								= get_size_dict(lowerCAmelCase_ ,  default_to_square=lowerCAmelCase_)
      lowercase_								= crop_size if crop_size is not None else self.crop_size
      lowercase_								= get_size_dict(lowerCAmelCase_ ,  param_name="""crop_size""")

      lowercase_								= 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:
        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_center_crop and crop_size is None:
        raise ValueError("""Crop size must be specified if do_center_crop is True.""")

      # All transformations expect numpy arrays.
      lowercase_								= [to_numpy_array(lowerCAmelCase_) for image in images]

      if do_resize:
        lowercase_								= [self.resize(image=lowerCAmelCase_ ,  size=lowerCAmelCase_ ,  resample=lowerCAmelCase_) for image in images]

      if do_center_crop:
        lowercase_								= [self.center_crop(image=lowerCAmelCase_ ,  size=lowerCAmelCase_) for image in images]

      if do_rescale:
        lowercase_								= [self.rescale(image=lowerCAmelCase_ ,  scale=lowerCAmelCase_) for image in images]

      # the pretrained checkpoints assume images are BGR, not RGB
      if do_flip_channel_order:
        lowercase_								= [self.flip_channel_order(image=lowerCAmelCase_) for image in images]

      lowercase_								= [to_channel_dimension_format(lowerCAmelCase_ ,  lowerCAmelCase_) for image in images]

      lowercase_								= {"""pixel_values""": images}
      return BatchFeature(data=lowerCAmelCase_ ,  tensor_type=lowerCAmelCase_)




    def 					_UpperCAmelCase	(				self			:				List[Any] ,  lowerCAmelCase_			:				Optional[int] ,  lowerCAmelCase_			:				List[Tuple] = None):







      """simple docstring"""







      lowercase_								= 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_):
          lowercase_								= target_sizes.numpy()

        lowercase_								= []

        for idx in range(len(lowerCAmelCase_)):
          lowercase_								= torch.nn.functional.interpolate(
              logits[idx].unsqueeze(dim=0) ,  size=target_sizes[idx] ,  mode="""bilinear""" ,  align_corners=lowerCAmelCase_)
          lowercase_								= resized_logits[0].argmax(dim=0)
          semantic_segmentation.append(lowerCAmelCase_)
      else:
        lowercase_								= logits.argmax(dim=1)
        lowercase_								= [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])]

      return semantic_segmentation






"""simple docstring"""
from typing import List

import datasets
from datasets.tasks import AudioClassification

from ..folder_based_builder import folder_based_builder


UpperCAmelCase						:		Union[str, Any]			= datasets.utils.logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE__ (							folder_based_builder.FolderBasedBuilderConfig   ):
    lowercase__      =   None
    lowercase__      =   None







class    SCREAMING_SNAKE_CASE__ (							folder_based_builder.FolderBasedBuilder   ):
    lowercase__      =   datasets.Audio()
    lowercase__      =   "audio"
    lowercase__      =   AudioFolderConfig
    lowercase__      =   42  # definition at the bottom of the script
    lowercase__      =   AudioClassification(audio_column="audio"      ,  label_column="label"   )







UpperCAmelCase						:		Tuple			= [
    ".aiff",
    ".au",
    ".avr",
    ".caf",
    ".flac",
    ".htk",
    ".svx",
    ".mat4",
    ".mat5",
    ".mpc2k",
    ".ogg",
    ".paf",
    ".pvf",
    ".raw",
    ".rf64",
    ".sd2",
    ".sds",
    ".ircam",
    ".voc",
    ".w64",
    ".wav",
    ".nist",
    ".wavex",
    ".wve",
    ".xi",
    ".mp3",
    ".opus",
]
UpperCAmelCase						:		Any			= AUDIO_EXTENSIONS

