



'''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__						:			Union[str, Any]										=			None
class        UpperCAmelCase__  (    unittest.TestCase):



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

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

  __UpperCamelCase				      =  input_size_patches
  __UpperCamelCase				      =  total_mask_patches
  __UpperCamelCase				      =  mask_group_max_patches
  __UpperCamelCase				      =  mask_group_min_patches
  __UpperCamelCase				      =  mask_group_min_aspect_ratio
  __UpperCamelCase				      =  mask_group_max_aspect_ratio

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



 def    __lowerCamelCase				(  self			)  ->							Optional[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    __lowerCamelCase				(  self			)  ->							Any:
  return (self.size["height"], self.size["width"])



 def    __lowerCamelCase				(  self			)  ->							Union[str, Any]:
  return (
      (self.input_size_patches, self.input_size_patches)
      if not isinstance(self.input_size_patches		,	lowercase			)
      else self.input_size_patches
  )




 def    __lowerCamelCase				(  self			)  ->							Dict:
  return (self.codebook_size["height"], self.codebook_size["width"])


@require_torch
@require_vision
class        UpperCAmelCase__  (    UpperCAmelCase_							,			unittest.TestCase):
 __SCREAMING_SNAKE_CASE					      =				FlavaImageProcessor if is_vision_available() else None
 __SCREAMING_SNAKE_CASE					      =				None



 def    __lowerCamelCase				(  self			)  ->							Optional[int]:
  __UpperCamelCase				      =  FlavaImageProcessingTester(self			)



 @property
 def    __lowerCamelCase				(  self			)  ->							int:
  return self.image_processor_tester.prepare_image_processor_dict()



 def    __lowerCamelCase				(  self			)  ->							Dict:
  __UpperCamelCase				      =  self.image_processing_class(**self.image_processor_dict			)
  self.assertTrue(hasattr(lowercase		,	"""image_mean"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""image_std"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""do_normalize"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""do_resize"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""resample"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""crop_size"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""do_center_crop"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""do_rescale"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""rescale_factor"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""masking_generator"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_do_resize"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_size"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_resample"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_do_center_crop"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_crop_size"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_do_map_pixels"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_do_normalize"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_image_mean"""			)			)
  self.assertTrue(hasattr(lowercase		,	"""codebook_image_std"""			)			)



 def    __lowerCamelCase				(  self			)  ->							Optional[int]:
  __UpperCamelCase				      =  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}			)

  __UpperCamelCase				      =  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    __lowerCamelCase				(  self			)  ->							Optional[int]:
  pass



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

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

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

  __UpperCamelCase      ,		__UpperCamelCase				      =  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
  __UpperCamelCase				      =  image_processing(lowercase		,	return_tensors="""pt"""			)
  __UpperCamelCase      ,		__UpperCamelCase				      =  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    __lowerCamelCase				(  self		,	lowercase		,	lowercase			)  ->							str:
  # Initialize image_processing
  __UpperCamelCase				      =  self.image_processing_class(**self.image_processor_dict			)
  # create random tensors
  __UpperCamelCase				      =  prepare_image_inputs(self.image_processor_tester		,	equal_resolution=lowercase		,	**lowercase			)
  for image in image_inputs:
   self.assertIsInstance(lowercase		,	lowercase			)

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

  __UpperCamelCase      ,		__UpperCamelCase				      =  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)		,	)

  __UpperCamelCase				      =  image_processing(lowercase		,	return_image_mask=lowercase		,	return_tensors="""pt"""			)

  __UpperCamelCase      ,		__UpperCamelCase				      =  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,
      )		,	)

  __UpperCamelCase      ,		__UpperCamelCase				      =  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
  __UpperCamelCase				      =  image_processing(lowercase		,	return_tensors="""pt"""			).pixel_values

  __UpperCamelCase      ,		__UpperCamelCase				      =  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
  __UpperCamelCase				      =  image_processing(lowercase		,	return_image_mask=lowercase		,	return_tensors="""pt"""			)

  __UpperCamelCase      ,		__UpperCamelCase				      =  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,
      )		,	)

  __UpperCamelCase      ,		__UpperCamelCase				      =  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    __lowerCamelCase				(  self			)  ->							int:
  self._test_call_framework(np.ndarray		,	prepare_kwargs={"""numpify""": True}			)



 def    __lowerCamelCase				(  self			)  ->							List[Any]:
  self._test_call_framework(torch.Tensor		,	prepare_kwargs={"""torchify""": True}			)



 def    __lowerCamelCase				(  self			)  ->							Union[str, Any]:
  # Initialize image_processing
  random.seed(1_2_3_4			)
  __UpperCamelCase				      =  self.image_processing_class(**self.image_processor_dict			)
  __UpperCamelCase				      =  prepare_image_inputs(self.image_processor_tester		,	equal_resolution=lowercase		,	torchify=lowercase			)

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




 def    __lowerCamelCase				(  self			)  ->							Dict:
  # Initialize image_processing
  __UpperCamelCase				      =  self.image_processing_class(**self.image_processor_dict			)
  # create random PIL images
  __UpperCamelCase				      =  prepare_image_inputs(self.image_processor_tester		,	equal_resolution=lowercase			)
  for image in image_inputs:
   self.assertIsInstance(lowercase		,	PIL.Image.Image			)

  # Test not batched input
  __UpperCamelCase				      =  image_processing(image_inputs[0]		,	return_codebook_pixels=lowercase		,	return_tensors="""pt"""			)
  __UpperCamelCase      ,		__UpperCamelCase				      =  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
  __UpperCamelCase				      =  image_processing(lowercase		,	return_codebook_pixels=lowercase		,	return_tensors="""pt"""			)
  __UpperCamelCase      ,		__UpperCamelCase				      =  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'''



def _lowercase   (  __A							,__A       ):



 '''simple docstring'''



 __UpperCamelCase				      =  1
 while b > 0:
  if b & 1:
   res *= a

  a *= a
  b >>= 1

 return res







def _lowercase   (  __A							,__A							,__A       ):



 '''simple docstring'''



 __UpperCamelCase				      =  1
 while b > 0:
  if b & 1:
   __UpperCamelCase				      =  ((res % c) * (a % c)) % c

  a *= a
  b >>= 1

 return res





