


'''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,
    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


__UpperCAmelCase       							=				logging.get_logger(__name__)




class 						a__							(				a__    ):





							'''simple docstring'''






							lowercase__     :  Optional[int]      		= ["pixel_values"]







							def __init__(						self   ,       lowerCamelCase_ = True   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = PILImageResampling.BILINEAR   ,       lowerCamelCase_ = True   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = True   ,       lowerCamelCase_ = 1 / 2_55   ,       lowerCamelCase_ = True   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       **lowerCamelCase_   ,       )			->				None:
														super().__init__(**lowerCamelCase_  )
														lowerCAmelCase__			  =      size if size is not None else {'''shortest_edge''': 2_56}
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_   ,       default_to_square=lowerCamelCase_  )
														lowerCAmelCase__			  =      crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24}
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_   ,       param_name='''crop_size'''  )
														lowerCAmelCase__			  =      do_resize
														lowerCAmelCase__			  =      size
														lowerCAmelCase__			  =      resample
														lowerCAmelCase__			  =      do_center_crop
														lowerCAmelCase__			  =      crop_size
														lowerCAmelCase__			  =      do_rescale
														lowerCAmelCase__			  =      rescale_factor
														lowerCAmelCase__			  =      do_normalize
														lowerCAmelCase__			  =      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
														lowerCAmelCase__			  =      image_std if image_std is not None else IMAGENET_STANDARD_STD







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_ = PILImageResampling.BICUBIC   ,       lowerCamelCase_ = None   ,       **lowerCamelCase_   ,       )			->				np.ndarray:
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_   ,       default_to_square=lowerCamelCase_  )
														if "shortest_edge" not in size:
																					raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""  )
														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_ = None   ,       **lowerCamelCase_   ,       )			->				np.ndarray:
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_  )
														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(lowerCamelCase_   ,       size=(size['''height'''], size['''width'''])   ,       data_format=lowerCamelCase_   ,       **lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_ = None   ,       **lowerCamelCase_  )			->				np.ndarray:
														return rescale(lowerCamelCase_   ,       scale=lowerCamelCase_   ,       data_format=lowerCamelCase_   ,       **lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_ = None   ,       **lowerCamelCase_   ,       )			->				np.ndarray:
														return normalize(lowerCamelCase_   ,       mean=lowerCamelCase_   ,       std=lowerCamelCase_   ,       data_format=lowerCamelCase_   ,       **lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = None   ,       lowerCamelCase_ = ChannelDimension.FIRST   ,       **lowerCamelCase_   ,       )			->				Dict:
														lowerCAmelCase__			  =      do_resize if do_resize is not None else self.do_resize
														lowerCAmelCase__			  =      size if size is not None else self.size
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_   ,       default_to_square=lowerCamelCase_  )
														lowerCAmelCase__			  =      resample if resample is not None else self.resample
														lowerCAmelCase__			  =      do_center_crop if do_center_crop is not None else self.do_center_crop
														lowerCAmelCase__			  =      crop_size if crop_size is not None else self.crop_size
														lowerCAmelCase__			  =      get_size_dict(lowerCamelCase_   ,       param_name='''crop_size'''  )
														lowerCAmelCase__			  =      do_rescale if do_rescale is not None else self.do_rescale
														lowerCAmelCase__			  =      rescale_factor if rescale_factor is not None else self.rescale_factor
														lowerCAmelCase__			  =      do_normalize if do_normalize is not None else self.do_normalize
														lowerCAmelCase__			  =      image_mean if image_mean is not None else self.image_mean
														lowerCAmelCase__			  =      image_std if image_std is not None else self.image_std

														lowerCAmelCase__			  =      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_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__			  =      [to_numpy_array(lowerCamelCase_  ) for image in images]

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

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

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

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

														lowerCAmelCase__			  =      [to_channel_dimension_format(lowerCamelCase_   ,       lowerCamelCase_  ) for image in images]

														lowerCAmelCase__			  =      {'''pixel_values''': images}
														return BatchFeature(data=lowerCamelCase_   ,       tensor_type=lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_ = None  )			->				List[Any]:
														lowerCAmelCase__			  =      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_  ):
																												lowerCAmelCase__			  =      target_sizes.numpy()

																					lowerCAmelCase__			  =      []

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

														return semantic_segmentation






"""simple docstring"""


from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader






class 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A = None						,   _A = None						,   _A = True						,   _A = None						,   _A = False						,   _A = None						,   _A = True						,   _A = "arrow"						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(
									    split=_A						,   features=_A						,   cache_dir=_A						,   keep_in_memory=_A						,   streaming=_A						,   **_A						,   )
									UpperCamelCase				:		Optional[Any]				   =			load_from_cache_file
									UpperCamelCase				:		List[str]				   =			file_format
									UpperCamelCase				:		Optional[Any]				   =			Spark(
									    df=_A						,   features=_A						,   cache_dir=_A						,   working_dir=_A						,   **_A						,   )







			def   _a      (      self    ):






									'''simple docstring'''







									if self.streaming:
															return self.builder.as_streaming_dataset(split=self.split    )
									UpperCamelCase				:		Dict				   =			None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
									self.builder.download_and_prepare(
									    download_mode=_A						,   file_format=self._file_format						,   )
									return self.builder.as_dataset(split=self.split    )



