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



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   tempfile.mkdtemp()
         UpperCAmelCase_           =   SamImageProcessor()
         UpperCAmelCase_           =   SamProcessor(__lowercase )
         processor.save_pretrained(self.tmpdirname )



    def 					_lowercase						(self    :    str   ,			**__a    :    Optional[Any] ):
         return AutoProcessor.from_pretrained(self.tmpdirname   ,			**__lowercase ).image_processor



    def 					_lowercase						(self    :    Optional[Any] ):
         shutil.rmtree(self.tmpdirname )



    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   [np.random.randint(255   ,			size=(3, 30, 400)   ,			dtype=np.uinta )]

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

         return image_inputs



    def 					_lowercase						(self    :    List[str] ):
         UpperCAmelCase_           =   SamProcessor(image_processor=self.get_image_processor() )
         processor.save_pretrained(self.tmpdirname )

         UpperCAmelCase_           =   self.get_image_processor(do_normalize=__lowercase   ,			padding_value=1.0 )

         UpperCAmelCase_           =   SamProcessor.from_pretrained(self.tmpdirname   ,			do_normalize=__lowercase   ,			padding_value=1.0 )

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



    def 					_lowercase						(self    :    str ):
         UpperCAmelCase_           =   self.get_image_processor()

         UpperCAmelCase_           =   SamProcessor(image_processor=__lowercase )

         UpperCAmelCase_           =   self.prepare_image_inputs()

         UpperCAmelCase_           =   image_processor(__lowercase   ,			return_tensors="np" )
         UpperCAmelCase_           =   processor(images=__lowercase   ,			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    :    Dict ):
         UpperCAmelCase_           =   self.get_image_processor()

         UpperCAmelCase_           =   SamProcessor(image_processor=__lowercase )
         UpperCAmelCase_           =   [torch.ones((1, 3, 5, 5) )]

         UpperCAmelCase_           =   [[1764, 2646]]

         UpperCAmelCase_           =   [[683, 1024]]
         UpperCAmelCase_           =   processor.post_process_masks(__lowercase   ,			__lowercase   ,			__lowercase )
         self.assertEqual(masks[0].shape   ,			(1, 3, 1764, 2646) )

         UpperCAmelCase_           =   processor.post_process_masks(
             __lowercase   ,			torch.tensor(__lowercase )   ,			torch.tensor(__lowercase ) )
         self.assertEqual(masks[0].shape   ,			(1, 3, 1764, 2646) )

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

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

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




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



    def 					_lowercase						(self    :    List[str] ):
         UpperCAmelCase_           =   tempfile.mkdtemp()
         UpperCAmelCase_           =   SamImageProcessor()
         UpperCAmelCase_           =   SamProcessor(__lowercase )
         processor.save_pretrained(self.tmpdirname )



    def 					_lowercase						(self    :    Optional[Any]   ,			**__a    :    int ):
         return AutoProcessor.from_pretrained(self.tmpdirname   ,			**__lowercase ).image_processor



    def 					_lowercase						(self    :    Dict ):
         shutil.rmtree(self.tmpdirname )



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_           =   [np.random.randint(255   ,			size=(3, 30, 400)   ,			dtype=np.uinta )]

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

         return image_inputs



    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   SamProcessor(image_processor=self.get_image_processor() )
         processor.save_pretrained(self.tmpdirname )

         UpperCAmelCase_           =   self.get_image_processor(do_normalize=__lowercase   ,			padding_value=1.0 )

         UpperCAmelCase_           =   SamProcessor.from_pretrained(self.tmpdirname   ,			do_normalize=__lowercase   ,			padding_value=1.0 )

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



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_           =   self.get_image_processor()

         UpperCAmelCase_           =   SamProcessor(image_processor=__lowercase )

         UpperCAmelCase_           =   self.prepare_image_inputs()

         UpperCAmelCase_           =   image_processor(__lowercase   ,			return_tensors="np" )
         UpperCAmelCase_           =   processor(images=__lowercase   ,			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    :    Optional[Any] ):
         UpperCAmelCase_           =   self.get_image_processor()

         UpperCAmelCase_           =   SamProcessor(image_processor=__lowercase )
         UpperCAmelCase_           =   [tf.ones((1, 3, 5, 5) )]

         UpperCAmelCase_           =   [[1764, 2646]]

         UpperCAmelCase_           =   [[683, 1024]]
         UpperCAmelCase_           =   processor.post_process_masks(__lowercase   ,			__lowercase   ,			__lowercase   ,			return_tensors="tf" )
         self.assertEqual(masks[0].shape   ,			(1, 3, 1764, 2646) )

         UpperCAmelCase_           =   processor.post_process_masks(
             __lowercase   ,			tf.convert_to_tensor(__lowercase )   ,			tf.convert_to_tensor(__lowercase )   ,			return_tensors="tf"   ,			)
         self.assertEqual(masks[0].shape   ,			(1, 3, 1764, 2646) )

         # should also work with np
         UpperCAmelCase_           =   [np.ones((1, 3, 5, 5) )]
         UpperCAmelCase_           =   processor.post_process_masks(
             __lowercase   ,			np.array(__lowercase )   ,			np.array(__lowercase )   ,			return_tensors="tf" )

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

         UpperCAmelCase_           =   [[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError ):
              UpperCAmelCase_           =   processor.post_process_masks(
                  __lowercase   ,			np.array(__lowercase )   ,			np.array(__lowercase )   ,			return_tensors="tf" )




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



    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   tempfile.mkdtemp()
         UpperCAmelCase_           =   SamImageProcessor()
         UpperCAmelCase_           =   SamProcessor(__lowercase )
         processor.save_pretrained(self.tmpdirname )



    def 					_lowercase						(self    :    Dict   ,			**__a    :    Optional[int] ):
         return AutoProcessor.from_pretrained(self.tmpdirname   ,			**__lowercase ).image_processor



    def 					_lowercase						(self    :    int ):
         shutil.rmtree(self.tmpdirname )



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_           =   [np.random.randint(255   ,			size=(3, 30, 400)   ,			dtype=np.uinta )]

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

         return image_inputs



    @is_pt_tf_cross_test
    def 					_lowercase						(self    :    Union[str, Any] ):
         UpperCAmelCase_           =   self.get_image_processor()

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

         UpperCAmelCase_           =   [[1764, 2646]]

         UpperCAmelCase_           =   [[683, 1024]]
         UpperCAmelCase_           =   processor.post_process_masks(
             __lowercase   ,			__lowercase   ,			__lowercase   ,			return_tensors="tf" )
         UpperCAmelCase_           =   processor.post_process_masks(
             __lowercase   ,			__lowercase   ,			__lowercase   ,			return_tensors="pt" )

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






    @is_pt_tf_cross_test
    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   self.get_image_processor()

         UpperCAmelCase_           =   SamProcessor(image_processor=__lowercase )

         UpperCAmelCase_           =   self.prepare_image_inputs()

         UpperCAmelCase_           =   image_processor(__lowercase   ,			return_tensors="pt" )['''pixel_values'''].numpy()
         UpperCAmelCase_           =   processor(images=__lowercase   ,			return_tensors="pt" )['''pixel_values'''].numpy()

         UpperCAmelCase_           =   image_processor(__lowercase   ,			return_tensors="tf" )['''pixel_values'''].numpy()
         UpperCAmelCase_           =   processor(images=__lowercase   ,			return_tensors="tf" )['''pixel_values'''].numpy()

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








import inspect
import unittest







class  snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						def   lowercase_       ( self							)    ->			int:
										try:
														import diffusers  # noqa: F401
										except ImportError:
														assert False




						def   lowercase_       ( self							)    ->			List[str]:
										import diffusers
										from diffusers.dependency_versions_table import deps

										lowerCAmelCase_       :					Any			 = inspect.getmembers(__lowercase      ,  inspect.isclass							)

										for cls_name, cls_module in all_classes:
														if "dummy_" in cls_module.__module__:
																		for backend in cls_module._backends:
																						if backend == "k_diffusion":
																										lowerCAmelCase_       :					Optional[int]			 = '''k-diffusion'''
																						elif backend == "invisible_watermark":
																										lowerCAmelCase_       :					Dict			 = '''invisible-watermark'''
																						assert backend in deps, f"""{backend} is not in the deps table!"""