






'''simple docstring'''




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

	def      __UpperCamelCase							(      self   :						Optional[int]):
			UpperCamelCase__				:					List[str]					=					tempfile.mkdtemp()
			UpperCamelCase__				:					Dict					=					SamImageProcessor()
			UpperCamelCase__				:					List[Any]					=					SamProcessor(UpperCAmelCase_)
			processor.save_pretrained(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						Optional[Any]  ,					**UpperCAmelCase_   :						Any):
			return AutoProcessor.from_pretrained(self.tmpdirname  ,					**UpperCAmelCase_).image_processor

	def      __UpperCamelCase							(      self   :						str):
			shutil.rmtree(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						Union[str, Any]):
			UpperCamelCase__				:					Optional[int]					=					[np.random.randint(255  ,					size=(3, 30, 400)  ,					dtype=np.uinta)]

			UpperCamelCase__				:					int					=					[Image.fromarray(np.moveaxis(UpperCAmelCase_  ,					0  ,					-1)) for x in image_inputs]

			return image_inputs

	def      __UpperCamelCase							(      self   :						Optional[Any]):
			UpperCamelCase__				:					List[str]					=					SamProcessor(image_processor=self.get_image_processor())
			processor.save_pretrained(self.tmpdirname)

			UpperCamelCase__				:					Any					=					self.get_image_processor(do_normalize=UpperCAmelCase_  ,					padding_value=1.0)

			UpperCamelCase__				:					Optional[int]					=					SamProcessor.from_pretrained(self.tmpdirname  ,					do_normalize=UpperCAmelCase_  ,					padding_value=1.0)

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

	def      __UpperCamelCase							(      self   :						List[Any]):
			UpperCamelCase__				:					Dict					=					self.get_image_processor()

			UpperCamelCase__				:					int					=					SamProcessor(image_processor=UpperCAmelCase_)

			UpperCamelCase__				:					List[str]					=					self.prepare_image_inputs()

			UpperCamelCase__				:					Optional[Any]					=					image_processor(UpperCAmelCase_  ,					return_tensors='np')
			UpperCamelCase__				:					str					=					processor(images=UpperCAmelCase_  ,					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      __UpperCamelCase							(      self   :						Union[str, Any]):
			UpperCamelCase__				:					str					=					self.get_image_processor()

			UpperCamelCase__				:					Union[str, Any]					=					SamProcessor(image_processor=UpperCAmelCase_)
			UpperCamelCase__				:					str					=					[torch.ones((1, 3, 5, 5))]

			UpperCamelCase__				:					List[Any]					=					[[1_764, 2_646]]

			UpperCamelCase__				:					Optional[int]					=					[[683, 1_024]]
			UpperCamelCase__				:					int					=					processor.post_process_masks(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_)
			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			UpperCamelCase__				:					Tuple					=					processor.post_process_masks(
			    UpperCAmelCase_  ,					torch.tensor(UpperCAmelCase_)  ,					torch.tensor(UpperCAmelCase_))
			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			# should also work with np
			UpperCamelCase__				:					Union[str, Any]					=					[np.ones((1, 3, 5, 5))]
			UpperCamelCase__				:					Optional[int]					=					processor.post_process_masks(UpperCAmelCase_  ,					np.array(UpperCAmelCase_)  ,					np.array(UpperCAmelCase_))

			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			UpperCamelCase__				:					Union[str, Any]					=					[[1, 0], [0, 1]]
			with self.assertRaises(UpperCAmelCase_):
					UpperCamelCase__				:					List[Any]					=					processor.post_process_masks(UpperCAmelCase_  ,					np.array(UpperCAmelCase_)  ,					np.array(UpperCAmelCase_))







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

	def      __UpperCamelCase							(      self   :						Optional[Any]):
			UpperCamelCase__				:					List[str]					=					tempfile.mkdtemp()
			UpperCamelCase__				:					Tuple					=					SamImageProcessor()
			UpperCamelCase__				:					List[Any]					=					SamProcessor(UpperCAmelCase_)
			processor.save_pretrained(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						Optional[Any]  ,					**UpperCAmelCase_   :						Union[str, Any]):
			return AutoProcessor.from_pretrained(self.tmpdirname  ,					**UpperCAmelCase_).image_processor

	def      __UpperCamelCase							(      self   :						str):
			shutil.rmtree(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						int):
			UpperCamelCase__				:					Union[str, Any]					=					[np.random.randint(255  ,					size=(3, 30, 400)  ,					dtype=np.uinta)]

			UpperCamelCase__				:					Optional[Any]					=					[Image.fromarray(np.moveaxis(UpperCAmelCase_  ,					0  ,					-1)) for x in image_inputs]

			return image_inputs

	def      __UpperCamelCase							(      self   :						List[Any]):
			UpperCamelCase__				:					Tuple					=					SamProcessor(image_processor=self.get_image_processor())
			processor.save_pretrained(self.tmpdirname)

			UpperCamelCase__				:					List[str]					=					self.get_image_processor(do_normalize=UpperCAmelCase_  ,					padding_value=1.0)

			UpperCamelCase__				:					Union[str, Any]					=					SamProcessor.from_pretrained(self.tmpdirname  ,					do_normalize=UpperCAmelCase_  ,					padding_value=1.0)

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

	def      __UpperCamelCase							(      self   :						Union[str, Any]):
			UpperCamelCase__				:					Dict					=					self.get_image_processor()

			UpperCamelCase__				:					Optional[Any]					=					SamProcessor(image_processor=UpperCAmelCase_)

			UpperCamelCase__				:					Optional[int]					=					self.prepare_image_inputs()

			UpperCamelCase__				:					Optional[Any]					=					image_processor(UpperCAmelCase_  ,					return_tensors='np')
			UpperCamelCase__				:					Optional[Any]					=					processor(images=UpperCAmelCase_  ,					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      __UpperCamelCase							(      self   :						List[str]):
			UpperCamelCase__				:					Tuple					=					self.get_image_processor()

			UpperCamelCase__				:					Optional[int]					=					SamProcessor(image_processor=UpperCAmelCase_)
			UpperCamelCase__				:					Optional[Any]					=					[tf.ones((1, 3, 5, 5))]

			UpperCamelCase__				:					Optional[int]					=					[[1_764, 2_646]]

			UpperCamelCase__				:					Optional[int]					=					[[683, 1_024]]
			UpperCamelCase__				:					Optional[Any]					=					processor.post_process_masks(UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					return_tensors='tf')
			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			UpperCamelCase__				:					int					=					processor.post_process_masks(
			    UpperCAmelCase_  ,					tf.convert_to_tensor(UpperCAmelCase_)  ,					tf.convert_to_tensor(UpperCAmelCase_)  ,					return_tensors='tf'  ,					)
			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			# should also work with np
			UpperCamelCase__				:					List[Any]					=					[np.ones((1, 3, 5, 5))]
			UpperCamelCase__				:					int					=					processor.post_process_masks(
			    UpperCAmelCase_  ,					np.array(UpperCAmelCase_)  ,					np.array(UpperCAmelCase_)  ,					return_tensors='tf')

			self.assertEqual(masks[0].shape  ,					(1, 3, 1_764, 2_646))

			UpperCamelCase__				:					Optional[Any]					=					[[1, 0], [0, 1]]
			with self.assertRaises(tf.errors.InvalidArgumentError):
					UpperCamelCase__				:					List[Any]					=					processor.post_process_masks(
					    UpperCAmelCase_  ,					np.array(UpperCAmelCase_)  ,					np.array(UpperCAmelCase_)  ,					return_tensors='tf')







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

	def      __UpperCamelCase							(      self   :						int):
			UpperCamelCase__				:					str					=					tempfile.mkdtemp()
			UpperCamelCase__				:					Union[str, Any]					=					SamImageProcessor()
			UpperCamelCase__				:					List[Any]					=					SamProcessor(UpperCAmelCase_)
			processor.save_pretrained(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						List[str]  ,					**UpperCAmelCase_   :						List[str]):
			return AutoProcessor.from_pretrained(self.tmpdirname  ,					**UpperCAmelCase_).image_processor

	def      __UpperCamelCase							(      self   :						Dict):
			shutil.rmtree(self.tmpdirname)

	def      __UpperCamelCase							(      self   :						Optional[int]):
			UpperCamelCase__				:					Tuple					=					[np.random.randint(255  ,					size=(3, 30, 400)  ,					dtype=np.uinta)]

			UpperCamelCase__				:					int					=					[Image.fromarray(np.moveaxis(UpperCAmelCase_  ,					0  ,					-1)) for x in image_inputs]

			return image_inputs

	@is_pt_tf_cross_test
	def      __UpperCamelCase							(      self   :						List[Any]):
			UpperCamelCase__				:					Any					=					self.get_image_processor()

			UpperCamelCase__				:					int					=					SamProcessor(image_processor=UpperCAmelCase_)
			UpperCamelCase__				:					str					=					np.random.randint(0  ,					2  ,					size=(1, 3, 5, 5)).astype(np.floataa)
			UpperCamelCase__				:					List[Any]					=					[tf.convert_to_tensor(UpperCAmelCase_)]
			UpperCamelCase__				:					str					=					[torch.tensor(UpperCAmelCase_)]

			UpperCamelCase__				:					List[Any]					=					[[1_764, 2_646]]

			UpperCamelCase__				:					List[Any]					=					[[683, 1_024]]
			UpperCamelCase__				:					Any					=					processor.post_process_masks(
			    UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					return_tensors='tf')
			UpperCamelCase__				:					Tuple					=					processor.post_process_masks(
			    UpperCAmelCase_  ,					UpperCAmelCase_  ,					UpperCAmelCase_  ,					return_tensors='pt')

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





	@is_pt_tf_cross_test
	def      __UpperCamelCase							(      self   :						Tuple):
			UpperCamelCase__				:					Any					=					self.get_image_processor()

			UpperCamelCase__				:					Any					=					SamProcessor(image_processor=UpperCAmelCase_)

			UpperCamelCase__				:					List[Any]					=					self.prepare_image_inputs()

			UpperCamelCase__				:					int					=					image_processor(UpperCAmelCase_  ,					return_tensors='pt')['pixel_values'].numpy()
			UpperCamelCase__				:					Tuple					=					processor(images=UpperCAmelCase_  ,					return_tensors='pt')['pixel_values'].numpy()

			UpperCamelCase__				:					Union[str, Any]					=					image_processor(UpperCAmelCase_  ,					return_tensors='tf')['pixel_values'].numpy()
			UpperCamelCase__				:					Dict					=					processor(images=UpperCAmelCase_  ,					return_tensors='tf')['pixel_values'].numpy()

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












'''simple docstring'''




from ..utils import DummyObject, requires_backends







class       __lowercase					(metaclass=__lowerCamelCase    ):
	_lowerCamelCase              =  ['''torch''', '''scipy''']

	def __init__(      self   :						List[Any]  ,					*UpperCAmelCase_   :						Any  ,					**UpperCAmelCase_   :						int):
			requires_backends(self  ,					['torch', 'scipy'])

	@classmethod
	def      __UpperCamelCase							(      cls   :						Union[str, Any]  ,					*UpperCAmelCase_   :						Any  ,					**UpperCAmelCase_   :						List[Any]):
			requires_backends(cls  ,					['torch', 'scipy'])





	@classmethod
	def      __UpperCamelCase							(      cls   :						Union[str, Any]  ,					*UpperCAmelCase_   :						Optional[Any]  ,					**UpperCAmelCase_   :						Any):
			requires_backends(cls  ,					['torch', 'scipy'])




