






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			:		Dict   				=    logging.get_logger(__name__)
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:str		      =    ['pixel_values']




							def __init__(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   Optional[Dict[str, int]] = None ,							SCREAMING_SNAKE_CASE_     :   PILImageResampling = PILImageResampling.BILINEAR ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   Dict[str, int] = None ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   Union[int, float] = 1 / 255 ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[float, List[float]]] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[float, List[float]]] = None ,							**SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							):
											super().__init__(**SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						size if size is not None else {'''shortest_edge''': 256}
											lowerCAmelCase__      			=						get_size_dict(SCREAMING_SNAKE_CASE_ ,							default_to_square=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
											lowerCAmelCase__      			=						get_size_dict(SCREAMING_SNAKE_CASE_ ,							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 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   np.ndarray ,							SCREAMING_SNAKE_CASE_     :   Dict[str, int] ,							SCREAMING_SNAKE_CASE_     :   PILImageResampling = PILImageResampling.BICUBIC ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[str, ChannelDimension]] = None ,							**SCREAMING_SNAKE_CASE_     :   List[Any] ,							):
											lowerCAmelCase__      			=						get_size_dict(SCREAMING_SNAKE_CASE_ ,							default_to_square=SCREAMING_SNAKE_CASE_	)
											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(SCREAMING_SNAKE_CASE_ ,							size=size['''shortest_edge'''] ,							default_to_square=SCREAMING_SNAKE_CASE_	)
											return resize(SCREAMING_SNAKE_CASE_ ,							size=SCREAMING_SNAKE_CASE_ ,							resample=SCREAMING_SNAKE_CASE_ ,							data_format=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)




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




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




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




							def 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   ImageInput ,							SCREAMING_SNAKE_CASE_     :   Optional[bool] = None ,							SCREAMING_SNAKE_CASE_     :   Dict[str, int] = None ,							SCREAMING_SNAKE_CASE_     :   PILImageResampling = None ,							SCREAMING_SNAKE_CASE_     :   bool = None ,							SCREAMING_SNAKE_CASE_     :   Dict[str, int] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[bool] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[float] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[bool] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[float, List[float]]] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[float, List[float]]] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[Union[str, TensorType]] = None ,							SCREAMING_SNAKE_CASE_     :   Union[str, ChannelDimension] = ChannelDimension.FIRST ,							**SCREAMING_SNAKE_CASE_     :   int ,							):
											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(SCREAMING_SNAKE_CASE_ ,							default_to_square=SCREAMING_SNAKE_CASE_	)
											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(SCREAMING_SNAKE_CASE_ ,							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(SCREAMING_SNAKE_CASE_	)

											if not valid_images(SCREAMING_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.
											lowerCAmelCase__      			=						[to_numpy_array(SCREAMING_SNAKE_CASE_	) for image in images]

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

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

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

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

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

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




							def 						__snake_case					(   self     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Tuple] = None	):
											lowerCAmelCase__      			=						outputs.logits

											# Resize logits and compute semantic segmentation maps
											if target_sizes is not None:
															if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_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(SCREAMING_SNAKE_CASE_	):
																			lowerCAmelCase__      			=						target_sizes.numpy()

															lowerCAmelCase__      			=						[]

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

											return semantic_segmentation













import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:Tuple		      =    ['image_processor', 'tokenizer']
							UpperCamelCase_		:Tuple		      =    'ViTImageProcessor'
							UpperCamelCase_		:Dict		      =    ('CLIPTokenizer', 'CLIPTokenizerFast')




							def __init__(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Dict=None ,							SCREAMING_SNAKE_CASE_     :   Tuple=None ,							**SCREAMING_SNAKE_CASE_     :   Union[str, Any]	):
											lowerCAmelCase__      			=						None
											if "feature_extractor" in kwargs:
															warnings.warn(
															    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
															    ''' instead.''' ,							SCREAMING_SNAKE_CASE_ ,							)
															lowerCAmelCase__      			=						kwargs.pop('''feature_extractor'''	)

											lowerCAmelCase__      			=						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__(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def __call__(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any=None ,							SCREAMING_SNAKE_CASE_     :   Tuple=None ,							SCREAMING_SNAKE_CASE_     :   Dict=None ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=None ,							**SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											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:
															lowerCAmelCase__      			=						self.tokenizer(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if visual_prompt is not None:
															lowerCAmelCase__      			=						self.image_processor(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if images is not None:
															lowerCAmelCase__      			=						self.image_processor(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if visual_prompt is not None and images is not None:
															lowerCAmelCase__      			=						{
															    '''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:
															lowerCAmelCase__      			=						image_features.pixel_values
															return encoding
											elif text is not None:
															return encoding
											elif visual_prompt is not None:
															lowerCAmelCase__      			=						{
															    '''conditional_pixel_values''': prompt_features.pixel_values,
															}
															return encoding
											else:
															return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_	) ,							tensor_type=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any] ,							*SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							**SCREAMING_SNAKE_CASE_     :   str	):
											return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[int] ,							*SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							**SCREAMING_SNAKE_CASE_     :   Tuple	):
											return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)




							@property
							def 						__snake_case					(   self     :   str	):
											warnings.warn(
											    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' ,							SCREAMING_SNAKE_CASE_ ,							)
											return self.image_processor_class




							@property
							def 						__snake_case					(   self     :   Tuple	):
											warnings.warn(
											    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' ,							SCREAMING_SNAKE_CASE_ ,							)
											return self.image_processor





