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





		"""simple docstring"""





		def _lowercase	(     self  )					->     int:
				_UpperCamelCase							:			Any     =   tempfile.mkdtemp()
				_UpperCamelCase							:			str     =   SamImageProcessor()
				_UpperCamelCase							:			Tuple     =   SamProcessor(lowerCamelCase_  )
				processor.save_pretrained(self.tmpdirname  )
		def _lowercase	(     self	,       **_snake_case  )					->     Any:
				return AutoProcessor.from_pretrained(self.tmpdirname	,       **lowerCamelCase_  ).image_processor
		def _lowercase	(     self  )					->     List[str]:
				shutil.rmtree(self.tmpdirname  )
		def _lowercase	(     self  )					->     List[str]:
				_UpperCamelCase							:			int     =   [np.random.randint(255	,       size=(3, 30, 400)	,       dtype=np.uinta  )]

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

				return image_inputs
		def _lowercase	(     self  )					->     Union[str, Any]:
				_UpperCamelCase							:			Optional[Any]     =   SamProcessor(image_processor=self.get_image_processor()  )
				processor.save_pretrained(self.tmpdirname  )

				_UpperCamelCase							:			List[Any]     =   self.get_image_processor(do_normalize=lowerCamelCase_	,       padding_value=1.0  )

				_UpperCamelCase							:			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 _lowercase	(     self  )					->     Dict:
				_UpperCamelCase							:			int     =   self.get_image_processor()

				_UpperCamelCase							:			Dict     =   SamProcessor(image_processor=lowerCamelCase_  )

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

				_UpperCamelCase							:			List[Any]     =   image_processor(lowerCamelCase_	,       return_tensors='''np'''  )
				_UpperCamelCase							:			int     =   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 _lowercase	(     self  )					->     str:
				_UpperCamelCase							:			Optional[Any]     =   self.get_image_processor()

				_UpperCamelCase							:			List[str]     =   SamProcessor(image_processor=lowerCamelCase_  )
				_UpperCamelCase							:			str     =   [torch.ones((1, 3, 5, 5)  )]

				_UpperCamelCase							:			List[str]     =   [[1764, 2646]]

				_UpperCamelCase							:			Optional[Any]     =   [[683, 1024]]
				_UpperCamelCase							:			int     =   processor.post_process_masks(lowerCamelCase_	,       lowerCamelCase_	,       lowerCamelCase_  )
				self.assertEqual(masks[0].shape	,       (1, 3, 1764, 2646)  )

				_UpperCamelCase							:			str     =   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							:			Optional[int]     =   [np.ones((1, 3, 5, 5)  )]
				_UpperCamelCase							:			Tuple     =   processor.post_process_masks(lowerCamelCase_	,       np.array(lowerCamelCase_  )	,       np.array(lowerCamelCase_  )  )

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

				_UpperCamelCase							:			Dict     =   [[1, 0], [0, 1]]
				with self.assertRaises(lowerCamelCase_  ):
						_UpperCamelCase							:			str     =   processor.post_process_masks(lowerCamelCase_	,       np.array(lowerCamelCase_  )	,       np.array(lowerCamelCase_  )  )







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





		"""simple docstring"""





		def _lowercase	(     self  )					->     str:
				_UpperCamelCase							:			Optional[int]     =   tempfile.mkdtemp()
				_UpperCamelCase							:			Tuple     =   SamImageProcessor()
				_UpperCamelCase							:			List[Any]     =   SamProcessor(lowerCamelCase_  )
				processor.save_pretrained(self.tmpdirname  )
		def _lowercase	(     self	,       **_snake_case  )					->     str:
				return AutoProcessor.from_pretrained(self.tmpdirname	,       **lowerCamelCase_  ).image_processor
		def _lowercase	(     self  )					->     Any:
				shutil.rmtree(self.tmpdirname  )
		def _lowercase	(     self  )					->     Tuple:
				_UpperCamelCase							:			Union[str, Any]     =   [np.random.randint(255	,       size=(3, 30, 400)	,       dtype=np.uinta  )]

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

				return image_inputs
		def _lowercase	(     self  )					->     Union[str, Any]:
				_UpperCamelCase							:			List[str]     =   SamProcessor(image_processor=self.get_image_processor()  )
				processor.save_pretrained(self.tmpdirname  )

				_UpperCamelCase							:			Optional[Any]     =   self.get_image_processor(do_normalize=lowerCamelCase_	,       padding_value=1.0  )

				_UpperCamelCase							:			Dict     =   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 _lowercase	(     self  )					->     List[str]:
				_UpperCamelCase							:			int     =   self.get_image_processor()

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

				_UpperCamelCase							:			Tuple     =   self.prepare_image_inputs()

				_UpperCamelCase							:			List[str]     =   image_processor(lowerCamelCase_	,       return_tensors='''np'''  )
				_UpperCamelCase							:			List[Any]     =   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 _lowercase	(     self  )					->     List[Any]:
				_UpperCamelCase							:			Union[str, Any]     =   self.get_image_processor()

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

				_UpperCamelCase							:			List[Any]     =   [[1764, 2646]]

				_UpperCamelCase							:			List[str]     =   [[683, 1024]]
				_UpperCamelCase							:			Any     =   processor.post_process_masks(lowerCamelCase_	,       lowerCamelCase_	,       lowerCamelCase_	,       return_tensors='''tf'''  )
				self.assertEqual(masks[0].shape	,       (1, 3, 1764, 2646)  )

				_UpperCamelCase							:			Optional[int]     =   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							:			Optional[int]     =   [np.ones((1, 3, 5, 5)  )]
				_UpperCamelCase							:			List[str]     =   processor.post_process_masks(
				    lowerCamelCase_	,       np.array(lowerCamelCase_  )	,       np.array(lowerCamelCase_  )	,       return_tensors='''tf'''  )

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

				_UpperCamelCase							:			Dict     =   [[1, 0], [0, 1]]
				with self.assertRaises(tf.errors.InvalidArgumentError  ):
						_UpperCamelCase							:			List[Any]     =   processor.post_process_masks(
						    lowerCamelCase_	,       np.array(lowerCamelCase_  )	,       np.array(lowerCamelCase_  )	,       return_tensors='''tf'''  )







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





		"""simple docstring"""





		def _lowercase	(     self  )					->     str:
				_UpperCamelCase							:			str     =   tempfile.mkdtemp()
				_UpperCamelCase							:			Any     =   SamImageProcessor()
				_UpperCamelCase							:			int     =   SamProcessor(lowerCamelCase_  )
				processor.save_pretrained(self.tmpdirname  )
		def _lowercase	(     self	,       **_snake_case  )					->     Tuple:
				return AutoProcessor.from_pretrained(self.tmpdirname	,       **lowerCamelCase_  ).image_processor
		def _lowercase	(     self  )					->     str:
				shutil.rmtree(self.tmpdirname  )
		def _lowercase	(     self  )					->     List[Any]:
				_UpperCamelCase							:			int     =   [np.random.randint(255	,       size=(3, 30, 400)	,       dtype=np.uinta  )]

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

				return image_inputs
		@is_pt_tf_cross_test
		def _lowercase	(     self  )					->     Union[str, Any]:
				_UpperCamelCase							:			Any     =   self.get_image_processor()

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

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

				_UpperCamelCase							:			Any     =   [[683, 1024]]
				_UpperCamelCase							:			int     =   processor.post_process_masks(
				    lowerCamelCase_	,       lowerCamelCase_	,       lowerCamelCase_	,       return_tensors='''tf'''  )
				_UpperCamelCase							:			Dict     =   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 _lowercase	(     self  )					->     List[str]:
				_UpperCamelCase							:			Any     =   self.get_image_processor()

				_UpperCamelCase							:			Tuple     =   SamProcessor(image_processor=lowerCamelCase_  )

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

				_UpperCamelCase							:			List[Any]     =   image_processor(lowerCamelCase_	,       return_tensors='''pt'''  )['''pixel_values'''].numpy()
				_UpperCamelCase							:			Optional[int]     =   processor(images=lowerCamelCase_	,       return_tensors='''pt'''  )['''pixel_values'''].numpy()

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






_SCREAMING_SNAKE_CASE 		=      9.8_06_65





def 			lowercase(	UpperCamelCase_							,					UpperCamelCase_							,					UpperCamelCase_ = g       )		->	float:







 '''simple docstring'''

 if fluid_density <= 0:
  raise ValueError("""Impossible fluid density"""       )
 if volume < 0:
  raise ValueError("""Impossible Object volume"""       )
 if gravity <= 0:
  raise ValueError("""Impossible Gravity"""       )

 return fluid_density * gravity * volume


if __name__ == "__main__":
     import doctest

     # run doctest
     doctest.testmod()
