
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 							UpperCamelCase_    (			self   :					Optional[Any]					):
					__A       =					tempfile.mkdtemp()
					__A       =					SamImageProcessor()
					__A       =					SamProcessor(A					)
					processor.save_pretrained(self.tmpdirname					)






	def 							UpperCamelCase_    (			self   :					Any				,**A   :					Optional[int]					):
					return AutoProcessor.from_pretrained(self.tmpdirname				,**A					).image_processor






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






	def 							UpperCamelCase_    (			self   :					Any					):
					__A       =					[np.random.randint(2_55				,size=(3, 30, 4_00)				,dtype=np.uinta					)]

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

					return image_inputs






	def 							UpperCamelCase_    (			self   :					Any					):
					__A       =					SamProcessor(image_processor=self.get_image_processor()					)
					processor.save_pretrained(self.tmpdirname					)

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

					__A       =					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 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.get_image_processor()

					__A       =					SamProcessor(image_processor=A					)

					__A       =					self.prepare_image_inputs()

					__A       =					image_processor(A				,return_tensors="np"					)
					__A       =					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 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.get_image_processor()

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

					__A       =					[[17_64, 26_46]]

					__A       =					[[6_83, 10_24]]
					__A       =					processor.post_process_masks(A				,A				,A					)
					self.assertEqual(masks[0].shape				,(1, 3, 17_64, 26_46)					)

					__A       =					processor.post_process_masks(
					    A				,torch.tensor(A					)				,torch.tensor(A					)					)
					self.assertEqual(masks[0].shape				,(1, 3, 17_64, 26_46)					)

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

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

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





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




	'''simple docstring'''





	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					tempfile.mkdtemp()
					__A       =					SamImageProcessor()
					__A       =					SamProcessor(A					)
					processor.save_pretrained(self.tmpdirname					)






	def 							UpperCamelCase_    (			self   :					Tuple				,**A   :					int					):
					return AutoProcessor.from_pretrained(self.tmpdirname				,**A					).image_processor






	def 							UpperCamelCase_    (			self   :					Tuple					):
					shutil.rmtree(self.tmpdirname					)






	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					__A       =					[np.random.randint(2_55				,size=(3, 30, 4_00)				,dtype=np.uinta					)]

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

					return image_inputs






	def 							UpperCamelCase_    (			self   :					int					):
					__A       =					SamProcessor(image_processor=self.get_image_processor()					)
					processor.save_pretrained(self.tmpdirname					)

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

					__A       =					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 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.get_image_processor()

					__A       =					SamProcessor(image_processor=A					)

					__A       =					self.prepare_image_inputs()

					__A       =					image_processor(A				,return_tensors="np"					)
					__A       =					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 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.get_image_processor()

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

					__A       =					[[17_64, 26_46]]

					__A       =					[[6_83, 10_24]]
					__A       =					processor.post_process_masks(A				,A				,A				,return_tensors="tf"					)
					self.assertEqual(masks[0].shape				,(1, 3, 17_64, 26_46)					)

					__A       =					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, 17_64, 26_46)					)

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

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

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





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




	'''simple docstring'''





	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					tempfile.mkdtemp()
					__A       =					SamImageProcessor()
					__A       =					SamProcessor(A					)
					processor.save_pretrained(self.tmpdirname					)






	def 							UpperCamelCase_    (			self   :					str				,**A   :					str					):
					return AutoProcessor.from_pretrained(self.tmpdirname				,**A					).image_processor






	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					shutil.rmtree(self.tmpdirname					)






	def 							UpperCamelCase_    (			self   :					Union[str, Any]					):
					__A       =					[np.random.randint(2_55				,size=(3, 30, 4_00)				,dtype=np.uinta					)]

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

					return image_inputs






	@is_pt_tf_cross_test
	def 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.get_image_processor()

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

					__A       =					[[17_64, 26_46]]

					__A       =					[[6_83, 10_24]]
					__A       =					processor.post_process_masks(
					    A				,A				,A				,return_tensors="tf"					)
					__A       =					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 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					self.get_image_processor()

					__A       =					SamProcessor(image_processor=A					)

					__A       =					self.prepare_image_inputs()

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

					__A       =					image_processor(A				,return_tensors="tf"					)["pixel_values"].numpy()
					__A       =					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					)					)








def     a     (		lowerCamelCase_							):






				'''simple docstring'''

				if p < 2:
								raise ValueError('''p should not be less than 2!'''							)
				elif p == 2:
								return True

				lowercase__  					=     4
				lowercase__  					=     (1 << p) - 1
				for _ in range(p - 2							):
								lowercase__  					=     ((s * s) - 2) % m
				return s == 0


if __name__ == "__main__":
							print(lucas_lehmer_test(7))
							print(lucas_lehmer_test(11))




