





import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
	import torch

if is_tf_available():
	import tensorflow as tf




@require_vision
@require_torchvision
class 	UpperCamelCase_							(  unittest.TestCase     ):


							def   _snake_case	(				self     :Dict		) -> List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   tempfile.mkdtemp()
									SCREAMING_SNAKE_CASE__								=   SamImageProcessor()
									SCREAMING_SNAKE_CASE__								=   SamProcessor(__A		)
									processor.save_pretrained(self.tmpdirname		)


							def   _snake_case	(				self     :Dict				,					**__A     :Any		) -> List[str]:



									"""simple docstring"""



									return AutoProcessor.from_pretrained(self.tmpdirname				,					**__A		).image_processor


							def   _snake_case	(				self     :Tuple		) -> List[Any]:



									"""simple docstring"""



									shutil.rmtree(self.tmpdirname		)


							def   _snake_case	(				self     :Dict		) -> List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [np.random.randint(255				,					size=(3, 30, 400)				,					dtype=np.uinta		)]

									SCREAMING_SNAKE_CASE__								=   [Image.fromarray(np.moveaxis(__A				,					0				,					-1		)		) for x in image_inputs]

									return image_inputs


							def   _snake_case	(				self     :Tuple		) -> Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=self.get_image_processor()		)
									processor.save_pretrained(self.tmpdirname		)

									SCREAMING_SNAKE_CASE__								=   self.get_image_processor(do_normalize=__A				,					padding_value=1.0		)

									SCREAMING_SNAKE_CASE__								=   SamProcessor.from_pretrained(self.tmpdirname				,					do_normalize=__A				,					padding_value=1.0		)

									self.assertEqual(processor.image_processor.to_json_string()				,					image_processor_add_kwargs.to_json_string()		)
									self.assertIsInstance(processor.image_processor				,					__A		)


							def   _snake_case	(				self     :Union[str, Any]		) -> List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)

									SCREAMING_SNAKE_CASE__								=   self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE__								=   image_processor(__A				,					return_tensors="""np"""		)
									SCREAMING_SNAKE_CASE__								=   processor(images=__A				,					return_tensors="""np"""		)

									input_feat_extract.pop("""original_sizes"""		)  # pop original_sizes as it is popped in the processor
									input_feat_extract.pop("""reshaped_input_sizes"""		)  # pop original_sizes as it is popped in the processor

									for key in input_feat_extract.keys():
											self.assertAlmostEqual(input_feat_extract[key].sum()				,					input_processor[key].sum()				,					delta=1E-2		)


							@require_torch
							def   _snake_case	(				self     :Union[str, Any]		) -> Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)
									SCREAMING_SNAKE_CASE__								=   [torch.ones((1, 3, 5, 5)		)]

									SCREAMING_SNAKE_CASE__								=   [[1764, 2646]]

									SCREAMING_SNAKE_CASE__								=   [[683, 1024]]
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(__A				,					__A				,					__A		)
									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
									    __A				,					torch.tensor(__A		)				,					torch.tensor(__A		)		)
									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									# should also work with np
									SCREAMING_SNAKE_CASE__								=   [np.ones((1, 3, 5, 5)		)]
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(__A				,					np.array(__A		)				,					np.array(__A		)		)

									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									SCREAMING_SNAKE_CASE__								=   [[1, 0], [0, 1]]
									with self.assertRaises(__A		):
											SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(__A				,					np.array(__A		)				,					np.array(__A		)		)




@require_vision
@require_tf
class 	UpperCamelCase_							(  unittest.TestCase     ):


							def   _snake_case	(				self     :str		) -> Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   tempfile.mkdtemp()
									SCREAMING_SNAKE_CASE__								=   SamImageProcessor()
									SCREAMING_SNAKE_CASE__								=   SamProcessor(__A		)
									processor.save_pretrained(self.tmpdirname		)


							def   _snake_case	(				self     :Any				,					**__A     :Union[str, Any]		) -> str:



									"""simple docstring"""



									return AutoProcessor.from_pretrained(self.tmpdirname				,					**__A		).image_processor


							def   _snake_case	(				self     :str		) -> List[str]:



									"""simple docstring"""



									shutil.rmtree(self.tmpdirname		)


							def   _snake_case	(				self     :int		) -> Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [np.random.randint(255				,					size=(3, 30, 400)				,					dtype=np.uinta		)]

									SCREAMING_SNAKE_CASE__								=   [Image.fromarray(np.moveaxis(__A				,					0				,					-1		)		) for x in image_inputs]

									return image_inputs


							def   _snake_case	(				self     :int		) -> List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=self.get_image_processor()		)
									processor.save_pretrained(self.tmpdirname		)

									SCREAMING_SNAKE_CASE__								=   self.get_image_processor(do_normalize=__A				,					padding_value=1.0		)

									SCREAMING_SNAKE_CASE__								=   SamProcessor.from_pretrained(self.tmpdirname				,					do_normalize=__A				,					padding_value=1.0		)

									self.assertEqual(processor.image_processor.to_json_string()				,					image_processor_add_kwargs.to_json_string()		)
									self.assertIsInstance(processor.image_processor				,					__A		)


							def   _snake_case	(				self     :List[Any]		) -> List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)

									SCREAMING_SNAKE_CASE__								=   self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE__								=   image_processor(__A				,					return_tensors="""np"""		)
									SCREAMING_SNAKE_CASE__								=   processor(images=__A				,					return_tensors="""np"""		)

									input_feat_extract.pop("""original_sizes"""		)  # pop original_sizes as it is popped in the processor
									input_feat_extract.pop("""reshaped_input_sizes"""		)  # pop reshaped_input_sizes as it is popped in the processor

									for key in input_feat_extract.keys():
											self.assertAlmostEqual(input_feat_extract[key].sum()				,					input_processor[key].sum()				,					delta=1E-2		)


							@require_tf
							def   _snake_case	(				self     :Dict		) -> Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)
									SCREAMING_SNAKE_CASE__								=   [tf.ones((1, 3, 5, 5)		)]

									SCREAMING_SNAKE_CASE__								=   [[1764, 2646]]

									SCREAMING_SNAKE_CASE__								=   [[683, 1024]]
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(__A				,					__A				,					__A				,					return_tensors="""tf"""		)
									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
									    __A				,					tf.convert_to_tensor(__A		)				,					tf.convert_to_tensor(__A		)				,					return_tensors="""tf"""				,					)
									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									# should also work with np
									SCREAMING_SNAKE_CASE__								=   [np.ones((1, 3, 5, 5)		)]
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
									    __A				,					np.array(__A		)				,					np.array(__A		)				,					return_tensors="""tf"""		)

									self.assertEqual(masks[0].shape				,					(1, 3, 1764, 2646)		)

									SCREAMING_SNAKE_CASE__								=   [[1, 0], [0, 1]]
									with self.assertRaises(tf.errors.InvalidArgumentError		):
											SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
											    __A				,					np.array(__A		)				,					np.array(__A		)				,					return_tensors="""tf"""		)




@require_vision
@require_torchvision
class 	UpperCamelCase_							(  unittest.TestCase     ):


							def   _snake_case	(				self     :List[str]		) -> List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   tempfile.mkdtemp()
									SCREAMING_SNAKE_CASE__								=   SamImageProcessor()
									SCREAMING_SNAKE_CASE__								=   SamProcessor(__A		)
									processor.save_pretrained(self.tmpdirname		)


							def   _snake_case	(				self     :Dict				,					**__A     :str		) -> Union[str, Any]:



									"""simple docstring"""



									return AutoProcessor.from_pretrained(self.tmpdirname				,					**__A		).image_processor


							def   _snake_case	(				self     :List[str]		) -> List[Any]:



									"""simple docstring"""



									shutil.rmtree(self.tmpdirname		)


							def   _snake_case	(				self     :Any		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [np.random.randint(255				,					size=(3, 30, 400)				,					dtype=np.uinta		)]

									SCREAMING_SNAKE_CASE__								=   [Image.fromarray(np.moveaxis(__A				,					0				,					-1		)		) for x in image_inputs]

									return image_inputs


							@is_pt_tf_cross_test
							def   _snake_case	(				self     :Union[str, Any]		) -> int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)
									SCREAMING_SNAKE_CASE__								=   np.random.randint(0				,					2				,					size=(1, 3, 5, 5)		).astype(np.floataa		)
									SCREAMING_SNAKE_CASE__								=   [tf.convert_to_tensor(__A		)]
									SCREAMING_SNAKE_CASE__								=   [torch.tensor(__A		)]

									SCREAMING_SNAKE_CASE__								=   [[1764, 2646]]

									SCREAMING_SNAKE_CASE__								=   [[683, 1024]]
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
									    __A				,					__A				,					__A				,					return_tensors="""tf"""		)
									SCREAMING_SNAKE_CASE__								=   processor.post_process_masks(
									    __A				,					__A				,					__A				,					return_tensors="""pt"""		)

									self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()		)		)


							@is_pt_tf_cross_test
							def   _snake_case	(				self     :Dict		) -> Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.get_image_processor()

									SCREAMING_SNAKE_CASE__								=   SamProcessor(image_processor=__A		)

									SCREAMING_SNAKE_CASE__								=   self.prepare_image_inputs()

									SCREAMING_SNAKE_CASE__								=   image_processor(__A				,					return_tensors="""pt"""		)["""pixel_values"""].numpy()
									SCREAMING_SNAKE_CASE__								=   processor(images=__A				,					return_tensors="""pt"""		)["""pixel_values"""].numpy()

									SCREAMING_SNAKE_CASE__								=   image_processor(__A				,					return_tensors="""tf"""		)["""pixel_values"""].numpy()
									SCREAMING_SNAKE_CASE__								=   processor(images=__A				,					return_tensors="""tf"""		)["""pixel_values"""].numpy()

									self.assertTrue(np.allclose(__A				,					__A		)		)
									self.assertTrue(np.allclose(__A				,					__A		)		)
									self.assertTrue(np.allclose(__A				,					__A		)		)






from torch import nn






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				str     ):
		if act_fn in ["swish", "silu"]:
				return nn.SiLU()
		elif act_fn == "mish":
				return nn.Mish()
		elif act_fn == "gelu":
				return nn.GELU()
		else:
				raise ValueError(f'''Unsupported activation function: {act_fn}'''     )