



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


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import ChineseCLIPImageProcessor





class  __magic_name__   (							unittest.TestCase						):






		def __init__(						self      :     List[Any]				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     List[str]=7				,		UpperCamelCase__      :     List[str]=3				,		UpperCamelCase__      :     List[str]=18				,		UpperCamelCase__      :     Any=30				,		UpperCamelCase__      :     Optional[Any]=4_00				,		UpperCamelCase__      :     Optional[Any]=True				,		UpperCamelCase__      :     Optional[Any]=None				,		UpperCamelCase__      :     Optional[int]=True				,		UpperCamelCase__      :     int=None				,		UpperCamelCase__      :     str=True				,		UpperCamelCase__      :     Dict=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]				,		UpperCamelCase__      :     List[str]=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]				,		UpperCamelCase__      :     Tuple=True				,		)				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =    size if size is not None else {"height": 2_24, "width": 2_24}
								UpperCAmelCase         =    crop_size if crop_size is not None else {"height": 18, "width": 18}
								UpperCAmelCase         =    parent
								UpperCAmelCase         =    batch_size
								UpperCAmelCase         =    num_channels
								UpperCAmelCase         =    image_size
								UpperCAmelCase         =    min_resolution
								UpperCAmelCase         =    max_resolution
								UpperCAmelCase         =    do_resize
								UpperCAmelCase         =    size
								UpperCAmelCase         =    do_center_crop
								UpperCAmelCase         =    crop_size
								UpperCAmelCase         =    do_normalize
								UpperCAmelCase         =    image_mean
								UpperCAmelCase         =    image_std
								UpperCAmelCase         =    do_convert_rgb






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







								return {
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "do_center_crop": self.do_center_crop,
								    "crop_size": self.crop_size,
								    "do_normalize": self.do_normalize,
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_convert_rgb": self.do_convert_rgb,
								}







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







								assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

								if equal_resolution:
														UpperCAmelCase         =    []
														for i in range(self.batch_size      ):
																				image_inputs.append(
																				    np.random.randint(
																				        2_55				,		size=(self.num_channels, self.max_resolution, self.max_resolution)				,		dtype=np.uinta      )      )
								else:
														UpperCAmelCase         =    []
														for i in range(self.batch_size      ):
																				UpperCAmelCase       ,		UpperCAmelCase         =    np.random.choice(np.arange(self.min_resolution				,		self.max_resolution      )				,		2      )
																				image_inputs.append(np.random.randint(2_55				,		size=(self.num_channels, width, height)				,		dtype=np.uinta      )      )

								if not numpify and not torchify:
														# PIL expects the channel dimension as last dimension
														UpperCAmelCase         =    [Image.fromarray(np.moveaxis(UpperCamelCase__				,		0				,		-1      )      ) for x in image_inputs]

								if torchify:
														UpperCAmelCase         =    [torch.from_numpy(UpperCamelCase__      ) for x in image_inputs]

								return image_inputs





@require_torch
@require_vision
class  __magic_name__   (							_snake_case,  unittest.TestCase						):
		lowercase      :       int          =ChineseCLIPImageProcessor if is_vision_available() else None






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







								UpperCAmelCase         =    ChineseCLIPImageProcessingTester(self				,		do_center_crop=UpperCamelCase__      )






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







								return self.image_processor_tester.prepare_image_processor_dict()






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







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_resize"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"size"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_center_crop"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"center_crop"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_normalize"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_mean"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_std"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_convert_rgb"      )      )






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







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

								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      :     Dict      )				->					List[Any]:
								'''simple docstring'''







								pass






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







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random PIL images
								UpperCAmelCase         =    self.image_processor_tester.prepare_inputs(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      :     Optional[int]      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random numpy tensors
								UpperCAmelCase         =    self.image_processor_tester.prepare_inputs(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      :     str      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random PyTorch tensors
								UpperCAmelCase         =    self.image_processor_tester.prepare_inputs(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"],
								    )				,		)





@require_torch
@require_vision
class  __magic_name__   (							_snake_case,  unittest.TestCase						):
		lowercase      :       Optional[int]          =ChineseCLIPImageProcessor if is_vision_available() else None






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







								UpperCAmelCase         =    ChineseCLIPImageProcessingTester(self				,		num_channels=4				,		do_center_crop=UpperCamelCase__      )
								UpperCAmelCase         =    3






		@property
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					Any:
								'''simple docstring'''







								return self.image_processor_tester.prepare_image_processor_dict()






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







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_resize"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"size"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_center_crop"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"center_crop"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_normalize"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_mean"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"image_std"      )      )
								self.assertTrue(hasattr(UpperCamelCase__				,		"do_convert_rgb"      )      )






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







								pass







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







								UpperCAmelCase         =    self.image_processing_class(**self.image_processor_dict      )
								# create random PIL images
								UpperCAmelCase         =    self.image_processor_tester.prepare_inputs(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.expected_encoded_image_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.expected_encoded_image_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      )



