


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


_lowercase    :    Optional[Any]			     =  logging.get_logger(__name__)






class  UpperCamelCase__(     lowerCAmelCase ):
				__magic_name__       :   Optional[Any]							= ["pixel_values"]




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

										"""simple docstring"""




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




				def   a__(	self    :				str   ,					lowerCAmelCase    :				np.ndarray   ,					lowerCAmelCase    :				Dict[str, int]   ,					lowerCAmelCase    :				PILImageResampling = PILImageResampling.BICUBIC   ,					lowerCAmelCase    :				Optional[Union[str, ChannelDimension]] = None   ,					**lowerCAmelCase    :				Tuple   ,					)->  np.ndarray:

										"""simple docstring"""




										UpperCAmelCase         =			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()}"""   )
										UpperCAmelCase         =			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   a__(	self    :				Optional[int]   ,					lowerCAmelCase    :				np.ndarray   ,					lowerCAmelCase    :				Dict[str, int]   ,					lowerCAmelCase    :				Optional[Union[str, ChannelDimension]] = None   ,					**lowerCAmelCase    :				Optional[int]   ,					)->  np.ndarray:

										"""simple docstring"""




										UpperCAmelCase         =			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   a__(	self    :				Any   ,					lowerCAmelCase    :				np.ndarray   ,					lowerCAmelCase    :				float   ,					lowerCAmelCase    :				Optional[Union[str, ChannelDimension]] = None   ,					**lowerCAmelCase    :				Optional[Any]   )->  np.ndarray:

										"""simple docstring"""




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




				def   a__(	self    :				List[str]   ,					lowerCAmelCase    :				np.ndarray   ,					lowerCAmelCase    :				Union[float, List[float]]   ,					lowerCAmelCase    :				Union[float, List[float]]   ,					lowerCAmelCase    :				Optional[Union[str, ChannelDimension]] = None   ,					**lowerCAmelCase    :				Union[str, Any]   ,					)->  np.ndarray:

										"""simple docstring"""




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




				def   a__(	self    :				Any   ,					lowerCAmelCase    :				ImageInput   ,					lowerCAmelCase    :				Optional[bool] = None   ,					lowerCAmelCase    :				Dict[str, int] = None   ,					lowerCAmelCase    :				PILImageResampling = None   ,					lowerCAmelCase    :				bool = None   ,					lowerCAmelCase    :				Dict[str, int] = None   ,					lowerCAmelCase    :				Optional[bool] = None   ,					lowerCAmelCase    :				Optional[float] = None   ,					lowerCAmelCase    :				Optional[bool] = None   ,					lowerCAmelCase    :				Optional[Union[float, List[float]]] = None   ,					lowerCAmelCase    :				Optional[Union[float, List[float]]] = None   ,					lowerCAmelCase    :				Optional[Union[str, TensorType]] = None   ,					lowerCAmelCase    :				Union[str, ChannelDimension] = ChannelDimension.FIRST   ,					**lowerCAmelCase    :				List[Any]   ,					)->  Optional[Any]:

										"""simple docstring"""




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

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

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

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

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

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

										UpperCAmelCase         =			[to_channel_dimension_format(lowerCAmelCase   ,					lowerCAmelCase   ) for image in images]

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





				def   a__(	self    :				Optional[int]   ,					lowerCAmelCase    :				Optional[int]   ,					lowerCAmelCase    :				List[Tuple] = None   )->  List[Any]:

										"""simple docstring"""




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

																UpperCAmelCase         =			[]

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

										return semantic_segmentation









'''simple docstring'''



import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class  UpperCamelCase__(     lowerCAmelCase ):
				__magic_name__       :   Tuple							= ["image_processor", "tokenizer"]
				__magic_name__       :   Any							= "ViTImageProcessor"
				__magic_name__       :   str							= ("CLIPTokenizer", "CLIPTokenizerFast")




				def __init__(	self    :				List[str]   ,					lowerCAmelCase    :				List[Any]=None   ,					lowerCAmelCase    :				List[str]=None   ,					**lowerCAmelCase    :				Optional[int]   )->  List[Any]:

										"""simple docstring"""




										UpperCAmelCase         =			None
										if "feature_extractor" in kwargs:
																warnings.warn(
																    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																    ''' instead.'''   ,					lowerCAmelCase   ,					)
																UpperCAmelCase         =			kwargs.pop('''feature_extractor'''   )

										UpperCAmelCase         =			image_processor if image_processor is not None else feature_extractor
										if image_processor is None:
																raise ValueError('''You need to specify an `image_processor`.'''   )
										if tokenizer is None:
																raise ValueError('''You need to specify a `tokenizer`.'''   )

										super().__init__(lowerCAmelCase   ,					lowerCAmelCase   )




				def __call__(	self    :				str   ,					lowerCAmelCase    :				Optional[Any]=None   ,					lowerCAmelCase    :				Optional[Any]=None   ,					lowerCAmelCase    :				Dict=None   ,					lowerCAmelCase    :				str=None   ,					**lowerCAmelCase    :				List[str]   )->  List[Any]:

										"""simple docstring"""




										if text is None and visual_prompt is None and images is None:
																raise ValueError('''You have to specify either text, visual prompt or images.'''   )

										if text is not None and visual_prompt is not None:
																raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.'''   )

										if text is not None:
																UpperCAmelCase         =			self.tokenizer(lowerCAmelCase   ,					return_tensors=lowerCAmelCase   ,					**lowerCAmelCase   )

										if visual_prompt is not None:
																UpperCAmelCase         =			self.image_processor(lowerCAmelCase   ,					return_tensors=lowerCAmelCase   ,					**lowerCAmelCase   )

										if images is not None:
																UpperCAmelCase         =			self.image_processor(lowerCAmelCase   ,					return_tensors=lowerCAmelCase   ,					**lowerCAmelCase   )

										if visual_prompt is not None and images is not None:
																UpperCAmelCase         =			{
																    '''pixel_values''': image_features.pixel_values,
																    '''conditional_pixel_values''': prompt_features.pixel_values,
																}
																return encoding
										elif text is not None and images is not None:
																UpperCAmelCase         =			image_features.pixel_values
																return encoding
										elif text is not None:
																return encoding
										elif visual_prompt is not None:
																UpperCAmelCase         =			{
																    '''conditional_pixel_values''': prompt_features.pixel_values,
																}
																return encoding
										else:
																return BatchEncoding(data=dict(**lowerCAmelCase   )   ,					tensor_type=lowerCAmelCase   )




				def   a__(	self    :				List[str]   ,					*lowerCAmelCase    :				str   ,					**lowerCAmelCase    :				Optional[int]   )->  Union[str, Any]:

										"""simple docstring"""




										return self.tokenizer.batch_decode(*lowerCAmelCase   ,					**lowerCAmelCase   )




				def   a__(	self    :				Dict   ,					*lowerCAmelCase    :				Tuple   ,					**lowerCAmelCase    :				List[str]   )->  Optional[Any]:

										"""simple docstring"""




										return self.tokenizer.decode(*lowerCAmelCase   ,					**lowerCAmelCase   )




				@property
				def   a__(	self    :				List[Any]   )->  Optional[Any]:

										"""simple docstring"""




										warnings.warn(
										    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''   ,					lowerCAmelCase   ,					)
										return self.image_processor_class





				@property
				def   a__(	self    :				Any   )->  List[Any]:

										"""simple docstring"""




										warnings.warn(
										    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''   ,					lowerCAmelCase   ,					)
										return self.image_processor





