






"""simple docstring"""
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:
      A_			: str				=			None



class 		a_      (	unittest.TestCase ):







      '''simple docstring'''

      def __init__(self,     lowerCamelCase_,     lowerCamelCase_=7,     lowerCamelCase_=3,     lowerCamelCase_=3_0,     lowerCamelCase_=4_0_0,     lowerCamelCase_=True,     lowerCamelCase_=None,     lowerCamelCase_=True,     lowerCamelCase_=None,     lowerCamelCase_=None,     lowerCamelCase_=True,     lowerCamelCase_=1 / 2_5_5,     lowerCamelCase_=True,     lowerCamelCase_=FLAVA_IMAGE_MEAN,     lowerCamelCase_=FLAVA_IMAGE_STD,     lowerCamelCase_=1_4,     lowerCamelCase_=7_5,     lowerCamelCase_=None,     lowerCamelCase_=1_6,     lowerCamelCase_=0.3,     lowerCamelCase_=None,     lowerCamelCase_=True,     lowerCamelCase_=None,     lowerCamelCase_=None,     lowerCamelCase_=True,     lowerCamelCase_=None,     lowerCamelCase_=True,     lowerCamelCase_=True,     lowerCamelCase_=FLAVA_CODEBOOK_MEAN,     lowerCamelCase_=FLAVA_CODEBOOK_STD,     ):

       '''simple docstring'''



       lowerCamelCase__     :			List[str]     				=						size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
       lowerCamelCase__     :			Optional[Any]     				=						crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
       lowerCamelCase__     :			List[str]     				=						codebook_size if codebook_size is not None else {'height': 1_1_2, 'width': 1_1_2}
       lowerCamelCase__     :			Union[str, Any]     				=						codebook_crop_size if codebook_crop_size is not None else {'height': 1_1_2, 'width': 1_1_2}

       lowerCamelCase__     :			Tuple     				=						parent
       lowerCamelCase__     :			int     				=						batch_size
       lowerCamelCase__     :			List[str]     				=						num_channels
       lowerCamelCase__     :			Dict     				=						do_resize
       lowerCamelCase__     :			List[str]     				=						do_rescale
       lowerCamelCase__     :			str     				=						rescale_factor
       lowerCamelCase__     :			List[str]     				=						min_resolution
       lowerCamelCase__     :			Optional[int]     				=						max_resolution
       lowerCamelCase__     :			Optional[int]     				=						size
       lowerCamelCase__     :			Optional[int]     				=						resample if resample is not None else PILImageResampling.BICUBIC
       lowerCamelCase__     :			List[Any]     				=						do_normalize
       lowerCamelCase__     :			Any     				=						image_mean
       lowerCamelCase__     :			List[Any]     				=						image_std
       lowerCamelCase__     :			Dict     				=						do_center_crop
       lowerCamelCase__     :			Optional[Any]     				=						crop_size

       lowerCamelCase__     :			Optional[int]     				=						input_size_patches
       lowerCamelCase__     :			Optional[Any]     				=						total_mask_patches
       lowerCamelCase__     :			List[str]     				=						mask_group_max_patches
       lowerCamelCase__     :			Any     				=						mask_group_min_patches
       lowerCamelCase__     :			Tuple     				=						mask_group_min_aspect_ratio
       lowerCamelCase__     :			List[Any]     				=						mask_group_max_aspect_ratio

       lowerCamelCase__     :			List[Any]     				=						codebook_do_resize
       lowerCamelCase__     :			Optional[Any]     				=						codebook_size
       lowerCamelCase__     :			str     				=						codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
       lowerCamelCase__     :			Dict     				=						codebook_do_center_crop
       lowerCamelCase__     :			Tuple     				=						codebook_crop_size
       lowerCamelCase__     :			Union[str, Any]     				=						codebook_do_map_pixels
       lowerCamelCase__     :			Dict     				=						codebook_do_normalize
       lowerCamelCase__     :			Union[str, Any]     				=						codebook_image_mean
       lowerCamelCase__     :			str     				=						codebook_image_std




      def 			a__							(self					):

       '''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,
           "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 			a__							(self					):

       '''simple docstring'''



       return (self.size["height"], self.size["width"])




      def 			a__							(self					):

       '''simple docstring'''



       return (
           (self.input_size_patches, self.input_size_patches)
           if not isinstance(self.input_size_patches,     lowerCamelCase_					)
           else self.input_size_patches
       )





      def 			a__							(self					):

       '''simple docstring'''



       return (self.codebook_size["height"], self.codebook_size["width"])



@require_torch
@require_vision
class 		a_      (	snake_case_  ,						unittest.TestCase ):







      '''simple docstring'''

      lowerCamelCase__       :				Optional[int] =	FlavaImageProcessor if is_vision_available() else None
      lowerCamelCase__       :				Dict =	None




      def 			a__							(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Tuple     				=						FlavaImageProcessingTester(self					)




      @property
      def 			a__							(self					):

       '''simple docstring'''



       return self.image_processor_tester.prepare_image_processor_dict()




      def 			a__							(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Tuple     				=						self.image_processing_class(**self.image_processor_dict					)
       self.assertTrue(hasattr(lowerCamelCase_,     'image_mean'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'image_std'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'do_normalize'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'do_resize'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'resample'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'crop_size'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'do_center_crop'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'do_rescale'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'rescale_factor'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'masking_generator'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_do_resize'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_size'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_resample'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_do_center_crop'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_crop_size'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_do_map_pixels'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_do_normalize'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_image_mean'					)					)
       self.assertTrue(hasattr(lowerCamelCase_,     'codebook_image_std'					)					)




      def 			a__							(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Tuple     				=						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__     :			List[str]     				=						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 			a__							(self					):

       '''simple docstring'''



       pass




      def 			a__							(self					):

       '''simple docstring'''



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

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

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

       lowerCamelCase__				,     lowerCamelCase__     :			List[Any]     				=						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__     :			str     				=						image_processing(lowerCamelCase_,     return_tensors='pt'					)
       lowerCamelCase__				,     lowerCamelCase__     :			Optional[Any]     				=						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 			a__							(self,     lowerCamelCase_,     lowerCamelCase_					):

       '''simple docstring'''



       lowerCamelCase__     :			List[str]     				=						self.image_processing_class(**self.image_processor_dict					)
       # create random tensors
       lowerCamelCase__     :			str     				=						prepare_image_inputs(self.image_processor_tester,     equal_resolution=lowerCamelCase_,     **lowerCamelCase_					)
       for image in image_inputs:
        self.assertIsInstance(lowerCamelCase_,     lowerCamelCase_					)

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

       lowerCamelCase__				,     lowerCamelCase__     :			Dict     				=						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__     :			int     				=						image_processing(lowerCamelCase_,     return_image_mask=lowerCamelCase_,     return_tensors='pt'					)

       lowerCamelCase__				,     lowerCamelCase__     :			List[str]     				=						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__     :			Dict     				=						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__     :			List[Any]     				=						image_processing(lowerCamelCase_,     return_tensors='pt'					).pixel_values

       lowerCamelCase__				,     lowerCamelCase__     :			List[str]     				=						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__     :			Optional[Any]     				=						image_processing(lowerCamelCase_,     return_image_mask=lowerCamelCase_,     return_tensors='pt'					)

       lowerCamelCase__				,     lowerCamelCase__     :			str     				=						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__     :			Tuple     				=						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 			a__							(self					):

       '''simple docstring'''



       self._test_call_framework(np.ndarray,     prepare_kwargs={'numpify': True}					)




      def 			a__							(self					):

       '''simple docstring'''



       self._test_call_framework(torch.Tensor,     prepare_kwargs={'torchify': True}					)




      def 			a__							(self					):

       '''simple docstring'''



       random.seed(1_2_3_4					)
       lowerCamelCase__     :			str     				=						self.image_processing_class(**self.image_processor_dict					)
       lowerCamelCase__     :			Tuple     				=						prepare_image_inputs(self.image_processor_tester,     equal_resolution=lowerCamelCase_,     torchify=lowerCamelCase_					)

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





      def 			a__							(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Union[str, Any]     				=						self.image_processing_class(**self.image_processor_dict					)
       # create random PIL images
       lowerCamelCase__     :			Union[str, Any]     				=						prepare_image_inputs(self.image_processor_tester,     equal_resolution=lowerCamelCase_					)
       for image in image_inputs:
        self.assertIsInstance(lowerCamelCase_,     PIL.Image.Image					)

       # Test not batched input
       lowerCamelCase__     :			int     				=						image_processing(image_inputs[0],     return_codebook_pixels=lowerCamelCase_,     return_tensors='pt'					)
       lowerCamelCase__				,     lowerCamelCase__     :			List[Any]     				=						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__     :			Optional[Any]     				=						image_processing(lowerCamelCase_,     return_codebook_pixels=lowerCamelCase_,     return_tensors='pt'					)
       lowerCamelCase__				,     lowerCamelCase__     :			Optional[Any]     				=						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,
           ),     )









"""simple docstring"""
class 		a_      :







      '''simple docstring'''

      class 		a_      :







            '''simple docstring'''

            lowerCamelCase__       :				List[Any] =	'_prev', '_data', '_next'




            def __init__(self,     lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					):

             '''simple docstring'''



             lowerCamelCase__     :			Optional[int]     				=						link_p
             lowerCamelCase__     :			Optional[Any]     				=						element
             lowerCamelCase__     :			List[Any]     				=						link_n





            def 			a__							(self					):

             '''simple docstring'''



             return (
                 f''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
             )




      def __init__(self					):

       '''simple docstring'''



       lowerCamelCase__     :			Any     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			Dict     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			List[str]     				=						self._trailer
       lowerCamelCase__     :			Union[str, Any]     				=						self._header
       lowerCamelCase__     :			Any     				=						0




      def __len__(self					):

       '''simple docstring'''



       return self._size




      def 			a__							(self					):

       '''simple docstring'''



       return self.__len__() == 0




      def 			a__							(self,     lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					):

       '''simple docstring'''



       lowerCamelCase__     :			int     				=						self._Node(lowerCamelCase_,     lowerCamelCase_,     lowerCamelCase_					)
       lowerCamelCase__     :			Optional[int]     				=						new_node
       lowerCamelCase__     :			List[Any]     				=						new_node
       self._size += 1
       return self





      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       lowerCamelCase__     :			Optional[Any]     				=						node._prev
       lowerCamelCase__     :			List[str]     				=						node._next

       lowerCamelCase__     :			Tuple     				=						successor
       lowerCamelCase__     :			Tuple     				=						predecessor
       self._size -= 1
       lowerCamelCase__     :			int     				=						node._data
       lowerCamelCase__     :			Dict     				=						None
       del node
       return temp



class 		a_      (	_DoublyLinkedBase ):







      '''simple docstring'''

      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise Exception('List is empty'					)
       return self._header._next._data




      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise Exception('List is empty'					)
       return self._trailer._prev._data




      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       return self._insert(self._header,     lowerCamelCase_,     self._header._next					)




      def 			a__							(self,     lowerCamelCase_					):

       '''simple docstring'''



       return self._insert(self._trailer._prev,     lowerCamelCase_,     self._trailer					)




      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise IndexError('remove_first from empty list'					)
       return self._delete(self._header._next					)





      def 			a__							(self					):

       '''simple docstring'''



       if self.is_empty():
        raise IndexError('remove_first from empty list'					)
       return self._delete(self._trailer._prev					)

