




'''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							:						Any								=  None





class      lowerCamelCase_							(       unittest.TestCase							):




					'''simple docstring'''






					def __init__(    self,  __UpperCamelCase,  __UpperCamelCase=7,  __UpperCamelCase=3,  __UpperCamelCase=30,  __UpperCamelCase=400,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=1 / 255,  __UpperCamelCase=True,  __UpperCamelCase=FLAVA_IMAGE_MEAN,  __UpperCamelCase=FLAVA_IMAGE_STD,  __UpperCamelCase=14,  __UpperCamelCase=75,  __UpperCamelCase=None,  __UpperCamelCase=16,  __UpperCamelCase=0.3,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=FLAVA_CODEBOOK_MEAN,  __UpperCamelCase=FLAVA_CODEBOOK_STD,  ):
							_snake_case	       =       size if size is not None else {"height": 224, "width": 224}
							_snake_case	       =       crop_size if crop_size is not None else {"height": 224, "width": 224}
							_snake_case	       =       codebook_size if codebook_size is not None else {"height": 112, "width": 112}
							_snake_case	       =       codebook_crop_size if codebook_crop_size is not None else {"height": 112, "width": 112}

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

							_snake_case	       =       input_size_patches
							_snake_case	       =       total_mask_patches
							_snake_case	       =       mask_group_max_patches
							_snake_case	       =       mask_group_min_patches
							_snake_case	       =       mask_group_min_aspect_ratio
							_snake_case	       =       mask_group_max_aspect_ratio

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






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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return (
							    (self.input_size_patches, self.input_size_patches)
							    if not isinstance(self.input_size_patches,  __UpperCamelCase )
							    else self.input_size_patches
							)






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





@require_torch
@require_vision
class      lowerCamelCase_							(       UpperCAmelCase_,       unittest.TestCase							):




					'''simple docstring'''






					lowercase__    = FlavaImageProcessor if is_vision_available() else None
					lowercase__    = None






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_snake_case	       =       FlavaImageProcessingTester(self )






					@property
					def 				__SCREAMING_SNAKE_CASE	(    self ):
							return self.image_processor_tester.prepare_image_processor_dict()






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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							_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} )

							_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 				__SCREAMING_SNAKE_CASE	(    self ):
							pass






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

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

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

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

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

							_snake_case       ,    _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),  )

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

							_snake_case       ,    _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,
							    ),  )

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

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

							_snake_case       ,    _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,
							    ),  )

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






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							self._test_call_framework(torch.Tensor,  prepare_kwargs={"torchify": True} )






					def 				__SCREAMING_SNAKE_CASE	(    self ):
							# Initialize image_processing
							random.seed(1_234 )
							_snake_case	       =       self.image_processing_class(**self.image_processor_dict )
							_snake_case	       =       prepare_image_inputs(self.image_processor_tester,  equal_resolution=__UpperCamelCase,  torchify=__UpperCamelCase )

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






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

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











from ..utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_torch_available,
    is_transformers_available,
)


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_pt_objects import *  # noqa F403
else:
     from .consistency_models import ConsistencyModelPipeline
     from .dance_diffusion import DanceDiffusionPipeline
     from .ddim import DDIMPipeline
     from .ddpm import DDPMPipeline
     from .dit import DiTPipeline
     from .latent_diffusion import LDMSuperResolutionPipeline
     from .latent_diffusion_uncond import LDMPipeline
     from .pipeline_utils import AudioPipelineOutput, DiffusionPipeline, ImagePipelineOutput
     from .pndm import PNDMPipeline
     from .repaint import RePaintPipeline
     from .score_sde_ve import ScoreSdeVePipeline
     from .stochastic_karras_ve import KarrasVePipeline

try:
     if not (is_torch_available() and is_librosa_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
     from .audio_diffusion import AudioDiffusionPipeline, Mel

try:
     if not (is_torch_available() and is_transformers_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
     from .alt_diffusion import AltDiffusionImgaImgPipeline, AltDiffusionPipeline
     from .audioldm import AudioLDMPipeline
     from .controlnet import (
         StableDiffusionControlNetImgaImgPipeline,
         StableDiffusionControlNetInpaintPipeline,
         StableDiffusionControlNetPipeline,
     )
     from .deepfloyd_if import (
         IFImgaImgPipeline,
         IFImgaImgSuperResolutionPipeline,
         IFInpaintingPipeline,
         IFInpaintingSuperResolutionPipeline,
         IFPipeline,
         IFSuperResolutionPipeline,
     )
     from .kandinsky import (
         KandinskyImgaImgPipeline,
         KandinskyInpaintPipeline,
         KandinskyPipeline,
         KandinskyPriorPipeline,
     )
     from .kandinskya_a import (
         KandinskyVaaControlnetImgaImgPipeline,
         KandinskyVaaControlnetPipeline,
         KandinskyVaaImgaImgPipeline,
         KandinskyVaaInpaintPipeline,
         KandinskyVaaPipeline,
         KandinskyVaaPriorEmbaEmbPipeline,
         KandinskyVaaPriorPipeline,
     )
     from .latent_diffusion import LDMTextToImagePipeline
     from .paint_by_example import PaintByExamplePipeline
     from .semantic_stable_diffusion import SemanticStableDiffusionPipeline
     from .shap_e import ShapEImgaImgPipeline, ShapEPipeline
     from .stable_diffusion import (
         CycleDiffusionPipeline,
         StableDiffusionAttendAndExcitePipeline,
         StableDiffusionDepthaImgPipeline,
         StableDiffusionDiffEditPipeline,
         StableDiffusionImageVariationPipeline,
         StableDiffusionImgaImgPipeline,
         StableDiffusionInpaintPipeline,
         StableDiffusionInpaintPipelineLegacy,
         StableDiffusionInstructPixaPixPipeline,
         StableDiffusionLatentUpscalePipeline,
         StableDiffusionLDMaDPipeline,
         StableDiffusionModelEditingPipeline,
         StableDiffusionPanoramaPipeline,
         StableDiffusionParadigmsPipeline,
         StableDiffusionPipeline,
         StableDiffusionPixaPixZeroPipeline,
         StableDiffusionSAGPipeline,
         StableDiffusionUpscalePipeline,
         StableUnCLIPImgaImgPipeline,
         StableUnCLIPPipeline,
     )
     from .stable_diffusion_safe import StableDiffusionPipelineSafe
     from .text_to_video_synthesis import TextToVideoSDPipeline, TextToVideoZeroPipeline, VideoToVideoSDPipeline
     from .unclip import UnCLIPImageVariationPipeline, UnCLIPPipeline
     from .unidiffuser import ImageTextPipelineOutput, UniDiffuserModel, UniDiffuserPipeline, UniDiffuserTextDecoder
     from .versatile_diffusion import (
         VersatileDiffusionDualGuidedPipeline,
         VersatileDiffusionImageVariationPipeline,
         VersatileDiffusionPipeline,
         VersatileDiffusionTextToImagePipeline,
     )
     from .vq_diffusion import VQDiffusionPipeline


try:
     if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
     from .stable_diffusion_xl import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
     if not is_onnx_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_onnx_objects import *  # noqa F403
else:
     from .onnx_utils import OnnxRuntimeModel

try:
     if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
     from .stable_diffusion import (
         OnnxStableDiffusionImgaImgPipeline,
         OnnxStableDiffusionInpaintPipeline,
         OnnxStableDiffusionInpaintPipelineLegacy,
         OnnxStableDiffusionPipeline,
         OnnxStableDiffusionUpscalePipeline,
         StableDiffusionOnnxPipeline,
     )

try:
     if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
     from .stable_diffusion import StableDiffusionKDiffusionPipeline

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_flax_objects import *  # noqa F403
else:
     from .pipeline_flax_utils import FlaxDiffusionPipeline


try:
     if not (is_flax_available() and is_transformers_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
     from .controlnet import FlaxStableDiffusionControlNetPipeline
     from .stable_diffusion import (
         FlaxStableDiffusionImgaImgPipeline,
         FlaxStableDiffusionInpaintPipeline,
         FlaxStableDiffusionPipeline,
     )
try:
     if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     from ..utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
     from .spectrogram_diffusion import MidiProcessor, SpectrogramDiffusionPipeline
