
'''simple docstring'''





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available


if is_torch_available():
 from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
 a__		:   Optional[int]            =       False

if is_vision_available():
 from PIL import Image

 from transformers import (
     AutoProcessor,
     PixaStructImageProcessor,
     PixaStructProcessor,
     PreTrainedTokenizerFast,
     TaTokenizer,
 )


@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11      ,     reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'      ,     )
@require_vision
@require_torch
class  UpperCamelCase__  (    unittest.TestCase):






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







       '''simple docstring'''



       __A       	=			tempfile.mkdtemp()

       __A       	=			PixaStructImageProcessor()
       __A       	=			TaTokenizer.from_pretrained('t5-small'			)

       __A       	=			PixaStructProcessor(_A	,				_A			)

       processor.save_pretrained(self.tmpdirname			)






 def 	lowercase_						(			self  :Optional[Any]	,				**_A  :int			)				->			Tuple:







       '''simple docstring'''



       return AutoProcessor.from_pretrained(self.tmpdirname	,				**_A			).tokenizer






 def 	lowercase_						(			self  :Optional[Any]	,				**_A  :List[str]			)				->			Tuple:







       '''simple docstring'''



       return AutoProcessor.from_pretrained(self.tmpdirname	,				**_A			).image_processor






 def 	lowercase_						(			self  :List[Any]			)				->			Optional[int]:







       '''simple docstring'''



       shutil.rmtree(self.tmpdirname			)






 def 	lowercase_						(			self  :List[Any]			)				->			Any:







       '''simple docstring'''



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

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

       return image_inputs






 def 	lowercase_						(			self  :Optional[int]			)				->			Tuple:







       '''simple docstring'''



       __A       	=			PixaStructProcessor(tokenizer=self.get_tokenizer()	,				image_processor=self.get_image_processor()			)
       processor.save_pretrained(self.tmpdirname			)

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

       __A       	=			PixaStructProcessor.from_pretrained(
           self.tmpdirname	,				bos_token='(BOS)'	,				eos_token='(EOS)'	,				do_normalize=_A	,				padding_value=1.0			)

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

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






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







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

       __A       	=			self.prepare_image_inputs()

       __A       	=			image_processor(_A	,				return_tensors='np'			)
       __A       	=			processor(images=_A	,				return_tensors='np'			)

       for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum()	,				input_processor[key].sum()	,				delta=1E-2			)






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







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

       __A       	=			'lower newer'

       __A       	=			processor(text=_A			)

       __A       	=			tokenizer(_A	,				return_token_type_ids=_A	,				add_special_tokens=_A			)

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






 def 	lowercase_						(			self  :int			)				->			List[str]:







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

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

       __A       	=			processor(text=_A	,				images=_A			)

       self.assertListEqual(
           list(inputs.keys()			)	,				['flattened_patches', 'attention_mask', 'decoder_attention_mask', 'decoder_input_ids']			)

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






 def 	lowercase_						(			self  :int			)				->			Any:







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

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

       __A       	=			processor(text=_A	,				images=_A			)

       __A       	=			[512, 1_024, 2_048, 4_096]
       __A       	=			[770, 770, 770, 770]
       # with text
       for i, max_patch in enumerate(_A			):
             __A       	=			processor(text=_A	,				images=_A	,				max_patches=_A			)
             self.assertEqual(inputs['flattened_patches'][0].shape[0]	,				_A			)
             self.assertEqual(inputs['flattened_patches'][0].shape[1]	,				expected_hidden_size[i]			)

       # without text input
       for i, max_patch in enumerate(_A			):
             __A       	=			processor(images=_A	,				max_patches=_A			)
             self.assertEqual(inputs['flattened_patches'][0].shape[0]	,				_A			)
             self.assertEqual(inputs['flattened_patches'][0].shape[1]	,				expected_hidden_size[i]			)






 def 	lowercase_						(			self  :Optional[int]			)				->			Tuple:







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

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

       __A       	=			processor.batch_decode(_A			)
       __A       	=			tokenizer.batch_decode(_A			)

       self.assertListEqual(_A	,				_A			)






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







       '''simple docstring'''



       __A       	=			self.get_image_processor()
       __A       	=			self.get_tokenizer()

       __A       	=			PixaStructProcessor(tokenizer=_A	,				image_processor=_A			)

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

       __A       	=			processor(text=_A	,				images=_A			)

       # For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
       self.assertListEqual(
           list(inputs.keys()			)	,				['flattened_patches', 'attention_mask', 'decoder_attention_mask', 'decoder_input_ids']			)

       __A       	=			processor(text=_A			)

       # For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
       self.assertListEqual(list(inputs.keys()			)	,				['input_ids', 'attention_mask']			)








'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import VideoMAEImageProcessor


class  UpperCamelCase__  (    unittest.TestCase):






 def __init__(			self  :Dict	,				_A  :Optional[Any]	,				_A  :Any=7	,				_A  :Dict=3	,				_A  :Optional[Any]=10	,				_A  :List[Any]=18	,				_A  :int=30	,				_A  :List[Any]=400	,				_A  :Any=True	,				_A  :Any=None	,				_A  :Optional[int]=True	,				_A  :Tuple=[0.5, 0.5, 0.5]	,				_A  :int=[0.5, 0.5, 0.5]	,				_A  :List[Any]=None	,				)				->			int:







       '''simple docstring'''



       __A       	=			size if size is not None else {'shortest_edge': 18}
       __A       	=			crop_size if crop_size is not None else {'height': 18, 'width': 18}

       __A       	=			parent
       __A       	=			batch_size
       __A       	=			num_channels
       __A       	=			num_frames
       __A       	=			image_size
       __A       	=			min_resolution
       __A       	=			max_resolution
       __A       	=			do_resize
       __A       	=			size
       __A       	=			do_normalize
       __A       	=			image_mean
       __A       	=			image_std
       __A       	=			crop_size






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







       '''simple docstring'''



       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "size": self.size,
           "crop_size": self.crop_size,
       }


@require_torch
@require_vision
class  UpperCamelCase__  (    SCREAMING_SNAKE_CASE      ,     unittest.TestCase):
 UpperCAmelCase__    :    Tuple										=					VideoMAEImageProcessor if is_vision_available() else None






 def 	lowercase_						(			self  :Dict			)				->			Dict:







       '''simple docstring'''



       __A       	=			VideoMAEImageProcessingTester(self			)






 @property
 def 	lowercase_						(			self  :Dict			)				->			List[str]:







       '''simple docstring'''



       return self.image_processor_tester.prepare_image_processor_dict()






 def 	lowercase_						(			self  :Dict			)				->			Optional[int]:







       '''simple docstring'''



       __A       	=			self.image_processing_class(**self.image_processor_dict			)
       self.assertTrue(hasattr(_A	,				'image_mean'			)			)
       self.assertTrue(hasattr(_A	,				'image_std'			)			)
       self.assertTrue(hasattr(_A	,				'do_normalize'			)			)
       self.assertTrue(hasattr(_A	,				'do_resize'			)			)
       self.assertTrue(hasattr(_A	,				'do_center_crop'			)			)
       self.assertTrue(hasattr(_A	,				'size'			)			)






 def 	lowercase_						(			self  :Optional[int]			)				->			List[Any]:







       '''simple docstring'''



       __A       	=			self.image_processing_class.from_dict(self.image_processor_dict			)
       self.assertEqual(image_processor.size	,				{'shortest_edge': 18}			)
       self.assertEqual(image_processor.crop_size	,				{'height': 18, 'width': 18}			)

       __A       	=			self.image_processing_class.from_dict(self.image_processor_dict	,				size=42	,				crop_size=84			)
       self.assertEqual(image_processor.size	,				{'shortest_edge': 42}			)
       self.assertEqual(image_processor.crop_size	,				{'height': 84, 'width': 84}			)






 def 	lowercase_						(			self  :Any			)				->			Dict:







       '''simple docstring'''



       pass






 def 	lowercase_						(			self  :Optional[int]			)				->			Tuple:







       '''simple docstring'''



       __A       	=			self.image_processing_class(**self.image_processor_dict			)
       # create random PIL videos
       __A       	=			prepare_video_inputs(self.image_processor_tester	,				equal_resolution=_A			)
       for video in video_inputs:
             self.assertIsInstance(_A	,				_A			)
             self.assertIsInstance(video[0]	,				Image.Image			)

       # Test not batched input
       __A       	=			image_processing(video_inputs[0]	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               1,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)

       # Test batched
       __A       	=			image_processing(_A	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)






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







       '''simple docstring'''



       __A       	=			self.image_processing_class(**self.image_processor_dict			)
       # create random numpy tensors
       __A       	=			prepare_video_inputs(self.image_processor_tester	,				equal_resolution=_A	,				numpify=_A			)
       for video in video_inputs:
             self.assertIsInstance(_A	,				_A			)
             self.assertIsInstance(video[0]	,				np.ndarray			)

       # Test not batched input
       __A       	=			image_processing(video_inputs[0]	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               1,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)

       # Test batched
       __A       	=			image_processing(_A	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)






 def 	lowercase_						(			self  :int			)				->			List[str]:







       '''simple docstring'''



       __A       	=			self.image_processing_class(**self.image_processor_dict			)
       # create random PyTorch tensors
       __A       	=			prepare_video_inputs(self.image_processor_tester	,				equal_resolution=_A	,				torchify=_A			)
       for video in video_inputs:
             self.assertIsInstance(_A	,				_A			)
             self.assertIsInstance(video[0]	,				torch.Tensor			)

       # Test not batched input
       __A       	=			image_processing(video_inputs[0]	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               1,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)

       # Test batched
       __A       	=			image_processing(_A	,				return_tensors='pt'			).pixel_values
       self.assertEqual(
           encoded_videos.shape	,				(
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_frames,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )	,				)






