



from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
 import PIL

if is_torch_available():
 import torch


a_   :Tuple        =      logging.get_logger(__name__)




class 					snake_case__						(       snake_case_     ):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	['''pixel_values''']






     def __init__(       self  :  Union[str, Any], _snake_case  :  Union[str, Any] = True, _snake_case  :  Dict = None, _snake_case  :  Union[str, Any] = PILImageResampling.BILINEAR, _snake_case  :  List[str] = True, _snake_case  :  Union[str, Any] = 1 / 2_5_5, _snake_case  :  Optional[Any] = True, _snake_case  :  Tuple = None, _snake_case  :  str = True, **_snake_case  :  Optional[int], )   ->None:
            super().__init__(**UpperCamelCase__					)
            snake_case__							:	int   	=	size if size is not None else {"shortest_edge": 2_2_4}
            snake_case__							:	Dict   	=	get_size_dict(UpperCamelCase__, default_to_square=UpperCamelCase__					)
            snake_case__							:	Tuple   	=	crop_size if crop_size is not None else {"height": 2_5_6, "width": 2_5_6}
            snake_case__							:	Dict   	=	get_size_dict(UpperCamelCase__, param_name='crop_size'					)

            snake_case__							:	List[Any]   	=	do_resize
            snake_case__							:	Any   	=	size
            snake_case__							:	Dict   	=	resample
            snake_case__							:	str   	=	do_rescale
            snake_case__							:	List[str]   	=	rescale_factor
            snake_case__							:	Dict   	=	do_center_crop
            snake_case__							:	List[Any]   	=	crop_size
            snake_case__							:	Union[str, Any]   	=	do_flip_channel_order






     def    lowercase_   (       self  :  Union[str, Any], _snake_case  :  str, _snake_case  :  List[Any], _snake_case  :  Optional[Any] = PIL.Image.BILINEAR, _snake_case  :  Dict = None, **_snake_case  :  List[Any], )   ->np.ndarray:
            snake_case__							:	str   	=	get_size_dict(UpperCamelCase__, default_to_square=UpperCamelCase__					)
            if "shortest_edge" not in size:
                   raise ValueError(F'''The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}'''					)
            snake_case__							:	List[str]   	=	get_resize_output_image_size(UpperCamelCase__, size=size['shortest_edge'], default_to_square=UpperCamelCase__					)
            return resize(UpperCamelCase__, size=UpperCamelCase__, resample=UpperCamelCase__, data_format=UpperCamelCase__, **UpperCamelCase__					)






     def    lowercase_   (       self  :  Optional[Any], _snake_case  :  str, _snake_case  :  Any, _snake_case  :  Optional[Any] = None, **_snake_case  :  Optional[int], )   ->np.ndarray:
            snake_case__							:	str   	=	get_size_dict(UpperCamelCase__					)
            if "height" not in size or "width" not in size:
                   raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''					)
            return center_crop(UpperCamelCase__, size=(size['height'], size['width']), data_format=UpperCamelCase__, **UpperCamelCase__					)






     def    lowercase_   (       self  :  str, _snake_case  :  Any, _snake_case  :  int, _snake_case  :  List[Any] = None, **_snake_case  :  Any, )   ->Union[str, Any]:
            return rescale(UpperCamelCase__, scale=UpperCamelCase__, data_format=UpperCamelCase__, **UpperCamelCase__					)






     def    lowercase_   (       self  :  Tuple, _snake_case  :  Optional[int], _snake_case  :  Tuple = None					)   ->np.ndarray:
            return flip_channel_order(UpperCamelCase__, data_format=UpperCamelCase__					)






     def    lowercase_   (       self  :  Any, _snake_case  :  Dict, _snake_case  :  int = None, _snake_case  :  List[Any] = None, _snake_case  :  Union[str, Any] = None, _snake_case  :  Optional[Any] = None, _snake_case  :  Dict = None, _snake_case  :  List[str] = None, _snake_case  :  int = None, _snake_case  :  Any = None, _snake_case  :  str = None, _snake_case  :  Union[str, Any] = ChannelDimension.FIRST, **_snake_case  :  Union[str, Any], )   ->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__							:	List[Any]   	=	do_rescale if do_rescale is not None else self.do_rescale
            snake_case__							:	Dict   	=	rescale_factor if rescale_factor is not None else self.rescale_factor
            snake_case__							:	Any   	=	do_center_crop if do_center_crop is not None else self.do_center_crop
            snake_case__							:	List[Any]   	=	(
                do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
            )

            snake_case__							:	Any   	=	size if size is not None else self.size
            snake_case__							:	Dict   	=	get_size_dict(UpperCamelCase__, default_to_square=UpperCamelCase__					)
            snake_case__							:	Union[str, Any]   	=	crop_size if crop_size is not None else self.crop_size
            snake_case__							:	int   	=	get_size_dict(UpperCamelCase__, param_name='crop_size'					)

            snake_case__							:	Tuple   	=	make_list_of_images(UpperCamelCase__					)

            if not valid_images(UpperCamelCase__					):
                   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_rescale and rescale_factor is None:
                   raise ValueError('Rescale factor must be specified if do_rescale is True.'					)

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

            # All transformations expect numpy arrays.
            snake_case__							:	List[Any]   	=	[to_numpy_array(UpperCamelCase__					) for image in images]

            if do_resize:
                   snake_case__							:	int   	=	[self.resize(image=UpperCamelCase__, size=UpperCamelCase__, resample=UpperCamelCase__					) for image in images]

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

            if do_rescale:
                   snake_case__							:	List[str]   	=	[self.rescale(image=UpperCamelCase__, scale=UpperCamelCase__					) for image in images]

            # the pretrained checkpoints assume images are BGR, not RGB
            if do_flip_channel_order:
                   snake_case__							:	List[str]   	=	[self.flip_channel_order(image=UpperCamelCase__					) for image in images]

            snake_case__							:	Tuple   	=	[to_channel_dimension_format(UpperCamelCase__, UpperCamelCase__					) for image in images]

            snake_case__							:	Union[str, Any]   	=	{"pixel_values": images}
            return BatchFeature(data=UpperCamelCase__, tensor_type=UpperCamelCase__					)






     def    lowercase_   (       self  :  Any, _snake_case  :  Optional[int], _snake_case  :  Union[str, Any] = None					)   ->Tuple:
            snake_case__							:	str   	=	outputs.logits

            # Resize logits and compute semantic segmentation maps
            if target_sizes is not None:
                   if len(UpperCamelCase__					) != len(UpperCamelCase__					):
                          raise ValueError(
                              'Make sure that you pass in as many target sizes as the batch dimension of the logits'					)

                   if is_torch_tensor(UpperCamelCase__					):
                          snake_case__							:	Optional[int]   	=	target_sizes.numpy()

                   snake_case__							:	List[Any]   	=	[]

                   for idx in range(len(UpperCamelCase__					)					):
                          snake_case__							:	Optional[int]   	=	torch.nn.functional.interpolate(
                              logits[idx].unsqueeze(dim=0					), size=target_sizes[idx], mode='bilinear', align_corners=UpperCamelCase__					)
                          snake_case__							:	Optional[Any]   	=	resized_logits[0].argmax(dim=0					)
                          semantic_segmentation.append(UpperCamelCase__					)
            else:
                   snake_case__							:	List[str]   	=	logits.argmax(dim=1					)
                   snake_case__							:	Tuple   	=	[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]					)]

            return semantic_segmentation










"""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 _lowerCAmelCase				(      snake_case_    ,							unittest.TestCase	):
   # TODO: is there an appropriate internal test set?
   __UpperCAmelCase :       Optional[Any]							  =  '''ssube/stable-diffusion-x4-upscaler-onnx'''







   def  lowerCamelCase		(	self						,	UpperCamelCase__=0						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      Union[str, Any]      					=    floats_tensor((1, 3, 128, 128)						,	rng=random.Random(UpperCamelCase__						)						)
       snake_case    :      Any      					=    torch.manual_seed(UpperCamelCase__						)
       snake_case    :      List[str]      					=    {
           "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  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,	provider="CPUExecutionProvider"						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      Tuple      					=    self.get_dummy_inputs()
       snake_case    :      int      					=    pipe(**UpperCamelCase__						).images
       snake_case    :      Tuple      					=    image[0, -3:, -3:, -1].flatten()

       # started as 128, should now be 512
       assert image.shape == (1, 512, 512, 3)
       snake_case    :      Optional[int]      					=    np.array(
           [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]						)
       assert np.abs(image_slice - expected_slice						).max() < 1e-1







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

       '''simple docstring'''




       snake_case    :      Tuple      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,	provider="CPUExecutionProvider"						)
       snake_case    :      Optional[Any]      					=    PNDMScheduler.from_config(pipe.scheduler.config						,	skip_prk_steps=UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      Tuple      					=    self.get_dummy_inputs()
       snake_case    :      Optional[Any]      					=    pipe(**UpperCamelCase__						).images
       snake_case    :      List[str]      					=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       snake_case    :      Dict      					=    np.array(
           [0.6898892, 0.59240556, 0.52499527, 0.58866215, 0.52258235, 0.52572715, 0.62414473, 0.6174387, 0.6214964]						)
       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,	provider="CPUExecutionProvider"						)
       snake_case    :      Optional[Any]      					=    DPMSolverMultistepScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      Any      					=    self.get_dummy_inputs()
       snake_case    :      List[Any]      					=    pipe(**UpperCamelCase__						).images
       snake_case    :      List[str]      					=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       snake_case    :      Optional[Any]      					=    np.array(
           [0.7659278, 0.76437664, 0.75579107, 0.7691116, 0.77666986, 0.7727672, 0.7758664, 0.7812226, 0.76942515]						)

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







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




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

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

       assert image.shape == (1, 512, 512, 3)
       snake_case    :      Union[str, Any]      					=    np.array(
           [0.6974782, 0.68902093, 0.70135885, 0.7583618, 0.7804545, 0.7854912, 0.78667426, 0.78743863, 0.78070223]						)
       assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-1







   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(self.hub_checkpoint						,	provider="CPUExecutionProvider"						)
       snake_case    :      Optional[Any]      					=    EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      Any      					=    self.get_dummy_inputs()
       snake_case    :      int      					=    pipe(**UpperCamelCase__						).images
       snake_case    :      List[Any]      					=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 512, 512, 3)
       snake_case    :      Union[str, Any]      					=    np.array(
           [0.77424496, 0.773601, 0.7645288, 0.7769598, 0.7772739, 0.7738688, 0.78187233, 0.77879584, 0.767043]						)

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







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







   @property
   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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







   @property
   def  lowerCamelCase		(	self						)	->		List[Any]:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    ort.SessionOptions()
       snake_case    :      str      					=    False
       return options







   def  lowerCamelCase		(	self						)	->		List[Any]:

       '''simple docstring'''




       snake_case    :      str      					=    load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/img2img/sketch-mountains-input.jpg"						)
       snake_case    :      Tuple      					=    init_image.resize((128, 128)						)
       # using the PNDM scheduler by default
       snake_case    :      Tuple      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(
           "ssube/stable-diffusion-x4-upscaler-onnx"						,	provider=self.gpu_provider						,	sess_options=self.gpu_options						,	)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      List[Any]      					=    "A fantasy landscape, trending on artstation"

       snake_case    :      Union[str, Any]      					=    torch.manual_seed(0						)
       snake_case    :      int      					=    pipe(
           prompt=UpperCamelCase__						,	image=UpperCamelCase__						,	guidance_scale=7.5						,	num_inference_steps=10						,	generator=UpperCamelCase__						,	output_type="np"						,	)
       snake_case    :      str      					=    output.images
       snake_case    :      Tuple      					=    images[0, 255:258, 383:386, -1]

       assert images.shape == (1, 512, 512, 3)
       snake_case    :      int      					=    np.array([0.4883, 0.4947, 0.4980, 0.4975, 0.4982, 0.4980, 0.5000, 0.5006, 0.4972]						)
       # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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







   def  lowerCamelCase		(	self						)	->		Optional[Any]:

       '''simple docstring'''




       snake_case    :      Tuple      					=    load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/img2img/sketch-mountains-input.jpg"						)
       snake_case    :      Optional[Any]      					=    init_image.resize((128, 128)						)
       snake_case    :      Union[str, Any]      					=    LMSDiscreteScheduler.from_pretrained(
           "ssube/stable-diffusion-x4-upscaler-onnx"						,	subfolder="scheduler"						)
       snake_case    :      List[Any]      					=    OnnxStableDiffusionUpscalePipeline.from_pretrained(
           "ssube/stable-diffusion-x4-upscaler-onnx"						,	scheduler=UpperCamelCase__						,	provider=self.gpu_provider						,	sess_options=self.gpu_options						,	)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case    :      Union[str, Any]      					=    "A fantasy landscape, trending on artstation"

       snake_case    :      Tuple      					=    torch.manual_seed(0						)
       snake_case    :      str      					=    pipe(
           prompt=UpperCamelCase__						,	image=UpperCamelCase__						,	guidance_scale=7.5						,	num_inference_steps=20						,	generator=UpperCamelCase__						,	output_type="np"						,	)
       snake_case    :      List[Any]      					=    output.images
       snake_case    :      List[Any]      					=    images[0, 255:258, 383:386, -1]

       assert images.shape == (1, 512, 512, 3)
       snake_case    :      int      					=    np.array(
           [0.50173753, 0.50223356, 0.502039, 0.50233036, 0.5023725, 0.5022601, 0.5018758, 0.50234085, 0.50241566]						)
       # TODO: lower the tolerance after finding the cause of onnxruntime reproducibility issues

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





