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   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							tempfile.mkdtemp()
										_lowerCAmelCase							=							SamImageProcessor()
										_lowerCAmelCase							=							SamProcessor(lowercase__						)
										processor.save_pretrained(self.tmpdirname						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]  ,  **lowercase__	:	List[str]						):
										return AutoProcessor.from_pretrained(self.tmpdirname  ,  **lowercase__						).image_processor





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										shutil.rmtree(self.tmpdirname						)





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

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

										return image_inputs





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							SamProcessor(image_processor=self.get_image_processor()						)
										processor.save_pretrained(self.tmpdirname						)

										_lowerCAmelCase							=							self.get_image_processor(do_normalize=lowercase__  ,  padding_value=1.0						)

										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							self.get_image_processor()

										_lowerCAmelCase							=							SamProcessor(image_processor=lowercase__						)

										_lowerCAmelCase							=							self.prepare_image_inputs()

										_lowerCAmelCase							=							image_processor(lowercase__  ,  return_tensors='np'						)
										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										_lowerCAmelCase							=							self.get_image_processor()

										_lowerCAmelCase							=							SamProcessor(image_processor=lowercase__						)
										_lowerCAmelCase							=							[torch.ones((1, 3, 5, 5)						)]

										_lowerCAmelCase							=							[[17_64, 26_46]]

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

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

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

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

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







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





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							tempfile.mkdtemp()
										_lowerCAmelCase							=							SamImageProcessor()
										_lowerCAmelCase							=							SamProcessor(lowercase__						)
										processor.save_pretrained(self.tmpdirname						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any  ,  **lowercase__	:	int						):
										return AutoProcessor.from_pretrained(self.tmpdirname  ,  **lowercase__						).image_processor





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										shutil.rmtree(self.tmpdirname						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							[np.random.randint(2_55  ,  size=(3, 30, 4_00)  ,  dtype=np.uinta						)]

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

										return image_inputs





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_lowerCAmelCase							=							SamProcessor(image_processor=self.get_image_processor()						)
										processor.save_pretrained(self.tmpdirname						)

										_lowerCAmelCase							=							self.get_image_processor(do_normalize=lowercase__  ,  padding_value=1.0						)

										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase							=							self.get_image_processor()

										_lowerCAmelCase							=							SamProcessor(image_processor=lowercase__						)

										_lowerCAmelCase							=							self.prepare_image_inputs()

										_lowerCAmelCase							=							image_processor(lowercase__  ,  return_tensors='np'						)
										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							self.get_image_processor()

										_lowerCAmelCase							=							SamProcessor(image_processor=lowercase__						)
										_lowerCAmelCase							=							[tf.ones((1, 3, 5, 5)						)]

										_lowerCAmelCase							=							[[17_64, 26_46]]

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

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

										# should also work with np
										_lowerCAmelCase							=							[np.ones((1, 3, 5, 5)						)]
										_lowerCAmelCase							=							processor.post_process_masks(
										    lowercase__  ,  np.array(lowercase__						)  ,  np.array(lowercase__						)  ,  return_tensors='tf'						)

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

										_lowerCAmelCase							=							[[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError						):
																	_lowerCAmelCase							=							processor.post_process_masks(
																	    lowercase__  ,  np.array(lowercase__						)  ,  np.array(lowercase__						)  ,  return_tensors='tf'						)







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





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_lowerCAmelCase							=							tempfile.mkdtemp()
										_lowerCAmelCase							=							SamImageProcessor()
										_lowerCAmelCase							=							SamProcessor(lowercase__						)
										processor.save_pretrained(self.tmpdirname						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]  ,  **lowercase__	:	Tuple						):
										return AutoProcessor.from_pretrained(self.tmpdirname  ,  **lowercase__						).image_processor





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										shutil.rmtree(self.tmpdirname						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										_lowerCAmelCase							=							[np.random.randint(2_55  ,  size=(3, 30, 4_00)  ,  dtype=np.uinta						)]

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

										return image_inputs





			@is_pt_tf_cross_test
			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										_lowerCAmelCase							=							self.get_image_processor()

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

										_lowerCAmelCase							=							[[17_64, 26_46]]

										_lowerCAmelCase							=							[[6_83, 10_24]]
										_lowerCAmelCase							=							processor.post_process_masks(
										    lowercase__  ,  lowercase__  ,  lowercase__  ,  return_tensors='tf'						)
										_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							self.get_image_processor()

										_lowerCAmelCase							=							SamProcessor(image_processor=lowercase__						)

										_lowerCAmelCase							=							self.prepare_image_inputs()

										_lowerCAmelCase							=							image_processor(lowercase__  ,  return_tensors='pt'						)['pixel_values'].numpy()
										_lowerCAmelCase							=							processor(images=lowercase__  ,  return_tensors='pt'						)['pixel_values'].numpy()

										_lowerCAmelCase							=							image_processor(lowercase__  ,  return_tensors='tf'						)['pixel_values'].numpy()
										_lowerCAmelCase							=							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__						)						)







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


_lowercase:	Any       			=		{'''configuration_swin''': ['''SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SwinConfig''', '''SwinOnnxConfig''']}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowercase:	str       			=		[
				    '''SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''SwinForImageClassification''',
				    '''SwinForMaskedImageModeling''',
				    '''SwinModel''',
				    '''SwinPreTrainedModel''',
				    '''SwinBackbone''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowercase:	str       			=		[
				    '''TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFSwinForImageClassification''',
				    '''TFSwinForMaskedImageModeling''',
				    '''TFSwinModel''',
				    '''TFSwinPreTrainedModel''',
				]

if TYPE_CHECKING:
				from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_swin import (
								    SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SwinBackbone,
								    SwinForImageClassification,
								    SwinForMaskedImageModeling,
								    SwinModel,
								    SwinPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_swin import (
								    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFSwinForImageClassification,
								    TFSwinForMaskedImageModeling,
								    TFSwinModel,
								    TFSwinPreTrainedModel,
								)

else:
				import sys

				_lowercase:	Tuple       			=		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






