





'''simple docstring'''






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







@slow
@require_torch_gpu
class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def   UpperCamelCase__      (				self    :					str  )     ->						Tuple:
    # clean up the VRAM after each test
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()


  def   UpperCamelCase__      (				self    :					str  )     ->						Tuple:
    _UpperCamelCase       			=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''  )
    _UpperCamelCase       			=sd_pipe.to(UpperCamelCase__  )
    sd_pipe.set_progress_bar_config(disable=UpperCamelCase__  )

    sd_pipe.set_scheduler('''sample_euler'''  )

    _UpperCamelCase       			='''A painting of a squirrel eating a burger'''
    _UpperCamelCase       			=torch.manual_seed(0  )
    _UpperCamelCase       			=sd_pipe([prompt]							,	generator=UpperCamelCase__							,	guidance_scale=9.0							,	num_inference_steps=20							,	output_type='''np'''  )

    _UpperCamelCase       			=output.images

    _UpperCamelCase       			=image[0, -3:, -3:, -1]

    assert image.shape == (1, 512, 512, 3)
    _UpperCamelCase       			=np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]  )

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


  def   UpperCamelCase__      (				self    :					Any  )     ->						Optional[int]:
    _UpperCamelCase       			=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
    _UpperCamelCase       			=sd_pipe.to(UpperCamelCase__  )
    sd_pipe.set_progress_bar_config(disable=UpperCamelCase__  )

    sd_pipe.set_scheduler('''sample_euler'''  )

    _UpperCamelCase       			='''A painting of a squirrel eating a burger'''
    _UpperCamelCase       			=torch.manual_seed(0  )
    _UpperCamelCase       			=sd_pipe([prompt]							,	generator=UpperCamelCase__							,	guidance_scale=9.0							,	num_inference_steps=20							,	output_type='''np'''  )

    _UpperCamelCase       			=output.images

    _UpperCamelCase       			=image[0, -3:, -3:, -1]

    assert image.shape == (1, 512, 512, 3)
    _UpperCamelCase       			=np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]  )

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




  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Optional[Any]:
    _UpperCamelCase       			=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''  )
    _UpperCamelCase       			=sd_pipe.to(UpperCamelCase__  )
    sd_pipe.set_progress_bar_config(disable=UpperCamelCase__  )

    sd_pipe.set_scheduler('''sample_dpmpp_2m'''  )

    _UpperCamelCase       			='''A painting of a squirrel eating a burger'''
    _UpperCamelCase       			=torch.manual_seed(0  )
    _UpperCamelCase       			=sd_pipe(
        [prompt]							,	generator=UpperCamelCase__							,	guidance_scale=7.5							,	num_inference_steps=15							,	output_type='''np'''							,	use_karras_sigmas=UpperCamelCase__							,	)

    _UpperCamelCase       			=output.images

    _UpperCamelCase       			=image[0, -3:, -3:, -1]

    assert image.shape == (1, 512, 512, 3)
    _UpperCamelCase       			=np.array(
        [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048]  )

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













'''simple docstring'''






from pathlib import Path

import cva
import numpy as np
from matplotlib import pyplot as plt


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=cva.getAffineTransform(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
  return cva.warpAffine(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      (rows, cols)   )


if __name__ == "__main__":
      # read original image
      __lowerCamelCase						: List[Any]		=   cva.imread(
          str(Path(__file__).resolve().parent.parent / 'image_data' / 'lena.jpg')
      )
      # turn image in gray scale value
      __lowerCamelCase						: List[Any]		=   cva.cvtColor(image, cva.COLOR_BGR2GRAY)
      # get image shape
      __lowerCamelCase		, __lowerCamelCase						: Any		=   gray_img.shape

      # set different points to rotate image
      __lowerCamelCase						: Optional[Any]		=   np.array([[50, 50], [200, 50], [50, 200]], np.floataa)
      __lowerCamelCase						: Any		=   np.array([[10, 100], [200, 50], [100, 250]], np.floataa)
      __lowerCamelCase						: int		=   np.array([[50, 50], [150, 50], [120, 200]], np.floataa)
      __lowerCamelCase						: List[Any]		=   np.array([[10, 100], [80, 50], [180, 250]], np.floataa)

      # add all rotated images in a list
      __lowerCamelCase						: int		=   [
          gray_img,
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
          get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
      ]

      # plot different image rotations
      __lowerCamelCase						: Optional[Any]		=   plt.figure(1)
      __lowerCamelCase						: Tuple		=   ['Original', 'Rotation 1', 'Rotation 2', 'Rotation 3']
      for i, image in enumerate(images):
            plt.subplot(2, 2, i + 1), plt.imshow(image, 'gray')
            plt.title(titles[i])
            plt.axis('off')
            plt.subplots_adjust(left=0.0, bottom=0.0_5, right=1.0, top=0.9_5)
      plt.show()






