
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_video_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import VivitImageProcessor


class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      def __init__(      self		:       List[Any]				,       _snake_case		:       int				,       _snake_case		:       Dict=7				,       _snake_case		:       Dict=3				,       _snake_case		:       List[str]=10				,       _snake_case		:       List[Any]=18				,       _snake_case		:       Union[str, Any]=30				,       _snake_case		:       Dict=400				,       _snake_case		:       Optional[Any]=True				,       _snake_case		:       List[str]=None				,       _snake_case		:       List[str]=True				,       _snake_case		:       Dict=[0.5, 0.5, 0.5]				,       _snake_case		:       str=[0.5, 0.5, 0.5]				,       _snake_case		:       Optional[Any]=None				,       ):
          __lowercase		:			int	    =       size if size is not None else {'''shortest_edge''': 18}
          __lowercase		:			Any	    =       crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

          __lowercase		:			Dict	    =       parent
          __lowercase		:			Any	    =       batch_size
          __lowercase		:			str	    =       num_channels
          __lowercase		:			Optional[Any]	    =       num_frames
          __lowercase		:			str	    =       image_size
          __lowercase		:			Optional[int]	    =       min_resolution
          __lowercase		:			List[Any]	    =       max_resolution
          __lowercase		:			Any	    =       do_resize
          __lowercase		:			Dict	    =       size
          __lowercase		:			List[Any]	    =       do_normalize
          __lowercase		:			Dict	    =       image_mean
          __lowercase		:			str	    =       image_std
          __lowercase		:			int	    =       crop_size




      def 		snake_case_			(      self		:       Dict				):
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
              "crop_size": self.crop_size,
          }






@require_torch
@require_vision
class 						__lowerCAmelCase	(	lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Any        =    VivitImageProcessor if is_vision_available() else None




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			Union[str, Any]	    =       VivitImageProcessingTester(self				)




      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return self.image_processor_tester.prepare_image_processor_dict()




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			List[Any]	    =       self.image_processing_class(**self.image_processor_dict				)
          self.assertTrue(hasattr(_snake_case				,       '''image_mean'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''image_std'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''do_normalize'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''do_resize'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''do_center_crop'''				)				)
          self.assertTrue(hasattr(_snake_case				,       '''size'''				)				)




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			int	    =       self.image_processing_class.from_dict(self.image_processor_dict				)
          self.assertEqual(image_processor.size				,       {'''shortest_edge''': 18}				)
          self.assertEqual(image_processor.crop_size				,       {'''height''': 18, '''width''': 18}				)

          __lowercase		:			Optional[int]	    =       self.image_processing_class.from_dict(self.image_processor_dict				,       size=42				,       crop_size=84				)
          self.assertEqual(image_processor.size				,       {'''shortest_edge''': 42}				)
          self.assertEqual(image_processor.crop_size				,       {'''height''': 84, '''width''': 84}				)




      def 		snake_case_			(      self		:       List[Any]				):
          # Initialize image_processing
          __lowercase		:			int	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random PIL videos
          __lowercase		:			Optional[int]	    =       prepare_video_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				)
          for video in video_inputs:
              self.assertIsInstance(_snake_case				,       _snake_case				)
              self.assertIsInstance(video[0]				,       Image.Image				)

          # Test not batched input
          __lowercase		:			Optional[int]	    =       image_processing(video_inputs[0]				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			Tuple	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )




      def 		snake_case_			(      self		:       List[Any]				):
          # Initialize image_processing
          __lowercase		:			List[Any]	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random numpy tensors
          __lowercase		:			List[str]	    =       prepare_video_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				,       numpify=_snake_case				)
          for video in video_inputs:
              self.assertIsInstance(_snake_case				,       _snake_case				)
              self.assertIsInstance(video[0]				,       np.ndarray				)

          # Test not batched input
          __lowercase		:			Dict	    =       image_processing(video_inputs[0]				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			Optional[Any]	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )




      def 		snake_case_			(      self		:       Any				):
          # Initialize image_processing
          __lowercase		:			Optional[int]	    =       self.image_processing_class(**self.image_processor_dict				)
          # create random PyTorch tensors
          __lowercase		:			Union[str, Any]	    =       prepare_video_inputs(self.image_processor_tester				,       equal_resolution=_snake_case				,       torchify=_snake_case				)
          for video in video_inputs:
              self.assertIsInstance(_snake_case				,       _snake_case				)
              self.assertIsInstance(video[0]				,       torch.Tensor				)

          # Test not batched input
          __lowercase		:			Optional[int]	    =       image_processing(video_inputs[0]				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )

          # Test batched
          __lowercase		:			str	    =       image_processing(_snake_case				,       return_tensors='''pt'''				).pixel_values
          self.assertEqual(
              encoded_videos.shape				,       (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['''height'''],
                  self.image_processor_tester.crop_size['''width'''],
              )				,       )




import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
 import onnxruntime as ort


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



      """simple docstring"""







      @property
      def 		snake_case_			(      self		:       Union[str, Any]				):
          return (
              "CUDAExecutionProvider",
              {
                  "gpu_mem_limit": "15000000000",  # 15GB
                  "arena_extend_strategy": "kSameAsRequested",
              },
          )




      @property
      def 		snake_case_			(      self		:       Optional[Any]				):
          __lowercase		:			Tuple	    =       ort.SessionOptions()
          __lowercase		:			List[Any]	    =       False
          return options




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			int	    =       load_image(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
              '''/in_paint/overture-creations-5sI6fQgYIuo.png'''				)
          __lowercase		:			int	    =       load_image(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
              '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''				)
          __lowercase		:			Optional[Any]	    =       load_numpy(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
              '''/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy'''				)

          # using the PNDM scheduler by default
          __lowercase		:			Union[str, Any]	    =       OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
              '''CompVis/stable-diffusion-v1-4'''				,       revision='''onnx'''				,       safety_checker=_snake_case				,       feature_extractor=_snake_case				,       provider=self.gpu_provider				,       sess_options=self.gpu_options				,       )
          pipe.set_progress_bar_config(disable=_snake_case				)

          __lowercase		:			Tuple	    =       '''A red cat sitting on a park bench'''

          __lowercase		:			int	    =       np.random.RandomState(0				)
          __lowercase		:			int	    =       pipe(
              prompt=_snake_case				,       image=_snake_case				,       mask_image=_snake_case				,       strength=0.75				,       guidance_scale=7.5				,       num_inference_steps=15				,       generator=_snake_case				,       output_type='''np'''				,       )

          __lowercase		:			Dict	    =       output.images[0]

          assert image.shape == (512, 512, 3)
          assert np.abs(expected_image - image				).max() < 1E-2


