






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


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				Optional[Any]:
								snake_case_				:							Optional[Any]						 =       tempfile.mkdtemp()
								snake_case_				:							Tuple						 =       SamImageProcessor()
								snake_case_				:							Dict						 =       SamProcessor(_SCREAMING_SNAKE_CASE				)
								processor.save_pretrained(self.tmpdirname				)






						def       _lowerCAmelCase       (	self		,				**_SCREAMING_SNAKE_CASE				) ->				str:
								return AutoProcessor.from_pretrained(self.tmpdirname		,				**_SCREAMING_SNAKE_CASE				).image_processor






						def       _lowerCAmelCase       (	self				) ->				int:
								shutil.rmtree(self.tmpdirname				)






						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								snake_case_				:							Any						 =       [np.random.randint(255		,				size=(3, 30, 400)		,				dtype=np.uinta				)]

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

								return image_inputs






						def       _lowerCAmelCase       (	self				) ->				str:
								snake_case_				:							Union[str, Any]						 =       SamProcessor(image_processor=self.get_image_processor()				)
								processor.save_pretrained(self.tmpdirname				)

								snake_case_				:							Optional[int]						 =       self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE		,				padding_value=1.0				)

								snake_case_				:							str						 =       SamProcessor.from_pretrained(self.tmpdirname		,				do_normalize=_SCREAMING_SNAKE_CASE		,				padding_value=1.0				)

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






						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								snake_case_				:							Union[str, Any]						 =       self.get_image_processor()

								snake_case_				:							Optional[int]						 =       SamProcessor(image_processor=_SCREAMING_SNAKE_CASE				)

								snake_case_				:							Dict						 =       self.prepare_image_inputs()

								snake_case_				:							Union[str, Any]						 =       image_processor(_SCREAMING_SNAKE_CASE		,				return_tensors="np"				)
								snake_case_				:							Dict						 =       processor(images=_SCREAMING_SNAKE_CASE		,				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       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							List[Any]						 =       self.get_image_processor()

								snake_case_				:							Any						 =       SamProcessor(image_processor=_SCREAMING_SNAKE_CASE				)
								snake_case_				:							Dict						 =       [torch.ones((1, 3, 5, 5)				)]

								snake_case_				:							str						 =       [[1764, 2646]]

								snake_case_				:							int						 =       [[683, 1024]]
								snake_case_				:							Union[str, Any]						 =       processor.post_process_masks(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)
								self.assertEqual(masks[0].shape		,				(1, 3, 1764, 2646)				)

								snake_case_				:							int						 =       processor.post_process_masks(
								    _SCREAMING_SNAKE_CASE		,				torch.tensor(_SCREAMING_SNAKE_CASE				)		,				torch.tensor(_SCREAMING_SNAKE_CASE				)				)
								self.assertEqual(masks[0].shape		,				(1, 3, 1764, 2646)				)

								# should also work with np
								snake_case_				:							str						 =       [np.ones((1, 3, 5, 5)				)]
								snake_case_				:							str						 =       processor.post_process_masks(_SCREAMING_SNAKE_CASE		,				np.array(_SCREAMING_SNAKE_CASE				)		,				np.array(_SCREAMING_SNAKE_CASE				)				)

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

								snake_case_				:							int						 =       [[1, 0], [0, 1]]
								with self.assertRaises(_SCREAMING_SNAKE_CASE				):
										snake_case_				:							Union[str, Any]						 =       processor.post_process_masks(_SCREAMING_SNAKE_CASE		,				np.array(_SCREAMING_SNAKE_CASE				)		,				np.array(_SCREAMING_SNAKE_CASE				)				)




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


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				List[str]:
								snake_case_				:							Optional[int]						 =       tempfile.mkdtemp()
								snake_case_				:							Optional[int]						 =       SamImageProcessor()
								snake_case_				:							List[str]						 =       SamProcessor(_SCREAMING_SNAKE_CASE				)
								processor.save_pretrained(self.tmpdirname				)






						def       _lowerCAmelCase       (	self		,				**_SCREAMING_SNAKE_CASE				) ->				str:
								return AutoProcessor.from_pretrained(self.tmpdirname		,				**_SCREAMING_SNAKE_CASE				).image_processor






						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								shutil.rmtree(self.tmpdirname				)






						def       _lowerCAmelCase       (	self				) ->				str:
								snake_case_				:							Union[str, Any]						 =       [np.random.randint(255		,				size=(3, 30, 400)		,				dtype=np.uinta				)]

								snake_case_				:							List[Any]						 =       [Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE		,				0		,				-1				)				) for x in image_inputs]

								return image_inputs






						def       _lowerCAmelCase       (	self				) ->				Optional[Any]:
								snake_case_				:							Tuple						 =       SamProcessor(image_processor=self.get_image_processor()				)
								processor.save_pretrained(self.tmpdirname				)

								snake_case_				:							Optional[int]						 =       self.get_image_processor(do_normalize=_SCREAMING_SNAKE_CASE		,				padding_value=1.0				)

								snake_case_				:							Dict						 =       SamProcessor.from_pretrained(self.tmpdirname		,				do_normalize=_SCREAMING_SNAKE_CASE		,				padding_value=1.0				)

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






						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								snake_case_				:							List[Any]						 =       self.get_image_processor()

								snake_case_				:							Optional[int]						 =       SamProcessor(image_processor=_SCREAMING_SNAKE_CASE				)

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

								snake_case_				:							List[Any]						 =       image_processor(_SCREAMING_SNAKE_CASE		,				return_tensors="np"				)
								snake_case_				:							Any						 =       processor(images=_SCREAMING_SNAKE_CASE		,				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       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							Dict						 =       self.get_image_processor()

								snake_case_				:							int						 =       SamProcessor(image_processor=_SCREAMING_SNAKE_CASE				)
								snake_case_				:							List[str]						 =       [tf.ones((1, 3, 5, 5)				)]

								snake_case_				:							int						 =       [[1764, 2646]]

								snake_case_				:							Optional[Any]						 =       [[683, 1024]]
								snake_case_				:							Any						 =       processor.post_process_masks(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				return_tensors="tf"				)
								self.assertEqual(masks[0].shape		,				(1, 3, 1764, 2646)				)

								snake_case_				:							List[Any]						 =       processor.post_process_masks(
								    _SCREAMING_SNAKE_CASE		,				tf.convert_to_tensor(_SCREAMING_SNAKE_CASE				)		,				tf.convert_to_tensor(_SCREAMING_SNAKE_CASE				)		,				return_tensors="tf"		,				)
								self.assertEqual(masks[0].shape		,				(1, 3, 1764, 2646)				)

								# should also work with np
								snake_case_				:							Tuple						 =       [np.ones((1, 3, 5, 5)				)]
								snake_case_				:							Optional[Any]						 =       processor.post_process_masks(
								    _SCREAMING_SNAKE_CASE		,				np.array(_SCREAMING_SNAKE_CASE				)		,				np.array(_SCREAMING_SNAKE_CASE				)		,				return_tensors="tf"				)

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

								snake_case_				:							Optional[int]						 =       [[1, 0], [0, 1]]
								with self.assertRaises(tf.errors.InvalidArgumentError				):
										snake_case_				:							List[str]						 =       processor.post_process_masks(
										    _SCREAMING_SNAKE_CASE		,				np.array(_SCREAMING_SNAKE_CASE				)		,				np.array(_SCREAMING_SNAKE_CASE				)		,				return_tensors="tf"				)




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


						'''simple docstring'''




						def       _lowerCAmelCase       (	self				) ->				Any:
								snake_case_				:							str						 =       tempfile.mkdtemp()
								snake_case_				:							Any						 =       SamImageProcessor()
								snake_case_				:							Any						 =       SamProcessor(_SCREAMING_SNAKE_CASE				)
								processor.save_pretrained(self.tmpdirname				)






						def       _lowerCAmelCase       (	self		,				**_SCREAMING_SNAKE_CASE				) ->				List[Any]:
								return AutoProcessor.from_pretrained(self.tmpdirname		,				**_SCREAMING_SNAKE_CASE				).image_processor






						def       _lowerCAmelCase       (	self				) ->				int:
								shutil.rmtree(self.tmpdirname				)






						def       _lowerCAmelCase       (	self				) ->				List[Any]:
								snake_case_				:							Dict						 =       [np.random.randint(255		,				size=(3, 30, 400)		,				dtype=np.uinta				)]

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

								return image_inputs






						@is_pt_tf_cross_test
						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							int						 =       self.get_image_processor()

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

								snake_case_				:							Optional[Any]						 =       [[1764, 2646]]

								snake_case_				:							Dict						 =       [[683, 1024]]
								snake_case_				:							Any						 =       processor.post_process_masks(
								    _SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				return_tensors="tf"				)
								snake_case_				:							str						 =       processor.post_process_masks(
								    _SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				return_tensors="pt"				)

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






						@is_pt_tf_cross_test
						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							Union[str, Any]						 =       self.get_image_processor()

								snake_case_				:							Optional[Any]						 =       SamProcessor(image_processor=_SCREAMING_SNAKE_CASE				)

								snake_case_				:							Tuple						 =       self.prepare_image_inputs()

								snake_case_				:							Any						 =       image_processor(_SCREAMING_SNAKE_CASE		,				return_tensors="pt"				)["pixel_values"].numpy()
								snake_case_				:							Union[str, Any]						 =       processor(images=_SCREAMING_SNAKE_CASE		,				return_tensors="pt"				)["pixel_values"].numpy()

								snake_case_				:							int						 =       image_processor(_SCREAMING_SNAKE_CASE		,				return_tensors="tf"				)["pixel_values"].numpy()
								snake_case_				:							Union[str, Any]						 =       processor(images=_SCREAMING_SNAKE_CASE		,				return_tensors="tf"				)["pixel_values"].numpy()

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








import fire

from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer

def      lowerCAmelCase__	(    _a       :			str  ,      _a       :			str  ,      **_a       :			Optional[int]    ):
		snake_case_				:							Any						 =       AutoConfig.from_pretrained(_a  ,      **_a    )
		snake_case_				:							str						 =       AutoModelForSeqaSeqLM.from_config(_a    )
		model.save_pretrained(_a    )
		AutoTokenizer.from_pretrained(_a    ).save_pretrained(_a    )
		return model


if __name__ == "__main__":
							fire.Fire(save_randomly_initialized_version)
