





"""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 ):


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	tempfile.mkdtemp()
								UpperCamelCase   =	SamImageProcessor()
								UpperCamelCase   =	SamProcessor(__a		)
								processor.save_pretrained(self.tmpdirname		)


						def     snake_case_   (self   ,							**__a		) ->   Optional[int]:
								return AutoProcessor.from_pretrained(self.tmpdirname   ,							**__a		).image_processor


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


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

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

								return image_inputs


						def     snake_case_   (self		) ->   Optional[Any]:
								UpperCamelCase   =	SamProcessor(image_processor=self.get_image_processor()		)
								processor.save_pretrained(self.tmpdirname		)

								UpperCamelCase   =	self.get_image_processor(do_normalize=__a   ,							padding_value=1.0		)

								UpperCamelCase   =	SamProcessor.from_pretrained(self.tmpdirname   ,							do_normalize=__a   ,							padding_value=1.0		)

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


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	self.get_image_processor()

								UpperCamelCase   =	SamProcessor(image_processor=__a		)

								UpperCamelCase   =	self.prepare_image_inputs()

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

								UpperCamelCase   =	SamProcessor(image_processor=__a		)
								UpperCamelCase   =	[torch.ones((1, 3, 5, 5)		)]

								UpperCamelCase   =	[[17_64, 26_46]]

								UpperCamelCase   =	[[6_83, 10_24]]
								UpperCamelCase   =	processor.post_process_masks(__a   ,							__a   ,							__a		)
								self.assertEqual(masks[0].shape   ,							(1, 3, 17_64, 26_46)		)

								UpperCamelCase   =	processor.post_process_masks(
								    __a   ,							torch.tensor(__a		)   ,							torch.tensor(__a		)		)
								self.assertEqual(masks[0].shape   ,							(1, 3, 17_64, 26_46)		)

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

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

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





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


						def     snake_case_   (self		) ->   int:
								UpperCamelCase   =	tempfile.mkdtemp()
								UpperCamelCase   =	SamImageProcessor()
								UpperCamelCase   =	SamProcessor(__a		)
								processor.save_pretrained(self.tmpdirname		)


						def     snake_case_   (self   ,							**__a		) ->   Optional[int]:
								return AutoProcessor.from_pretrained(self.tmpdirname   ,							**__a		).image_processor


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


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

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

								return image_inputs


						def     snake_case_   (self		) ->   Optional[Any]:
								UpperCamelCase   =	SamProcessor(image_processor=self.get_image_processor()		)
								processor.save_pretrained(self.tmpdirname		)

								UpperCamelCase   =	self.get_image_processor(do_normalize=__a   ,							padding_value=1.0		)

								UpperCamelCase   =	SamProcessor.from_pretrained(self.tmpdirname   ,							do_normalize=__a   ,							padding_value=1.0		)

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


						def     snake_case_   (self		) ->   Tuple:
								UpperCamelCase   =	self.get_image_processor()

								UpperCamelCase   =	SamProcessor(image_processor=__a		)

								UpperCamelCase   =	self.prepare_image_inputs()

								UpperCamelCase   =	image_processor(__a   ,							return_tensors="np"		)
								UpperCamelCase   =	processor(images=__a   ,							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		) ->   Tuple:
								UpperCamelCase   =	self.get_image_processor()

								UpperCamelCase   =	SamProcessor(image_processor=__a		)
								UpperCamelCase   =	[tf.ones((1, 3, 5, 5)		)]

								UpperCamelCase   =	[[17_64, 26_46]]

								UpperCamelCase   =	[[6_83, 10_24]]
								UpperCamelCase   =	processor.post_process_masks(__a   ,							__a   ,							__a   ,							return_tensors="tf"		)
								self.assertEqual(masks[0].shape   ,							(1, 3, 17_64, 26_46)		)

								UpperCamelCase   =	processor.post_process_masks(
								    __a   ,							tf.convert_to_tensor(__a		)   ,							tf.convert_to_tensor(__a		)   ,							return_tensors="tf"   ,							)
								self.assertEqual(masks[0].shape   ,							(1, 3, 17_64, 26_46)		)

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

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

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





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


						def     snake_case_   (self		) ->   List[Any]:
								UpperCamelCase   =	tempfile.mkdtemp()
								UpperCamelCase   =	SamImageProcessor()
								UpperCamelCase   =	SamProcessor(__a		)
								processor.save_pretrained(self.tmpdirname		)


						def     snake_case_   (self   ,							**__a		) ->   List[str]:
								return AutoProcessor.from_pretrained(self.tmpdirname   ,							**__a		).image_processor


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


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

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

								return image_inputs


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

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

								UpperCamelCase   =	[[17_64, 26_46]]

								UpperCamelCase   =	[[6_83, 10_24]]
								UpperCamelCase   =	processor.post_process_masks(
								    __a   ,							__a   ,							__a   ,							return_tensors="tf"		)
								UpperCamelCase   =	processor.post_process_masks(
								    __a   ,							__a   ,							__a   ,							return_tensors="pt"		)

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




						@is_pt_tf_cross_test
						def     snake_case_   (self		) ->   Tuple:
								UpperCamelCase   =	self.get_image_processor()

								UpperCamelCase   =	SamProcessor(image_processor=__a		)

								UpperCamelCase   =	self.prepare_image_inputs()

								UpperCamelCase   =	image_processor(__a   ,							return_tensors="pt"		)["pixel_values"].numpy()
								UpperCamelCase   =	processor(images=__a   ,							return_tensors="pt"		)["pixel_values"].numpy()

								UpperCamelCase   =	image_processor(__a   ,							return_tensors="tf"		)["pixel_values"].numpy()
								UpperCamelCase   =	processor(images=__a   ,							return_tensors="tf"		)["pixel_values"].numpy()

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













"""simple docstring"""





import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


lowerCAmelCase__     			=			HUGGINGFACE_HUB_CACHE


lowerCAmelCase__     			=			'''config.json'''
lowerCAmelCase__     			=			'''diffusion_pytorch_model.bin'''
lowerCAmelCase__     			=			'''diffusion_flax_model.msgpack'''
lowerCAmelCase__     			=			'''model.onnx'''
lowerCAmelCase__     			=			'''diffusion_pytorch_model.safetensors'''
lowerCAmelCase__     			=			'''weights.pb'''
lowerCAmelCase__     			=			'''https://huggingface.co'''
lowerCAmelCase__     			=			default_cache_path
lowerCAmelCase__     			=			'''diffusers_modules'''
lowerCAmelCase__     			=			os.getenv('''HF_MODULES_CACHE''', os.path.join(hf_cache_home, '''modules'''))
lowerCAmelCase__     			=			['''fp16''', '''non-ema''']
lowerCAmelCase__     			=			'''.self_attn'''






