
"""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        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			tempfile.mkdtemp()

											# fmt: off
											_lowerCAmelCase  	=			['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  	=			dict(zip(UpperCAmelCase_				,     range(len(UpperCAmelCase_  )  )  )  )
											_lowerCAmelCase  	=			['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
											_lowerCAmelCase  	=			{'unk_token': '<unk>'}

											_lowerCAmelCase  	=			os.path.join(self.tmpdirname				,     VOCAB_FILES_NAMES['vocab_file']  )
											_lowerCAmelCase  	=			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(UpperCAmelCase_  ) + '\n'  )
											with open(self.merges_file				,     'w'				,     encoding='utf-8'  ) as fp:
																		fp.write('\n'.join(UpperCAmelCase_  )  )

											_lowerCAmelCase  	=			{
											    'do_resize': True,
											    'size': 20,
											    'do_center_crop': True,
											    'crop_size': 18,
											    'do_normalize': True,
											    'image_mean': [0.48145466, 0.4578275, 0.40821073],
											    'image_std': [0.26862954, 0.26130258, 0.27577711],
											}
											_lowerCAmelCase  	=			os.path.join(self.tmpdirname				,     UpperCAmelCase_  )
											with open(self.image_processor_file				,     'w'				,     encoding='utf-8'  ) as fp:
																		json.dump(UpperCAmelCase_				,     UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			str				,     **UpperCAmelCase_	:			str  )		->  Any:







											"""simple docstring"""




											return CLIPTokenizer.from_pretrained(self.tmpdirname				,     **UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			List[str]				,     **UpperCAmelCase_	:			Any  )		->  Union[str, Any]:







											"""simple docstring"""




											return CLIPTokenizerFast.from_pretrained(self.tmpdirname				,     **UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			str				,     **UpperCAmelCase_	:			Dict  )		->  Any:







											"""simple docstring"""




											return CLIPImageProcessor.from_pretrained(self.tmpdirname				,     **UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Tuple:







											"""simple docstring"""




											shutil.rmtree(self.tmpdirname  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			[np.random.randint(255				,     size=(3, 30, 400)				,     dtype=np.uinta  )]

											_lowerCAmelCase  	=			[Image.fromarray(np.moveaxis(UpperCAmelCase_				,     0				,     -1  )  ) for x in image_inputs]

											return image_inputs


				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_tokenizer()
											_lowerCAmelCase  	=			self.get_rust_tokenizer()
											_lowerCAmelCase  	=			self.get_image_processor()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )
											processor_slow.save_pretrained(self.tmpdirname  )
											_lowerCAmelCase  	=			CLIPProcessor.from_pretrained(self.tmpdirname				,     use_fast=UpperCAmelCase_  )

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )
											processor_fast.save_pretrained(self.tmpdirname  )
											_lowerCAmelCase  	=			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				,     UpperCAmelCase_  )
											self.assertIsInstance(processor_fast.tokenizer				,     UpperCAmelCase_  )

											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				,     UpperCAmelCase_  )
											self.assertIsInstance(processor_fast.image_processor				,     UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=self.get_tokenizer()				,     image_processor=self.get_image_processor()  )
											processor.save_pretrained(self.tmpdirname  )

											_lowerCAmelCase  	=			self.get_tokenizer(bos_token='(BOS)'				,     eos_token='(EOS)'  )
											_lowerCAmelCase  	=			self.get_image_processor(do_normalize=UpperCAmelCase_				,     padding_value=1.0  )

											_lowerCAmelCase  	=			CLIPProcessor.from_pretrained(
											    self.tmpdirname				,     bos_token='(BOS)'				,     eos_token='(EOS)'				,     do_normalize=UpperCAmelCase_				,     padding_value=1.0  )

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

											self.assertEqual(processor.image_processor.to_json_string()				,     image_processor_add_kwargs.to_json_string()  )
											self.assertIsInstance(processor.image_processor				,     UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_image_processor()
											_lowerCAmelCase  	=			self.get_tokenizer()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )

											_lowerCAmelCase  	=			self.prepare_image_inputs()

											_lowerCAmelCase  	=			image_processor(UpperCAmelCase_				,     return_tensors='np'  )
											_lowerCAmelCase  	=			processor(images=UpperCAmelCase_				,     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 				__lowerCamelCase	(					self	:			List[str]  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_image_processor()
											_lowerCAmelCase  	=			self.get_tokenizer()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )

											_lowerCAmelCase  	=			'lower newer'

											_lowerCAmelCase  	=			processor(text=UpperCAmelCase_  )

											_lowerCAmelCase  	=			tokenizer(UpperCAmelCase_  )

											for key in encoded_tok.keys():
																		self.assertListEqual(encoded_tok[key]				,     encoded_processor[key]  )


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_image_processor()
											_lowerCAmelCase  	=			self.get_tokenizer()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )

											_lowerCAmelCase  	=			'lower newer'
											_lowerCAmelCase  	=			self.prepare_image_inputs()

											_lowerCAmelCase  	=			processor(text=UpperCAmelCase_				,     images=UpperCAmelCase_  )

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

											# test if it raises when no input is passed
											with pytest.raises(UpperCAmelCase_  ):
																		processor()


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_image_processor()
											_lowerCAmelCase  	=			self.get_tokenizer()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )

											_lowerCAmelCase  	=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											_lowerCAmelCase  	=			processor.batch_decode(UpperCAmelCase_  )
											_lowerCAmelCase  	=			tokenizer.batch_decode(UpperCAmelCase_  )

											self.assertListEqual(UpperCAmelCase_				,     UpperCAmelCase_  )







				def 				__lowerCamelCase	(					self	:			Dict  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.get_image_processor()
											_lowerCAmelCase  	=			self.get_tokenizer()

											_lowerCAmelCase  	=			CLIPProcessor(tokenizer=UpperCAmelCase_				,     image_processor=UpperCAmelCase_  )

											_lowerCAmelCase  	=			'lower newer'
											_lowerCAmelCase  	=			self.prepare_image_inputs()

											_lowerCAmelCase  	=			processor(text=UpperCAmelCase_				,     images=UpperCAmelCase_  )

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








"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_snake_case          =						logging.get_logger(__name__)

_snake_case          =						{
    '''google/mobilenet_v1_1.0_224''': '''https://huggingface.co/google/mobilenet_v1_1.0_224/resolve/main/config.json''',
    '''google/mobilenet_v1_0.75_192''': '''https://huggingface.co/google/mobilenet_v1_0.75_192/resolve/main/config.json''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
}
class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Dict							=					"mobilenet_v1"


				def __init__(					self	:			List[Any]				,     UpperCAmelCase_	:			Optional[int]=3				,     UpperCAmelCase_	:			str=224				,     UpperCAmelCase_	:			Union[str, Any]=1.0				,     UpperCAmelCase_	:			List[str]=8				,     UpperCAmelCase_	:			Union[str, Any]="relu6"				,     UpperCAmelCase_	:			Optional[Any]=True				,     UpperCAmelCase_	:			Optional[int]=0.999				,     UpperCAmelCase_	:			Optional[Any]=0.02				,     UpperCAmelCase_	:			Union[str, Any]=0.001				,     **UpperCAmelCase_	:			List[Any]				,     )		->  Dict:







											"""simple docstring"""




											super().__init__(**UpperCAmelCase_  )

											if depth_multiplier <= 0:
																		raise ValueError('depth_multiplier must be greater than zero.'  )

											_lowerCAmelCase  	=			num_channels
											_lowerCAmelCase  	=			image_size
											_lowerCAmelCase  	=			depth_multiplier
											_lowerCAmelCase  	=			min_depth
											_lowerCAmelCase  	=			hidden_act
											_lowerCAmelCase  	=			tf_padding
											_lowerCAmelCase  	=			classifier_dropout_prob
											_lowerCAmelCase  	=			initializer_range
											_lowerCAmelCase  	=			layer_norm_eps






class        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Optional[int]							=					version.parse("1.11"							)


				@property
				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Mapping[str, Mapping[int, str]]:







											"""simple docstring"""




											return OrderedDict([('pixel_values', {0: 'batch'})]  )


				@property
				def 				__lowerCamelCase	(					self	:			Dict  )		->  Mapping[str, Mapping[int, str]]:







											"""simple docstring"""




											if self.task == "image-classification":
																		return OrderedDict([('logits', {0: 'batch'})]  )
											else:
																		return OrderedDict([('last_hidden_state', {0: 'batch'}), ('pooler_output', {0: 'batch'})]  )







				@property
				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  float:







											"""simple docstring"""




											return 1E-4






