





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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import SwinaSRImageProcessor
     from transformers.image_transforms import get_image_size



class 				_UpperCamelCase       (						unittest.TestCase  ):

       def __init__(		self:			List[Any]       , _SCREAMING_SNAKE_CASE:			Tuple       , _SCREAMING_SNAKE_CASE:			Optional[Any]=7       , _SCREAMING_SNAKE_CASE:			Any=3       , _SCREAMING_SNAKE_CASE:			List[Any]=18       , _SCREAMING_SNAKE_CASE:			Tuple=30       , _SCREAMING_SNAKE_CASE:			List[str]=400       , _SCREAMING_SNAKE_CASE:			Tuple=True       , _SCREAMING_SNAKE_CASE:			List[str]=1 / 255       , _SCREAMING_SNAKE_CASE:			List[Any]=True       , _SCREAMING_SNAKE_CASE:			Tuple=8       , )							->			Optional[int]:




              """simple docstring"""


              UpperCamelCase_ 							=	parent
              UpperCamelCase_ 							=	batch_size
              UpperCamelCase_ 							=	num_channels
              UpperCamelCase_ 							=	image_size
              UpperCamelCase_ 							=	min_resolution
              UpperCamelCase_ 							=	max_resolution
              UpperCamelCase_ 							=	do_rescale
              UpperCamelCase_ 							=	rescale_factor
              UpperCamelCase_ 							=	do_pad
              UpperCamelCase_ 							=	pad_size

       def lowercase			(		self:			Tuple		)							->			Optional[Any]:




              """simple docstring"""


              return {
                  "do_rescale": self.do_rescale,
                  "rescale_factor": self.rescale_factor,
                  "do_pad": self.do_pad,
                  "pad_size": self.pad_size,
              }



       def lowercase			(		self:			Tuple       , _SCREAMING_SNAKE_CASE:			List[Any]=False       , _SCREAMING_SNAKE_CASE:			List[str]=False       , _SCREAMING_SNAKE_CASE:			Any=False		)							->			List[str]:




              """simple docstring"""


              assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

              if equal_resolution:
                     UpperCamelCase_ 							=	[]
                     for i in range(self.batch_size		):
                            image_inputs.append(
                                np.random.randint(
                                    255       , size=(self.num_channels, self.max_resolution, self.max_resolution)       , dtype=np.uinta		)		)
              else:
                     UpperCamelCase_ 							=	[]
                     for i in range(self.batch_size		):
                            UpperCamelCase_							,			UpperCamelCase_ 							=	np.random.choice(np.arange(self.min_resolution       , self.max_resolution		)       , 2		)
                            image_inputs.append(np.random.randint(255       , size=(self.num_channels, width, height)       , dtype=np.uinta		)		)

              if not numpify and not torchify:
                     # PIL expects the channel dimension as last dimension
                     UpperCamelCase_ 							=	[Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE       , 0       , -1		)		) for x in image_inputs]

              if torchify:
                     UpperCamelCase_ 							=	[torch.from_numpy(_SCREAMING_SNAKE_CASE		) for x in image_inputs]

              return image_inputs



@require_torch
@require_vision
class 				_UpperCamelCase       (						lowerCAmelCase_	,    unittest.TestCase  ):
       _UpperCamelCase					:       Union[str, Any]       = SwinaSRImageProcessor if is_vision_available() else None

       def lowercase			(		self:			Union[str, Any]		)							->			str:




              """simple docstring"""


              UpperCamelCase_ 							=	SwinaSRImageProcessingTester(self		)

       @property
       def lowercase			(		self:			Optional[int]		)							->			Optional[int]:




              """simple docstring"""


              return self.image_processor_tester.prepare_image_processor_dict()

       def lowercase			(		self:			Tuple		)							->			Optional[Any]:




              """simple docstring"""


              UpperCamelCase_ 							=	self.image_processing_class(**self.image_processor_dict		)
              self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE       , "do_rescale"		)		)
              self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE       , "rescale_factor"		)		)
              self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE       , "do_pad"		)		)
              self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE       , "pad_size"		)		)

       def lowercase			(		self:			List[Any]		)							->			Tuple:




              """simple docstring"""


              pass

       def lowercase			(		self:			int       , _SCREAMING_SNAKE_CASE:			Optional[Any]		)							->			Dict:




              """simple docstring"""


              UpperCamelCase_							,			UpperCamelCase_ 							=	get_image_size(_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_ 							=	self.image_processor_tester.pad_size

              UpperCamelCase_ 							=	(old_height // size + 1) * size - old_height
              UpperCamelCase_ 							=	(old_width // size + 1) * size - old_width
              return old_height + pad_height, old_width + pad_width

       def lowercase			(		self:			Dict		)							->			Any:




              """simple docstring"""


              UpperCamelCase_ 							=	self.image_processing_class(**self.image_processor_dict		)
              # create random PIL images
              UpperCamelCase_ 							=	self.image_processor_tester.prepare_inputs(equal_resolution=_SCREAMING_SNAKE_CASE		)
              for image in image_inputs:
                     self.assertIsInstance(_SCREAMING_SNAKE_CASE       , Image.Image		)

              # Test not batched input
              UpperCamelCase_ 							=	image_processor(image_inputs[0]       , return_tensors="pt"		).pixel_values
              UpperCamelCase_							,			UpperCamelCase_ 							=	self.calculate_expected_size(np.array(image_inputs[0]		)		)
              self.assertEqual(
                  encoded_images.shape       , (
                      1,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )       , )

       def lowercase			(		self:			Any		)							->			Any:




              """simple docstring"""


              UpperCamelCase_ 							=	self.image_processing_class(**self.image_processor_dict		)
              # create random numpy tensors
              UpperCamelCase_ 							=	self.image_processor_tester.prepare_inputs(equal_resolution=_SCREAMING_SNAKE_CASE       , numpify=_SCREAMING_SNAKE_CASE		)
              for image in image_inputs:
                     self.assertIsInstance(_SCREAMING_SNAKE_CASE       , np.ndarray		)

              # Test not batched input
              UpperCamelCase_ 							=	image_processor(image_inputs[0]       , return_tensors="pt"		).pixel_values
              UpperCamelCase_							,			UpperCamelCase_ 							=	self.calculate_expected_size(image_inputs[0]		)
              self.assertEqual(
                  encoded_images.shape       , (
                      1,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )       , )



       def lowercase			(		self:			str		)							->			Optional[int]:




              """simple docstring"""


              UpperCamelCase_ 							=	self.image_processing_class(**self.image_processor_dict		)
              # create random PyTorch tensors
              UpperCamelCase_ 							=	self.image_processor_tester.prepare_inputs(equal_resolution=_SCREAMING_SNAKE_CASE       , torchify=_SCREAMING_SNAKE_CASE		)
              for image in image_inputs:
                     self.assertIsInstance(_SCREAMING_SNAKE_CASE       , torch.Tensor		)

              # Test not batched input
              UpperCamelCase_ 							=	image_processor(image_inputs[0]       , return_tensors="pt"		).pixel_values
              UpperCamelCase_							,			UpperCamelCase_ 							=	self.calculate_expected_size(image_inputs[0]		)
              self.assertEqual(
                  encoded_images.shape       , (
                      1,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )       , )





import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
		from .testing_utils import (
		    floats_tensor,
		    load_hf_numpy,
		    load_image,
		    load_numpy,
		    load_pt,
		    nightly,
		    parse_flag_from_env,
		    print_tensor_test,
		    require_torch_a,
		    require_torch_gpu,
		    skip_mps,
		    slow,
		    torch_all_close,
		    torch_device,
		)
		from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


lowerCamelCase		=						get_logger(__name__)






def 	lowerCamelCase_       (			_a			):




		"""simple docstring"""






		if version.parse(_a			) < version.parse(_a			):
				if "dev" in min_version:
						lowerCAmelCase__  :     Tuple       	=   (
						    '''This example requires a source install from HuggingFace diffusers (see '''
						    '''`https://huggingface.co/docs/diffusers/installation#install-from-source`),'''
						)
				else:
						lowerCAmelCase__  :     Dict       	=   f'This example requires a minimum version of {min_version},'
				error_message += f' but the version found is {__version__}.\n'
				raise ImportError(_a			)




