


"""simple docstring"""





import numpy as np
from cva import COLOR_BGR2GRAY, CV_8UC3, cvtColor, filteraD, imread, imshow, waitKey


def    _snake_case  (  _snake_case :	int       ,      _snake_case :	int       ,      _snake_case :	int       ,      _snake_case :	int       ,      _snake_case :	int       ,      _snake_case :	int	):

      # prepare kernel
      # the kernel size have to be odd
      if (ksize % 2) == 0:
            lowerCAmelCase    :		Optional[Any]				=      ksize + 1
      lowerCAmelCase    :		List[Any]				=      np.zeros((ksize, ksize)       ,      dtype=np.floataa	)

      # each value
      for y in range(_snake_case	):
            for x in range(_snake_case	):
                  # distance from center
                  lowerCAmelCase    :		Any				=      x - ksize // 2
                  lowerCAmelCase    :		Any				=      y - ksize // 2

                  # degree to radiant
                  lowerCAmelCase    :		Any				=      theta / 180 * np.pi
                  lowerCAmelCase    :		Tuple				=      np.cos(_theta	)
                  lowerCAmelCase    :		Dict				=      np.sin(_theta	)

                  # get kernel x
                  lowerCAmelCase    :		str				=      cos_theta * px + sin_theta * py

                  # get kernel y
                  lowerCAmelCase    :		List[Any]				=      -sin_theta * px + cos_theta * py

                  # fill kernel
                  lowerCAmelCase    :		Tuple				=      np.exp(
                      -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)	) * np.cos(2 * np.pi * _x / lambd + psi	)

      return gabor


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 # read original image
 snake_case__						:   List[str]			 = imread('''../image_data/lena.jpg''')
 # turn image in gray scale value
 snake_case__						:   Optional[int]			 = cvtColor(img, COLOR_BGR2GRAY)

 # Apply multiple Kernel to detect edges
 snake_case__						:   Any			 = np.zeros(gray.shape[:2])
 for theta in [0, 30, 60, 90, 120, 150]:
  snake_case__						:   Any			 = gabor_filter_kernel(10, 8, theta, 10, 0, 0)
  out += filteraD(gray, CV_8UC3, kernel_aa)
 snake_case__						:   Tuple			 = out / out.max() * 255
 snake_case__						:   Optional[Any]			 = out.astype(np.uinta)

 imshow('''Original''', gray)
 imshow('''Gabor filter with 20x20 mask and 6 directions''', out)

 waitKey(0)






"""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 						snake_case_(				unittest.TestCase	):






     def    lowerCamelCase__   (		self     :    int ):
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase,					lowerCAmelCase    :		Optional[int]				=      FlaxStableDiffusionPipeline.from_pretrained(
               '''stabilityai/stable-diffusion-2'''    , revision='''bf16'''    , dtype=jnp.bfloataa    , )

           lowerCAmelCase    :		Optional[int]				=      '''A painting of a squirrel eating a burger'''
           lowerCAmelCase    :		List[str]				=      jax.device_count()
           lowerCAmelCase    :		Optional[int]				=      num_samples * [prompt]
           lowerCAmelCase    :		Any				=      sd_pipe.prepare_inputs(UpperCamelCase_ )

           lowerCAmelCase    :		Optional[int]				=      replicate(UpperCamelCase_ )
           lowerCAmelCase    :		Union[str, Any]				=      shard(UpperCamelCase_ )

           lowerCAmelCase    :		Optional[int]				=      jax.random.PRNGKey(0 )
           lowerCAmelCase    :		Optional[Any]				=      jax.random.split(UpperCamelCase_    , jax.device_count() )

           lowerCAmelCase    :		str				=      sd_pipe(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , num_inference_steps=2_5    , jit=UpperCamelCase_ )[0]
           assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

           lowerCAmelCase    :		str				=      images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
           lowerCAmelCase    :		List[str]				=      images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

           lowerCAmelCase    :		Dict				=      jnp.asarray(jax.device_get(image_slice.flatten() ) )
           lowerCAmelCase    :		List[str]				=      jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512] )
           print(F'''output_slice: {output_slice}''' )
           assert jnp.abs(output_slice - expected_slice ).max() < 1E-2






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase    :		Union[str, Any]				=      '''stabilityai/stable-diffusion-2'''
           lowerCAmelCase,					lowerCAmelCase    :		Dict				=      FlaxDPMSolverMultistepScheduler.from_pretrained(UpperCamelCase_    , subfolder='''scheduler''' )
           lowerCAmelCase,					lowerCAmelCase    :		int				=      FlaxStableDiffusionPipeline.from_pretrained(
               UpperCamelCase_    , scheduler=UpperCamelCase_    , revision='''bf16'''    , dtype=jnp.bfloataa    , )
           lowerCAmelCase    :		List[Any]				=      scheduler_params

           lowerCAmelCase    :		List[Any]				=      '''A painting of a squirrel eating a burger'''
           lowerCAmelCase    :		Any				=      jax.device_count()
           lowerCAmelCase    :		int				=      num_samples * [prompt]
           lowerCAmelCase    :		int				=      sd_pipe.prepare_inputs(UpperCamelCase_ )

           lowerCAmelCase    :		Dict				=      replicate(UpperCamelCase_ )
           lowerCAmelCase    :		Tuple				=      shard(UpperCamelCase_ )

           lowerCAmelCase    :		int				=      jax.random.PRNGKey(0 )
           lowerCAmelCase    :		Optional[int]				=      jax.random.split(UpperCamelCase_    , jax.device_count() )

           lowerCAmelCase    :		Tuple				=      sd_pipe(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_    , num_inference_steps=2_5    , jit=UpperCamelCase_ )[0]
           assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

           lowerCAmelCase    :		Any				=      images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
           lowerCAmelCase    :		str				=      images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

           lowerCAmelCase    :		Optional[int]				=      jnp.asarray(jax.device_get(image_slice.flatten() ) )
           lowerCAmelCase    :		Tuple				=      jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297] )
           print(F'''output_slice: {output_slice}''' )
           assert jnp.abs(output_slice - expected_slice ).max() < 1E-2


