






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Tuple:
							a_		:  int											=      tempfile.mkdtemp()
							a_		:  str											=      SamImageProcessor()
							a_		:  Optional[int]											=      SamProcessor(__SCREAMING_SNAKE_CASE					)
							processor.save_pretrained(self.tmpdirname					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]  ,						**__SCREAMING_SNAKE_CASE      :			List[str]					)			->			Union[str, Any]:
							return AutoProcessor.from_pretrained(self.tmpdirname  ,						**__SCREAMING_SNAKE_CASE					).image_processor






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[Any]:
							shutil.rmtree(self.tmpdirname					)






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

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

							return image_inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			str:
							a_		:  List[Any]											=      SamProcessor(image_processor=self.get_image_processor()					)
							processor.save_pretrained(self.tmpdirname					)

							a_		:  Optional[int]											=      self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE  ,						padding_value=1.0					)

							a_		:  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 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Union[str, Any]:
							a_		:  List[Any]											=      self.get_image_processor()

							a_		:  List[Any]											=      SamProcessor(image_processor=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      self.prepare_image_inputs()

							a_		:  int											=      image_processor(__SCREAMING_SNAKE_CASE  ,						return_tensors='''np'''					)
							a_		:  Union[str, 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 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 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Union[str, Any]:
							a_		:  Tuple											=      self.get_image_processor()

							a_		:  List[str]											=      SamProcessor(image_processor=__SCREAMING_SNAKE_CASE					)
							a_		:  Any											=      [torch.ones((1, 3, 5, 5)					)]

							a_		:  Tuple											=      [[1764, 2646]]

							a_		:  List[str]											=      [[683, 1024]]
							a_		:  Dict											=      processor.post_process_masks(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							self.assertEqual(masks[0].shape  ,						(1, 3, 1764, 2646)					)

							a_		:  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
							a_		:  Dict											=      [np.ones((1, 3, 5, 5)					)]
							a_		:  int											=      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)					)

							a_		:  Union[str, Any]											=      [[1, 0], [0, 1]]
							with self.assertRaises(__SCREAMING_SNAKE_CASE					):
											a_		:  Dict											=      processor.post_process_masks(__SCREAMING_SNAKE_CASE  ,						np.array(__SCREAMING_SNAKE_CASE					)  ,						np.array(__SCREAMING_SNAKE_CASE					)					)





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






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							a_		:  Any											=      tempfile.mkdtemp()
							a_		:  str											=      SamImageProcessor()
							a_		:  Optional[Any]											=      SamProcessor(__SCREAMING_SNAKE_CASE					)
							processor.save_pretrained(self.tmpdirname					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						**__SCREAMING_SNAKE_CASE      :			str					)			->			Dict:
							return AutoProcessor.from_pretrained(self.tmpdirname  ,						**__SCREAMING_SNAKE_CASE					).image_processor






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[str]:
							shutil.rmtree(self.tmpdirname					)






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

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

							return image_inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Any:
							a_		:  Optional[int]											=      SamProcessor(image_processor=self.get_image_processor()					)
							processor.save_pretrained(self.tmpdirname					)

							a_		:  Union[str, Any]											=      self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE  ,						padding_value=1.0					)

							a_		:  Any											=      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 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							a_		:  str											=      self.get_image_processor()

							a_		:  List[str]											=      SamProcessor(image_processor=__SCREAMING_SNAKE_CASE					)

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

							a_		:  Union[str, Any]											=      image_processor(__SCREAMING_SNAKE_CASE  ,						return_tensors='''np'''					)
							a_		:  str											=      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 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Tuple:
							a_		:  Optional[int]											=      self.get_image_processor()

							a_		:  str											=      SamProcessor(image_processor=__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[Any]											=      [tf.ones((1, 3, 5, 5)					)]

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

							a_		:  Union[str, Any]											=      [[683, 1024]]
							a_		:  Optional[int]											=      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)					)

							a_		:  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
							a_		:  int											=      [np.ones((1, 3, 5, 5)					)]
							a_		:  int											=      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)					)

							a_		:  Tuple											=      [[1, 0], [0, 1]]
							with self.assertRaises(tf.errors.InvalidArgumentError					):
											a_		:  Optional[Any]											=      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 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			int:
							a_		:  List[Any]											=      tempfile.mkdtemp()
							a_		:  Dict											=      SamImageProcessor()
							a_		:  List[str]											=      SamProcessor(__SCREAMING_SNAKE_CASE					)
							processor.save_pretrained(self.tmpdirname					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						**__SCREAMING_SNAKE_CASE      :			Tuple					)			->			int:
							return AutoProcessor.from_pretrained(self.tmpdirname  ,						**__SCREAMING_SNAKE_CASE					).image_processor






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			Any:
							shutil.rmtree(self.tmpdirname					)






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

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

							return image_inputs






			@is_pt_tf_cross_test
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							a_		:  int											=      self.get_image_processor()

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

							a_		:  int											=      [[1764, 2646]]

							a_		:  Any											=      [[683, 1024]]
							a_		:  Dict											=      processor.post_process_masks(
							    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						return_tensors='''tf'''					)
							a_		:  Tuple											=      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 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			List[Any]:
							a_		:  Optional[Any]											=      self.get_image_processor()

							a_		:  Optional[Any]											=      SamProcessor(image_processor=__SCREAMING_SNAKE_CASE					)

							a_		:  str											=      self.prepare_image_inputs()

							a_		:  Dict											=      image_processor(__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''					)['''pixel_values'''].numpy()
							a_		:  List[str]											=      processor(images=__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''					)['''pixel_values'''].numpy()

							a_		:  List[Any]											=      image_processor(__SCREAMING_SNAKE_CASE  ,						return_tensors='''tf'''					)['''pixel_values'''].numpy()
							a_		:  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					)					)












'''simple docstring'''







from __future__ import annotations





def 		_UpperCAmelCase   (							__A							:   list[int]      ):
				a_		:  int											=      len(__A      ) // 2

				# choose the middle 3 elements
				a_		:  Dict											=      lst[m - 1 : m + 2]

				# if middle element is peak
				if three[1] > three[0] and three[1] > three[2]:
								return three[1]

				# if increasing, recurse on right
				elif three[0] < three[2]:
								if len(lst[:m]      ) == 2:
												m -= 1
								return peak(lst[m:]      )

				# decreasing
				else:
								if len(lst[:m]      ) == 2:
												m += 1
								return peak(lst[:m]      )


if __name__ == "__main__":
		import doctest

		doctest.testmod()




