






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       __UpperCAmelCase		(_UpperCamelCase    ):
			__snake_case				:      List[str] 						=							["pixel_values"]







			def __init__(	self:  List[str]      ,	UpperCAmelCase_:  bool = True      ,	UpperCAmelCase_:  Optional[Dict[str, int]] = None      ,	UpperCAmelCase_:  PILImageResampling = PILImageResampling.BILINEAR      ,	UpperCAmelCase_:  bool = True      ,	UpperCAmelCase_:  Dict[str, int] = None      ,	UpperCAmelCase_:  bool = True      ,	UpperCAmelCase_:  Union[int, float] = 1 / 255      ,	UpperCAmelCase_:  bool = True      ,	UpperCAmelCase_:  Optional[Union[float, List[float]]] = None      ,	UpperCAmelCase_:  Optional[Union[float, List[float]]] = None      ,	**UpperCAmelCase_:  str      ,	):







									'''simple docstring'''




									super().__init__(**lowerCAmelCase_     )
									_SCREAMING_SNAKE_CASE	      =  size if size is not None else {"""shortest_edge""": 256}
									_SCREAMING_SNAKE_CASE	      =  get_size_dict(lowerCAmelCase_      ,	default_to_square=lowerCAmelCase_     )
									_SCREAMING_SNAKE_CASE	      =  crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
									_SCREAMING_SNAKE_CASE	      =  get_size_dict(lowerCAmelCase_      ,	param_name="""crop_size"""     )
									_SCREAMING_SNAKE_CASE	      =  do_resize
									_SCREAMING_SNAKE_CASE	      =  size
									_SCREAMING_SNAKE_CASE	      =  resample
									_SCREAMING_SNAKE_CASE	      =  do_center_crop
									_SCREAMING_SNAKE_CASE	      =  crop_size
									_SCREAMING_SNAKE_CASE	      =  do_rescale
									_SCREAMING_SNAKE_CASE	      =  rescale_factor
									_SCREAMING_SNAKE_CASE	      =  do_normalize
									_SCREAMING_SNAKE_CASE	      =  image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
									_SCREAMING_SNAKE_CASE	      =  image_std if image_std is not None else IMAGENET_STANDARD_STD







			def 							UpperCamelCase      (	self:  Union[str, Any]      ,	UpperCAmelCase_:  np.ndarray      ,	UpperCAmelCase_:  Dict[str, int]      ,	UpperCAmelCase_:  PILImageResampling = PILImageResampling.BICUBIC      ,	UpperCAmelCase_:  Optional[Union[str, ChannelDimension]] = None      ,	**UpperCAmelCase_:  Any      ,	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  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()}'     )
									_SCREAMING_SNAKE_CASE	      =  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:  Optional[Any]      ,	UpperCAmelCase_:  np.ndarray      ,	UpperCAmelCase_:  Dict[str, int]      ,	UpperCAmelCase_:  Optional[Union[str, ChannelDimension]] = None      ,	**UpperCAmelCase_:  Optional[Any]      ,	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  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 							UpperCamelCase      (	self:  List[str]      ,	UpperCAmelCase_:  np.ndarray      ,	UpperCAmelCase_:  float      ,	UpperCAmelCase_:  Optional[Union[str, ChannelDimension]] = None      ,	**UpperCAmelCase_:  Optional[Any]     ):







									'''simple docstring'''




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







			def 							UpperCamelCase      (	self:  Any      ,	UpperCAmelCase_:  np.ndarray      ,	UpperCAmelCase_:  Union[float, List[float]]      ,	UpperCAmelCase_:  Union[float, List[float]]      ,	UpperCAmelCase_:  Optional[Union[str, ChannelDimension]] = None      ,	**UpperCAmelCase_:  Optional[Any]      ,	):







									'''simple docstring'''




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







			def 							UpperCamelCase      (	self:  Tuple      ,	UpperCAmelCase_:  ImageInput      ,	UpperCAmelCase_:  Optional[bool] = None      ,	UpperCAmelCase_:  Dict[str, int] = None      ,	UpperCAmelCase_:  PILImageResampling = None      ,	UpperCAmelCase_:  bool = None      ,	UpperCAmelCase_:  Dict[str, int] = None      ,	UpperCAmelCase_:  Optional[bool] = None      ,	UpperCAmelCase_:  Optional[float] = None      ,	UpperCAmelCase_:  Optional[bool] = None      ,	UpperCAmelCase_:  Optional[Union[float, List[float]]] = None      ,	UpperCAmelCase_:  Optional[Union[float, List[float]]] = None      ,	UpperCAmelCase_:  Optional[Union[str, TensorType]] = None      ,	UpperCAmelCase_:  Union[str, ChannelDimension] = ChannelDimension.FIRST      ,	**UpperCAmelCase_:  int      ,	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE	      =  do_resize if do_resize is not None else self.do_resize
									_SCREAMING_SNAKE_CASE	      =  size if size is not None else self.size
									_SCREAMING_SNAKE_CASE	      =  get_size_dict(lowerCAmelCase_      ,	default_to_square=lowerCAmelCase_     )
									_SCREAMING_SNAKE_CASE	      =  resample if resample is not None else self.resample
									_SCREAMING_SNAKE_CASE	      =  do_center_crop if do_center_crop is not None else self.do_center_crop
									_SCREAMING_SNAKE_CASE	      =  crop_size if crop_size is not None else self.crop_size
									_SCREAMING_SNAKE_CASE	      =  get_size_dict(lowerCAmelCase_      ,	param_name="""crop_size"""     )
									_SCREAMING_SNAKE_CASE	      =  do_rescale if do_rescale is not None else self.do_rescale
									_SCREAMING_SNAKE_CASE	      =  rescale_factor if rescale_factor is not None else self.rescale_factor
									_SCREAMING_SNAKE_CASE	      =  do_normalize if do_normalize is not None else self.do_normalize
									_SCREAMING_SNAKE_CASE	      =  image_mean if image_mean is not None else self.image_mean
									_SCREAMING_SNAKE_CASE	      =  image_std if image_std is not None else self.image_std

									_SCREAMING_SNAKE_CASE	      =  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.
									_SCREAMING_SNAKE_CASE	      =  [to_numpy_array(lowerCAmelCase_     ) for image in images]

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

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

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

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

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

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







			def 							UpperCamelCase      (	self:  Optional[int]      ,	UpperCAmelCase_:  Dict      ,	UpperCAmelCase_:  List[Tuple] = None     ):







									'''simple docstring'''




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

															_SCREAMING_SNAKE_CASE	      =  []

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

									return semantic_segmentation








from timeit import timeit

def   a_    ( lowerCAmelCase_      :			int	):
   if number < 0:
      raise ValueError('the value of input must not be negative'	)
   __lowerCAmelCase								=	0
   while number:
      number &= number - 1
      result += 1
   return result

def   a_    ( lowerCAmelCase_      :			int	):
   if number < 0:
      raise ValueError('the value of input must not be negative'	)
   __lowerCAmelCase								=	0
   while number:
      if number % 2 == 1:
         result += 1
      number >>= 1
   return result

def   a_    ( ):

   def do_benchmark(lowerCAmelCase_      :			int	) -> None:
      __lowerCAmelCase								=	'import __main__ as z'
      print(F"""Benchmark when {number = }:"""	)
      print(F"""{get_set_bits_count_using_modulo_operator(lowerCAmelCase_	) = }"""	)
      __lowerCAmelCase								=	timeit('z.get_set_bits_count_using_modulo_operator(25)',	setup=lowerCAmelCase_	)
      print(F"""timeit() runs in {timing} seconds"""	)
      print(F"""{get_set_bits_count_using_brian_kernighans_algorithm(lowerCAmelCase_	) = }"""	)
      __lowerCAmelCase								=	timeit(
          'z.get_set_bits_count_using_brian_kernighans_algorithm(25)',	setup=lowerCAmelCase_,	)
      print(F"""timeit() runs in {timing} seconds"""	)

   for number in (25, 37, 58, 0):
      do_benchmark(lowerCAmelCase_	)
      print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()

