





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






     """simple docstring"""






     def      lowerCAmelCase			(   self							:			Dict						)->	Union[str, Any]:
        snake_case 						=   tempfile.mkdtemp()
        snake_case 						=   SamImageProcessor()
        snake_case 						=   SamProcessor(__snake_case						)
        processor.save_pretrained(self.tmpdirname						)



     def      lowerCAmelCase			(   self							:			List[Any]   ,       **__snake_case							:			List[Any]						)->	Dict:
        return AutoProcessor.from_pretrained(self.tmpdirname   ,       **__snake_case						).image_processor



     def      lowerCAmelCase			(   self							:			Dict						)->	Union[str, Any]:
        shutil.rmtree(self.tmpdirname						)



     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Any:
        snake_case 						=   [np.random.randint(2_55   ,       size=(3, 30, 4_00)   ,       dtype=np.uinta						)]

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

        return image_inputs



     def      lowerCAmelCase			(   self							:			List[str]						)->	Optional[int]:
        snake_case 						=   SamProcessor(image_processor=self.get_image_processor()						)
        processor.save_pretrained(self.tmpdirname						)

        snake_case 						=   self.get_image_processor(do_normalize=__snake_case   ,       padding_value=1.0						)

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

        snake_case 						=   SamProcessor(image_processor=__snake_case						)

        snake_case 						=   self.prepare_image_inputs()

        snake_case 						=   image_processor(__snake_case   ,       return_tensors="""np"""						)
        snake_case 						=   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      lowerCAmelCase			(   self							:			Optional[Any]						)->	int:
        snake_case 						=   self.get_image_processor()

        snake_case 						=   SamProcessor(image_processor=__snake_case						)
        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(__snake_case   ,       __snake_case   ,       __snake_case						)
        self.assertEqual(masks[0].shape   ,       (1, 3, 17_64, 26_46)						)

        snake_case 						=   processor.post_process_masks(
            __snake_case   ,       torch.tensor(__snake_case						)   ,       torch.tensor(__snake_case						)						)
        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(__snake_case   ,       np.array(__snake_case						)   ,       np.array(__snake_case						)						)

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

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




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






     """simple docstring"""






     def      lowerCAmelCase			(   self							:			Tuple						)->	str:
        snake_case 						=   tempfile.mkdtemp()
        snake_case 						=   SamImageProcessor()
        snake_case 						=   SamProcessor(__snake_case						)
        processor.save_pretrained(self.tmpdirname						)



     def      lowerCAmelCase			(   self							:			str   ,       **__snake_case							:			int						)->	str:
        return AutoProcessor.from_pretrained(self.tmpdirname   ,       **__snake_case						).image_processor



     def      lowerCAmelCase			(   self							:			Tuple						)->	Tuple:
        shutil.rmtree(self.tmpdirname						)



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

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

        return image_inputs



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

        snake_case 						=   self.get_image_processor(do_normalize=__snake_case   ,       padding_value=1.0						)

        snake_case 						=   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      lowerCAmelCase			(   self							:			Any						)->	int:
        snake_case 						=   self.get_image_processor()

        snake_case 						=   SamProcessor(image_processor=__snake_case						)

        snake_case 						=   self.prepare_image_inputs()

        snake_case 						=   image_processor(__snake_case   ,       return_tensors="""np"""						)
        snake_case 						=   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      lowerCAmelCase			(   self							:			Any						)->	Optional[int]:
        snake_case 						=   self.get_image_processor()

        snake_case 						=   SamProcessor(image_processor=__snake_case						)
        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(__snake_case   ,       __snake_case   ,       __snake_case   ,       return_tensors="""tf"""						)
        self.assertEqual(masks[0].shape   ,       (1, 3, 17_64, 26_46)						)

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

        # should also work with np
        snake_case 						=   [np.ones((1, 3, 5, 5)						)]
        snake_case 						=   processor.post_process_masks(
            __snake_case   ,       np.array(__snake_case						)   ,       np.array(__snake_case						)   ,       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(
               __snake_case   ,       np.array(__snake_case						)   ,       np.array(__snake_case						)   ,       return_tensors="""tf"""						)




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






     """simple docstring"""






     def      lowerCAmelCase			(   self							:			Dict						)->	List[Any]:
        snake_case 						=   tempfile.mkdtemp()
        snake_case 						=   SamImageProcessor()
        snake_case 						=   SamProcessor(__snake_case						)
        processor.save_pretrained(self.tmpdirname						)



     def      lowerCAmelCase			(   self							:			Tuple   ,       **__snake_case							:			int						)->	Any:
        return AutoProcessor.from_pretrained(self.tmpdirname   ,       **__snake_case						).image_processor



     def      lowerCAmelCase			(   self							:			Dict						)->	Optional[int]:
        shutil.rmtree(self.tmpdirname						)



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

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

        return image_inputs



     @is_pt_tf_cross_test
     def      lowerCAmelCase			(   self							:			Union[str, Any]						)->	Any:
        snake_case 						=   self.get_image_processor()

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

        snake_case 						=   [[17_64, 26_46]]

        snake_case 						=   [[6_83, 10_24]]
        snake_case 						=   processor.post_process_masks(
            __snake_case   ,       __snake_case   ,       __snake_case   ,       return_tensors="""tf"""						)
        snake_case 						=   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      lowerCAmelCase			(   self							:			str						)->	Union[str, Any]:
        snake_case 						=   self.get_image_processor()

        snake_case 						=   SamProcessor(image_processor=__snake_case						)

        snake_case 						=   self.prepare_image_inputs()

        snake_case 						=   image_processor(__snake_case   ,       return_tensors="""pt"""						)["""pixel_values"""].numpy()
        snake_case 						=   processor(images=__snake_case   ,       return_tensors="""pt"""						)["""pixel_values"""].numpy()

        snake_case 						=   image_processor(__snake_case   ,       return_tensors="""tf"""						)["""pixel_values"""].numpy()
        snake_case 						=   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 re





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  list:
   return [char.split() for char in re.split(r"""[^ a-z A-Z 0-9 \s]"""      ,    str_		)]





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   snake_case 						=   split_input(str_		)
   return "".join(
       ["""""".join([char.capitalize() for char in sub_str]		) for sub_str in string_split]		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool      ,    __lowerCAmelCase       :							str		)		->  str:
   try:
      snake_case 						=   split_input(__lowerCAmelCase		)
      if upper:
         snake_case 						=   """""".join(
             [
                 separator.join([char.upper() for char in sub_str]		)
                 for sub_str in string_split
             ]		)
      else:
         snake_case 						=   """""".join(
             [
                 separator.join([char.lower() for char in sub_str]		)
                 for sub_str in string_split
             ]		)
      return res_str
   except IndexError:
      return "not valid string"





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   return to_simple_case(__lowerCAmelCase		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str		)		->  str:
   try:
      snake_case 						=   to_simple_case(__lowerCAmelCase		)
      return res_str[0].lower() + res_str[1:]
   except IndexError:
      return "not valid string"





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool		)		->  str:
   return to_complex_case(__lowerCAmelCase      ,    __lowerCAmelCase      ,    """_"""		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							str      ,    __lowerCAmelCase       :							bool		)		->  str:
   return to_complex_case(__lowerCAmelCase      ,    __lowerCAmelCase      ,    """-"""		)


if __name__ == "__main__":
 __import__("doctest").testmod()

