


'''simple docstring'''


import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
 import jax

 from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
 from PIL import Image

 from transformers import BeitImageProcessor
class 				a_       (    unittest.TestCase							):





     def __init__(			self		: Any     ,				lowercase		: Dict     ,				lowercase		: List[str]=100     ,				lowercase		: List[Any]=13     ,				lowercase		: Optional[Any]=30     ,				lowercase		: Any=2     ,				lowercase		: List[str]=3     ,				lowercase		: List[Any]=True     ,				lowercase		: str=True     ,				lowercase		: List[Any]=32     ,				lowercase		: List[str]=5     ,				lowercase		: str=4     ,				lowercase		: Optional[int]=37     ,				lowercase		: Optional[int]="gelu"     ,				lowercase		: Dict=0.1     ,				lowercase		: int=0.1     ,				lowercase		: List[str]=10     ,				lowercase		: Tuple=0.02     ,				lowercase		: List[Any]=3     ,				):





        """simple docstring"""




        lowercase_      :Tuple         =			parent
        lowercase_      :Union[str, Any]         =			vocab_size
        lowercase_      :Dict         =			batch_size
        lowercase_      :str         =			image_size
        lowercase_      :int         =			patch_size
        lowercase_      :Dict         =			num_channels
        lowercase_      :Tuple         =			is_training
        lowercase_      :int         =			use_labels
        lowercase_      :str         =			hidden_size
        lowercase_      :Union[str, Any]         =			num_hidden_layers
        lowercase_      :Tuple         =			num_attention_heads
        lowercase_      :str         =			intermediate_size
        lowercase_      :int         =			hidden_act
        lowercase_      :int         =			hidden_dropout_prob
        lowercase_      :Union[str, Any]         =			attention_probs_dropout_prob
        lowercase_      :Union[str, Any]         =			type_sequence_label_size
        lowercase_      :Any         =			initializer_range

        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        lowercase_      :Tuple         =			(image_size // patch_size) ** 2
        lowercase_      :Union[str, Any]         =			num_patches + 1





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      :Tuple         =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

        lowercase_      :List[Any]         =			BeitConfig(
            vocab_size=self.vocab_size     ,				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=_a     ,				initializer_range=self.initializer_range     ,				)

        return config, pixel_values, labels





     def 							lowercase__				(			self		: Optional[int]     ,				lowercase		: str     ,				lowercase		: Union[str, Any]     ,				lowercase		: Any						):





        """simple docstring"""




        lowercase_      :str         =			FlaxBeitModel(config=_a						)
        lowercase_      :List[Any]         =			model(_a						)
        self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)						)





     def 							lowercase__				(			self		: int     ,				lowercase		: Optional[int]     ,				lowercase		: Optional[Any]     ,				lowercase		: List[Any]						):





        """simple docstring"""




        lowercase_      :int         =			FlaxBeitForMaskedImageModeling(config=_a						)
        lowercase_      :Any         =			model(_a						)
        self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length - 1, self.vocab_size)						)





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: str     ,				lowercase		: str     ,				lowercase		: Tuple						):





        """simple docstring"""




        lowercase_      :List[str]         =			self.type_sequence_label_size
        lowercase_      :Optional[Any]         =			FlaxBeitForImageClassification(config=_a						)
        lowercase_      :Tuple         =			model(_a						)
        self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.type_sequence_label_size)						)

        # test greyscale images
        lowercase_      :Dict         =			1
        lowercase_      :Any         =			FlaxBeitForImageClassification(_a						)

        lowercase_      :Any         =			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        lowercase_      :Optional[Any]         =			model(_a						)







     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      :str         =			self.prepare_config_and_inputs()
        (
            (
            lowercase_				
        )      ,							(
            lowercase_				
        )      ,							(
            lowercase_				
        )      ,											
        )      :Optional[int]         =			config_and_inputs
        lowercase_      :Tuple         =			{"pixel_values": pixel_values}
        return config, inputs_dict







@require_flax
class 				a_       (    _lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      (
         (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
     )





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Tuple         =			FlaxBeitModelTester(self						)
        lowercase_      :Any         =			ConfigTester(self     ,				config_class=_a     ,				has_text_modality=_a     ,				hidden_size=37						)





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Union[str, Any]         =			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowercase_      :Tuple         =			model_class(_a						)
           lowercase_      :str         =			inspect.signature(model.__call__						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase_      :Optional[int]         =			[*signature.parameters.keys()]

           lowercase_      :Tuple         =			["pixel_values"]
           self.assertListEqual(arg_names[:1]     ,				_a						)





     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Optional[Any]         =			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           with self.subTest(model_class.__name__						):
              lowercase_      :Optional[int]         =			self._prepare_for_class(_a     ,				_a						)
              lowercase_      :Optional[int]         =			model_class(_a						)

              @jax.jit
              def model_jitted(lowercase		: Any     ,				**lowercase		: Any						):
                 return model(pixel_values=_a     ,				**_a						)

              with self.subTest("JIT Enabled"						):
                 lowercase_      :str         =			model_jitted(**_a						).to_tuple()

              with self.subTest("JIT Disabled"						):
                 with jax.disable_jit():
                    lowercase_      :Union[str, Any]         =			model_jitted(**_a						).to_tuple()

              self.assertEqual(len(_a						)     ,				len(_a						)						)
              for jitted_output, output in zip(_a     ,				_a						):
                 self.assertEqual(jitted_output.shape     ,				output.shape						)





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :List[Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a						)





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*_a						)





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_a						)







     @slow
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        for model_class_name in self.all_model_classes:
           lowercase_      :int         =			model_class_name.from_pretrained("microsoft/beit-base-patch16-224"						)
           lowercase_      :int         =			model(np.ones((1, 3, 224, 224)						)						)
           self.assertIsNotNone(_a						)







def 							UpperCAmelCase_  (						):
   lowercase_      :int         =			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
   return image


@require_vision
@require_flax
class 				a_       (    unittest.TestCase							):





     @cached_property
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"						) if is_vision_available() else None





     @slow
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"						)

        lowercase_      :Any         =			self.default_image_processor
        lowercase_      :Union[str, Any]         =			prepare_img()
        lowercase_      :List[str]         =			image_processor(images=_a     ,				return_tensors="np"						).pixel_values

        # prepare bool_masked_pos
        lowercase_      :List[str]         =			np.ones((1, 196)     ,				dtype=_a						)

        # forward pass
        lowercase_      :List[str]         =			model(pixel_values=_a     ,				bool_masked_pos=_a						)
        lowercase_      :List[Any]         =			outputs.logits

        # verify the logits
        lowercase_      :Union[str, Any]         =			(1, 196, 8_192)
        self.assertEqual(logits.shape     ,				_a						)

        lowercase_      :int         =			np.array(
            [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]						)

        self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]     ,				_a     ,				atol=1e-2						)						)





     @slow
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      :int         =			FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"						)

        lowercase_      :int         =			self.default_image_processor
        lowercase_      :Optional[int]         =			prepare_img()
        lowercase_      :Tuple         =			image_processor(images=_a     ,				return_tensors="np"						)

        # forward pass
        lowercase_      :Optional[Any]         =			model(**_a						)
        lowercase_      :str         =			outputs.logits

        # verify the logits
        lowercase_      :Optional[int]         =			(1, 1_000)
        self.assertEqual(logits.shape     ,				_a						)

        lowercase_      :Tuple         =			np.array([-1.23_85, -1.09_87, -1.01_08]						)

        self.assertTrue(np.allclose(logits[0, :3]     ,				_a     ,				atol=1e-4						)						)

        lowercase_      :Tuple         =			281
        self.assertEqual(logits.argmax(-1						).item()     ,				_a						)







     @slow
     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"						)

        lowercase_      :List[Any]         =			self.default_image_processor
        lowercase_      :int         =			prepare_img()
        lowercase_      :Any         =			image_processor(images=_a     ,				return_tensors="np"						)

        # forward pass
        lowercase_      :Optional[Any]         =			model(**_a						)
        lowercase_      :Dict         =			outputs.logits

        # verify the logits
        lowercase_      :Dict         =			(1, 21_841)
        self.assertEqual(logits.shape     ,				_a						)

        lowercase_      :int         =			np.array([1.68_81, -0.27_87, 0.59_01]						)

        self.assertTrue(np.allclose(logits[0, :3]     ,				_a     ,				atol=1e-4						)						)

        lowercase_      :Optional[Any]         =			2_396
        self.assertEqual(logits.argmax(-1						).item()     ,				_a						)









'''simple docstring'''


from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
 import PIL

lowerCAmelCase :					Any 							=logging.get_logger(__name__)
def 							UpperCAmelCase_  (						__lowerCamelCase : List[str]			):
   if isinstance(__lowerCamelCase     ,(list, tuple)			) and isinstance(videos[0]     ,(list, tuple)			) and is_valid_image(videos[0][0]			):
      return videos

   elif isinstance(__lowerCamelCase     ,(list, tuple)			) and is_valid_image(videos[0]			):
      return [videos]

   elif is_valid_image(__lowerCamelCase			):
      return [[videos]]

   raise ValueError(F'Could not make batched video from {videos}'			)


class 				a_       (    _lowerCAmelCase							):
     __A								=      ["pixel_values"]





     def __init__(			self		: List[str]     ,				lowercase		: bool = True     ,				lowercase		: Dict[str, int] = None     ,				lowercase		: PILImageResampling = PILImageResampling.BILINEAR     ,				lowercase		: bool = True     ,				lowercase		: Dict[str, int] = None     ,				lowercase		: bool = True     ,				lowercase		: Union[int, float] = 1 / 255     ,				lowercase		: bool = True     ,				lowercase		: bool = True     ,				lowercase		: Optional[Union[float, List[float]]] = None     ,				lowercase		: Optional[Union[float, List[float]]] = None     ,				**lowercase		: Tuple     ,				):





        """simple docstring"""




        super().__init__(**lowercase						)
        lowercase_      :Any         =			size if size is not None else {"shortest_edge": 256}
        lowercase_      :int         =			get_size_dict(lowercase     ,				default_to_square=lowercase						)
        lowercase_      :str         =			crop_size if crop_size is not None else {"height": 224, "width": 224}
        lowercase_      :List[str]         =			get_size_dict(lowercase     ,				param_name="crop_size"						)

        lowercase_      :List[str]         =			do_resize
        lowercase_      :Any         =			size
        lowercase_      :Union[str, Any]         =			do_center_crop
        lowercase_      :Union[str, Any]         =			crop_size
        lowercase_      :Optional[Any]         =			resample
        lowercase_      :List[str]         =			do_rescale
        lowercase_      :List[Any]         =			rescale_factor
        lowercase_      :Dict         =			offset
        lowercase_      :Optional[Any]         =			do_normalize
        lowercase_      :Dict         =			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
        lowercase_      :Optional[int]         =			image_std if image_std is not None else IMAGENET_STANDARD_STD





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: np.ndarray     ,				lowercase		: Dict[str, int]     ,				lowercase		: PILImageResampling = PILImageResampling.BILINEAR     ,				lowercase		: Optional[Union[str, ChannelDimension]] = None     ,				**lowercase		: Optional[Any]     ,				):





        """simple docstring"""




        lowercase_      :List[Any]         =			get_size_dict(lowercase     ,				default_to_square=lowercase						)
        if "shortest_edge" in size:
           lowercase_      :int         =			get_resize_output_image_size(lowercase     ,				size["shortest_edge"]     ,				default_to_square=lowercase						)
        elif "height" in size and "width" in size:
           lowercase_      :Union[str, Any]         =			(size["height"], size["width"])
        else:
           raise ValueError(F'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'						)
        return resize(lowercase     ,				size=lowercase     ,				resample=lowercase     ,				data_format=lowercase     ,				**lowercase						)





     def 							lowercase__				(			self		: str     ,				lowercase		: np.ndarray     ,				lowercase		: Dict[str, int]     ,				lowercase		: Optional[Union[str, ChannelDimension]] = None     ,				**lowercase		: str     ,				):





        """simple docstring"""




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





     def 							lowercase__				(			self		: List[str]     ,				lowercase		: np.ndarray     ,				lowercase		: Union[int, float]     ,				lowercase		: bool = True     ,				lowercase		: Optional[Union[str, ChannelDimension]] = None     ,				**lowercase		: List[str]     ,				):





        """simple docstring"""




        lowercase_      :List[str]         =			image.astype(np.floataa						)
        if offset:
           lowercase_      :List[str]         =			image - (scale / 2)
        return rescale(lowercase     ,				scale=lowercase     ,				data_format=lowercase     ,				**lowercase						)





     def 							lowercase__				(			self		: Tuple     ,				lowercase		: np.ndarray     ,				lowercase		: Union[float, List[float]]     ,				lowercase		: Union[float, List[float]]     ,				lowercase		: Optional[Union[str, ChannelDimension]] = None     ,				**lowercase		: Dict     ,				):





        """simple docstring"""




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





     def 							lowercase__				(			self		: Tuple     ,				lowercase		: ImageInput     ,				lowercase		: bool = None     ,				lowercase		: Dict[str, int] = None     ,				lowercase		: PILImageResampling = None     ,				lowercase		: bool = None     ,				lowercase		: Dict[str, int] = None     ,				lowercase		: bool = None     ,				lowercase		: float = None     ,				lowercase		: bool = None     ,				lowercase		: bool = None     ,				lowercase		: Optional[Union[float, List[float]]] = None     ,				lowercase		: Optional[Union[float, List[float]]] = None     ,				lowercase		: Optional[ChannelDimension] = ChannelDimension.FIRST     ,				):





        """simple docstring"""




        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_size is None:
           raise ValueError("Crop size 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."						)

        if offset and not do_rescale:
           raise ValueError("For offset, do_rescale must also be set to True."						)

        # All transformations expect numpy arrays.
        lowercase_      :Optional[int]         =			to_numpy_array(lowercase						)

        if do_resize:
           lowercase_      :Tuple         =			self.resize(image=lowercase     ,				size=lowercase     ,				resample=lowercase						)

        if do_center_crop:
           lowercase_      :Any         =			self.center_crop(lowercase     ,				size=lowercase						)

        if do_rescale:
           lowercase_      :Optional[Any]         =			self.rescale(image=lowercase     ,				scale=lowercase     ,				offset=lowercase						)

        if do_normalize:
           lowercase_      :Tuple         =			self.normalize(image=lowercase     ,				mean=lowercase     ,				std=lowercase						)

        lowercase_      :Optional[Any]         =			to_channel_dimension_format(lowercase     ,				lowercase						)
        return image







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





        """simple docstring"""




        lowercase_      :str         =			do_resize if do_resize is not None else self.do_resize
        lowercase_      :Optional[Any]         =			resample if resample is not None else self.resample
        lowercase_      :Tuple         =			do_center_crop if do_center_crop is not None else self.do_center_crop
        lowercase_      :Dict         =			do_rescale if do_rescale is not None else self.do_rescale
        lowercase_      :Optional[int]         =			rescale_factor if rescale_factor is not None else self.rescale_factor
        lowercase_      :Dict         =			offset if offset is not None else self.offset
        lowercase_      :Tuple         =			do_normalize if do_normalize is not None else self.do_normalize
        lowercase_      :int         =			image_mean if image_mean is not None else self.image_mean
        lowercase_      :Optional[int]         =			image_std if image_std is not None else self.image_std

        lowercase_      :int         =			size if size is not None else self.size
        lowercase_      :Optional[int]         =			get_size_dict(lowercase     ,				default_to_square=lowercase						)
        lowercase_      :List[Any]         =			crop_size if crop_size is not None else self.crop_size
        lowercase_      :List[str]         =			get_size_dict(lowercase     ,				param_name="crop_size"						)

        if not valid_images(lowercase						):
           raise ValueError(
               "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
               "torch.Tensor, tf.Tensor or jax.ndarray."						)

        lowercase_      :List[str]         =			make_batched(lowercase						)

        lowercase_      :List[Any]         =			[
            [
                self._preprocess_image(
                    image=lowercase     ,				do_resize=lowercase     ,				size=lowercase     ,				resample=lowercase     ,				do_center_crop=lowercase     ,				crop_size=lowercase     ,				do_rescale=lowercase     ,				rescale_factor=lowercase     ,				offset=lowercase     ,				do_normalize=lowercase     ,				image_mean=lowercase     ,				image_std=lowercase     ,				data_format=lowercase     ,				)
                for img in video
            ]
            for video in videos
        ]

        lowercase_      :Optional[int]         =			{"pixel_values": videos}
        return BatchFeature(data=lowercase     ,				tensor_type=lowercase						)


