





'''simple docstring'''




import unittest

import numpy as np
import requests

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_image_inputs


if is_torch_available():
  import torch

  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  lowercase__    :      Optional[Any]						=    False

if is_vision_available():
  from PIL import Image

  from transformers import PixaStructImageProcessor


class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def __init__(						self						,	_UpperCAmelCase						,	_UpperCAmelCase=7						,	_UpperCAmelCase=3						,	_UpperCAmelCase=18						,	_UpperCAmelCase=30						,	_UpperCAmelCase=400						,	_UpperCAmelCase=None						,	_UpperCAmelCase=True						,	_UpperCAmelCase=True						,	_UpperCAmelCase=None						,	):

          '''simple docstring'''






          __A						: str      =       size if size is not None else {'height': 20, 'width': 20}
          __A						: Union[str, Any]      =       parent
          __A						: Tuple      =       batch_size
          __A						: Optional[Any]      =       num_channels
          __A						: Optional[Any]      =       image_size
          __A						: Any      =       min_resolution
          __A						: Dict      =       max_resolution
          __A						: Optional[int]      =       size
          __A						: Any      =       do_normalize
          __A						: Tuple      =       do_convert_rgb
          __A						: Dict      =       [512, 1024, 2048, 4096]
          __A						: List[str]      =       patch_size if patch_size is not None else {'height': 16, 'width': 16}


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
          __A						: str      =       Image.open(requests.get(_UpperCAmelCase						,	stream=_UpperCAmelCase).raw).convert('RGB')
          return raw_image




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,		reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''					,		)
@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE    (a__					,		unittest.TestCase							):
     lowerCAmelCase					=       PixaStructImageProcessor if is_vision_available() else None


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       PixaStructImageProcessingTester(self)


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return self.image_processor_tester.prepare_image_processor_dict()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_normalize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_convert_rgb'))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       self.image_processor_tester.prepare_dummy_image()

          __A						: Any      =       self.image_processing_class(**self.image_processor_dict)
          __A						: List[Any]      =       2048

          __A						: str      =       image_processor(_UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase)
          self.assertTrue(torch.allclose(inputs.flattened_patches.mean()						,	torch.tensor(0.0606)						,	atol=1e-3						,	rtol=1e-3))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Tuple      =       self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          __A						: List[str]      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	Image.Image)

          # Test not batched input
          __A						: Dict      =       (
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
               # Test not batched input
               __A						: Tuple      =       image_processor(
                   image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(1, max_patch, expected_hidden_dim)						,	)

               # Test batched
               __A						: Optional[Any]      =       image_processor(
                   _UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          __A						: List[Any]      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	Image.Image)

          # Test not batched input
          __A						: Optional[int]      =       (
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          __A						: List[str]      =       True

          for max_patch in self.image_processor_tester.max_patches:
               # Test not batched input
               with self.assertRaises(_UpperCAmelCase):
                    __A						: str      =       image_processor(
                        image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches

               __A						: str      =       'Hello'

               __A						: Tuple      =       image_processor(
                   image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase						,	header_text=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(1, max_patch, expected_hidden_dim)						,	)

               # Test batched
               __A						: Any      =       image_processor(
                   _UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase						,	header_text=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          __A						: Optional[int]      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase						,	numpify=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	np.ndarray)

          __A						: Dict      =       (
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
               # Test not batched input
               __A						: Optional[int]      =       image_processor(
                   image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(1, max_patch, expected_hidden_dim)						,	)

               # Test batched
               __A						: List[str]      =       image_processor(
                   _UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,	)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          __A						: Optional[Any]      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase						,	torchify=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	torch.Tensor)

          # Test not batched input
          __A						: List[str]      =       (
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
               # Test not batched input
               __A						: Optional[int]      =       image_processor(
                   image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(1, max_patch, expected_hidden_dim)						,	)

               # Test batched
               __A						: Any      =       image_processor(
                   _UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,	)




@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,		reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''					,		)
@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE    (a__					,		unittest.TestCase							):
     lowerCAmelCase					=       PixaStructImageProcessor if is_vision_available() else None


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       PixaStructImageProcessingTester(self						,	num_channels=4)
          __A						: int      =       3


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return self.image_processor_tester.prepare_image_processor_dict()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_normalize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,	'do_convert_rgb'))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[Any]      =       self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          __A						: Any      =       prepare_image_inputs(self.image_processor_tester						,	equal_resolution=_UpperCAmelCase)
          for image in image_inputs:
               self.assertIsInstance(_UpperCAmelCase						,	Image.Image)

          # Test not batched input
          __A						: Optional[int]      =       (
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * (self.image_processor_tester.num_channels - 1)
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
               # Test not batched input
               __A						: Optional[Any]      =       image_processor(
                   image_inputs[0]						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(1, max_patch, expected_hidden_dim)						,	)

               # Test batched
               __A						: Any      =       image_processor(
                   _UpperCAmelCase						,	return_tensors='pt'						,	max_patches=_UpperCAmelCase).flattened_patches
               self.assertEqual(
                   encoded_images.shape						,	(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)						,	)






'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast


@require_vision
class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       tempfile.mkdtemp()

          __A						: Optional[int]      =       BlipImageProcessor()
          __A						: List[str]      =       BertTokenizer.from_pretrained('hf-internal-testing/tiny-random-BertModel')

          __A						: Any      =       BlipProcessor(_UpperCAmelCase						,	_UpperCAmelCase)

          processor.save_pretrained(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self						,	**_UpperCAmelCase):

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						self						,	**_UpperCAmelCase):

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          shutil.rmtree(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          __A						: Union[str, Any]      =       [Image.fromarray(np.moveaxis(_UpperCAmelCase						,	0						,	-1)) for x in image_inputs]

          return image_inputs


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       BlipProcessor(tokenizer=self.get_tokenizer()						,	image_processor=self.get_image_processor())
          processor.save_pretrained(self.tmpdirname)

          __A						: int      =       self.get_tokenizer(bos_token='(BOS)'						,	eos_token='(EOS)')
          __A						: List[Any]      =       self.get_image_processor(do_normalize=_UpperCAmelCase						,	padding_value=1.0)

          __A						: Tuple      =       BlipProcessor.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):

          '''simple docstring'''






          __A						: List[str]      =       self.get_image_processor()
          __A						: str      =       self.get_tokenizer()

          __A						: int      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: List[str]      =       self.prepare_image_inputs()

          __A						: str      =       image_processor(_UpperCAmelCase						,	return_tensors='np')
          __A						: str      =       processor(images=_UpperCAmelCase						,	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 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.get_image_processor()
          __A						: Tuple      =       self.get_tokenizer()

          __A						: Union[str, Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: str      =       'lower newer'

          __A						: Dict      =       processor(text=_UpperCAmelCase)

          __A						: Tuple      =       tokenizer(_UpperCAmelCase						,	return_token_type_ids=_UpperCAmelCase)

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[Any]      =       self.get_image_processor()
          __A						: Optional[Any]      =       self.get_tokenizer()

          __A						: Optional[Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: Dict      =       'lower newer'
          __A						: int      =       self.prepare_image_inputs()

          __A						: List[Any]      =       processor(text=_UpperCAmelCase						,	images=_UpperCAmelCase)

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

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.get_image_processor()
          __A						: Optional[Any]      =       self.get_tokenizer()

          __A						: Union[str, Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

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

          __A						: List[str]      =       processor.batch_decode(_UpperCAmelCase)
          __A						: List[str]      =       tokenizer.batch_decode(_UpperCAmelCase)

          self.assertListEqual(_UpperCAmelCase						,	_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.get_image_processor()
          __A						: Union[str, Any]      =       self.get_tokenizer()

          __A						: Optional[Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

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

          __A						: Optional[int]      =       processor(text=_UpperCAmelCase						,	images=_UpperCAmelCase)

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