

import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert 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 AlignProcessor, EfficientNetImageProcessor

@require_vision
class 				__UpperCamelCase				(					unittest.TestCase			):







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						__magic_name__							=    tempfile.mkdtemp()

						__magic_name__							=    [
						    '[UNK]',
						    '[CLS]',
						    '[SEP]',
						    '[PAD]',
						    '[MASK]',
						    'want',
						    '##want',
						    '##ed',
						    'wa',
						    'un',
						    'runn',
						    '##ing',
						    ',',
						    'low',
						    'lowest',
						]
						__magic_name__							=    os.path.join(self.tmpdirname  , VOCAB_FILES_NAMES['vocab_file']      )
						with open(self.vocab_file  , 'w'  , encoding='utf-8'      ) as vocab_writer:
											vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]      )      )

						__magic_name__							=    {
						    '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],
						}
						__magic_name__							=    os.path.join(self.tmpdirname  , __UpperCamelCase      )
						with open(self.image_processor_file  , 'w'  , encoding='utf-8'      ) as fp:
											json.dump(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str  , **__UpperCamelCase: Optional[int]      ):

						'''simple docstring'''





						return BertTokenizer.from_pretrained(self.tmpdirname  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any  , **__UpperCamelCase: Tuple      ):

						'''simple docstring'''





						return BertTokenizerFast.from_pretrained(self.tmpdirname  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , **__UpperCamelCase: List[str]      ):

						'''simple docstring'''





						return EfficientNetImageProcessor.from_pretrained(self.tmpdirname  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





						__magic_name__							=    [np.random.randint(2_55  , size=(3, 30, 4_00)  , dtype=np.uinta      )]
						__magic_name__							=    [Image.fromarray(np.moveaxis(__UpperCamelCase  , 0  , -1      )      ) for x in image_inputs]
						return image_inputs







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





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

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )
						processor_slow.save_pretrained(self.tmpdirname      )
						__magic_name__							=    AlignProcessor.from_pretrained(self.tmpdirname  , use_fast=__UpperCamelCase      )

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )
						processor_fast.save_pretrained(self.tmpdirname      )
						__magic_name__							=    AlignProcessor.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 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						__magic_name__							=    AlignProcessor(tokenizer=self.get_tokenizer()  , image_processor=self.get_image_processor()      )
						processor.save_pretrained(self.tmpdirname      )

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

						__magic_name__							=    AlignProcessor.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 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





						__magic_name__							=    self.get_image_processor()
						__magic_name__							=    self.get_tokenizer()

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )

						__magic_name__							=    self.prepare_image_inputs()

						__magic_name__							=    image_processor(__UpperCamelCase  , return_tensors='np'      )
						__magic_name__							=    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 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    self.get_image_processor()
						__magic_name__							=    self.get_tokenizer()

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )

						__magic_name__							=    'lower newer'

						__magic_name__							=    processor(text=__UpperCamelCase      )

						__magic_name__							=    tokenizer(__UpperCamelCase  , padding='max_length'  , max_length=64      )

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







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						__magic_name__							=    self.get_image_processor()
						__magic_name__							=    self.get_tokenizer()

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )

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

						__magic_name__							=    processor(text=__UpperCamelCase  , images=__UpperCamelCase      )

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

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







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    self.get_image_processor()
						__magic_name__							=    self.get_tokenizer()

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )

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

						__magic_name__							=    processor.batch_decode(__UpperCamelCase      )
						__magic_name__							=    tokenizer.batch_decode(__UpperCamelCase      )

						self.assertListEqual(__UpperCamelCase  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						__magic_name__							=    self.get_image_processor()
						__magic_name__							=    self.get_tokenizer()

						__magic_name__							=    AlignProcessor(tokenizer=__UpperCamelCase  , image_processor=__UpperCamelCase      )

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

						__magic_name__							=    processor(text=__UpperCamelCase  , images=__UpperCamelCase      )

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







from typing import Dict, List, Optional, 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_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
						import PIL


A__						=							logging.get_logger(__name__)

class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
	_lowercase  :   Tuple       				=  ["pixel_values"]







	def __init__(   self: Optional[Any]  , __UpperCamelCase: bool = True  , __UpperCamelCase: Dict[str, int] = None  , __UpperCamelCase: int = 0.9  , __UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC  , __UpperCamelCase: bool = True  , __UpperCamelCase: Dict[str, int] = None  , __UpperCamelCase: Union[int, float] = 1 / 2_55  , __UpperCamelCase: bool = True  , __UpperCamelCase: bool = True  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , **__UpperCamelCase: List[str]  , ):

						'''simple docstring'''





						super().__init__(**__UpperCamelCase      )
						__magic_name__							=    size if size is not None else {'shortest_edge': 2_24}
						__magic_name__							=    get_size_dict(__UpperCamelCase  , default_to_square=__UpperCamelCase      )
						__magic_name__							=    crop_size if crop_size is not None else {'height': 2_24, 'width': 2_24}
						__magic_name__							=    get_size_dict(__UpperCamelCase  , param_name='crop_size'      )

						__magic_name__							=    do_resize
						__magic_name__							=    size
						__magic_name__							=    crop_pct
						__magic_name__							=    resample
						__magic_name__							=    do_center_crop
						__magic_name__							=    crop_size
						__magic_name__							=    do_rescale
						__magic_name__							=    rescale_factor
						__magic_name__							=    do_normalize
						__magic_name__							=    image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
						__magic_name__							=    image_std if image_std is not None else IMAGENET_DEFAULT_STD







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Dict[str, int]  , __UpperCamelCase: Optional[float] = None  , __UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





						__magic_name__							=    get_size_dict(__UpperCamelCase  , default_to_square=__UpperCamelCase      )
						if "shortest_edge" not in size and ("height" not in size or "width" not in size):
											raise ValueError(F'size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'      )
						if crop_pct is not None:
											if "shortest_edge" in size:
																__magic_name__							=    int(size['shortest_edge'] / crop_pct      )
											elif "height" in size and "width" in size:
																if size["height"] == size["width"]:
																					__magic_name__							=    int(size['height'] / crop_pct      )
																else:
																					__magic_name__							=    (int(size['height'] / crop_pct      ), int(size['width'] / crop_pct      ))
											else:
																raise ValueError('Invalid size for resize: {}'.format(__UpperCamelCase      )      )

											__magic_name__							=    get_resize_output_image_size(__UpperCamelCase  , size=__UpperCamelCase  , default_to_square=__UpperCamelCase      )
						else:
											if "shortest_edge" in size:
																__magic_name__							=    get_resize_output_image_size(__UpperCamelCase  , size=size['shortest_edge']  , default_to_square=__UpperCamelCase      )
											elif "height" in size and "width" in size:
																__magic_name__							=    (size['height'], size['width'])
											else:
																raise ValueError('Invalid size for resize: {}'.format(__UpperCamelCase      )      )

						return resize(__UpperCamelCase  , size=__UpperCamelCase  , resample=__UpperCamelCase  , data_format=__UpperCamelCase  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Dict[str, int]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: List[str]  , ):

						'''simple docstring'''





						__magic_name__							=    get_size_dict(__UpperCamelCase      )
						if "height" not in size or "width" not in size:
											raise ValueError(F'size must contain \'height\' and \'width\' as keys. Got {size.keys()}'      )
						return center_crop(__UpperCamelCase  , size=(size['height'], size['width'])  , data_format=__UpperCamelCase  , **__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Union[int, float]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





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







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Union[float, List[float]]  , __UpperCamelCase: Union[float, List[float]]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Optional[int]  , ):

						'''simple docstring'''





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







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

						'''simple docstring'''





						__magic_name__							=    do_resize if do_resize is not None else self.do_resize
						__magic_name__							=    crop_pct if crop_pct is not None else self.crop_pct
						__magic_name__							=    resample if resample is not None else self.resample
						__magic_name__							=    do_center_crop if do_center_crop is not None else self.do_center_crop
						__magic_name__							=    do_rescale if do_rescale is not None else self.do_rescale
						__magic_name__							=    rescale_factor if rescale_factor is not None else self.rescale_factor
						__magic_name__							=    do_normalize if do_normalize is not None else self.do_normalize
						__magic_name__							=    image_mean if image_mean is not None else self.image_mean
						__magic_name__							=    image_std if image_std is not None else self.image_std

						__magic_name__							=    size if size is not None else self.size
						__magic_name__							=    get_size_dict(__UpperCamelCase  , default_to_square=__UpperCamelCase      )
						__magic_name__							=    crop_size if crop_size is not None else self.crop_size
						__magic_name__							=    get_size_dict(__UpperCamelCase  , param_name='crop_size'      )

						__magic_name__							=    make_list_of_images(__UpperCamelCase      )

						if not valid_images(__UpperCamelCase      ):
											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 or resample is None:
											raise ValueError('Size and resample must be specified if do_resize is True.'      )

						if do_center_crop and crop_pct is None:
											raise ValueError('Crop_pct 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.
						__magic_name__							=    [to_numpy_array(__UpperCamelCase      ) for image in images]

						if do_resize:
											__magic_name__							=    [self.resize(image=__UpperCamelCase  , size=__UpperCamelCase  , crop_pct=__UpperCamelCase  , resample=__UpperCamelCase      ) for image in images]

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

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

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

						__magic_name__							=    [to_channel_dimension_format(__UpperCamelCase  , __UpperCamelCase      ) for image in images]

						__magic_name__							=    {'pixel_values': images}
						return BatchFeature(data=__UpperCamelCase  , tensor_type=__UpperCamelCase      )




