import math



def A__  (			lowercase:     int					)				->		bool:
       assert isinstance(lowercase,		lowercase					) and (
           number >= 0
       ), "'number' must been an int and positive"

       if 1 < number < 4:
              # 2 and 3 are primes
              return True
       elif number < 2 or not number % 2:
              # Negatives, 0, 1 and all even numbers are not primes
              return False

       A						:     Optional[int]         =range(3,		int(math.sqrt(lowercase					) + 1					),		2					)
       return not any(not number % i for i in odd_numbers					)



def A__  (			lowercase:     List[Any],		lowercase:     List[str]=1,		**lowercase:     Tuple					)				->		Dict:
       A						:     Dict         =factor * value
       A						:     Any         =value

       while not is_prime(lowercase					):
              value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1

       if value == first_value_val:
              return next_prime(value + 1,		**lowercase					)
       return value







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


  def 		SCREAMING_SNAKE_CASE_		(				self    :					List[str]     )    ->				Union[str, Any]:
         A						:     Dict         =tempfile.mkdtemp()
         A						:     int         =SamImageProcessor()
         A						:     Union[str, Any]         =SamProcessor(SCREAMING_SNAKE_CASE__     )
         processor.save_pretrained(self.tmpdirname     )







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Optional[int]      ,	**SCREAMING_SNAKE_CASE__    :					Optional[Any]     )    ->				Optional[int]:
         return AutoProcessor.from_pretrained(self.tmpdirname      ,	**SCREAMING_SNAKE_CASE__     ).image_processor







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







  def 		SCREAMING_SNAKE_CASE_		(				self    :					List[str]     )    ->				Optional[int]:
         A						:     str         =[np.random.randint(2_55      ,	size=(3, 30, 4_00)      ,	dtype=np.uinta     )]

         A						:     Optional[int]         =[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__      ,	0      ,	-1     )     ) for x in image_inputs]

         return image_inputs







  def 		SCREAMING_SNAKE_CASE_		(				self    :					str     )    ->				Tuple:
         A						:     Optional[int]         =SamProcessor(image_processor=self.get_image_processor()     )
         processor.save_pretrained(self.tmpdirname     )

         A						:     str         =self.get_image_processor(do_normalize=SCREAMING_SNAKE_CASE__      ,	padding_value=1.0     )

         A						:     Union[str, 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    :					Tuple     )    ->				Optional[int]:
         A						:     Optional[Any]         =self.get_image_processor()

         A						:     Optional[Any]         =SamProcessor(image_processor=SCREAMING_SNAKE_CASE__     )

         A						:     Dict         =self.prepare_image_inputs()

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

         A						:     Union[str, Any]         =SamProcessor(image_processor=SCREAMING_SNAKE_CASE__     )
         A						:     str         =[torch.ones((1, 3, 5, 5)     )]

         A						:     Optional[Any]         =[[17_64, 26_46]]

         A						:     List[Any]         =[[6_83, 10_24]]
         A						:     Union[str, Any]         =processor.post_process_masks(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__     )
         self.assertEqual(masks[0].shape      ,	(1, 3, 17_64, 26_46)     )

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

         # should also work with np
         A						:     str         =[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, 17_64, 26_46)     )

         A						:     Any         =[[1, 0], [0, 1]]
         with self.assertRaises(SCREAMING_SNAKE_CASE__     ):
                A						:     Any         =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			):
  '''simple docstring'''


  def 		SCREAMING_SNAKE_CASE_		(				self    :					str     )    ->				str:
         A						:     Tuple         =tempfile.mkdtemp()
         A						:     Union[str, Any]         =SamImageProcessor()
         A						:     Union[str, Any]         =SamProcessor(SCREAMING_SNAKE_CASE__     )
         processor.save_pretrained(self.tmpdirname     )







  def 		SCREAMING_SNAKE_CASE_		(				self    :					int      ,	**SCREAMING_SNAKE_CASE__    :					str     )    ->				Union[str, Any]:
         return AutoProcessor.from_pretrained(self.tmpdirname      ,	**SCREAMING_SNAKE_CASE__     ).image_processor







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







  def 		SCREAMING_SNAKE_CASE_		(				self    :					List[str]     )    ->				Tuple:
         A						:     Optional[Any]         =[np.random.randint(2_55      ,	size=(3, 30, 4_00)      ,	dtype=np.uinta     )]

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

         return image_inputs







  def 		SCREAMING_SNAKE_CASE_		(				self    :					List[Any]     )    ->				List[str]:
         A						:     Optional[Any]         =SamProcessor(image_processor=self.get_image_processor()     )
         processor.save_pretrained(self.tmpdirname     )

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

         A						:     Any         =SamProcessor(image_processor=SCREAMING_SNAKE_CASE__     )

         A						:     int         =self.prepare_image_inputs()

         A						:     Tuple         =image_processor(SCREAMING_SNAKE_CASE__      ,	return_tensors='np'     )
         A						:     List[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 		SCREAMING_SNAKE_CASE_		(				self    :					List[Any]     )    ->				Tuple:
         A						:     int         =self.get_image_processor()

         A						:     Any         =SamProcessor(image_processor=SCREAMING_SNAKE_CASE__     )
         A						:     Optional[int]         =[tf.ones((1, 3, 5, 5)     )]

         A						:     Tuple         =[[17_64, 26_46]]

         A						:     Union[str, Any]         =[[6_83, 10_24]]
         A						:     int         =processor.post_process_masks(SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	return_tensors='tf'     )
         self.assertEqual(masks[0].shape      ,	(1, 3, 17_64, 26_46)     )

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

         # should also work with np
         A						:     Any         =[np.ones((1, 3, 5, 5)     )]
         A						:     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, 17_64, 26_46)     )

         A						:     Any         =[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError     ):
                A						:     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  SCREAMING_SNAKE_CASE_ (					unittest.TestCase			):
  '''simple docstring'''


  def 		SCREAMING_SNAKE_CASE_		(				self    :					Optional[int]     )    ->				Union[str, Any]:
         A						:     Optional[int]         =tempfile.mkdtemp()
         A						:     Union[str, Any]         =SamImageProcessor()
         A						:     Dict         =SamProcessor(SCREAMING_SNAKE_CASE__     )
         processor.save_pretrained(self.tmpdirname     )







  def 		SCREAMING_SNAKE_CASE_		(				self    :					int      ,	**SCREAMING_SNAKE_CASE__    :					List[str]     )    ->				Any:
         return AutoProcessor.from_pretrained(self.tmpdirname      ,	**SCREAMING_SNAKE_CASE__     ).image_processor







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







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Union[str, Any]     )    ->				Tuple:
         A						:     Any         =[np.random.randint(2_55      ,	size=(3, 30, 4_00)      ,	dtype=np.uinta     )]

         A						:     Tuple         =[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    :					List[str]     )    ->				List[str]:
         A						:     Optional[Any]         =self.get_image_processor()

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

         A						:     int         =[[17_64, 26_46]]

         A						:     int         =[[6_83, 10_24]]
         A						:     Dict         =processor.post_process_masks(
             SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	SCREAMING_SNAKE_CASE__      ,	return_tensors='tf'     )
         A						:     Optional[Any]         =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[int]     )    ->				Any:
         A						:     Union[str, Any]         =self.get_image_processor()

         A						:     int         =SamProcessor(image_processor=SCREAMING_SNAKE_CASE__     )

         A						:     int         =self.prepare_image_inputs()

         A						:     List[Any]         =image_processor(SCREAMING_SNAKE_CASE__      ,	return_tensors='pt'     )['pixel_values'].numpy()
         A						:     Tuple         =processor(images=SCREAMING_SNAKE_CASE__      ,	return_tensors='pt'     )['pixel_values'].numpy()

         A						:     Optional[int]         =image_processor(SCREAMING_SNAKE_CASE__      ,	return_tensors='tf'     )['pixel_values'].numpy()
         A						:     Dict         =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__     )     )






