






"""simple docstring"""
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 MobileNetVaImageProcessor



class      SCREAMING_SNAKE_CASE  (				unittest.TestCase						):





	"""simple docstring"""




	def __init__(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_=7     ,     lowerCAmelCase_=3     ,     lowerCAmelCase_=18     ,     lowerCAmelCase_=30     ,     lowerCAmelCase_=400     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=True     ,     lowerCAmelCase_=None     ,     ):
		A_		:						Optional[Any]	    =   size if size is not None else {"shortest_edge": 20}
		A_		:						List[str]	    =   crop_size if crop_size is not None else {"height": 18, "width": 18}
		A_		:						Union[str, Any]	    =   parent
		A_		:						Union[str, Any]	    =   batch_size
		A_		:						Optional[Any]	    =   num_channels
		A_		:						List[str]	    =   image_size
		A_		:						List[str]	    =   min_resolution
		A_		:						Tuple	    =   max_resolution
		A_		:						Optional[Any]	    =   do_resize
		A_		:						Any	    =   size
		A_		:						Dict	    =   do_center_crop
		A_		:						int	    =   crop_size





	def    lowerCamelCase(self							):
		return {
		    "do_resize": self.do_resize,
		    "size": self.size,
		    "do_center_crop": self.do_center_crop,
		    "crop_size": self.crop_size,
		}






@require_torch
@require_vision
class      SCREAMING_SNAKE_CASE  (				_A       ,       unittest.TestCase						):





	"""simple docstring"""




	_A       :				Union[str, Any]    =				MobileNetVaImageProcessor if is_vision_available() else None





	def    lowerCamelCase(self							):
		A_		:						List[Any]	    =   MobileNetVaImageProcessingTester(self							)





	@property
	def    lowerCamelCase(self							):
		return self.image_processor_tester.prepare_image_processor_dict()





	def    lowerCamelCase(self							):
		A_		:						List[str]	    =   self.image_processing_class(**self.image_processor_dict							)
		self.assertTrue(hasattr(lowerCAmelCase_     ,     """do_resize"""							)							)
		self.assertTrue(hasattr(lowerCAmelCase_     ,     """size"""							)							)
		self.assertTrue(hasattr(lowerCAmelCase_     ,     """do_center_crop"""							)							)
		self.assertTrue(hasattr(lowerCAmelCase_     ,     """crop_size"""							)							)





	def    lowerCamelCase(self							):
		A_		:						List[str]	    =   self.image_processing_class.from_dict(self.image_processor_dict							)
		self.assertEqual(image_processor.size     ,     {"""shortest_edge""": 20}							)
		self.assertEqual(image_processor.crop_size     ,     {"""height""": 18, """width""": 18}							)

		A_		:						Optional[int]	    =   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    lowerCamelCase(self							):
		pass





	def    lowerCamelCase(self							):
		# Initialize image_processing
		A_		:						Tuple	    =   self.image_processing_class(**self.image_processor_dict							)
		# create random PIL images
		A_		:						int	    =   prepare_image_inputs(self.image_processor_tester     ,     equal_resolution=lowerCAmelCase_							)
		for image in image_inputs:
			self.assertIsInstance(lowerCAmelCase_     ,     Image.Image							)

		# Test not batched input
		A_		:						Optional[Any]	    =   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
		A_		:						Union[str, Any]	    =   image_processing(lowerCAmelCase_     ,     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    lowerCamelCase(self							):
		# Initialize image_processing
		A_		:						Union[str, Any]	    =   self.image_processing_class(**self.image_processor_dict							)
		# create random numpy tensors
		A_		:						Optional[Any]	    =   prepare_image_inputs(self.image_processor_tester     ,     equal_resolution=lowerCAmelCase_     ,     numpify=lowerCAmelCase_							)
		for image in image_inputs:
			self.assertIsInstance(lowerCAmelCase_     ,     np.ndarray							)

		# Test not batched input
		A_		:						Dict	    =   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
		A_		:						List[Any]	    =   image_processing(lowerCAmelCase_     ,     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    lowerCamelCase(self							):
		# Initialize image_processing
		A_		:						Tuple	    =   self.image_processing_class(**self.image_processor_dict							)
		# create random PyTorch tensors
		A_		:						Union[str, Any]	    =   prepare_image_inputs(self.image_processor_tester     ,     equal_resolution=lowerCAmelCase_     ,     torchify=lowerCAmelCase_							)
		for image in image_inputs:
			self.assertIsInstance(lowerCAmelCase_     ,     torch.Tensor							)

		# Test not batched input
		A_		:						Any	    =   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
		A_		:						List[Any]	    =   image_processing(lowerCAmelCase_     ,     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"""],
		    )     ,     )









"""simple docstring"""
import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation



class      SCREAMING_SNAKE_CASE  (				unittest.TestCase						):





	"""simple docstring"""




	def    lowerCamelCase(self							):
		A_		:						Optional[int]	    =   get_activation("""swish"""							)

		self.assertIsInstance(lowerCAmelCase_     ,     nn.SiLU							)

		self.assertEqual(act(torch.tensor(-100     ,     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    lowerCamelCase(self							):
		A_		:						Dict	    =   get_activation("""silu"""							)

		self.assertIsInstance(lowerCAmelCase_     ,     nn.SiLU							)

		self.assertEqual(act(torch.tensor(-100     ,     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    lowerCamelCase(self							):
		A_		:						Optional[Any]	    =   get_activation("""mish"""							)

		self.assertIsInstance(lowerCAmelCase_     ,     nn.Mish							)

		self.assertEqual(act(torch.tensor(-200     ,     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    lowerCamelCase(self							):
		A_		:						int	    =   get_activation("""gelu"""							)

		self.assertIsInstance(lowerCAmelCase_     ,     nn.GELU							)

		self.assertEqual(act(torch.tensor(-100     ,     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							)

