


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

 '''simple docstring'''

 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   tempfile.mkdtemp()
       snake_case_ =   SamImageProcessor()
       snake_case_ =   SamProcessor(_UpperCAmelCase      )
       processor.save_pretrained(self.tmpdirname      )



 def 		UpperCamelCase__  (			self	,   **_UpperCAmelCase      ):
       return AutoProcessor.from_pretrained(self.tmpdirname	,   **_UpperCAmelCase      ).image_processor



 def 		UpperCamelCase__  (			self      ):
       shutil.rmtree(self.tmpdirname      )



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

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

       return image_inputs



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   SamProcessor(image_processor=self.get_image_processor()      )
       processor.save_pretrained(self.tmpdirname      )

       snake_case_ =   self.get_image_processor(do_normalize=_UpperCAmelCase	,   padding_value=1.0      )

       snake_case_ =   SamProcessor.from_pretrained(self.tmpdirname	,   do_normalize=_UpperCAmelCase	,   padding_value=1.0      )

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



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.get_image_processor()

       snake_case_ =   SamProcessor(image_processor=_UpperCAmelCase      )

       snake_case_ =   self.prepare_image_inputs()

       snake_case_ =   image_processor(_UpperCAmelCase	,   return_tensors='''np'''      )
       snake_case_ =   processor(images=_UpperCAmelCase	,   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      ):
       snake_case_ =   self.get_image_processor()

       snake_case_ =   SamProcessor(image_processor=_UpperCAmelCase      )
       snake_case_ =   [torch.ones((1, 3, 5, 5)      )]

       snake_case_ =   [[17_64, 26_46]]

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

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

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

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

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







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

 '''simple docstring'''

 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   tempfile.mkdtemp()
       snake_case_ =   SamImageProcessor()
       snake_case_ =   SamProcessor(_UpperCAmelCase      )
       processor.save_pretrained(self.tmpdirname      )



 def 		UpperCamelCase__  (			self	,   **_UpperCAmelCase      ):
       return AutoProcessor.from_pretrained(self.tmpdirname	,   **_UpperCAmelCase      ).image_processor



 def 		UpperCamelCase__  (			self      ):
       shutil.rmtree(self.tmpdirname      )



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

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

       return image_inputs



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   SamProcessor(image_processor=self.get_image_processor()      )
       processor.save_pretrained(self.tmpdirname      )

       snake_case_ =   self.get_image_processor(do_normalize=_UpperCAmelCase	,   padding_value=1.0      )

       snake_case_ =   SamProcessor.from_pretrained(self.tmpdirname	,   do_normalize=_UpperCAmelCase	,   padding_value=1.0      )

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



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.get_image_processor()

       snake_case_ =   SamProcessor(image_processor=_UpperCAmelCase      )

       snake_case_ =   self.prepare_image_inputs()

       snake_case_ =   image_processor(_UpperCAmelCase	,   return_tensors='''np'''      )
       snake_case_ =   processor(images=_UpperCAmelCase	,   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      ):
       snake_case_ =   self.get_image_processor()

       snake_case_ =   SamProcessor(image_processor=_UpperCAmelCase      )
       snake_case_ =   [tf.ones((1, 3, 5, 5)      )]

       snake_case_ =   [[17_64, 26_46]]

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

       snake_case_ =   processor.post_process_masks(
           _UpperCAmelCase	,   tf.convert_to_tensor(_UpperCAmelCase      )	,   tf.convert_to_tensor(_UpperCAmelCase      )	,   return_tensors='''tf'''	,   )
       self.assertEqual(masks[0].shape	,   (1, 3, 17_64, 26_46)      )

       # should also work with np
       snake_case_ =   [np.ones((1, 3, 5, 5)      )]
       snake_case_ =   processor.post_process_masks(
           _UpperCAmelCase	,   np.array(_UpperCAmelCase      )	,   np.array(_UpperCAmelCase      )	,   return_tensors='''tf'''      )

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

       snake_case_ =   [[1, 0], [0, 1]]
       with self.assertRaises(tf.errors.InvalidArgumentError      ):
             snake_case_ =   processor.post_process_masks(
                 _UpperCAmelCase	,   np.array(_UpperCAmelCase      )	,   np.array(_UpperCAmelCase      )	,   return_tensors='''tf'''      )







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

 '''simple docstring'''

 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   tempfile.mkdtemp()
       snake_case_ =   SamImageProcessor()
       snake_case_ =   SamProcessor(_UpperCAmelCase      )
       processor.save_pretrained(self.tmpdirname      )



 def 		UpperCamelCase__  (			self	,   **_UpperCAmelCase      ):
       return AutoProcessor.from_pretrained(self.tmpdirname	,   **_UpperCAmelCase      ).image_processor



 def 		UpperCamelCase__  (			self      ):
       shutil.rmtree(self.tmpdirname      )



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

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

       return image_inputs



 @is_pt_tf_cross_test
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.get_image_processor()

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

       snake_case_ =   [[17_64, 26_46]]

       snake_case_ =   [[6_83, 10_24]]
       snake_case_ =   processor.post_process_masks(
           _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   return_tensors='''tf'''      )
       snake_case_ =   processor.post_process_masks(
           _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase	,   return_tensors='''pt'''      )

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





 @is_pt_tf_cross_test
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.get_image_processor()

       snake_case_ =   SamProcessor(image_processor=_UpperCAmelCase      )

       snake_case_ =   self.prepare_image_inputs()

       snake_case_ =   image_processor(_UpperCAmelCase	,   return_tensors='''pt'''      )['''pixel_values'''].numpy()
       snake_case_ =   processor(images=_UpperCAmelCase	,   return_tensors='''pt'''      )['''pixel_values'''].numpy()

       snake_case_ =   image_processor(_UpperCAmelCase	,   return_tensors='''tf'''      )['''pixel_values'''].numpy()
       snake_case_ =   processor(images=_UpperCAmelCase	,   return_tensors='''tf'''      )['''pixel_values'''].numpy()

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



def  __lowerCAmelCase					(SCREAMING_SNAKE_CASE = 6008_5147_5143      )->						int:




      """simple docstring"""

      try:
            snake_case_ =   int(SCREAMING_SNAKE_CASE      )
      except (TypeError, ValueError):
            raise TypeError('''Parameter n must be int or castable to int.'''      )
      if n <= 0:
            raise ValueError('''Parameter n must be greater than or equal to one.'''      )
      snake_case_ =   1
      snake_case_ =   2
      while i * i <= n:
            while n % i == 0:
                  snake_case_ =   i
                  n //= i
            i += 1
      if n > 1:
            snake_case_ =   n
      return int(SCREAMING_SNAKE_CASE      )


if __name__ == "__main__":
   print(f'''{solution() = }''')