




import random
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_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      import PIL

      from transformers import FlavaImageProcessor
      from transformers.image_utils import PILImageResampling
      from transformers.models.flava.image_processing_flava import (
          FLAVA_CODEBOOK_MEAN,
          FLAVA_CODEBOOK_STD,
          FLAVA_IMAGE_MEAN,
          FLAVA_IMAGE_STD,
      )
else:
      __SCREAMING_SNAKE_CASE      :				Any				      =None






class      A     (						unittest.TestCase						):
     """simple docstring"""

     def __init__(      self					:				int		,					A__					:				int		,					A__					:				Union[str, Any]=7		,					A__					:				str=3		,					A__					:				Any=3_0		,					A__					:				Optional[int]=4_0_0		,					A__					:				Any=True		,					A__					:				str=None		,					A__					:				List[str]=True		,					A__					:				List[Any]=None		,					A__					:				int=None		,					A__					:				str=True		,					A__					:				str=1 / 2_5_5		,					A__					:				Optional[int]=True		,					A__					:				Optional[Any]=FLAVA_IMAGE_MEAN		,					A__					:				Tuple=FLAVA_IMAGE_STD		,					A__					:				List[Any]=1_4		,					A__					:				Any=7_5		,					A__					:				int=None		,					A__					:				List[Any]=1_6		,					A__					:				int=0.3		,					A__					:				Any=None		,					A__					:				Any=True		,					A__					:				Any=None		,					A__					:				Any=None		,					A__					:				List[str]=True		,					A__					:				Tuple=None		,					A__					:				Any=True		,					A__					:				Any=True		,					A__					:				Optional[int]=FLAVA_CODEBOOK_MEAN		,					A__					:				Union[str, Any]=FLAVA_CODEBOOK_STD		,					)			->    Tuple:
           lowerCamelCase__ 					=			size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
           lowerCamelCase__ 					=			crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
           lowerCamelCase__ 					=			codebook_size if codebook_size is not None else {'height': 1_1_2, 'width': 1_1_2}
           lowerCamelCase__ 					=			codebook_crop_size if codebook_crop_size is not None else {'height': 1_1_2, 'width': 1_1_2}

           lowerCamelCase__ 					=			parent
           lowerCamelCase__ 					=			batch_size
           lowerCamelCase__ 					=			num_channels
           lowerCamelCase__ 					=			do_resize
           lowerCamelCase__ 					=			do_rescale
           lowerCamelCase__ 					=			rescale_factor
           lowerCamelCase__ 					=			min_resolution
           lowerCamelCase__ 					=			max_resolution
           lowerCamelCase__ 					=			size
           lowerCamelCase__ 					=			resample if resample is not None else PILImageResampling.BICUBIC
           lowerCamelCase__ 					=			do_normalize
           lowerCamelCase__ 					=			image_mean
           lowerCamelCase__ 					=			image_std
           lowerCamelCase__ 					=			do_center_crop
           lowerCamelCase__ 					=			crop_size

           lowerCamelCase__ 					=			input_size_patches
           lowerCamelCase__ 					=			total_mask_patches
           lowerCamelCase__ 					=			mask_group_max_patches
           lowerCamelCase__ 					=			mask_group_min_patches
           lowerCamelCase__ 					=			mask_group_min_aspect_ratio
           lowerCamelCase__ 					=			mask_group_max_aspect_ratio

           lowerCamelCase__ 					=			codebook_do_resize
           lowerCamelCase__ 					=			codebook_size
           lowerCamelCase__ 					=			codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
           lowerCamelCase__ 					=			codebook_do_center_crop
           lowerCamelCase__ 					=			codebook_crop_size
           lowerCamelCase__ 					=			codebook_do_map_pixels
           lowerCamelCase__ 					=			codebook_do_normalize
           lowerCamelCase__ 					=			codebook_image_mean
           lowerCamelCase__ 					=			codebook_image_std




     def 	__snake_case				(      self					:				Tuple     )			->    int:
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "resample": self.resample,
               "do_rescale": self.do_rescale,
               "rescale_factor": self.rescale_factor,
               "do_center_crop": self.do_center_crop,
               "crop_size": self.crop_size,
               "input_size_patches": self.input_size_patches,
               "total_mask_patches": self.total_mask_patches,
               "mask_group_max_patches": self.mask_group_max_patches,
               "mask_group_min_patches": self.mask_group_min_patches,
               "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
               "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
               "codebook_do_resize": self.codebook_do_resize,
               "codebook_size": self.codebook_size,
               "codebook_resample": self.codebook_resample,
               "codebook_do_center_crop": self.codebook_do_center_crop,
               "codebook_crop_size": self.codebook_crop_size,
               "codebook_do_map_pixels": self.codebook_do_map_pixels,
               "codebook_do_normalize": self.codebook_do_normalize,
               "codebook_image_mean": self.codebook_image_mean,
               "codebook_image_std": self.codebook_image_std,
           }




     def 	__snake_case				(      self					:				List[str]     )			->    Optional[Any]:
           return (self.size["height"], self.size["width"])




     def 	__snake_case				(      self					:				List[str]     )			->    List[str]:
           return (
               (self.input_size_patches, self.input_size_patches)
               if not isinstance(self.input_size_patches		,					A__     )
               else self.input_size_patches
           )




     def 	__snake_case				(      self					:				List[str]     )			->    Union[str, Any]:
           return (self.codebook_size["height"], self.codebook_size["width"])






@require_torch
@require_vision
class      A     (						_lowerCAmelCase     ,  unittest.TestCase						):
     """simple docstring"""

     _snake_case:		List[str]       =FlavaImageProcessor if is_vision_available() else None
     _snake_case:		str       =None




     def 	__snake_case				(      self					:				Tuple     )			->    int:
           lowerCamelCase__ 					=			FlavaImageProcessingTester(self     )




     @property
     def 	__snake_case				(      self					:				Tuple     )			->    Union[str, Any]:
           return self.image_processor_tester.prepare_image_processor_dict()




     def 	__snake_case				(      self					:				Dict     )			->    Union[str, Any]:
           lowerCamelCase__ 					=			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__		,					'resample'     )     )
           self.assertTrue(hasattr(A__		,					'crop_size'     )     )
           self.assertTrue(hasattr(A__		,					'do_center_crop'     )     )
           self.assertTrue(hasattr(A__		,					'do_rescale'     )     )
           self.assertTrue(hasattr(A__		,					'rescale_factor'     )     )
           self.assertTrue(hasattr(A__		,					'masking_generator'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_do_resize'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_size'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_resample'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_do_center_crop'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_crop_size'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_do_map_pixels'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_do_normalize'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_image_mean'     )     )
           self.assertTrue(hasattr(A__		,					'codebook_image_std'     )     )




     def 	__snake_case				(      self					:				int     )			->    List[Any]:
           lowerCamelCase__ 					=			self.image_processing_class.from_dict(self.image_processor_dict     )
           self.assertEqual(image_processor.size		,					{'height': 2_2_4, 'width': 2_2_4}     )
           self.assertEqual(image_processor.crop_size		,					{'height': 2_2_4, 'width': 2_2_4}     )
           self.assertEqual(image_processor.codebook_size		,					{'height': 1_1_2, 'width': 1_1_2}     )
           self.assertEqual(image_processor.codebook_crop_size		,					{'height': 1_1_2, 'width': 1_1_2}     )

           lowerCamelCase__ 					=			self.image_processing_class.from_dict(
               self.image_processor_dict		,					size=4_2		,					crop_size=8_4		,					codebook_size=3_3		,					codebook_crop_size=6_6     )
           self.assertEqual(image_processor.size		,					{'height': 4_2, 'width': 4_2}     )
           self.assertEqual(image_processor.crop_size		,					{'height': 8_4, 'width': 8_4}     )
           self.assertEqual(image_processor.codebook_size		,					{'height': 3_3, 'width': 3_3}     )
           self.assertEqual(image_processor.codebook_crop_size		,					{'height': 6_6, 'width': 6_6}     )




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




     def 	__snake_case				(      self					:				Optional[Any]     )			->    Union[str, Any]:
           # Initialize image_processing
           lowerCamelCase__ 					=			self.image_processing_class(**self.image_processor_dict     )
           # create random PIL images
           lowerCamelCase__ 					=			prepare_image_inputs(self.image_processor_tester		,					equal_resolution=A__     )
           for image in image_inputs:
                 self.assertIsInstance(A__		,					PIL.Image.Image     )

           # Test not batched input
           lowerCamelCase__ 					=			image_processing(image_inputs[0]		,					return_tensors='pt'     )

           # Test no bool masked pos
           self.assertFalse('bool_masked_pos' in encoded_images     )

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()

           self.assertEqual(
               encoded_images.pixel_values.shape		,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)		,					)

           # Test batched
           lowerCamelCase__ 					=			image_processing(A__		,					return_tensors='pt'     )
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()

           # Test no bool masked pos
           self.assertFalse('bool_masked_pos' in encoded_images     )

           self.assertEqual(
               encoded_images.pixel_values.shape		,					(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )		,					)




     def 	__snake_case				(      self					:				Optional[Any]		,					A__					:				Dict		,					A__					:				Tuple     )			->    Any:
           # Initialize image_processing
           lowerCamelCase__ 					=			self.image_processing_class(**self.image_processor_dict     )
           # create random tensors
           lowerCamelCase__ 					=			prepare_image_inputs(self.image_processor_tester		,					equal_resolution=A__		,					**A__     )
           for image in image_inputs:
                 self.assertIsInstance(A__		,					A__     )

           # Test not batched input
           lowerCamelCase__ 					=			image_processing(image_inputs[0]		,					return_tensors='pt'     )

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape		,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)		,					)

           lowerCamelCase__ 					=			image_processing(A__		,					return_image_mask=A__		,					return_tensors='pt'     )

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape		,					(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )		,					)

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_mask_size()
           self.assertEqual(
               encoded_images.bool_masked_pos.shape		,					(
                   self.image_processor_tester.batch_size,
                   expected_height,
                   expected_width,
               )		,					)

           # Test batched
           lowerCamelCase__ 					=			image_processing(A__		,					return_tensors='pt'     ).pixel_values

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.shape		,					(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )		,					)

           # Test masking
           lowerCamelCase__ 					=			image_processing(A__		,					return_image_mask=A__		,					return_tensors='pt'     )

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape		,					(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )		,					)

           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_mask_size()
           self.assertEqual(
               encoded_images.bool_masked_pos.shape		,					(
                   self.image_processor_tester.batch_size,
                   expected_height,
                   expected_width,
               )		,					)




     def 	__snake_case				(      self					:				Any     )			->    Tuple:
           self._test_call_framework(np.ndarray		,					prepare_kwargs={'numpify': True}     )




     def 	__snake_case				(      self					:				Tuple     )			->    Tuple:
           self._test_call_framework(torch.Tensor		,					prepare_kwargs={'torchify': True}     )




     def 	__snake_case				(      self					:				Dict     )			->    Tuple:
           # Initialize image_processing
           random.seed(1_2_3_4     )
           lowerCamelCase__ 					=			self.image_processing_class(**self.image_processor_dict     )
           lowerCamelCase__ 					=			prepare_image_inputs(self.image_processor_tester		,					equal_resolution=A__		,					torchify=A__     )

           # Test not batched input
           lowerCamelCase__ 					=			image_processing(image_inputs[0]		,					return_image_mask=A__		,					return_tensors='pt'     )
           self.assertEqual(encoded_images.bool_masked_pos.sum().item()		,					7_5     )




     def 	__snake_case				(      self					:				Any     )			->    Optional[int]:
           # Initialize image_processing
           lowerCamelCase__ 					=			self.image_processing_class(**self.image_processor_dict     )
           # create random PIL images
           lowerCamelCase__ 					=			prepare_image_inputs(self.image_processor_tester		,					equal_resolution=A__     )
           for image in image_inputs:
                 self.assertIsInstance(A__		,					PIL.Image.Image     )

           # Test not batched input
           lowerCamelCase__ 					=			image_processing(image_inputs[0]		,					return_codebook_pixels=A__		,					return_tensors='pt'     )
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_codebook_image_size()
           self.assertEqual(
               encoded_images.codebook_pixel_values.shape		,					(1, self.image_processor_tester.num_channels, expected_height, expected_width)		,					)

           # Test batched
           lowerCamelCase__ 					=			image_processing(A__		,					return_codebook_pixels=A__		,					return_tensors='pt'     )
           lowerCamelCase__					,						lowerCamelCase__ 					=			self.image_processor_tester.get_expected_codebook_image_size()
           self.assertEqual(
               encoded_images.codebook_pixel_values.shape		,					(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )		,					)





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__SCREAMING_SNAKE_CASE      :				int				      ={"""configuration_nat""": ["""NAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NatConfig"""]}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __SCREAMING_SNAKE_CASE      :				Optional[int]				      =[
          """NAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """NatForImageClassification""",
          """NatModel""",
          """NatPreTrainedModel""",
          """NatBackbone""",
      ]

if TYPE_CHECKING:
      from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_nat import (
                NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
                NatBackbone,
                NatForImageClassification,
                NatModel,
                NatPreTrainedModel,
            )

else:
      import sys

      __SCREAMING_SNAKE_CASE      :				Any				      =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)