




'''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     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=tempfile.mkdtemp()
										__magic_name__	:	Optional[int]								=SamImageProcessor()
										__magic_name__	:	int								=SamProcessor(__snake_case )
										processor.save_pretrained(self.tmpdirname )

						def     A__						(		self   :Optional[int]    , **__snake_case   :Any ):
										'''simple docstring'''

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

						def     A__						(		self   :str ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	int								=[np.random.randint(2_55    , size=(3, 30, 4_00)    , dtype=np.uinta )]

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

										return image_inputs

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

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

										__magic_name__	:	Dict								=self.get_image_processor(do_normalize=__snake_case    , padding_value=1.0 )

										__magic_name__	:	Any								=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     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=self.get_image_processor()

										__magic_name__	:	str								=SamProcessor(image_processor=__snake_case )

										__magic_name__	:	Union[str, Any]								=self.prepare_image_inputs()

										__magic_name__	:	List[str]								=image_processor(__snake_case    , return_tensors="""np""" )
										__magic_name__	:	str								=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     A__						(		self   :int ):
										'''simple docstring'''

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

										__magic_name__	:	Union[str, Any]								=SamProcessor(image_processor=__snake_case )
										__magic_name__	:	List[str]								=[torch.ones((1, 3, 5, 5) )]

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

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

										__magic_name__	:	int								=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
										__magic_name__	:	Tuple								=[np.ones((1, 3, 5, 5) )]
										__magic_name__	:	Tuple								=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) )

										__magic_name__	:	Union[str, Any]								=[[1, 0], [0, 1]]
										with self.assertRaises(__snake_case ):
														__magic_name__	:	Dict								=processor.post_process_masks(__snake_case    , np.array(__snake_case )    , np.array(__snake_case ) )




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

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

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

						def     A__						(		self   :Any    , **__snake_case   :Optional[int] ):
										'''simple docstring'''

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=[np.random.randint(2_55    , size=(3, 30, 4_00)    , dtype=np.uinta )]

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

										return image_inputs

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=SamProcessor(image_processor=self.get_image_processor() )
										processor.save_pretrained(self.tmpdirname )

										__magic_name__	:	Optional[int]								=self.get_image_processor(do_normalize=__snake_case    , padding_value=1.0 )

										__magic_name__	:	List[Any]								=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     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=self.get_image_processor()

										__magic_name__	:	Union[str, Any]								=SamProcessor(image_processor=__snake_case )

										__magic_name__	:	Optional[Any]								=self.prepare_image_inputs()

										__magic_name__	:	Union[str, Any]								=image_processor(__snake_case    , return_tensors="""np""" )
										__magic_name__	:	Dict								=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     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=self.get_image_processor()

										__magic_name__	:	List[str]								=SamProcessor(image_processor=__snake_case )
										__magic_name__	:	int								=[tf.ones((1, 3, 5, 5) )]

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

										__magic_name__	:	str								=[[6_83, 10_24]]
										__magic_name__	:	List[str]								=processor.post_process_masks(__snake_case    , __snake_case    , __snake_case    , return_tensors="""tf""" )
										self.assertEqual(masks[0].shape    , (1, 3, 17_64, 26_46) )

										__magic_name__	:	int								=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
										__magic_name__	:	Any								=[np.ones((1, 3, 5, 5) )]
										__magic_name__	:	List[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, 17_64, 26_46) )

										__magic_name__	:	Tuple								=[[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError ):
														__magic_name__	:	Optional[Any]								=processor.post_process_masks(
														    __snake_case    , np.array(__snake_case )    , np.array(__snake_case )    , return_tensors="""tf""" )




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

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	Dict								=tempfile.mkdtemp()
										__magic_name__	:	Union[str, Any]								=SamImageProcessor()
										__magic_name__	:	Optional[Any]								=SamProcessor(__snake_case )
										processor.save_pretrained(self.tmpdirname )

						def     A__						(		self   :Optional[Any]    , **__snake_case   :List[Any] ):
										'''simple docstring'''

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

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=[np.random.randint(2_55    , size=(3, 30, 4_00)    , dtype=np.uinta )]

										__magic_name__	:	List[str]								=[Image.fromarray(np.moveaxis(__snake_case    , 0    , -1 ) ) for x in image_inputs]

										return image_inputs

						@is_pt_tf_cross_test
						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

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

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

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

										__magic_name__	:	Optional[Any]								=[[6_83, 10_24]]
										__magic_name__	:	int								=processor.post_process_masks(
										    __snake_case    , __snake_case    , __snake_case    , return_tensors="""tf""" )
										__magic_name__	:	Tuple								=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     A__						(		self   :int ):
										'''simple docstring'''

										__magic_name__	:	str								=self.get_image_processor()

										__magic_name__	:	List[Any]								=SamProcessor(image_processor=__snake_case )

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

										__magic_name__	:	Optional[int]								=image_processor(__snake_case    , return_tensors="""pt""" )["""pixel_values"""].numpy()
										__magic_name__	:	Union[str, Any]								=processor(images=__snake_case    , return_tensors="""pt""" )["""pixel_values"""].numpy()

										__magic_name__	:	Union[str, Any]								=image_processor(__snake_case    , return_tensors="""tf""" )["""pixel_values"""].numpy()
										__magic_name__	:	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 ) )










import inspect
import warnings
from typing import Any, Dict, Optional, Union

from packaging import version




def 		lowerCAmelCase_   (	*lowerCamelCase	,     lowerCamelCase = None	,     lowerCamelCase=True	,     lowerCamelCase=2   ):
				from .. import __version__

				__magic_name__	:	Optional[int]								=take_from
				__magic_name__	:	Tuple								=()
				if not isinstance(args[0]	,     lowerCamelCase   ):
								__magic_name__	:	List[Any]								=(args,)

				for attribute, version_name, message in args:
								if version.parse(version.parse(lowerCamelCase   ).base_version   ) >= version.parse(lowerCamelCase   ):
												raise ValueError(
												    F"The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers'"
												    F" version {__version__} is >= {version_name}"   )

								__magic_name__	:	List[str]								=None
								if isinstance(lowerCamelCase	,     lowerCamelCase   ) and attribute in deprecated_kwargs:
												values += (deprecated_kwargs.pop(lowerCamelCase   ),)
												__magic_name__	:	List[str]								=F"The `{attribute}` argument is deprecated and will be removed in version {version_name}."
								elif hasattr(lowerCamelCase	,     lowerCamelCase   ):
												values += (getattr(lowerCamelCase	,     lowerCamelCase   ),)
												__magic_name__	:	List[str]								=F"The `{attribute}` attribute is deprecated and will be removed in version {version_name}."
								elif deprecated_kwargs is None:
												__magic_name__	:	List[str]								=F"`{attribute}` is deprecated and will be removed in version {version_name}."

								if warning is not None:
												__magic_name__	:	List[Any]								=warning + """ """ if standard_warn else """"""
												warnings.warn(warning + message	,     lowerCamelCase	,     stacklevel=lowerCamelCase   )

				if isinstance(lowerCamelCase	,     lowerCamelCase   ) and len(lowerCamelCase   ) > 0:
								__magic_name__	:	List[Any]								=inspect.getouterframes(inspect.currentframe()   )[1]
								__magic_name__	:	Optional[int]								=call_frame.filename
								__magic_name__	:	Tuple								=call_frame.lineno
								__magic_name__	:	str								=call_frame.function
								__magic_name__				, __magic_name__	:	Optional[int]								=next(iter(deprecated_kwargs.items()   )   )
								raise TypeError(F"{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`"   )

				if len(lowerCamelCase   ) == 0:
								return
				elif len(lowerCamelCase   ) == 1:
								return values[0]
				return values




