



"""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:
    lowerCamelCase				:     List[str]								=  None




class    __snake_case(					unittest.TestCase       ):




    def __init__(   self    ,							A_    ,							A_=7    ,							A_=3    ,							A_=30    ,							A_=400    ,							A_=True    ,							A_=None    ,							A_=True    ,							A_=None    ,							A_=None    ,							A_=True    ,							A_=1 / 255    ,							A_=True    ,							A_=FLAVA_IMAGE_MEAN    ,							A_=FLAVA_IMAGE_STD    ,							A_=14    ,							A_=75    ,							A_=None    ,							A_=16    ,							A_=0.3    ,							A_=None    ,							A_=True    ,							A_=None    ,							A_=None    ,							A_=True    ,							A_=None    ,							A_=True    ,							A_=True    ,							A_=FLAVA_CODEBOOK_MEAN    ,							A_=FLAVA_CODEBOOK_STD    ,							):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			size if size is not None else {'''height''': 224, '''width''': 224}
      _SCREAMING_SNAKE_CASE        =			crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
      _SCREAMING_SNAKE_CASE        =			codebook_size if codebook_size is not None else {'''height''': 112, '''width''': 112}
      _SCREAMING_SNAKE_CASE        =			codebook_crop_size if codebook_crop_size is not None else {'''height''': 112, '''width''': 112}

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

      _SCREAMING_SNAKE_CASE        =			input_size_patches
      _SCREAMING_SNAKE_CASE        =			total_mask_patches
      _SCREAMING_SNAKE_CASE        =			mask_group_max_patches
      _SCREAMING_SNAKE_CASE        =			mask_group_min_patches
      _SCREAMING_SNAKE_CASE        =			mask_group_min_aspect_ratio
      _SCREAMING_SNAKE_CASE        =			mask_group_max_aspect_ratio

      _SCREAMING_SNAKE_CASE        =			codebook_do_resize
      _SCREAMING_SNAKE_CASE        =			codebook_size
      _SCREAMING_SNAKE_CASE        =			codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
      _SCREAMING_SNAKE_CASE        =			codebook_do_center_crop
      _SCREAMING_SNAKE_CASE        =			codebook_crop_size
      _SCREAMING_SNAKE_CASE        =			codebook_do_map_pixels
      _SCREAMING_SNAKE_CASE        =			codebook_do_normalize
      _SCREAMING_SNAKE_CASE        =			codebook_image_mean
      _SCREAMING_SNAKE_CASE        =			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    ,							A_			)
          else self.input_size_patches
      )




    def   A   (   self			):





      '''simple docstring'''



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




@require_torch
@require_vision
class    __snake_case(					__A			,			unittest.TestCase       ):
    _A       = FlavaImageProcessor if is_vision_available() else None
    _A       = None




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			FlavaImageProcessingTester(self			)




    @property
    def   A   (   self			):





      '''simple docstring'''



      return self.image_processor_tester.prepare_image_processor_dict()




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			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   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class.from_dict(self.image_processor_dict			)
      self.assertEqual(image_processor.size    ,							{'''height''': 224, '''width''': 224}			)
      self.assertEqual(image_processor.crop_size    ,							{'''height''': 224, '''width''': 224}			)
      self.assertEqual(image_processor.codebook_size    ,							{'''height''': 112, '''width''': 112}			)
      self.assertEqual(image_processor.codebook_crop_size    ,							{'''height''': 112, '''width''': 112}			)

      _SCREAMING_SNAKE_CASE        =			self.image_processing_class.from_dict(
          self.image_processor_dict    ,							size=42    ,							crop_size=84    ,							codebook_size=33    ,							codebook_crop_size=66			)
      self.assertEqual(image_processor.size    ,							{'''height''': 42, '''width''': 42}			)
      self.assertEqual(image_processor.crop_size    ,							{'''height''': 84, '''width''': 84}			)
      self.assertEqual(image_processor.codebook_size    ,							{'''height''': 33, '''width''': 33}			)
      self.assertEqual(image_processor.codebook_crop_size    ,							{'''height''': 66, '''width''': 66}			)




    def   A   (   self			):





      '''simple docstring'''



      pass




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random PIL images
      _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(image_inputs[0]    ,							return_tensors='''pt'''			)

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

      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(A_    ,							return_tensors='''pt'''			)
      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random tensors
      _SCREAMING_SNAKE_CASE        =			prepare_image_inputs(self.image_processor_tester    ,							equal_resolution=A_    ,							**A_			)
      for image in image_inputs:
        self.assertIsInstance(A_    ,							A_			)

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

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

      _SCREAMING_SNAKE_CASE        =			image_processing(A_    ,							return_image_mask=A_    ,							return_tensors='''pt'''			)

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

      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(A_    ,							return_tensors='''pt'''			).pixel_values

      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(A_    ,							return_image_mask=A_    ,							return_tensors='''pt'''			)

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

      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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_234			)
      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      _SCREAMING_SNAKE_CASE        =			prepare_image_inputs(self.image_processor_tester    ,							equal_resolution=A_    ,							torchify=A_			)

      # Test not batched input
      _SCREAMING_SNAKE_CASE        =			image_processing(image_inputs[0]    ,							return_image_mask=A_    ,							return_tensors='''pt'''			)
      self.assertEqual(encoded_images.bool_masked_pos.sum().item()    ,							75			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.image_processing_class(**self.image_processor_dict			)
      # create random PIL images
      _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(image_inputs[0]    ,							return_codebook_pixels=A_    ,							return_tensors='''pt'''			)
      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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
      _SCREAMING_SNAKE_CASE        =			image_processing(A_    ,							return_codebook_pixels=A_    ,							return_tensors='''pt'''			)
      _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE        =			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"""






import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()




class    __snake_case(					unittest.TestCase       ):




    @property
    def   A   (   self			):





      '''simple docstring'''



      torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			UNetaDModel(
          block_out_channels=(32, 64)    ,							layers_per_block=2    ,							sample_size=32    ,							in_channels=3    ,							out_channels=3    ,							down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')    ,							up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')    ,							)
      return model




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			'''cpu'''
      _SCREAMING_SNAKE_CASE        =			self.dummy_uncond_unet
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler()

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.Generator(device=A_			).manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			torch.Generator(device=A_			).manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''    ,							return_dict=A_			)[0]

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]
      _SCREAMING_SNAKE_CASE        =			image_from_tuple[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			np.array(
          [9.956e-01, 5.785e-01, 4.675e-01, 9.930e-01, 0.0, 1.000, 1.199e-03, 2.648e-04, 5.101e-04]			)

      assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-2
      assert np.abs(image_from_tuple_slice.flatten() - expected_slice			).max() < 1e-2




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.dummy_uncond_unet
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler(prediction_type='''sample'''			)

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							num_inference_steps=2    ,							output_type='''numpy'''			)[0]

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]
      _SCREAMING_SNAKE_CASE        =			image_eps[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			1e-2 if torch_device != '''mps''' else 3e-2
      assert np.abs(image_slice.flatten() - image_eps_slice.flatten()			).max() < tolerance




@slow
@require_torch_gpu
class    __snake_case(					unittest.TestCase       ):




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			'''google/ddpm-cifar10-32'''

      _SCREAMING_SNAKE_CASE        =			UNetaDModel.from_pretrained(A_			)
      _SCREAMING_SNAKE_CASE        =			DDPMScheduler.from_pretrained(A_			)

      _SCREAMING_SNAKE_CASE        =			DDPMPipeline(unet=A_    ,							scheduler=A_			)
      ddpm.to(A_			)
      ddpm.set_progress_bar_config(disable=A_			)

      _SCREAMING_SNAKE_CASE        =			torch.manual_seed(0			)
      _SCREAMING_SNAKE_CASE        =			ddpm(generator=A_    ,							output_type='''numpy'''			).images

      _SCREAMING_SNAKE_CASE        =			image[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _SCREAMING_SNAKE_CASE        =			np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385]			)
      assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-2
