



'''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__:     List[str]										=    logging.get_logger(__name__)

class        SCREAMING_SNAKE_CASE(							A__    ):







	"""simple docstring"""






	lowerCamelCase__    				= ["""pixel_values"""]







	def __init__(		self    :  Union[str, Any]	,    __snake_case    :  bool = True	,    __snake_case    :  Optional[Dict[str, int]] = None	,    __snake_case    :  PILImageResampling = PILImageResampling.BILINEAR	,    __snake_case    :  bool = True	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  bool = True	,    __snake_case    :  Union[int, float] = 1 / 255	,    __snake_case    :  bool = True	,    __snake_case    :  Optional[Union[float, List[float]]] = None	,    __snake_case    :  Optional[Union[float, List[float]]] = None	,    **__snake_case    :  Dict	,    )    ->	None:
								super().__init__(**__snake_case	)
								UpperCAmelCase				: List[str]											=	size if size is not None else {'''shortest_edge''': 256}
								UpperCAmelCase				: List[str]											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								UpperCAmelCase				: int											=	crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
								UpperCAmelCase				: List[str]											=	get_size_dict(__snake_case	,    param_name='''crop_size'''	)
								UpperCAmelCase				: Optional[int]											=	do_resize
								UpperCAmelCase				: Optional[int]											=	size
								UpperCAmelCase				: int											=	resample
								UpperCAmelCase				: List[str]											=	do_center_crop
								UpperCAmelCase				: Optional[int]											=	crop_size
								UpperCAmelCase				: Optional[Any]											=	do_rescale
								UpperCAmelCase				: List[str]											=	rescale_factor
								UpperCAmelCase				: int											=	do_normalize
								UpperCAmelCase				: List[Any]											=	image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
								UpperCAmelCase				: Any											=	image_std if image_std is not None else IMAGENET_STANDARD_STD







	def   A			(		self    :  List[Any]	,    __snake_case    :  np.ndarray	,    __snake_case    :  Dict[str, int]	,    __snake_case    :  PILImageResampling = PILImageResampling.BICUBIC	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  str	,    )    ->	np.ndarray:
								UpperCAmelCase				: Tuple											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								if "shortest_edge" not in size:
															raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""	)
								UpperCAmelCase				: int											=	get_resize_output_image_size(__snake_case	,    size=size['''shortest_edge''']	,    default_to_square=__snake_case	)
								return resize(__snake_case	,    size=__snake_case	,    resample=__snake_case	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  List[Any]	,    __snake_case    :  np.ndarray	,    __snake_case    :  Dict[str, int]	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  List[str]	,    )    ->	np.ndarray:
								UpperCAmelCase				: Union[str, Any]											=	get_size_dict(__snake_case	)
								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(__snake_case	,    size=(size['''height'''], size['''width'''])	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  int	,    __snake_case    :  np.ndarray	,    __snake_case    :  float	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  Optional[Any]	)    ->	np.ndarray:
								return rescale(__snake_case	,    scale=__snake_case	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  List[str]	,    __snake_case    :  np.ndarray	,    __snake_case    :  Union[float, List[float]]	,    __snake_case    :  Union[float, List[float]]	,    __snake_case    :  Optional[Union[str, ChannelDimension]] = None	,    **__snake_case    :  Any	,    )    ->	np.ndarray:
								return normalize(__snake_case	,    mean=__snake_case	,    std=__snake_case	,    data_format=__snake_case	,    **__snake_case	)







	def   A			(		self    :  Any	,    __snake_case    :  ImageInput	,    __snake_case    :  Optional[bool] = None	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  PILImageResampling = None	,    __snake_case    :  bool = None	,    __snake_case    :  Dict[str, int] = None	,    __snake_case    :  Optional[bool] = None	,    __snake_case    :  Optional[float] = None	,    __snake_case    :  Optional[bool] = None	,    __snake_case    :  Optional[Union[float, List[float]]] = None	,    __snake_case    :  Optional[Union[float, List[float]]] = None	,    __snake_case    :  Optional[Union[str, TensorType]] = None	,    __snake_case    :  Union[str, ChannelDimension] = ChannelDimension.FIRST	,    **__snake_case    :  int	,    )    ->	str:
								UpperCAmelCase				: Union[str, Any]											=	do_resize if do_resize is not None else self.do_resize
								UpperCAmelCase				: Optional[Any]											=	size if size is not None else self.size
								UpperCAmelCase				: Dict											=	get_size_dict(__snake_case	,    default_to_square=__snake_case	)
								UpperCAmelCase				: Optional[Any]											=	resample if resample is not None else self.resample
								UpperCAmelCase				: int											=	do_center_crop if do_center_crop is not None else self.do_center_crop
								UpperCAmelCase				: Any											=	crop_size if crop_size is not None else self.crop_size
								UpperCAmelCase				: int											=	get_size_dict(__snake_case	,    param_name='''crop_size'''	)
								UpperCAmelCase				: Union[str, Any]											=	do_rescale if do_rescale is not None else self.do_rescale
								UpperCAmelCase				: Dict											=	rescale_factor if rescale_factor is not None else self.rescale_factor
								UpperCAmelCase				: Optional[int]											=	do_normalize if do_normalize is not None else self.do_normalize
								UpperCAmelCase				: List[Any]											=	image_mean if image_mean is not None else self.image_mean
								UpperCAmelCase				: List[Any]											=	image_std if image_std is not None else self.image_std

								UpperCAmelCase				: Optional[Any]											=	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:
															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.
								UpperCAmelCase				: Optional[Any]											=	[to_numpy_array(__snake_case	) for image in images]

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

								if do_center_crop:
															UpperCAmelCase				: Optional[int]											=	[self.center_crop(image=__snake_case	,    size=__snake_case	) for image in images]

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

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

								UpperCAmelCase				: str											=	[to_channel_dimension_format(__snake_case	,    __snake_case	) for image in images]

								UpperCAmelCase				: Dict											=	{'''pixel_values''': images}
								return BatchFeature(data=__snake_case	,    tensor_type=__snake_case	)







	def   A			(		self    :  str	,    __snake_case    :  Dict	,    __snake_case    :  List[Tuple] = None	)    ->	str:
								UpperCAmelCase				: List[str]											=	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	):
																						UpperCAmelCase				: Any											=	target_sizes.numpy()

															UpperCAmelCase				: List[Any]											=	[]

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

								return semantic_segmentation










'''simple docstring'''






import math

import tensorflow as tf
from packaging import version


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     Dict:
							UpperCAmelCase				: Tuple											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Dict											=	0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)     ,   x.dtype						)						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		int						)   ->     List[Any]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(math.pi     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: List[Any]											=	0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(_lowerCAmelCase     ,   3						))						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Any:
							UpperCAmelCase				: List[Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)

							return x * tf.tanh(tf.math.softplus(_lowerCAmelCase						)						)


def 				snake_case_  (      _lowerCAmelCase						:		str						)   ->     Tuple:
							UpperCAmelCase				: Union[str, Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.7_9_7_8_8_4_5_6_0_8     ,   x.dtype						)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Optional[int]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Optional[int]											=	tf.cast(1.7_0_2     ,   x.dtype						)
							return x * tf.math.sigmoid(coeff * x						)


def 				snake_case_  (      _lowerCAmelCase						:		Any						)   ->     Any:
							return tf.clip_by_value(_gelu(_lowerCAmelCase						)     ,   -10     ,   10						)


def 				snake_case_  (      _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		Optional[Any]=-1						)   ->     Union[str, Any]:
							UpperCAmelCase							,	UpperCAmelCase				: Optional[Any]											=	tf.split(_lowerCAmelCase     ,   2     ,   axis=_lowerCAmelCase						)
							return a * tf.math.sigmoid(_lowerCAmelCase						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):


						def 				snake_case_  (      _lowerCAmelCase						:		Tuple						)   ->     Any:
													return tf.keras.activations.gelu(_lowerCAmelCase     ,   approximate=_lowerCAmelCase						)

						UpperCamelCase__:     List[Any]										=    tf.keras.activations.gelu




						UpperCamelCase__:     List[Any]										=    approximate_gelu_wrap
else:
						UpperCamelCase__:     List[str]										=    _gelu
						UpperCamelCase__:     Tuple										=    _gelu_new


UpperCamelCase__:     List[Any]										=    {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}




def 				snake_case_  (      _lowerCAmelCase						:		Dict						)   ->     List[Any]:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(f"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"""						)





