




import io
import math
from typing import Dict, Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    get_image_size,
    infer_channel_dimension_format,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_vision_available, logging
from ...utils.import_utils import requires_backends


if is_vision_available():
  import textwrap

  from PIL import Image, ImageDraw, ImageFont

if is_torch_available():
  import torch

  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  _lowerCamelCase			:	Dict							 =     False


_lowerCamelCase			:	Tuple							 =     logging.get_logger(__name__)
_lowerCamelCase			:	int							 =     """ybelkada/fonts"""







def SCREAMING_SNAKE_CASE       ( )     -> Tuple:



       """simple docstring"""

       if is_torch_available() and not is_torch_greater_or_equal_than_1_11:
              raise ImportError(
                  f"""You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use """
                  '''Pix2StructImageProcessor. Please upgrade torch.'''		)







def SCREAMING_SNAKE_CASE       ( lowercase_				,       lowercase_				,       lowercase_		)     -> List[Any]:



       """simple docstring"""

       requires_backends(lowercase_				,       ['''torch''']		)
       _check_torch_version()

       A__   				=					image_tensor.unsqueeze(0		)
       A__   				=					torch.nn.functional.unfold(lowercase_				,       (patch_height, patch_width)				,       stride=(patch_height, patch_width)		)
       A__   				=					patches.reshape(image_tensor.size(0		)				,       image_tensor.size(1		)				,       lowercase_				,       lowercase_				,       -1		)
       A__   				=					patches.permute(0				,       4				,       2				,       3				,       1		).reshape(
           image_tensor.size(2		) // patch_height				,       image_tensor.size(3		) // patch_width				,       image_tensor.size(1		) * patch_height * patch_width				,       )
       return patches.unsqueeze(0		)







def SCREAMING_SNAKE_CASE       ( lowercase_				,       lowercase_ = 36				,       lowercase_ = "black"				,       lowercase_ = "white"				,       lowercase_ = 5				,       lowercase_ = 5				,       lowercase_ = 5				,       lowercase_ = 5				,       lowercase_ = None				,       lowercase_ = None				,       )     -> Image.Image:



       """simple docstring"""

       requires_backends(lowercase_				,       '''vision'''		)
       # Add new lines so that each line is no more than 80 characters.

       A__   				=					textwrap.TextWrapper(width=80		)
       A__   				=					wrapper.wrap(text=lowercase_		)
       A__   				=					'''\n'''.join(lowercase_		)

       if font_bytes is not None and font_path is None:
              A__   				=					io.BytesIO(lowercase_		)
       elif font_path is not None:
              A__   				=					font_path
       else:
              A__   				=					hf_hub_download(lowercase_				,       '''Arial.TTF'''		)
       A__   				=					ImageFont.truetype(lowercase_				,       encoding='''UTF-8'''				,       size=lowercase_		)

       # Use a temporary canvas to determine the width and height in pixels when
       # rendering the text.
       A__   				=					ImageDraw.Draw(Image.new('''RGB'''				,       (1, 1)				,       lowercase_		)		)
       A__				,							A__				,							A__				,							A__   				=					temp_draw.textbbox((0, 0)				,       lowercase_				,       lowercase_		)

       # Create the actual image with a bit of padding around the text.
       A__   				=					text_width + left_padding + right_padding
       A__   				=					text_height + top_padding + bottom_padding
       A__   				=					Image.new('''RGB'''				,       (image_width, image_height)				,       lowercase_		)
       A__   				=					ImageDraw.Draw(lowercase_		)
       draw.text(xy=(left_padding, top_padding)				,       text=lowercase_				,       fill=lowercase_				,       font=lowercase_		)
       return image







def SCREAMING_SNAKE_CASE       ( lowercase_				,       lowercase_				,       **lowercase_		)     -> Tuple:



       """simple docstring"""

       requires_backends(lowercase_				,       '''vision'''		)

       # Convert to PIL image if necessary
       A__   				=					to_pil_image(lowercase_		)

       A__   				=					render_text(lowercase_				,       **lowercase_		)
       A__   				=					max(header_image.width				,       image.width		)

       A__   				=					int(image.height * (new_width / image.width)		)
       A__   				=					int(header_image.height * (new_width / header_image.width)		)

       A__   				=					Image.new('''RGB'''				,       (new_width, new_height + new_header_height)				,       '''white'''		)
       new_image.paste(header_image.resize((new_width, new_header_height)		)				,       (0, 0)		)
       new_image.paste(image.resize((new_width, new_height)		)				,       (0, new_header_height)		)

       # Convert back to the original framework if necessary
       A__   				=					to_numpy_array(lowercase_		)

       if infer_channel_dimension_format(lowercase_		) == ChannelDimension.LAST:
              A__   				=					to_channel_dimension_format(lowercase_				,       ChannelDimension.LAST		)

       return new_image


class        UpperCamelCase_					(				UpperCAmelCase__  ):

    '''simple docstring'''






    UpperCAmelCase__													=   ['''flattened_patches''']


    def __init__(    self   :			str      ,				UpperCAmelCase__   :			bool = True      ,				UpperCAmelCase__   :			bool = True      ,				UpperCAmelCase__   :			Dict[str, int] = None      ,				UpperCAmelCase__   :			int = 2_048      ,				UpperCAmelCase__   :			bool = False      ,				**UpperCAmelCase__   :			Union[str, Any]      ,				)		->None:





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)
           A__   				=					patch_size if patch_size is not None else {'''height''': 16, '''width''': 16}
           A__   				=					do_normalize
           A__   				=					do_convert_rgb
           A__   				=					max_patches
           A__   				=					is_vqa


    def  SCREAMING_SNAKE_CASE							(    self   :			Union[str, Any]      ,				UpperCAmelCase__   :			np.ndarray      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			dict      ,				**UpperCAmelCase__   :			int)		->np.ndarray:





           '''simple docstring'''

           requires_backends(self.extract_flattened_patches      ,				'''torch''')
           _check_torch_version()

           # convert to torch
           A__   				=					to_channel_dimension_format(UpperCAmelCase__      ,				ChannelDimension.FIRST)
           A__   				=					torch.from_numpy(UpperCAmelCase__)

           A__				,							A__   				=					patch_size['''height'''], patch_size['''width''']
           A__				,							A__   				=					get_image_size(UpperCAmelCase__)

           # maximize scale s.t.
           A__   				=					math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width))
           A__   				=					max(min(math.floor(scale * image_height / patch_height)      ,				UpperCAmelCase__)      ,				1)
           A__   				=					max(min(math.floor(scale * image_width / patch_width)      ,				UpperCAmelCase__)      ,				1)
           A__   				=					max(num_feasible_rows * patch_height      ,				1)
           A__   				=					max(num_feasible_cols * patch_width      ,				1)

           A__   				=					torch.nn.functional.interpolate(
               image.unsqueeze(0)      ,				size=(resized_height, resized_width)      ,				mode='''bilinear'''      ,				align_corners=UpperCAmelCase__      ,				antialias=UpperCAmelCase__      ,				).squeeze(0)

           # [1, rows, columns, patch_height * patch_width * image_channels]
           A__   				=					torch_extract_patches(UpperCAmelCase__      ,				UpperCAmelCase__      ,				UpperCAmelCase__)

           A__   				=					patches.shape
           A__   				=					patches_shape[1]
           A__   				=					patches_shape[2]
           A__   				=					patches_shape[3]

           # [rows * columns, patch_height * patch_width * image_channels]
           A__   				=					patches.reshape([rows * columns, depth])

           # [rows * columns, 1]
           A__   				=					torch.arange(UpperCAmelCase__).reshape([rows, 1]).repeat(1      ,				UpperCAmelCase__).reshape([rows * columns, 1])
           A__   				=					torch.arange(UpperCAmelCase__).reshape([1, columns]).repeat(UpperCAmelCase__      ,				1).reshape([rows * columns, 1])

           # Offset by 1 so the ids do not contain zeros, which represent padding.
           row_ids += 1
           col_ids += 1

           # Prepare additional patch features.
           # [rows * columns, 1]
           A__   				=					row_ids.to(torch.floataa)
           A__   				=					col_ids.to(torch.floataa)

           # [rows * columns, 2 + patch_height * patch_width * image_channels]
           A__   				=					torch.cat([row_ids, col_ids, patches]      ,				-1)

           # [max_patches, 2 + patch_height * patch_width * image_channels]
           A__   				=					torch.nn.functional.pad(UpperCAmelCase__      ,				[0, 0, 0, max_patches - (rows * columns)]).float()

           A__   				=					to_numpy_array(UpperCAmelCase__)

           return result


    def  SCREAMING_SNAKE_CASE							(    self   :			int      ,				UpperCAmelCase__   :			np.ndarray      ,				UpperCAmelCase__   :			Optional[Union[str, ChannelDimension]] = None      ,				**UpperCAmelCase__   :			Tuple)		->np.ndarray:





           '''simple docstring'''

           if image.dtype == np.uinta:
                  A__   				=					image.astype(np.floataa)

           # take mean across the whole `image`
           A__   				=					np.mean(UpperCAmelCase__)
           A__   				=					np.std(UpperCAmelCase__)
           A__   				=					max(UpperCAmelCase__      ,				1.0 / math.sqrt(np.prod(image.shape)))

           return normalize(UpperCAmelCase__      ,				mean=UpperCAmelCase__      ,				std=UpperCAmelCase__      ,				**UpperCAmelCase__)





    def  SCREAMING_SNAKE_CASE							(    self   :			Tuple      ,				UpperCAmelCase__   :			ImageInput      ,				UpperCAmelCase__   :			Optional[str] = None      ,				UpperCAmelCase__   :			bool = None      ,				UpperCAmelCase__   :			Optional[bool] = None      ,				UpperCAmelCase__   :			Optional[int] = None      ,				UpperCAmelCase__   :			Optional[Dict[str, int]] = None      ,				UpperCAmelCase__   :			Optional[Union[str, TensorType]] = None      ,				UpperCAmelCase__   :			ChannelDimension = ChannelDimension.FIRST      ,				**UpperCAmelCase__   :			Any      ,				)		->ImageInput:





           '''simple docstring'''

           A__   				=					do_normalize if do_normalize is not None else self.do_normalize
           A__   				=					do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb
           A__   				=					patch_size if patch_size is not None else self.patch_size
           A__   				=					max_patches if max_patches is not None else self.max_patches
           A__   				=					self.is_vqa

           if kwargs.get('''data_format'''      ,				UpperCAmelCase__) is not None:
                  raise ValueError('''data_format is not an accepted input as the outputs are ''')

           A__   				=					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.''')

           # PIL RGBA images are converted to RGB
           if do_convert_rgb:
                  A__   				=					[convert_to_rgb(UpperCAmelCase__) for image in images]

           # All transformations expect numpy arrays.
           A__   				=					[to_numpy_array(UpperCAmelCase__) for image in images]

           if is_vqa:
                  if header_text is None:
                         raise ValueError('''A header text must be provided for VQA models.''')
                  A__   				=					kwargs.pop('''font_bytes'''      ,				UpperCAmelCase__)
                  A__   				=					kwargs.pop('''font_path'''      ,				UpperCAmelCase__)

                  if isinstance(UpperCAmelCase__      ,				UpperCAmelCase__):
                         A__   				=					[header_text] * len(UpperCAmelCase__)

                  A__   				=					[
                      render_header(UpperCAmelCase__      ,				header_text[i]      ,				font_bytes=UpperCAmelCase__      ,				font_path=UpperCAmelCase__)
                      for i, image in enumerate(UpperCAmelCase__)
                  ]

           if do_normalize:
                  A__   				=					[self.normalize(image=UpperCAmelCase__) for image in images]

           # convert to torch tensor and permute
           A__   				=					[
               self.extract_flattened_patches(image=UpperCAmelCase__      ,				max_patches=UpperCAmelCase__      ,				patch_size=UpperCAmelCase__)
               for image in images
           ]

           # create attention mask in numpy
           A__   				=					[(image.sum(axis=-1) != 0).astype(np.floataa) for image in images]

           A__   				=					BatchFeature(
               data={'''flattened_patches''': images, '''attention_mask''': attention_masks}      ,				tensor_type=UpperCAmelCase__)

           return encoded_outputs








from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
  from PIL import Image

  from transformers import ViTImageProcessor


class        UpperCamelCase_					:

    '''simple docstring'''






    def __init__(    self   :			Dict      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			str=13      ,				UpperCAmelCase__   :			Union[str, Any]=30      ,				UpperCAmelCase__   :			List[str]=2      ,				UpperCAmelCase__   :			List[Any]=3      ,				UpperCAmelCase__   :			int=True      ,				UpperCAmelCase__   :			Tuple=True      ,				UpperCAmelCase__   :			Optional[int]=32      ,				UpperCAmelCase__   :			Tuple=2      ,				UpperCAmelCase__   :			Optional[Any]=4      ,				UpperCAmelCase__   :			Any=37      ,				UpperCAmelCase__   :			List[Any]="gelu"      ,				UpperCAmelCase__   :			Dict=0.1      ,				UpperCAmelCase__   :			List[Any]=0.1      ,				UpperCAmelCase__   :			Union[str, Any]=10      ,				UpperCAmelCase__   :			int=0.02      ,				UpperCAmelCase__   :			Optional[Any]=3      ,				UpperCAmelCase__   :			List[Any]=None      ,				)		->Dict:





           '''simple docstring'''

           A__   				=					parent
           A__   				=					batch_size
           A__   				=					image_size
           A__   				=					patch_size
           A__   				=					num_channels
           A__   				=					is_training
           A__   				=					use_labels
           A__   				=					hidden_size
           A__   				=					num_hidden_layers
           A__   				=					num_attention_heads
           A__   				=					intermediate_size
           A__   				=					hidden_act
           A__   				=					hidden_dropout_prob
           A__   				=					attention_probs_dropout_prob
           A__   				=					type_sequence_label_size
           A__   				=					initializer_range
           A__   				=					scope

           # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           A__   				=					(image_size // patch_size) ** 2
           A__   				=					num_patches + 1


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





           '''simple docstring'''

           A__   				=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

           A__   				=					None
           if self.use_labels:
                  A__   				=					ids_tensor([self.batch_size]      ,				self.type_sequence_label_size)

           A__   				=					self.get_config()

           return config, pixel_values, labels


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





           '''simple docstring'''

           return ViTConfig(
               image_size=self.image_size      ,				patch_size=self.patch_size      ,				num_channels=self.num_channels      ,				hidden_size=self.hidden_size      ,				num_hidden_layers=self.num_hidden_layers      ,				num_attention_heads=self.num_attention_heads      ,				intermediate_size=self.intermediate_size      ,				hidden_act=self.hidden_act      ,				hidden_dropout_prob=self.hidden_dropout_prob      ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,				is_decoder=UpperCAmelCase__      ,				initializer_range=self.initializer_range      ,				)


    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any]      ,				UpperCAmelCase__   :			List[str]      ,				UpperCAmelCase__   :			Any      ,				UpperCAmelCase__   :			str)		->Tuple:





           '''simple docstring'''

           A__   				=					TFViTModel(config=UpperCAmelCase__)
           A__   				=					model(UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size))

           # Test with an image with different size than the one specified in config.
           A__   				=					self.image_size // 2
           A__   				=					pixel_values[:, :, :image_size, :image_size]
           A__   				=					model(UpperCAmelCase__      ,				interpolate_pos_encoding=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           A__   				=					(image_size // self.patch_size) ** 2 + 1
           self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, seq_length, self.hidden_size))


    def  SCREAMING_SNAKE_CASE							(    self   :			Any      ,				UpperCAmelCase__   :			Tuple      ,				UpperCAmelCase__   :			List[Any]      ,				UpperCAmelCase__   :			List[str])		->List[str]:





           '''simple docstring'''

           A__   				=					self.type_sequence_label_size
           A__   				=					TFViTForImageClassification(UpperCAmelCase__)
           A__   				=					model(UpperCAmelCase__      ,				labels=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))

           # Test with an image with different size than the one specified in config.
           A__   				=					self.image_size // 2
           A__   				=					pixel_values[:, :, :image_size, :image_size]
           A__   				=					model(UpperCAmelCase__      ,				interpolate_pos_encoding=UpperCAmelCase__      ,				training=UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))

           # test greyscale images
           A__   				=					1
           A__   				=					TFViTForImageClassification(UpperCAmelCase__)
           A__   				=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
           A__   				=					model(UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.type_sequence_label_size))





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





           '''simple docstring'''

           A__   				=					self.prepare_config_and_inputs()
           A__				,							A__				,							A__   				=					config_and_inputs
           A__   				=					{'''pixel_values''': pixel_values}
           return config, inputs_dict


@require_tf
class        UpperCamelCase_					(				UpperCAmelCase__	,      UpperCAmelCase__	,      unittest.TestCase  ):

    '''simple docstring'''






    UpperCAmelCase__													=   (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
    UpperCAmelCase__													=   (
        {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
        if is_tf_available()
        else {}
    )

    UpperCAmelCase__													=   False
    UpperCAmelCase__													=   False
    UpperCAmelCase__													=   False


    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Optional[int]:





           '''simple docstring'''

           A__   				=					TFViTModelTester(self)
           A__   				=					ConfigTester(self      ,				config_class=UpperCAmelCase__      ,				has_text_modality=UpperCAmelCase__      ,				hidden_size=37)


    def  SCREAMING_SNAKE_CASE							(    self   :			str)		->List[str]:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    @unittest.skip(reason='''ViT does not use inputs_embeds''')
    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any])		->Tuple:





           '''simple docstring'''

           pass


    @unittest.skip(reason='''ViT does not use inputs_embeds''')
    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Optional[Any]:





           '''simple docstring'''

           pass


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





           '''simple docstring'''

           A__				,							A__   				=					self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__   				=					model_class(UpperCAmelCase__)
                  self.assertIsInstance(model.get_input_embeddings()      ,				(tf.keras.layers.Layer))
                  A__   				=					model.get_output_embeddings()
                  self.assertTrue(x is None or isinstance(UpperCAmelCase__      ,				tf.keras.layers.Layer))


    def  SCREAMING_SNAKE_CASE							(    self   :			int)		->Any:





           '''simple docstring'''

           A__				,							A__   				=					self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A__   				=					model_class(UpperCAmelCase__)
                  A__   				=					inspect.signature(model.call)
                  # signature.parameters is an OrderedDict => so arg_names order is deterministic
                  A__   				=					[*signature.parameters.keys()]

                  A__   				=					['''pixel_values''']
                  self.assertListEqual(arg_names[:1]      ,				UpperCAmelCase__)


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





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Optional[Any]:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__)





    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->List[Any]:





           '''simple docstring'''

           A__   				=					TFViTModel.from_pretrained('''google/vit-base-patch16-224''')
           self.assertIsNotNone(UpperCAmelCase__)







def SCREAMING_SNAKE_CASE       ( )     -> int:



       """simple docstring"""

       A__   				=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
       return image


@require_tf
@require_vision
class        UpperCamelCase_					(				unittest.TestCase  ):

    '''simple docstring'''






    @cached_property
    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Optional[Any]:





           '''simple docstring'''

           return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224''') if is_vision_available() else None





    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Any)		->int:





           '''simple docstring'''

           A__   				=					TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224''')

           A__   				=					self.default_image_processor
           A__   				=					prepare_img()
           A__   				=					image_processor(images=UpperCAmelCase__      ,				return_tensors='''tf''')

           # forward pass
           A__   				=					model(**UpperCAmelCase__)

           # verify the logits
           A__   				=					tf.TensorShape((1, 1_000))
           self.assertEqual(outputs.logits.shape      ,				UpperCAmelCase__)

           A__   				=					tf.constant([-0.2744, 0.8215, -0.0836])

           tf.debugging.assert_near(outputs.logits[0, :3]      ,				UpperCAmelCase__      ,				atol=1e-4)


