



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_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import PoolFormerImageProcessor





class  __magic_name__   (							unittest.TestCase						):






		def __init__(						self      :     Union[str, Any]				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Any=7				,		UpperCamelCase__      :     Union[str, Any]=3				,		UpperCamelCase__      :     Optional[Any]=30				,		UpperCamelCase__      :     Dict=4_00				,		UpperCamelCase__      :     Optional[Any]=True				,		UpperCamelCase__      :     Tuple=None				,		UpperCamelCase__      :     int=0.9				,		UpperCamelCase__      :     List[str]=None				,		UpperCamelCase__      :     Dict=True				,		UpperCamelCase__      :     Tuple=[0.5, 0.5, 0.5]				,		UpperCamelCase__      :     Dict=[0.5, 0.5, 0.5]				,		)				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =    size if size is not None else {"shortest_edge": 30}
								UpperCAmelCase         =    crop_size if crop_size is not None else {"height": 30, "width": 30}
								UpperCAmelCase         =    parent
								UpperCAmelCase         =    batch_size
								UpperCAmelCase         =    num_channels
								UpperCAmelCase         =    min_resolution
								UpperCAmelCase         =    max_resolution
								UpperCAmelCase         =    do_resize_and_center_crop
								UpperCAmelCase         =    size
								UpperCAmelCase         =    crop_pct
								UpperCAmelCase         =    crop_size
								UpperCAmelCase         =    do_normalize
								UpperCAmelCase         =    image_mean
								UpperCAmelCase         =    image_std







		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Dict:
								'''simple docstring'''







								return {
								    "size": self.size,
								    "do_resize_and_center_crop": self.do_resize_and_center_crop,
								    "crop_pct": self.crop_pct,
								    "crop_size": self.crop_size,
								    "do_normalize": self.do_normalize,
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								}





@require_torch
@require_vision
class  __magic_name__   (							A__,  unittest.TestCase						):
		lowercase      :       List[str]          =PoolFormerImageProcessor if is_vision_available() else None






		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =    PoolFormerImageProcessingTester(self      )






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Any:
								'''simple docstring'''







								return self.image_processor_tester.prepare_image_processor_dict()






		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_resize_and_center_crop"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"size"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"crop_pct"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_normalize"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_mean"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_std"      )      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class.from_dict(self.image_processor_dict      )
								self.assertEqual(image_processor.size				,		{"shortest_edge": 30}      )
								self.assertEqual(image_processor.crop_size				,		{"height": 30, "width": 30}      )

								UpperCAmelCase         =    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 SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Union[str, Any]:
								'''simple docstring'''







								pass






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random PIL images
								UpperCAmelCase         =    prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__      )
								for image in image_inputs:
														self.assertIsInstance(UpperCamelCase__				,		Image.Image      )

								# Test not batched input
								UpperCAmelCase         =    image_processing(image_inputs[0]				,		return_tensors="pt"      ).pixel_values

								self.assertEqual(
								    encoded_images.shape				,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)

								# Test batched
								UpperCAmelCase         =    image_processing(UpperCamelCase__				,		return_tensors="pt"      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random numpy tensors
								UpperCAmelCase         =    prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__				,		numpify=UpperCamelCase__      )
								for image in image_inputs:
														self.assertIsInstance(UpperCamelCase__				,		np.ndarray      )

								# Test not batched input
								UpperCAmelCase         =    image_processing(image_inputs[0]				,		return_tensors="pt"      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)

								# Test batched
								UpperCAmelCase         =    image_processing(UpperCamelCase__				,		return_tensors="pt"      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)







		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random PyTorch tensors
								UpperCAmelCase         =    prepare_image_inputs(self.image_processor_tester				,		equal_resolution=UpperCamelCase__				,		torchify=UpperCamelCase__      )
								for image in image_inputs:
														self.assertIsInstance(UpperCamelCase__				,		torch.Tensor      )

								# Test not batched input
								UpperCAmelCase         =    image_processing(image_inputs[0]				,		return_tensors="pt"      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)

								# Test batched
								UpperCAmelCase         =    image_processing(UpperCamelCase__				,		return_tensors="pt"      ).pixel_values
								self.assertEqual(
								    encoded_images.shape				,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["height"],
								        self.image_processor_tester.crop_size["width"],
								    )				,		)








import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation





class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("swish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("silu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("mish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.Mish      )

								self.assertEqual(act(torch.tensor(-2_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("gelu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.GELU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )



