



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,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
						import torch


_snake_case	=						logging.get_logger(__name__)



class 						UpperCAmelCase_					( a):
							lowerCamelCase__         =	['pixel_values']







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

														'''simple docstring'''

														super().__init__(**__a)
														_lowerCAmelCase  :							Dict				     =					size if size is not None else {"shortest_edge": 256}
														_lowerCAmelCase  :							List[str]				     =					get_size_dict(__a,    default_to_square=__a)
														_lowerCAmelCase  :							Optional[Any]				     =					crop_size if crop_size is not None else {"height": 224, "width": 224}
														_lowerCAmelCase  :							Optional[int]				     =					get_size_dict(__a,    param_name="crop_size")
														_lowerCAmelCase  :							str				     =					do_resize
														_lowerCAmelCase  :							List[str]				     =					size
														_lowerCAmelCase  :							Optional[Any]				     =					resample
														_lowerCAmelCase  :							Optional[int]				     =					do_center_crop
														_lowerCAmelCase  :							str				     =					crop_size
														_lowerCAmelCase  :							Dict				     =					do_rescale
														_lowerCAmelCase  :							Union[str, Any]				     =					rescale_factor
														_lowerCAmelCase  :							Optional[Any]				     =					do_normalize
														_lowerCAmelCase  :							Any				     =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
														_lowerCAmelCase  :							Tuple				     =					image_std if image_std is not None else IMAGENET_STANDARD_STD







							def      snake_case__   (     self,    __a,    __a,    __a = PILImageResampling.BICUBIC,    __a = None,    **__a,    ):

														'''simple docstring'''

														_lowerCAmelCase  :							Tuple				     =					get_size_dict(__a,    default_to_square=__a)
														if "shortest_edge" not in size:
																					raise ValueError(f"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}")
														_lowerCAmelCase  :							List[str]				     =					get_resize_output_image_size(__a,    size=size["shortest_edge"],    default_to_square=__a)
														return resize(__a,    size=__a,    resample=__a,    data_format=__a,    **__a)







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

														'''simple docstring'''

														_lowerCAmelCase  :							Any				     =					get_size_dict(__a)
														if "height" not in size or "width" not in size:
																					raise ValueError(f"The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}")
														return center_crop(__a,    size=(size["height"], size["width"]),    data_format=__a,    **__a)







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

														'''simple docstring'''

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







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

														'''simple docstring'''

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







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

														'''simple docstring'''

														_lowerCAmelCase  :							Any				     =					do_resize if do_resize is not None else self.do_resize
														_lowerCAmelCase  :							Optional[int]				     =					size if size is not None else self.size
														_lowerCAmelCase  :							Optional[Any]				     =					get_size_dict(__a,    default_to_square=__a)
														_lowerCAmelCase  :							Tuple				     =					resample if resample is not None else self.resample
														_lowerCAmelCase  :							int				     =					do_center_crop if do_center_crop is not None else self.do_center_crop
														_lowerCAmelCase  :							Dict				     =					crop_size if crop_size is not None else self.crop_size
														_lowerCAmelCase  :							List[Any]				     =					get_size_dict(__a,    param_name="crop_size")
														_lowerCAmelCase  :							Dict				     =					do_rescale if do_rescale is not None else self.do_rescale
														_lowerCAmelCase  :							List[str]				     =					rescale_factor if rescale_factor is not None else self.rescale_factor
														_lowerCAmelCase  :							Any				     =					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  :							Union[str, Any]				     =					image_std if image_std is not None else self.image_std

														_lowerCAmelCase  :							Optional[int]				     =					make_list_of_images(__a)

														if not valid_images(__a):
																					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_center_crop and crop_size is None:
																					raise ValueError("Crop size must be specified if do_center_crop 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  :							Tuple				     =					[to_numpy_array(__a) for image in images]

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

														if do_center_crop:
																					_lowerCAmelCase  :							List[str]				     =					[self.center_crop(image=__a,    size=__a) for image in images]

														if do_rescale:
																					_lowerCAmelCase  :							str				     =					[self.rescale(image=__a,    scale=__a) for image in images]

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

														_lowerCAmelCase  :							int				     =					[to_channel_dimension_format(__a,    __a) for image in images]

														_lowerCAmelCase  :							Any				     =					{"pixel_values": images}
														return BatchFeature(data=__a,    tensor_type=__a)







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

														'''simple docstring'''

														_lowerCAmelCase  :							Dict				     =					outputs.logits

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

																					if is_torch_tensor(__a):
																												_lowerCAmelCase  :							int				     =					target_sizes.numpy()

																					_lowerCAmelCase  :							Any				     =					[]

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

														return semantic_segmentation





from __future__ import annotations






def        A       (      _lowerCamelCase						):





							'''simple docstring'''




							if not nums:
														raise ValueError("List is empty"						)
							return sum(_lowerCamelCase						) / len(_lowerCamelCase						)


if __name__ == "__main__":
						import doctest

						doctest.testmod()
