




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
	import PIL


__UpperCAmelCase							:   Tuple							=  logging.get_logger(__name__)


class 					_snake_case						(			a__ ):
	_A          =   ["pixel_values"]






	def __init__(		self					,UpperCamelCase = True					,UpperCamelCase = None					,UpperCamelCase = PIL.Image.BICUBIC					,UpperCamelCase = True					,UpperCamelCase = None					,UpperCamelCase = 1 / 255					,UpperCamelCase = True					,UpperCamelCase = True					,UpperCamelCase = None					,UpperCamelCase = None					,**UpperCamelCase					,)						->    None:
					super().__init__(**lowerCamelCase_       )
					snake_case__      :int         =   size if size is not None else {"height": 256, "width": 256}
					snake_case__      :Dict         =   get_size_dict(lowerCamelCase_       )
					snake_case__      :Optional[int]         =   crop_size if crop_size is not None else {"height": 224, "width": 224}
					snake_case__      :Union[str, Any]         =   get_size_dict(lowerCamelCase_					,param_name="crop_size"       )

					snake_case__      :str         =   do_resize
					snake_case__      :List[Any]         =   size
					snake_case__      :Union[str, Any]         =   resample
					snake_case__      :Any         =   do_center_crop
					snake_case__      :List[Any]         =   crop_size
					snake_case__      :Optional[Any]         =   do_rescale
					snake_case__      :Tuple         =   rescale_factor
					snake_case__      :Optional[Any]         =   do_normalize
					snake_case__      :str         =   image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
					snake_case__      :str         =   image_std if image_std is not None else IMAGENET_STANDARD_STD






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = PIL.Image.BICUBIC					,UpperCamelCase = None					,**UpperCamelCase					,)						->    np.ndarray:
					snake_case__      :int         =   get_size_dict(lowerCamelCase_       )
					if "height" not in size or "width" not in size:
									raise ValueError(f'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'       )
					return resize(
					    lowerCamelCase_					,size=(size["height"], size["width"])					,resample=lowerCamelCase_					,data_format=lowerCamelCase_					,**lowerCamelCase_       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = None					,**UpperCamelCase					,)						->    np.ndarray:
					snake_case__      :int         =   get_size_dict(lowerCamelCase_       )
					if "height" not in size or "width" not in size:
									raise ValueError(f'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'       )
					return center_crop(lowerCamelCase_					,size=(size["height"], size["width"])					,data_format=lowerCamelCase_					,**lowerCamelCase_       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = None					,**UpperCamelCase					,)						->    List[Any]:
					return rescale(lowerCamelCase_					,scale=lowerCamelCase_					,data_format=lowerCamelCase_					,**lowerCamelCase_       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = None					,**UpperCamelCase					,)						->    np.ndarray:
					return normalize(lowerCamelCase_					,mean=lowerCamelCase_					,std=lowerCamelCase_					,data_format=lowerCamelCase_					,**lowerCamelCase_       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase=None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = None					,UpperCamelCase = ChannelDimension.FIRST					,**UpperCamelCase					,)						->    PIL.Image.Image:
					snake_case__      :Tuple         =   do_resize if do_resize is not None else self.do_resize
					snake_case__      :Union[str, Any]         =   resample if resample is not None else self.resample
					snake_case__      :Dict         =   do_center_crop if do_center_crop is not None else self.do_center_crop
					snake_case__      :Dict         =   do_rescale if do_rescale is not None else self.do_rescale
					snake_case__      :List[Any]         =   rescale_factor if rescale_factor is not None else self.rescale_factor
					snake_case__      :List[str]         =   do_normalize if do_normalize is not None else self.do_normalize
					snake_case__      :Union[str, Any]         =   image_mean if image_mean is not None else self.image_mean
					snake_case__      :List[str]         =   image_std if image_std is not None else self.image_std

					snake_case__      :Union[str, Any]         =   size if size is not None else self.size
					snake_case__      :Optional[int]         =   get_size_dict(lowerCamelCase_       )
					snake_case__      :List[Any]         =   crop_size if crop_size is not None else self.crop_size
					snake_case__      :int         =   get_size_dict(lowerCamelCase_					,param_name="crop_size"       )

					snake_case__      :Optional[int]         =   make_list_of_images(lowerCamelCase_       )

					if not valid_images(lowerCamelCase_       ):
									raise ValueError(
									    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
									    "torch.Tensor, tf.Tensor or jax.ndarray."       )

					if do_resize and size is None or resample is None:
									raise ValueError("Size and resample must be specified if do_resize is True."       )

					if do_center_crop and crop_size is None:
									raise ValueError("Crop size must be specified if do_center_crop is True."       )

					if do_rescale and rescale_factor is None:
									raise ValueError("Rescale factor must be specified if do_rescale is True."       )

					if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("Image mean and std must be specified if do_normalize is True."       )

					# All transformations expect numpy arrays.
					snake_case__      :str         =   [to_numpy_array(lowerCamelCase_       ) for image in images]

					if do_resize:
									snake_case__      :List[str]         =   [self.resize(image=lowerCamelCase_					,size=lowerCamelCase_					,resample=lowerCamelCase_       ) for image in images]

					if do_center_crop:
									snake_case__      :Optional[int]         =   [self.center_crop(image=lowerCamelCase_					,size=lowerCamelCase_       ) for image in images]

					if do_rescale:
									snake_case__      :Union[str, Any]         =   [self.rescale(image=lowerCamelCase_					,scale=lowerCamelCase_       ) for image in images]

					if do_normalize:
									snake_case__      :Any         =   [self.normalize(image=lowerCamelCase_					,mean=lowerCamelCase_					,std=lowerCamelCase_       ) for image in images]

					snake_case__      :Any         =   [to_channel_dimension_format(lowerCamelCase_					,lowerCamelCase_       ) for image in images]

					snake_case__      :Dict         =   {"pixel_values": images}
					return BatchFeature(data=lowerCamelCase_					,tensor_type=lowerCamelCase_       )







import os
import sys
import unittest


__UpperCAmelCase							:   str							=  os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
__UpperCAmelCase							:   Tuple							=  os.path.join(git_repo_path, "src", "diffusers")


class 					_snake_case						(			unittest.TestCase ):






	def    lowerCAmelCase_							(		self       )						->    Union[str, Any]:
					snake_case__      :Tuple         =   find_backend("    if not is_torch_available():"       )
					self.assertEqual(UpperCamelCase					,"torch"       )

					# backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
					# self.assertEqual(backend_with_underscore, "tensorflow_text")

					snake_case__      :Tuple         =   find_backend("    if not (is_torch_available() and is_transformers_available()):"       )
					self.assertEqual(UpperCamelCase					,"torch_and_transformers"       )

					# double_backend_with_underscore = find_backend(
					#    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
					# )
					# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

					snake_case__      :str         =   find_backend(
					    "    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"       )
					self.assertEqual(UpperCamelCase					,"torch_and_transformers_and_onnx"       )






	def    lowerCAmelCase_							(		self       )						->    str:
					snake_case__      :int         =   read_init()
					# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
					self.assertIn("torch"					,UpperCamelCase       )
					self.assertIn("torch_and_transformers"					,UpperCamelCase       )
					self.assertIn("flax_and_transformers"					,UpperCamelCase       )
					self.assertIn("torch_and_transformers_and_onnx"					,UpperCamelCase       )

					# Likewise, we can't assert on the exact content of a key
					self.assertIn("UNet2DModel"					,objects["torch"]       )
					self.assertIn("FlaxUNet2DConditionModel"					,objects["flax"]       )
					self.assertIn("StableDiffusionPipeline"					,objects["torch_and_transformers"]       )
					self.assertIn("FlaxStableDiffusionPipeline"					,objects["flax_and_transformers"]       )
					self.assertIn("LMSDiscreteScheduler"					,objects["torch_and_scipy"]       )
					self.assertIn("OnnxStableDiffusionPipeline"					,objects["torch_and_transformers_and_onnx"]       )






	def    lowerCAmelCase_							(		self       )						->    Any:
					snake_case__      :Union[str, Any]         =   create_dummy_object("CONSTANT"					,"'torch'"       )
					self.assertEqual(UpperCamelCase					,"\nCONSTANT = None\n"       )

					snake_case__      :Optional[Any]         =   create_dummy_object("function"					,"'torch'"       )
					self.assertEqual(
					    UpperCamelCase					,"\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"       )

					snake_case__      :str         =   "\nclass FakeClass(metaclass=DummyObject):\n    _backends = 'torch'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, 'torch')\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n"
					snake_case__      :List[str]         =   create_dummy_object("FakeClass"					,"'torch'"       )
					self.assertEqual(UpperCamelCase					,UpperCamelCase       )






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					snake_case__      :Tuple         =   "# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = [\"torch\"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, [\"torch\"])\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n"
					snake_case__      :int         =   create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}       )
					self.assertEqual(dummy_files["torch"]					,UpperCamelCase       )