


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




				def   a__(	self    :				List[Any]   )->  int:

										"""simple docstring"""




										UpperCAmelCase         =			tempfile.mkdtemp()
										UpperCAmelCase         =			SamImageProcessor()
										UpperCAmelCase         =			SamProcessor(lowerCAmelCase   )
										processor.save_pretrained(self.tmpdirname   )




				def   a__(	self    :				List[Any]   ,					**lowerCAmelCase    :				int   )->  Tuple:

										"""simple docstring"""




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




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

										"""simple docstring"""




										shutil.rmtree(self.tmpdirname   )




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

										"""simple docstring"""




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

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

										return image_inputs




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

										"""simple docstring"""




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

										UpperCAmelCase         =			self.get_image_processor(do_normalize=lowerCAmelCase   ,					padding_value=1.0   )

										UpperCAmelCase         =			SamProcessor.from_pretrained(self.tmpdirname   ,					do_normalize=lowerCAmelCase   ,					padding_value=1.0   )

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




				def   a__(	self    :				str   )->  str:

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

										UpperCAmelCase         =			SamProcessor(image_processor=lowerCAmelCase   )

										UpperCAmelCase         =			self.prepare_image_inputs()

										UpperCAmelCase         =			image_processor(lowerCAmelCase   ,					return_tensors='''np'''   )
										UpperCAmelCase         =			processor(images=lowerCAmelCase   ,					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   a__(	self    :				List[Any]   )->  List[str]:

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

										UpperCAmelCase         =			SamProcessor(image_processor=lowerCAmelCase   )
										UpperCAmelCase         =			[torch.ones((1, 3, 5, 5)   )]

										UpperCAmelCase         =			[[1764, 2646]]

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

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

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

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

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







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




				def   a__(	self    :				int   )->  str:

										"""simple docstring"""




										UpperCAmelCase         =			tempfile.mkdtemp()
										UpperCAmelCase         =			SamImageProcessor()
										UpperCAmelCase         =			SamProcessor(lowerCAmelCase   )
										processor.save_pretrained(self.tmpdirname   )




				def   a__(	self    :				Any   ,					**lowerCAmelCase    :				Union[str, Any]   )->  Tuple:

										"""simple docstring"""




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




				def   a__(	self    :				Optional[int]   )->  Optional[int]:

										"""simple docstring"""




										shutil.rmtree(self.tmpdirname   )




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

										"""simple docstring"""




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

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

										return image_inputs




				def   a__(	self    :				Tuple   )->  Optional[Any]:

										"""simple docstring"""




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

										UpperCAmelCase         =			self.get_image_processor(do_normalize=lowerCAmelCase   ,					padding_value=1.0   )

										UpperCAmelCase         =			SamProcessor.from_pretrained(self.tmpdirname   ,					do_normalize=lowerCAmelCase   ,					padding_value=1.0   )

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




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

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

										UpperCAmelCase         =			SamProcessor(image_processor=lowerCAmelCase   )

										UpperCAmelCase         =			self.prepare_image_inputs()

										UpperCAmelCase         =			image_processor(lowerCAmelCase   ,					return_tensors='''np'''   )
										UpperCAmelCase         =			processor(images=lowerCAmelCase   ,					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   a__(	self    :				Union[str, Any]   )->  Dict:

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

										UpperCAmelCase         =			SamProcessor(image_processor=lowerCAmelCase   )
										UpperCAmelCase         =			[tf.ones((1, 3, 5, 5)   )]

										UpperCAmelCase         =			[[1764, 2646]]

										UpperCAmelCase         =			[[683, 1024]]
										UpperCAmelCase         =			processor.post_process_masks(lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					return_tensors='''tf'''   )
										self.assertEqual(masks[0].shape   ,					(1, 3, 1764, 2646)   )

										UpperCAmelCase         =			processor.post_process_masks(
										    lowerCAmelCase   ,					tf.convert_to_tensor(lowerCAmelCase   )   ,					tf.convert_to_tensor(lowerCAmelCase   )   ,					return_tensors='''tf'''   ,					)
										self.assertEqual(masks[0].shape   ,					(1, 3, 1764, 2646)   )

										# should also work with np
										UpperCAmelCase         =			[np.ones((1, 3, 5, 5)   )]
										UpperCAmelCase         =			processor.post_process_masks(
										    lowerCAmelCase   ,					np.array(lowerCAmelCase   )   ,					np.array(lowerCAmelCase   )   ,					return_tensors='''tf'''   )

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

										UpperCAmelCase         =			[[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError   ):
																UpperCAmelCase         =			processor.post_process_masks(
																    lowerCAmelCase   ,					np.array(lowerCAmelCase   )   ,					np.array(lowerCAmelCase   )   ,					return_tensors='''tf'''   )







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




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

										"""simple docstring"""




										UpperCAmelCase         =			tempfile.mkdtemp()
										UpperCAmelCase         =			SamImageProcessor()
										UpperCAmelCase         =			SamProcessor(lowerCAmelCase   )
										processor.save_pretrained(self.tmpdirname   )




				def   a__(	self    :				str   ,					**lowerCAmelCase    :				Optional[int]   )->  int:

										"""simple docstring"""




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




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

										"""simple docstring"""




										shutil.rmtree(self.tmpdirname   )




				def   a__(	self    :				int   )->  Any:

										"""simple docstring"""




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

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

										return image_inputs




				@is_pt_tf_cross_test
				def   a__(	self    :				int   )->  Tuple:

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

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

										UpperCAmelCase         =			[[1764, 2646]]

										UpperCAmelCase         =			[[683, 1024]]
										UpperCAmelCase         =			processor.post_process_masks(
										    lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					return_tensors='''tf'''   )
										UpperCAmelCase         =			processor.post_process_masks(
										    lowerCAmelCase   ,					lowerCAmelCase   ,					lowerCAmelCase   ,					return_tensors='''pt'''   )

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





				@is_pt_tf_cross_test
				def   a__(	self    :				Dict   )->  List[Any]:

										"""simple docstring"""




										UpperCAmelCase         =			self.get_image_processor()

										UpperCAmelCase         =			SamProcessor(image_processor=lowerCAmelCase   )

										UpperCAmelCase         =			self.prepare_image_inputs()

										UpperCAmelCase         =			image_processor(lowerCAmelCase   ,					return_tensors='''pt'''   )['''pixel_values'''].numpy()
										UpperCAmelCase         =			processor(images=lowerCAmelCase   ,					return_tensors='''pt'''   )['''pixel_values'''].numpy()

										UpperCAmelCase         =			image_processor(lowerCAmelCase   ,					return_tensors='''tf'''   )['''pixel_values'''].numpy()
										UpperCAmelCase         =			processor(images=lowerCAmelCase   ,					return_tensors='''tf'''   )['''pixel_values'''].numpy()

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









'''simple docstring'''



from ..utils import DummyObject, requires_backends






class  UpperCamelCase__(     metaclass=lowerCAmelCase ):
				__magic_name__       :   List[str]							= ["note_seq"]




				def __init__(	self    :				Any   ,					*lowerCAmelCase    :				List[str]   ,					**lowerCAmelCase    :				int   )->  Optional[int]:

										"""simple docstring"""




										requires_backends(self   ,					['''note_seq''']   )




				@classmethod
				def   a__(	cls    :				Dict   ,					*lowerCAmelCase    :				int   ,					**lowerCAmelCase    :				Optional[int]   )->  Dict:

										"""simple docstring"""




										requires_backends(cls   ,					['''note_seq''']   )





				@classmethod
				def   a__(	cls    :				int   ,					*lowerCAmelCase    :				Tuple   ,					**lowerCAmelCase    :				Optional[Any]   )->  Union[str, Any]:

										"""simple docstring"""




										requires_backends(cls   ,					['''note_seq''']   )





