




"""simple docstring"""






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import CLIPImageProcessor, CLIPProcessor






@require_vision
class lowerCAmelCase__			(  unittest.TestCase		):






			'''simple docstring'''



			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					tempfile.mkdtemp()

							# fmt: off
							_lowerCamelCase  :    List[Any]					=					["l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
							# fmt: on
							_lowerCamelCase  :    Optional[Any]					=					dict(zip(lowercase						,   range(len(lowercase						)						)						)						)
							_lowerCamelCase  :    Any					=					["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
							_lowerCamelCase  :    Optional[int]					=					{"unk_token": "<unk>"}

							_lowerCamelCase  :    List[str]					=					os.path.join(self.tmpdirname						,   VOCAB_FILES_NAMES['vocab_file']						)
							_lowerCamelCase  :    List[str]					=					os.path.join(self.tmpdirname						,   VOCAB_FILES_NAMES['merges_file']						)
							with open(self.vocab_file						,   'w'						,   encoding='utf-8'						) as fp:
											fp.write(json.dumps(lowercase						) + '\n'						)
							with open(self.merges_file						,   'w'						,   encoding='utf-8'						) as fp:
											fp.write('\n'.join(lowercase						)						)

							_lowerCamelCase  :    Optional[Any]					=					{
							    "do_resize": True,
							    "size": 20,
							    "do_center_crop": True,
							    "crop_size": 18,
							    "do_normalize": True,
							    "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
							    "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
							}
							_lowerCamelCase  :    int					=					os.path.join(self.tmpdirname						,   lowercase						)
							with open(self.image_processor_file						,   'w'						,   encoding='utf-8'						) as fp:
											json.dump(lowercase						,   lowercase						)
			def       A_					(						self						,   **lowercase						):
							return CLIPTokenizer.from_pretrained(self.tmpdirname						,   **lowercase						)
			def       A_					(						self						,   **lowercase						):
							return CLIPTokenizerFast.from_pretrained(self.tmpdirname						,   **lowercase						)
			def       A_					(						self						,   **lowercase						):
							return CLIPImageProcessor.from_pretrained(self.tmpdirname						,   **lowercase						)
			def       A_					(						self						):
							shutil.rmtree(self.tmpdirname						)
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[int]					=					[np.random.randint(255						,   size=(3, 30, 400)						,   dtype=np.uinta						)]

							_lowerCamelCase  :    Optional[int]					=					[Image.fromarray(np.moveaxis(lowercase						,   0						,   -1						)						) for x in image_inputs]

							return image_inputs
			def       A_					(						self						):
							_lowerCamelCase  :    str					=					self.get_tokenizer()
							_lowerCamelCase  :    Union[str, Any]					=					self.get_rust_tokenizer()
							_lowerCamelCase  :    Union[str, Any]					=					self.get_image_processor()

							_lowerCamelCase  :    Any					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)
							processor_slow.save_pretrained(self.tmpdirname						)
							_lowerCamelCase  :    Optional[int]					=					CLIPProcessor.from_pretrained(self.tmpdirname						,   use_fast=lowercase						)

							_lowerCamelCase  :    Optional[int]					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)
							processor_fast.save_pretrained(self.tmpdirname						)
							_lowerCamelCase  :    str					=					CLIPProcessor.from_pretrained(self.tmpdirname						)

							self.assertEqual(processor_slow.tokenizer.get_vocab()						,   tokenizer_slow.get_vocab()						)
							self.assertEqual(processor_fast.tokenizer.get_vocab()						,   tokenizer_fast.get_vocab()						)
							self.assertEqual(tokenizer_slow.get_vocab()						,   tokenizer_fast.get_vocab()						)
							self.assertIsInstance(processor_slow.tokenizer						,   lowercase						)
							self.assertIsInstance(processor_fast.tokenizer						,   lowercase						)

							self.assertEqual(processor_slow.image_processor.to_json_string()						,   image_processor.to_json_string()						)
							self.assertEqual(processor_fast.image_processor.to_json_string()						,   image_processor.to_json_string()						)
							self.assertIsInstance(processor_slow.image_processor						,   lowercase						)
							self.assertIsInstance(processor_fast.image_processor						,   lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[int]					=					CLIPProcessor(tokenizer=self.get_tokenizer()						,   image_processor=self.get_image_processor()						)
							processor.save_pretrained(self.tmpdirname						)

							_lowerCamelCase  :    Dict					=					self.get_tokenizer(bos_token='(BOS)'						,   eos_token='(EOS)'						)
							_lowerCamelCase  :    Optional[Any]					=					self.get_image_processor(do_normalize=lowercase						,   padding_value=1.0						)

							_lowerCamelCase  :    Any					=					CLIPProcessor.from_pretrained(
							    self.tmpdirname						,   bos_token='(BOS)'						,   eos_token='(EOS)'						,   do_normalize=lowercase						,   padding_value=1.0						)

							self.assertEqual(processor.tokenizer.get_vocab()						,   tokenizer_add_kwargs.get_vocab()						)
							self.assertIsInstance(processor.tokenizer						,   lowercase						)

							self.assertEqual(processor.image_processor.to_json_string()						,   image_processor_add_kwargs.to_json_string()						)
							self.assertIsInstance(processor.image_processor						,   lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    Any					=					self.get_image_processor()
							_lowerCamelCase  :    Dict					=					self.get_tokenizer()

							_lowerCamelCase  :    List[str]					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)

							_lowerCamelCase  :    List[Any]					=					self.prepare_image_inputs()

							_lowerCamelCase  :    Optional[Any]					=					image_processor(lowercase						,   return_tensors='np'						)
							_lowerCamelCase  :    Dict					=					processor(images=lowercase						,   return_tensors='np'						)

							for key in input_image_proc.keys():
											self.assertAlmostEqual(input_image_proc[key].sum()						,   input_processor[key].sum()						,   delta=1E-2						)
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					self.get_image_processor()
							_lowerCamelCase  :    int					=					self.get_tokenizer()

							_lowerCamelCase  :    Dict					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)

							_lowerCamelCase  :    Optional[int]					=					"lower newer"

							_lowerCamelCase  :    List[str]					=					processor(text=lowercase						)

							_lowerCamelCase  :    Optional[Any]					=					tokenizer(lowercase						)

							for key in encoded_tok.keys():
											self.assertListEqual(encoded_tok[key]						,   encoded_processor[key]						)
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					self.get_image_processor()
							_lowerCamelCase  :    Optional[Any]					=					self.get_tokenizer()

							_lowerCamelCase  :    List[Any]					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)

							_lowerCamelCase  :    str					=					"lower newer"
							_lowerCamelCase  :    Dict					=					self.prepare_image_inputs()

							_lowerCamelCase  :    int					=					processor(text=lowercase						,   images=lowercase						)

							self.assertListEqual(list(inputs.keys()						)						,   ['input_ids', 'attention_mask', 'pixel_values']						)

							# test if it raises when no input is passed
							with pytest.raises(lowercase						):
											processor()
			def       A_					(						self						):
							_lowerCamelCase  :    int					=					self.get_image_processor()
							_lowerCamelCase  :    Optional[Any]					=					self.get_tokenizer()

							_lowerCamelCase  :    Any					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)

							_lowerCamelCase  :    int					=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							_lowerCamelCase  :    Any					=					processor.batch_decode(lowercase						)
							_lowerCamelCase  :    Dict					=					tokenizer.batch_decode(lowercase						)

							self.assertListEqual(lowercase						,   lowercase						)






			def       A_					(						self						):
							_lowerCamelCase  :    Any					=					self.get_image_processor()
							_lowerCamelCase  :    Any					=					self.get_tokenizer()

							_lowerCamelCase  :    Dict					=					CLIPProcessor(tokenizer=lowercase						,   image_processor=lowercase						)

							_lowerCamelCase  :    Dict					=					"lower newer"
							_lowerCamelCase  :    Any					=					self.prepare_image_inputs()

							_lowerCamelCase  :    str					=					processor(text=lowercase						,   images=lowercase						)

							self.assertListEqual(list(inputs.keys()						)						,   processor.model_input_names						)





"""simple docstring"""






from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
			import torch

if is_tf_available():
			import tensorflow as tf






class lowerCAmelCase__			(  lowercase		):






			'''simple docstring'''



			lowerCamelCase__   =    ["""image_processor"""]
			lowerCamelCase__   =    """SamImageProcessor"""
			def __init__(						self						,   lowercase						):
							super().__init__(lowercase						)
							_lowerCamelCase  :    str					=					self.image_processor
							_lowerCamelCase  :    Optional[int]					=					-10
							_lowerCamelCase  :    Optional[Any]					=					self.image_processor.size['longest_edge']
			def __call__(						self						,   lowercase=None						,   lowercase=None						,   lowercase=None						,   lowercase=None						,   lowercase = None						,   **lowercase						,   ):
							_lowerCamelCase  :    int					=					self.image_processor(
							    lowercase						,   return_tensors=lowercase						,   **lowercase						,   )

							# pop arguments that are not used in the foward but used nevertheless
							_lowerCamelCase  :    int					=					encoding_image_processor['original_sizes']

							if hasattr(lowercase						,   'numpy'						):  # Checks if Torch or TF tensor
											_lowerCamelCase  :    Tuple					=					original_sizes.numpy()

							_lowerCamelCase,				_lowerCamelCase,				_lowerCamelCase  :    Optional[Any]					=					self._check_and_preprocess_points(
							    input_points=lowercase						,   input_labels=lowercase						,   input_boxes=lowercase						,   )

							_lowerCamelCase  :    Tuple					=					self._normalize_and_convert(
							    lowercase						,   lowercase						,   input_points=lowercase						,   input_labels=lowercase						,   input_boxes=lowercase						,   return_tensors=lowercase						,   )

							return encoding_image_processor
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase=None						,   lowercase=None						,   lowercase=None						,   lowercase="pt"						,   ):
							if input_points is not None:
											if len(lowercase						) != len(lowercase						):
															_lowerCamelCase  :    str					=					[
															    self._normalize_coordinates(self.target_size						,   lowercase						,   original_sizes[0]						) for point in input_points
															]
											else:
															_lowerCamelCase  :    Optional[Any]					=					[
															    self._normalize_coordinates(self.target_size						,   lowercase						,   lowercase						)
															    for point, original_size in zip(lowercase						,   lowercase						)
															]
											# check that all arrays have the same shape
											if not all(point.shape == input_points[0].shape for point in input_points						):
															if input_labels is not None:
																			_lowerCamelCase,				_lowerCamelCase  :    List[str]					=					self._pad_points_and_labels(lowercase						,   lowercase						)

											_lowerCamelCase  :    Optional[int]					=					np.array(lowercase						)

							if input_labels is not None:
											_lowerCamelCase  :    List[Any]					=					np.array(lowercase						)

							if input_boxes is not None:
											if len(lowercase						) != len(lowercase						):
															_lowerCamelCase  :    Optional[int]					=					[
															    self._normalize_coordinates(self.target_size						,   lowercase						,   original_sizes[0]						,   is_bounding_box=lowercase						)
															    for box in input_boxes
															]
											else:
															_lowerCamelCase  :    Optional[int]					=					[
															    self._normalize_coordinates(self.target_size						,   lowercase						,   lowercase						,   is_bounding_box=lowercase						)
															    for box, original_size in zip(lowercase						,   lowercase						)
															]
											_lowerCamelCase  :    Any					=					np.array(lowercase						)

							if input_boxes is not None:
											if return_tensors == "pt":
															_lowerCamelCase  :    List[str]					=					torch.from_numpy(lowercase						)
															# boxes batch size of 1 by default
															_lowerCamelCase  :    str					=					input_boxes.unsqueeze(1						) if len(input_boxes.shape						) != 3 else input_boxes
											elif return_tensors == "tf":
															_lowerCamelCase  :    int					=					tf.convert_to_tensor(lowercase						)
															# boxes batch size of 1 by default
															_lowerCamelCase  :    Union[str, Any]					=					tf.expand_dims(lowercase						,   1						) if len(input_boxes.shape						) != 3 else input_boxes
											encoding_image_processor.update({'input_boxes': input_boxes}						)
							if input_points is not None:
											if return_tensors == "pt":
															_lowerCamelCase  :    Optional[Any]					=					torch.from_numpy(lowercase						)
															# point batch size of 1 by default
															_lowerCamelCase  :    Union[str, Any]					=					input_points.unsqueeze(1						) if len(input_points.shape						) != 4 else input_points
											elif return_tensors == "tf":
															_lowerCamelCase  :    List[str]					=					tf.convert_to_tensor(lowercase						)
															# point batch size of 1 by default
															_lowerCamelCase  :    Union[str, Any]					=					tf.expand_dims(lowercase						,   1						) if len(input_points.shape						) != 4 else input_points
											encoding_image_processor.update({'input_points': input_points}						)
							if input_labels is not None:
											if return_tensors == "pt":
															_lowerCamelCase  :    Union[str, Any]					=					torch.from_numpy(lowercase						)
															# point batch size of 1 by default
															_lowerCamelCase  :    Optional[Any]					=					input_labels.unsqueeze(1						) if len(input_labels.shape						) != 3 else input_labels
											elif return_tensors == "tf":
															_lowerCamelCase  :    Dict					=					tf.convert_to_tensor(lowercase						)
															# point batch size of 1 by default
															_lowerCamelCase  :    Any					=					tf.expand_dims(lowercase						,   1						) if len(input_labels.shape						) != 3 else input_labels
											encoding_image_processor.update({'input_labels': input_labels}						)

							return encoding_image_processor
			def       A_					(						self						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Optional[int]					=					max([point.shape[0] for point in input_points]						)
							_lowerCamelCase  :    Optional[int]					=					[]
							for i, point in enumerate(lowercase						):
											if point.shape[0] != expected_nb_points:
															_lowerCamelCase  :    Tuple					=					np.concatenate(
															    [point, np.zeros((expected_nb_points - point.shape[0], 2)						) + self.point_pad_value]						,   axis=0						)
															_lowerCamelCase  :    Optional[Any]					=					np.append(input_labels[i]						,   [self.point_pad_value]						)
											processed_input_points.append(lowercase						)
							_lowerCamelCase  :    Any					=					processed_input_points
							return input_points, input_labels
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase=False						):
							_lowerCamelCase,				_lowerCamelCase  :    List[str]					=					original_size
							_lowerCamelCase,				_lowerCamelCase  :    Optional[int]					=					self.image_processor._get_preprocess_shape(lowercase						,   longest_edge=lowercase						)
							_lowerCamelCase  :    Tuple					=					deepcopy(lowercase						).astype(lowercase						)

							if is_bounding_box:
											_lowerCamelCase  :    List[str]					=					coords.reshape(-1						,   2						,   2						)

							_lowerCamelCase  :    Optional[Any]					=					coords[..., 0] * (new_w / old_w)
							_lowerCamelCase  :    List[Any]					=					coords[..., 1] * (new_h / old_h)

							if is_bounding_box:
											_lowerCamelCase  :    Union[str, Any]					=					coords.reshape(-1						,   4						)

							return coords
			def       A_					(						self						,   lowercase=None						,   lowercase=None						,   lowercase=None						,   ):
							if input_points is not None:
											if hasattr(lowercase						,   'numpy'						):  # Checks for TF or Torch tensor
															_lowerCamelCase  :    int					=					input_points.numpy().tolist()

											if not isinstance(lowercase						,   lowercase						) or not isinstance(input_points[0]						,   lowercase						):
															raise ValueError('Input points must be a list of list of floating points.'						)
											_lowerCamelCase  :    Optional[int]					=					[np.array(lowercase						) for input_point in input_points]
							else:
											_lowerCamelCase  :    Optional[Any]					=					None

							if input_labels is not None:
											if hasattr(lowercase						,   'numpy'						):
															_lowerCamelCase  :    Any					=					input_labels.numpy().tolist()

											if not isinstance(lowercase						,   lowercase						) or not isinstance(input_labels[0]						,   lowercase						):
															raise ValueError('Input labels must be a list of list integers.'						)
											_lowerCamelCase  :    List[str]					=					[np.array(lowercase						) for label in input_labels]
							else:
											_lowerCamelCase  :    List[str]					=					None

							if input_boxes is not None:
											if hasattr(lowercase						,   'numpy'						):
															_lowerCamelCase  :    Optional[int]					=					input_boxes.numpy().tolist()

											if (
											    not isinstance(lowercase						,   lowercase						)
											    or not isinstance(input_boxes[0]						,   lowercase						)
											    or not isinstance(input_boxes[0][0]						,   lowercase						)
											):
															raise ValueError('Input boxes must be a list of list of list of floating points.'						)
											_lowerCamelCase  :    int					=					[np.array(lowercase						).astype(np.floataa						) for box in input_boxes]
							else:
											_lowerCamelCase  :    str					=					None

							return input_points, input_labels, input_boxes
			@property
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[Any]					=					self.image_processor.model_input_names
							return list(dict.fromkeys(lowercase						)						)






			def       A_					(						self						,   *lowercase						,   **lowercase						):
							return self.image_processor.post_process_masks(*lowercase						,   **lowercase						)