





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     ):



						"""simple docstring"""

						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   tempfile.mkdtemp()
										_snake_case :			Any  						=   SamImageProcessor()
										_snake_case :			int  						=   SamProcessor(lowerCamelCase_					)
										processor.save_pretrained(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		Any ,				**lowerCamelCase_			:		Optional[int]					):





										'''simple docstring'''







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







						def 		__UpperCAmelCase       (    self			:		str					):





										'''simple docstring'''







										shutil.rmtree(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		Tuple					):





										'''simple docstring'''







										_snake_case :			Optional[int]  						=   [np.random.randint(2_55 ,				size=(3, 30, 4_00) ,				dtype=np.uinta					)]

										_snake_case :			Dict  						=   [Image.fromarray(np.moveaxis(lowerCamelCase_ ,				0 ,				-1					)					) for x in image_inputs]

										return image_inputs







						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										_snake_case :			int  						=   SamProcessor(image_processor=self.get_image_processor()					)
										processor.save_pretrained(self.tmpdirname					)

										_snake_case :			Tuple  						=   self.get_image_processor(do_normalize=lowerCamelCase_ ,				padding_value=1.0					)

										_snake_case :			Optional[int]  						=   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 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   self.get_image_processor()

										_snake_case :			List[Any]  						=   SamProcessor(image_processor=lowerCamelCase_					)

										_snake_case :			Tuple  						=   self.prepare_image_inputs()

										_snake_case :			int  						=   image_processor(lowerCamelCase_ ,				return_tensors='np'					)
										_snake_case :			Dict  						=   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 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										_snake_case :			Any  						=   self.get_image_processor()

										_snake_case :			Optional[int]  						=   SamProcessor(image_processor=lowerCamelCase_					)
										_snake_case :			Dict  						=   [torch.ones((1, 3, 5, 5)					)]

										_snake_case :			Dict  						=   [[17_64, 26_46]]

										_snake_case :			Dict  						=   [[6_83, 10_24]]
										_snake_case :			Tuple  						=   processor.post_process_masks(lowerCamelCase_ ,				lowerCamelCase_ ,				lowerCamelCase_					)
										self.assertEqual(masks[0].shape ,				(1, 3, 17_64, 26_46)					)

										_snake_case :			Any  						=   processor.post_process_masks(
										    lowerCamelCase_ ,				torch.tensor(lowerCamelCase_					) ,				torch.tensor(lowerCamelCase_					)					)
										self.assertEqual(masks[0].shape ,				(1, 3, 17_64, 26_46)					)

										# should also work with np
										_snake_case :			Tuple  						=   [np.ones((1, 3, 5, 5)					)]
										_snake_case :			Union[str, Any]  						=   processor.post_process_masks(lowerCamelCase_ ,				np.array(lowerCamelCase_					) ,				np.array(lowerCamelCase_					)					)

										self.assertEqual(masks[0].shape ,				(1, 3, 17_64, 26_46)					)

										_snake_case :			Optional[int]  						=   [[1, 0], [0, 1]]
										with self.assertRaises(lowerCamelCase_					):
														_snake_case :			List[Any]  						=   processor.post_process_masks(lowerCamelCase_ ,				np.array(lowerCamelCase_					) ,				np.array(lowerCamelCase_					)					)



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



						"""simple docstring"""

						def 		__UpperCAmelCase       (    self			:		str					):





										'''simple docstring'''







										_snake_case :			Any  						=   tempfile.mkdtemp()
										_snake_case :			Tuple  						=   SamImageProcessor()
										_snake_case :			int  						=   SamProcessor(lowerCamelCase_					)
										processor.save_pretrained(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		str ,				**lowerCamelCase_			:		Optional[int]					):





										'''simple docstring'''







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







						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										shutil.rmtree(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			Dict  						=   [np.random.randint(2_55 ,				size=(3, 30, 4_00) ,				dtype=np.uinta					)]

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

										return image_inputs







						def 		__UpperCAmelCase       (    self			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			int  						=   SamProcessor(image_processor=self.get_image_processor()					)
										processor.save_pretrained(self.tmpdirname					)

										_snake_case :			Any  						=   self.get_image_processor(do_normalize=lowerCamelCase_ ,				padding_value=1.0					)

										_snake_case :			Tuple  						=   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 		__UpperCAmelCase       (    self			:		Any					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   self.get_image_processor()

										_snake_case :			Dict  						=   SamProcessor(image_processor=lowerCamelCase_					)

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

										_snake_case :			List[str]  						=   image_processor(lowerCamelCase_ ,				return_tensors='np'					)
										_snake_case :			Tuple  						=   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 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										_snake_case :			List[Any]  						=   self.get_image_processor()

										_snake_case :			Tuple  						=   SamProcessor(image_processor=lowerCamelCase_					)
										_snake_case :			List[str]  						=   [tf.ones((1, 3, 5, 5)					)]

										_snake_case :			Tuple  						=   [[17_64, 26_46]]

										_snake_case :			Tuple  						=   [[6_83, 10_24]]
										_snake_case :			Tuple  						=   processor.post_process_masks(lowerCamelCase_ ,				lowerCamelCase_ ,				lowerCamelCase_ ,				return_tensors='tf'					)
										self.assertEqual(masks[0].shape ,				(1, 3, 17_64, 26_46)					)

										_snake_case :			Any  						=   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, 17_64, 26_46)					)

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

										self.assertEqual(masks[0].shape ,				(1, 3, 17_64, 26_46)					)

										_snake_case :			int  						=   [[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError					):
														_snake_case :			Tuple  						=   processor.post_process_masks(
														    lowerCamelCase_ ,				np.array(lowerCamelCase_					) ,				np.array(lowerCamelCase_					) ,				return_tensors='tf'					)



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



						"""simple docstring"""

						def 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										_snake_case :			int  						=   tempfile.mkdtemp()
										_snake_case :			List[str]  						=   SamImageProcessor()
										_snake_case :			Union[str, Any]  						=   SamProcessor(lowerCamelCase_					)
										processor.save_pretrained(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		Dict ,				**lowerCamelCase_			:		str					):





										'''simple docstring'''







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







						def 		__UpperCAmelCase       (    self			:		int					):





										'''simple docstring'''







										shutil.rmtree(self.tmpdirname					)







						def 		__UpperCAmelCase       (    self			:		int					):





										'''simple docstring'''







										_snake_case :			List[str]  						=   [np.random.randint(2_55 ,				size=(3, 30, 4_00) ,				dtype=np.uinta					)]

										_snake_case :			List[str]  						=   [Image.fromarray(np.moveaxis(lowerCamelCase_ ,				0 ,				-1					)					) for x in image_inputs]

										return image_inputs







						@is_pt_tf_cross_test
						def 		__UpperCAmelCase       (    self			:		Optional[Any]					):





										'''simple docstring'''







										_snake_case :			Optional[int]  						=   self.get_image_processor()

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

										_snake_case :			str  						=   [[17_64, 26_46]]

										_snake_case :			Optional[Any]  						=   [[6_83, 10_24]]
										_snake_case :			List[str]  						=   processor.post_process_masks(
										    lowerCamelCase_ ,				lowerCamelCase_ ,				lowerCamelCase_ ,				return_tensors='tf'					)
										_snake_case :			Tuple  						=   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 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										_snake_case :			str  						=   self.get_image_processor()

										_snake_case :			Any  						=   SamProcessor(image_processor=lowerCamelCase_					)

										_snake_case :			Union[str, Any]  						=   self.prepare_image_inputs()

										_snake_case :			str  						=   image_processor(lowerCamelCase_ ,				return_tensors='pt'					)['pixel_values'].numpy()
										_snake_case :			Union[str, Any]  						=   processor(images=lowerCamelCase_ ,				return_tensors='pt'					)['pixel_values'].numpy()

										_snake_case :			str  						=   image_processor(lowerCamelCase_ ,				return_tensors='tf'					)['pixel_values'].numpy()
										_snake_case :			str  						=   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_					)					)













def       A__(		__lowerCAmelCase ):
				assert column_title.isupper()
				_snake_case :			List[Any]  						=   0
				_snake_case :			List[str]  						=   len(__lowerCAmelCase ) - 1
				_snake_case :			Dict  						=   0

				while index >= 0:
								_snake_case :			List[str]  						=   (ord(column_title[index] ) - 64) * pow(26	,			__lowerCAmelCase )
								answer += value
								power += 1
								index -= 1

				return answer


if __name__ == "__main__":
		from doctest import testmod

		testmod()






