


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






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				tempfile.mkdtemp()
              lowercase		:Tuple       							=				SamImageProcessor()
              lowercase		:int       							=				SamProcessor(snake_case__				)
              processor.save_pretrained(self.tmpdirname				)






       def  __snake_case						(   self  :    Tuple  ,     **snake_case__  :    Dict				):

              '''simple docstring'''






              return AutoProcessor.from_pretrained(self.tmpdirname  ,     **snake_case__				).image_processor






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              shutil.rmtree(self.tmpdirname				)






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:List[str]       							=				[np.random.randint(2_5_5  ,     size=(3, 3_0, 4_0_0)  ,     dtype=np.uinta				)]

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

              return image_inputs






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				SamProcessor(image_processor=self.get_image_processor()				)
              processor.save_pretrained(self.tmpdirname				)

              lowercase		:Optional[int]       							=				self.get_image_processor(do_normalize=snake_case__  ,     padding_value=1.0				)

              lowercase		:str       							=				SamProcessor.from_pretrained(self.tmpdirname  ,     do_normalize=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  ,     snake_case__				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:int       							=				self.get_image_processor()

              lowercase		:Optional[int]       							=				SamProcessor(image_processor=snake_case__				)

              lowercase		:List[Any]       							=				self.prepare_image_inputs()

              lowercase		:List[str]       							=				image_processor(snake_case__  ,     return_tensors='''np'''				)
              lowercase		:Optional[Any]       							=				processor(images=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  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase		:str       							=				self.get_image_processor()

              lowercase		:Any       							=				SamProcessor(image_processor=snake_case__				)
              lowercase		:Dict       							=				[torch.ones((1, 3, 5, 5)				)]

              lowercase		:Optional[Any]       							=				[[1_7_6_4, 2_6_4_6]]

              lowercase		:Tuple       							=				[[6_8_3, 1_0_2_4]]
              lowercase		:Any       							=				processor.post_process_masks(snake_case__  ,     snake_case__  ,     snake_case__				)
              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              lowercase		:Tuple       							=				processor.post_process_masks(
                  snake_case__  ,     torch.tensor(snake_case__				)  ,     torch.tensor(snake_case__				)				)
              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              # should also work with np
              lowercase		:Dict       							=				[np.ones((1, 3, 5, 5)				)]
              lowercase		:Union[str, Any]       							=				processor.post_process_masks(snake_case__  ,     np.array(snake_case__				)  ,     np.array(snake_case__				)				)

              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              lowercase		:Union[str, Any]       							=				[[1, 0], [0, 1]]
              with self.assertRaises(snake_case__				):
                     lowercase		:Optional[Any]       							=				processor.post_process_masks(snake_case__  ,     np.array(snake_case__				)  ,     np.array(snake_case__				)				)







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






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:List[str]       							=				tempfile.mkdtemp()
              lowercase		:Any       							=				SamImageProcessor()
              lowercase		:List[str]       							=				SamProcessor(snake_case__				)
              processor.save_pretrained(self.tmpdirname				)






       def  __snake_case						(   self  :    List[Any]  ,     **snake_case__  :    Tuple				):

              '''simple docstring'''






              return AutoProcessor.from_pretrained(self.tmpdirname  ,     **snake_case__				).image_processor






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              shutil.rmtree(self.tmpdirname				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Tuple       							=				[np.random.randint(2_5_5  ,     size=(3, 3_0, 4_0_0)  ,     dtype=np.uinta				)]

              lowercase		:Union[str, Any]       							=				[Image.fromarray(np.moveaxis(snake_case__  ,     0  ,     -1				)				) for x in image_inputs]

              return image_inputs






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Dict       							=				SamProcessor(image_processor=self.get_image_processor()				)
              processor.save_pretrained(self.tmpdirname				)

              lowercase		:List[Any]       							=				self.get_image_processor(do_normalize=snake_case__  ,     padding_value=1.0				)

              lowercase		:Tuple       							=				SamProcessor.from_pretrained(self.tmpdirname  ,     do_normalize=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  ,     snake_case__				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				self.get_image_processor()

              lowercase		:List[Any]       							=				SamProcessor(image_processor=snake_case__				)

              lowercase		:List[str]       							=				self.prepare_image_inputs()

              lowercase		:Dict       							=				image_processor(snake_case__  ,     return_tensors='''np'''				)
              lowercase		:List[Any]       							=				processor(images=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  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				self.get_image_processor()

              lowercase		:Dict       							=				SamProcessor(image_processor=snake_case__				)
              lowercase		:Any       							=				[tf.ones((1, 3, 5, 5)				)]

              lowercase		:Dict       							=				[[1_7_6_4, 2_6_4_6]]

              lowercase		:Union[str, Any]       							=				[[6_8_3, 1_0_2_4]]
              lowercase		:int       							=				processor.post_process_masks(snake_case__  ,     snake_case__  ,     snake_case__  ,     return_tensors='''tf'''				)
              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              lowercase		:Optional[Any]       							=				processor.post_process_masks(
                  snake_case__  ,     tf.convert_to_tensor(snake_case__				)  ,     tf.convert_to_tensor(snake_case__				)  ,     return_tensors='''tf'''  ,     )
              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              # should also work with np
              lowercase		:Dict       							=				[np.ones((1, 3, 5, 5)				)]
              lowercase		:Union[str, Any]       							=				processor.post_process_masks(
                  snake_case__  ,     np.array(snake_case__				)  ,     np.array(snake_case__				)  ,     return_tensors='''tf'''				)

              self.assertEqual(masks[0].shape  ,     (1, 3, 1_7_6_4, 2_6_4_6)				)

              lowercase		:List[str]       							=				[[1, 0], [0, 1]]
              with self.assertRaises(tf.errors.InvalidArgumentError				):
                     lowercase		:Union[str, Any]       							=				processor.post_process_masks(
                         snake_case__  ,     np.array(snake_case__				)  ,     np.array(snake_case__				)  ,     return_tensors='''tf'''				)







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






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Dict       							=				tempfile.mkdtemp()
              lowercase		:Any       							=				SamImageProcessor()
              lowercase		:Optional[int]       							=				SamProcessor(snake_case__				)
              processor.save_pretrained(self.tmpdirname				)






       def  __snake_case						(   self  :    List[Any]  ,     **snake_case__  :    Dict				):

              '''simple docstring'''






              return AutoProcessor.from_pretrained(self.tmpdirname  ,     **snake_case__				).image_processor






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              shutil.rmtree(self.tmpdirname				)






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				[np.random.randint(2_5_5  ,     size=(3, 3_0, 4_0_0)  ,     dtype=np.uinta				)]

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

              return image_inputs






       @is_pt_tf_cross_test
       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				self.get_image_processor()

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

              lowercase		:Tuple       							=				[[1_7_6_4, 2_6_4_6]]

              lowercase		:Union[str, Any]       							=				[[6_8_3, 1_0_2_4]]
              lowercase		:str       							=				processor.post_process_masks(
                  snake_case__  ,     snake_case__  ,     snake_case__  ,     return_tensors='''tf'''				)
              lowercase		:List[Any]       							=				processor.post_process_masks(
                  snake_case__  ,     snake_case__  ,     snake_case__  ,     return_tensors='''pt'''				)

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







       @is_pt_tf_cross_test
       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase		:Dict       							=				self.get_image_processor()

              lowercase		:Any       							=				SamProcessor(image_processor=snake_case__				)

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

              lowercase		:Optional[Any]       							=				image_processor(snake_case__  ,     return_tensors='''pt'''				)['''pixel_values'''].numpy()
              lowercase		:Tuple       							=				processor(images=snake_case__  ,     return_tensors='''pt'''				)['''pixel_values'''].numpy()

              lowercase		:Optional[Any]       							=				image_processor(snake_case__  ,     return_tensors='''tf'''				)['''pixel_values'''].numpy()
              lowercase		:Optional[int]       							=				processor(images=snake_case__  ,     return_tensors='''tf'''				)['''pixel_values'''].numpy()

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





"""simple docstring"""



import math






def       lowerCamelCase					(a_						:int)     ->							bool:
       assert isinstance(a_				,				a_) 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

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






def       lowerCamelCase					(a_						:Optional[int]				,				a_						:Optional[int]=1				,				**a_						:List[str])     ->							Any:
       lowercase		:str       							=				factor * value
       lowercase		:int       							=				value

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

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

