



"""simple docstring"""






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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


lowerCAmelCase__      =    logging.get_logger(__name__)


if is_vision_available():
   import PIL






class 	__snake_case	(     _lowercase):
  snake_case__    :       Optional[Any]        =						["pixel_values"]




  def __init__(			self					:						Any						, __lowerCAmelCase					:						bool = True						, __lowerCAmelCase					:						Dict[str, int] = None						, __lowerCAmelCase					:						PILImageResampling = PILImageResampling.BICUBIC						, __lowerCAmelCase					:						bool = True						, __lowerCAmelCase					:						Dict[str, int] = None						, __lowerCAmelCase					:						bool = True						, __lowerCAmelCase					:						Union[int, float] = 1 / 2_5_5						, __lowerCAmelCase					:						bool = True						, __lowerCAmelCase					:						Optional[Union[float, List[float]]] = None						, __lowerCAmelCase					:						Optional[Union[float, List[float]]] = None						, __lowerCAmelCase					:						bool = True						, **__lowerCAmelCase					:						Optional[Any]						, ):






         """simple docstring"""






         super().__init__(**__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[Any]         =			size if size is not None else {'''shortest_edge''': 2_2_4}
         _lowerCamelCase						:    Tuple         =			get_size_dict(__lowerCAmelCase						, default_to_square=__lowerCAmelCase		)
         _lowerCamelCase						:    Optional[int]         =			crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
         _lowerCamelCase						:    List[Any]         =			get_size_dict(__lowerCAmelCase						, default_to_square=__lowerCAmelCase						, param_name='''crop_size'''		)

         _lowerCamelCase						:    Optional[Any]         =			do_resize
         _lowerCamelCase						:    Optional[int]         =			size
         _lowerCamelCase						:    Optional[int]         =			resample
         _lowerCamelCase						:    Dict         =			do_center_crop
         _lowerCamelCase						:    int         =			crop_size
         _lowerCamelCase						:    List[str]         =			do_rescale
         _lowerCamelCase						:    Union[str, Any]         =			rescale_factor
         _lowerCamelCase						:    Any         =			do_normalize
         _lowerCamelCase						:    Union[str, Any]         =			image_mean if image_mean is not None else OPENAI_CLIP_MEAN
         _lowerCamelCase						:    List[Any]         =			image_std if image_std is not None else OPENAI_CLIP_STD
         _lowerCamelCase						:    Optional[Any]         =			do_convert_rgb




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]						, __lowerCAmelCase					:						np.ndarray						, __lowerCAmelCase					:						Dict[str, int]						, __lowerCAmelCase					:						PILImageResampling = PILImageResampling.BICUBIC						, __lowerCAmelCase					:						Optional[Union[str, ChannelDimension]] = None						, **__lowerCAmelCase					:						List[str]						, ):






         """simple docstring"""






         _lowerCamelCase						:    List[Any]         =			get_size_dict(__lowerCAmelCase						, default_to_square=__lowerCAmelCase		)
         if "shortest_edge" not in size:
                raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''		)
         _lowerCamelCase						:    List[Any]         =			get_resize_output_image_size(__lowerCAmelCase						, size=size['''shortest_edge''']						, default_to_square=__lowerCAmelCase		)
         return resize(__lowerCAmelCase						, size=__lowerCAmelCase						, resample=__lowerCAmelCase						, data_format=__lowerCAmelCase						, **__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						np.ndarray						, __lowerCAmelCase					:						Dict[str, int]						, __lowerCAmelCase					:						Optional[Union[str, ChannelDimension]] = None						, **__lowerCAmelCase					:						Optional[Any]						, ):






         """simple docstring"""






         _lowerCamelCase						:    int         =			get_size_dict(__lowerCAmelCase		)
         if "height" not in size or "width" not in size:
                raise ValueError(f'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''		)
         return center_crop(__lowerCAmelCase						, size=(size['''height'''], size['''width'''])						, data_format=__lowerCAmelCase						, **__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						str						, __lowerCAmelCase					:						np.ndarray						, __lowerCAmelCase					:						Union[int, float]						, __lowerCAmelCase					:						Optional[Union[str, ChannelDimension]] = None						, **__lowerCAmelCase					:						List[Any]						, ):






         """simple docstring"""






         return rescale(__lowerCAmelCase						, scale=__lowerCAmelCase						, data_format=__lowerCAmelCase						, **__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						np.ndarray						, __lowerCAmelCase					:						Union[float, List[float]]						, __lowerCAmelCase					:						Union[float, List[float]]						, __lowerCAmelCase					:						Optional[Union[str, ChannelDimension]] = None						, **__lowerCAmelCase					:						int						, ):






         """simple docstring"""






         return normalize(__lowerCAmelCase						, mean=__lowerCAmelCase						, std=__lowerCAmelCase						, data_format=__lowerCAmelCase						, **__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						Union[str, Any]						, __lowerCAmelCase					:						ImageInput						, __lowerCAmelCase					:						bool = None						, __lowerCAmelCase					:						Dict[str, int] = None						, __lowerCAmelCase					:						PILImageResampling = None						, __lowerCAmelCase					:						bool = None						, __lowerCAmelCase					:						int = None						, __lowerCAmelCase					:						bool = None						, __lowerCAmelCase					:						float = None						, __lowerCAmelCase					:						bool = None						, __lowerCAmelCase					:						Optional[Union[float, List[float]]] = None						, __lowerCAmelCase					:						Optional[Union[float, List[float]]] = None						, __lowerCAmelCase					:						bool = None						, __lowerCAmelCase					:						Optional[Union[str, TensorType]] = None						, __lowerCAmelCase					:						Optional[ChannelDimension] = ChannelDimension.FIRST						, **__lowerCAmelCase					:						Tuple						, ):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			do_resize if do_resize is not None else self.do_resize
         _lowerCamelCase						:    List[str]         =			size if size is not None else self.size
         _lowerCamelCase						:    Optional[Any]         =			get_size_dict(__lowerCAmelCase						, param_name='''size'''						, default_to_square=__lowerCAmelCase		)
         _lowerCamelCase						:    str         =			resample if resample is not None else self.resample
         _lowerCamelCase						:    Dict         =			do_center_crop if do_center_crop is not None else self.do_center_crop
         _lowerCamelCase						:    int         =			crop_size if crop_size is not None else self.crop_size
         _lowerCamelCase						:    Optional[int]         =			get_size_dict(__lowerCAmelCase						, param_name='''crop_size'''						, default_to_square=__lowerCAmelCase		)
         _lowerCamelCase						:    List[Any]         =			do_rescale if do_rescale is not None else self.do_rescale
         _lowerCamelCase						:    int         =			rescale_factor if rescale_factor is not None else self.rescale_factor
         _lowerCamelCase						:    Optional[int]         =			do_normalize if do_normalize is not None else self.do_normalize
         _lowerCamelCase						:    str         =			image_mean if image_mean is not None else self.image_mean
         _lowerCamelCase						:    Optional[int]         =			image_std if image_std is not None else self.image_std
         _lowerCamelCase						:    Union[str, Any]         =			do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

         _lowerCamelCase						:    str         =			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:
                raise ValueError('''Size 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.'''		)

         # PIL RGBA images are converted to RGB
         if do_convert_rgb:
                _lowerCamelCase						:    List[str]         =			[convert_to_rgb(__lowerCAmelCase		) for image in images]

         # All transformations expect numpy arrays.
         _lowerCamelCase						:    Dict         =			[to_numpy_array(__lowerCAmelCase		) for image in images]

         if do_resize:
                _lowerCamelCase						:    str         =			[self.resize(image=__lowerCAmelCase						, size=__lowerCAmelCase						, resample=__lowerCAmelCase		) for image in images]

         if do_center_crop:
                _lowerCamelCase						:    int         =			[self.center_crop(image=__lowerCAmelCase						, size=__lowerCAmelCase		) for image in images]

         if do_rescale:
                _lowerCamelCase						:    Optional[Any]         =			[self.rescale(image=__lowerCAmelCase						, scale=__lowerCAmelCase		) for image in images]

         if do_normalize:
                _lowerCamelCase						:    int         =			[self.normalize(image=__lowerCAmelCase						, mean=__lowerCAmelCase						, std=__lowerCAmelCase		) for image in images]

         _lowerCamelCase						:    Tuple         =			[to_channel_dimension_format(__lowerCAmelCase						, __lowerCAmelCase		) for image in images]

         _lowerCamelCase						:    Optional[Any]         =			{'''pixel_values''': images}
         return BatchFeature(data=__lowerCAmelCase						, tensor_type=__lowerCAmelCase		)







"""simple docstring"""






import random
import unittest

import numpy as np
import torch

from diffusers import (
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionUpscalePipeline,
    PNDMScheduler,
)
from diffusers.utils import floats_tensor
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
   import onnxruntime as ort






class 	__snake_case	(     _lowercase  ,					unittest.TestCase):
  # TODO: is there an appropriate internal test set?
  snake_case__    :       List[str]        =						"ssube/stable-diffusion-x4-upscaler-onnx"




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						int=0		):






         """simple docstring"""






         _lowerCamelCase						:    Tuple         =			floats_tensor((1, 3, 1_2_8, 1_2_8)						, rng=random.Random(__lowerCAmelCase		)		)
         _lowerCamelCase						:    Union[str, Any]         =			torch.manual_seed(__lowerCAmelCase		)
         _lowerCamelCase						:    Tuple         =			{
             '''prompt''': '''A painting of a squirrel eating a burger''',
             '''image''': image,
             '''generator''': generator,
             '''num_inference_steps''': 3,
             '''guidance_scale''': 7.5,
             '''output_type''': '''numpy''',
         }
         return inputs




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Union[str, Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Any         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Dict         =			image[0, -3:, -3:, -1].flatten()

         # started as 128, should now be 512
         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23]		)
         assert np.abs(image_slice - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    List[str]         =			PNDMScheduler.from_config(pipe.scheduler.config						, skip_prk_steps=__lowerCAmelCase		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    int         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Optional[Any]         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    str         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Optional[int]         =			np.array(
             [0.6_89_88_92, 0.59_24_05_56, 0.52_49_95_27, 0.58_86_62_15, 0.52_25_82_35, 0.52_57_27_15, 0.62_41_44_73, 0.6_17_43_87, 0.6_21_49_64]		)
         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    Optional[int]         =			DPMSolverMultistepScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Tuple         =			self.get_dummy_inputs()
         _lowerCamelCase						:    str         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Tuple         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.7_65_92_78, 0.76_43_76_64, 0.75_57_91_07, 0.7_69_11_16, 0.77_66_69_86, 0.7_72_76_72, 0.7_75_86_64, 0.7_81_22_26, 0.76_94_25_15]		)

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Any         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    Union[str, Any]         =			EulerDiscreteScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Optional[Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    Tuple         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Union[str, Any]         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Union[str, Any]         =			np.array(
             [0.6_97_47_82, 0.68_90_20_93, 0.70_13_58_85, 0.7_58_36_18, 0.7_80_45_45, 0.7_85_49_12, 0.78_66_74_26, 0.78_74_38_63, 0.78_07_02_23]		)
         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Dict         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						, provider='''CPUExecutionProvider'''		)
         _lowerCamelCase						:    int         =			EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config		)
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Union[str, Any]         =			self.get_dummy_inputs()
         _lowerCamelCase						:    List[Any]         =			pipe(**__lowerCAmelCase		).images
         _lowerCamelCase						:    Optional[Any]         =			image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    Optional[int]         =			np.array(
             [0.77_42_44_96, 0.77_36_01, 0.7_64_52_88, 0.7_76_95_98, 0.7_77_27_39, 0.7_73_86_88, 0.78_18_72_33, 0.77_87_95_84, 0.76_70_43]		)

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-1






@nightly
@require_onnxruntime
@require_torch_gpu
class 	__snake_case	(     unittest.TestCase):




  @property
  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         return (
             "CUDAExecutionProvider",
             {
                 "gpu_mem_limit": "15000000000",  # 15GB
                 "arena_extend_strategy": "kSameAsRequested",
             },
         )




  @property
  def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			ort.SessionOptions()
         _lowerCamelCase						:    List[str]         =			False
         return options




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
             '''/img2img/sketch-mountains-input.jpg'''		)
         _lowerCamelCase						:    Any         =			init_image.resize((1_2_8, 1_2_8)		)
         # using the PNDM scheduler by default
         _lowerCamelCase						:    List[str]         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, provider=self.gpu_provider						, sess_options=self.gpu_options						, )
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    int         =			'''A fantasy landscape, trending on artstation'''

         _lowerCamelCase						:    List[Any]         =			torch.manual_seed(0		)
         _lowerCamelCase						:    List[str]         =			pipe(
             prompt=__lowerCAmelCase						, image=__lowerCAmelCase						, guidance_scale=7.5						, num_inference_steps=1_0						, generator=__lowerCAmelCase						, output_type='''np'''						, )
         _lowerCamelCase						:    List[Any]         =			output.images
         _lowerCamelCase						:    List[Any]         =			images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

         assert images.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array([0.48_83, 0.49_47, 0.49_80, 0.49_75, 0.49_82, 0.49_80, 0.50_00, 0.50_06, 0.49_72]		)
         # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 2E-2




  def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






         """simple docstring"""






         _lowerCamelCase						:    Union[str, Any]         =			load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
             '''/img2img/sketch-mountains-input.jpg'''		)
         _lowerCamelCase						:    int         =			init_image.resize((1_2_8, 1_2_8)		)
         _lowerCamelCase						:    str         =			LMSDiscreteScheduler.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, subfolder='''scheduler'''		)
         _lowerCamelCase						:    Dict         =			OnnxStableDiffusionUpscalePipeline.from_pretrained(
             '''ssube/stable-diffusion-x4-upscaler-onnx'''						, scheduler=__lowerCAmelCase						, provider=self.gpu_provider						, sess_options=self.gpu_options						, )
         pipe.set_progress_bar_config(disable=__lowerCAmelCase		)

         _lowerCamelCase						:    Optional[int]         =			'''A fantasy landscape, trending on artstation'''

         _lowerCamelCase						:    int         =			torch.manual_seed(0		)
         _lowerCamelCase						:    List[str]         =			pipe(
             prompt=__lowerCAmelCase						, image=__lowerCAmelCase						, guidance_scale=7.5						, num_inference_steps=2_0						, generator=__lowerCAmelCase						, output_type='''np'''						, )
         _lowerCamelCase						:    Union[str, Any]         =			output.images
         _lowerCamelCase						:    Optional[Any]         =			images[0, 2_5_5:2_5_8, 3_8_3:3_8_6, -1]

         assert images.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCamelCase						:    str         =			np.array(
             [0.50_17_37_53, 0.50_22_33_56, 0.50_20_39, 0.50_23_30_36, 0.5_02_37_25, 0.5_02_26_01, 0.5_01_87_58, 0.50_23_40_85, 0.50_24_15_66]		)
         # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

         assert np.abs(image_slice.flatten() - expected_slice		).max() < 2E-2


