





'''simple docstring'''






import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
      import jax
      import jax.numpy as jnp
      from flax.jax_utils import replicate
      from flax.training.common_utils import shard




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




      def     lowercase__   (      self      :			List[str]  )    ->				Union[str, Any]:
            # clean up the VRAM after each test
            super().tearDown()
            gc.collect()




      def     lowercase__   (      self      :			Optional[int]  )    ->				List[Any]:
            _lowerCAmelCase					,							_lowerCAmelCase												=							FlaxStableDiffusionPipeline.from_pretrained(
                """stabilityai/stable-diffusion-2"""      ,      revision="""bf16"""      ,      dtype=jnp.bfloataa      ,      )

            _lowerCAmelCase												=							"""A painting of a squirrel eating a burger"""
            _lowerCAmelCase												=							jax.device_count()
            _lowerCAmelCase												=							num_samples * [prompt]
            _lowerCAmelCase												=							sd_pipe.prepare_inputs(__snake_case  )

            _lowerCAmelCase												=							replicate(__snake_case  )
            _lowerCAmelCase												=							shard(__snake_case  )

            _lowerCAmelCase												=							jax.random.PRNGKey(0  )
            _lowerCAmelCase												=							jax.random.split(__snake_case      ,      jax.device_count()  )

            _lowerCAmelCase												=							sd_pipe(__snake_case      ,      __snake_case      ,      __snake_case      ,      num_inference_steps=25      ,      jit=__snake_case  )[0]
            assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

            _lowerCAmelCase												=							images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
            _lowerCAmelCase												=							images[0, 2_53:2_56, 2_53:2_56, -1]

            _lowerCAmelCase												=							jnp.asarray(jax.device_get(image_slice.flatten()  )  )
            _lowerCAmelCase												=							jnp.array([0.42_38, 0.44_14, 0.43_95, 0.44_53, 0.46_29, 0.45_90, 0.45_31, 0.4_55_08, 0.45_12]  )
            print(f"output_slice: {output_slice}"  )
            assert jnp.abs(output_slice - expected_slice  ).max() < 1E-2




      def     lowercase__   (      self      :			Optional[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							"""stabilityai/stable-diffusion-2"""
            _lowerCAmelCase					,							_lowerCAmelCase												=							FlaxDPMSolverMultistepScheduler.from_pretrained(__snake_case      ,      subfolder="""scheduler"""  )
            _lowerCAmelCase					,							_lowerCAmelCase												=							FlaxStableDiffusionPipeline.from_pretrained(
                __snake_case      ,      scheduler=__snake_case      ,      revision="""bf16"""      ,      dtype=jnp.bfloataa      ,      )
            _lowerCAmelCase												=							scheduler_params

            _lowerCAmelCase												=							"""A painting of a squirrel eating a burger"""
            _lowerCAmelCase												=							jax.device_count()
            _lowerCAmelCase												=							num_samples * [prompt]
            _lowerCAmelCase												=							sd_pipe.prepare_inputs(__snake_case  )

            _lowerCAmelCase												=							replicate(__snake_case  )
            _lowerCAmelCase												=							shard(__snake_case  )

            _lowerCAmelCase												=							jax.random.PRNGKey(0  )
            _lowerCAmelCase												=							jax.random.split(__snake_case      ,      jax.device_count()  )

            _lowerCAmelCase												=							sd_pipe(__snake_case      ,      __snake_case      ,      __snake_case      ,      num_inference_steps=25      ,      jit=__snake_case  )[0]
            assert images.shape == (jax.device_count(), 1, 7_68, 7_68, 3)

            _lowerCAmelCase												=							images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]  )
            _lowerCAmelCase												=							images[0, 2_53:2_56, 2_53:2_56, -1]

            _lowerCAmelCase												=							jnp.asarray(jax.device_get(image_slice.flatten()  )  )
            _lowerCAmelCase												=							jnp.array([0.43_36, 0.4_29_69, 0.44_53, 0.41_99, 0.42_97, 0.45_31, 0.44_34, 0.44_34, 0.42_97]  )
            print(f"output_slice: {output_slice}"  )
            assert jnp.abs(output_slice - expected_slice  ).max() < 1E-2













'''simple docstring'''






from pathlib import Path

import cva
import numpy as np
from matplotlib import pyplot as plt
def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase  ):





      """simple docstring"""


      _lowerCAmelCase												=							cva.getAffineTransform(lowerCAmelCase							,     lowerCAmelCase  )
      return cva.warpAffine(lowerCAmelCase							,     lowerCAmelCase							,     (rows, cols)  )


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

      # set different points to rotate image
      A__    :       Optional[int]              =np.array([[50, 50], [2_00, 50], [50, 2_00]], np.floataa)
      A__    :       Tuple              =np.array([[10, 1_00], [2_00, 50], [1_00, 2_50]], np.floataa)
      A__    :       List[str]              =np.array([[50, 50], [1_50, 50], [1_20, 2_00]], np.floataa)
      A__    :       Dict              =np.array([[10, 1_00], [80, 50], [1_80, 2_50]], np.floataa)

      # add all rotated images in a list
      A__    :       List[Any]              =[
          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
      A__    :       Tuple              =plt.figure(1)
      A__    :       int              =['''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.05, right=1.0, top=0.95)
      plt.show()






