"""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  ):
       _lowerCAmelCase						:       str  			=     tempfile.mkdtemp()
       _lowerCAmelCase						:       str  			=     SamImageProcessor()
       _lowerCAmelCase						:       str  			=     SamProcessor(a__  )
       processor.save_pretrained(self.tmpdirname  )






     def     __A  (		self				,     **a__  ):
       return AutoProcessor.from_pretrained(self.tmpdirname				,     **a__  ).image_processor






     def     __A  (		self  ):
       shutil.rmtree(self.tmpdirname  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     [np.random.randint(255				,     size=(3, 30, 400)				,     dtype=np.uinta  )]

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

       return image_inputs






     def     __A  (		self  ):
       _lowerCAmelCase						:       str  			=     SamProcessor(image_processor=self.get_image_processor()  )
       processor.save_pretrained(self.tmpdirname  )

       _lowerCAmelCase						:       Optional[int]  			=     self.get_image_processor(do_normalize=a__				,     padding_value=1.0  )

       _lowerCAmelCase						:       Dict  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       Optional[int]  			=     self.get_image_processor()

       _lowerCAmelCase						:       List[str]  			=     SamProcessor(image_processor=a__  )

       _lowerCAmelCase						:       str  			=     self.prepare_image_inputs()

       _lowerCAmelCase						:       Any  			=     image_processor(a__				,     return_tensors="""np"""  )
       _lowerCAmelCase						:       Dict  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.get_image_processor()

       _lowerCAmelCase						:       Dict  			=     SamProcessor(image_processor=a__  )
       _lowerCAmelCase						:       Union[str, Any]  			=     [torch.ones((1, 3, 5, 5)  )]

       _lowerCAmelCase						:       Optional[int]  			=     [[1764, 2646]]

       _lowerCAmelCase						:       Optional[Any]  			=     [[683, 1024]]
       _lowerCAmelCase						:       Union[str, Any]  			=     processor.post_process_masks(a__				,     a__				,     a__  )
       self.assertEqual(masks[0].shape				,     (1, 3, 1764, 2646)  )

       _lowerCAmelCase						:       int  			=     processor.post_process_masks(
           a__				,     torch.tensor(a__  )				,     torch.tensor(a__  )  )
       self.assertEqual(masks[0].shape				,     (1, 3, 1764, 2646)  )

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

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

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







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






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     tempfile.mkdtemp()
       _lowerCAmelCase						:       Any  			=     SamImageProcessor()
       _lowerCAmelCase						:       Dict  			=     SamProcessor(a__  )
       processor.save_pretrained(self.tmpdirname  )






     def     __A  (		self				,     **a__  ):
       return AutoProcessor.from_pretrained(self.tmpdirname				,     **a__  ).image_processor






     def     __A  (		self  ):
       shutil.rmtree(self.tmpdirname  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     [np.random.randint(255				,     size=(3, 30, 400)				,     dtype=np.uinta  )]

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

       return image_inputs






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

       _lowerCAmelCase						:       str  			=     self.get_image_processor(do_normalize=a__				,     padding_value=1.0  )

       _lowerCAmelCase						:       Union[str, Any]  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       int  			=     self.get_image_processor()

       _lowerCAmelCase						:       List[Any]  			=     SamProcessor(image_processor=a__  )

       _lowerCAmelCase						:       int  			=     self.prepare_image_inputs()

       _lowerCAmelCase						:       Optional[int]  			=     image_processor(a__				,     return_tensors="""np"""  )
       _lowerCAmelCase						:       str  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.get_image_processor()

       _lowerCAmelCase						:       List[Any]  			=     SamProcessor(image_processor=a__  )
       _lowerCAmelCase						:       Union[str, Any]  			=     [tf.ones((1, 3, 5, 5)  )]

       _lowerCAmelCase						:       Tuple  			=     [[1764, 2646]]

       _lowerCAmelCase						:       int  			=     [[683, 1024]]
       _lowerCAmelCase						:       List[str]  			=     processor.post_process_masks(a__				,     a__				,     a__				,     return_tensors="""tf"""  )
       self.assertEqual(masks[0].shape				,     (1, 3, 1764, 2646)  )

       _lowerCAmelCase						:       Optional[Any]  			=     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, 1764, 2646)  )

       # should also work with np
       _lowerCAmelCase						:       Dict  			=     [np.ones((1, 3, 5, 5)  )]
       _lowerCAmelCase						:       List[Any]  			=     processor.post_process_masks(
           a__				,     np.array(a__  )				,     np.array(a__  )				,     return_tensors="""tf"""  )

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

       _lowerCAmelCase						:       str  			=     [[1, 0], [0, 1]]
       with self.assertRaises(tf.errors.InvalidArgumentError  ):
         _lowerCAmelCase						:       Dict  			=     processor.post_process_masks(
             a__				,     np.array(a__  )				,     np.array(a__  )				,     return_tensors="""tf"""  )







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






     def     __A  (		self  ):
       _lowerCAmelCase						:       Dict  			=     tempfile.mkdtemp()
       _lowerCAmelCase						:       int  			=     SamImageProcessor()
       _lowerCAmelCase						:       List[str]  			=     SamProcessor(a__  )
       processor.save_pretrained(self.tmpdirname  )






     def     __A  (		self				,     **a__  ):
       return AutoProcessor.from_pretrained(self.tmpdirname				,     **a__  ).image_processor






     def     __A  (		self  ):
       shutil.rmtree(self.tmpdirname  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       str  			=     [np.random.randint(255				,     size=(3, 30, 400)				,     dtype=np.uinta  )]

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

       return image_inputs






     @is_pt_tf_cross_test
     def     __A  (		self  ):
       _lowerCAmelCase						:       Tuple  			=     self.get_image_processor()

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

       _lowerCAmelCase						:       Optional[int]  			=     [[1764, 2646]]

       _lowerCAmelCase						:       Union[str, Any]  			=     [[683, 1024]]
       _lowerCAmelCase						:       int  			=     processor.post_process_masks(
           a__				,     a__				,     a__				,     return_tensors="""tf"""  )
       _lowerCAmelCase						:       Optional[Any]  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.get_image_processor()

       _lowerCAmelCase						:       Optional[int]  			=     SamProcessor(image_processor=a__  )

       _lowerCAmelCase						:       Any  			=     self.prepare_image_inputs()

       _lowerCAmelCase						:       List[Any]  			=     image_processor(a__				,     return_tensors="""pt"""  )["""pixel_values"""].numpy()
       _lowerCAmelCase						:       List[str]  			=     processor(images=a__				,     return_tensors="""pt"""  )["""pixel_values"""].numpy()

       _lowerCAmelCase						:       int  			=     image_processor(a__				,     return_tensors="""tf"""  )["""pixel_values"""].numpy()
       _lowerCAmelCase						:       Tuple  			=     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"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a      :		List[Any]           =   logging.get_logger(__name__)

_a      :		Any           =   {
    'microsoft/swinv2-tiny-patch4-window8-256': (
        'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json'
    ),
}
class 						__A							( SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:			int							=		"swinv2"

     _UpperCamelCase							:			List[str]							=		{
         "num_attention_heads": "num_heads",
         "num_hidden_layers": "num_layers",
     }






     def __init__(		self				,     a__=224				,     a__=4				,     a__=3				,     a__=96				,     a__=[2, 2, 6, 2]				,     a__=[3, 6, 12, 24]				,     a__=7				,     a__=4.0				,     a__=True				,     a__=0.0				,     a__=0.0				,     a__=0.1				,     a__="gelu"				,     a__=False				,     a__=0.0_2				,     a__=1e-5				,     a__=32				,     **a__				,     ):
       super().__init__(**a__  )

       _lowerCAmelCase						:       int  			=     image_size
       _lowerCAmelCase						:       Optional[Any]  			=     patch_size
       _lowerCAmelCase						:       Any  			=     num_channels
       _lowerCAmelCase						:       List[Any]  			=     embed_dim
       _lowerCAmelCase						:       Optional[int]  			=     depths
       _lowerCAmelCase						:       List[Any]  			=     len(a__  )
       _lowerCAmelCase						:       Any  			=     num_heads
       _lowerCAmelCase						:       Tuple  			=     window_size
       _lowerCAmelCase						:       Tuple  			=     mlp_ratio
       _lowerCAmelCase						:       Any  			=     qkv_bias
       _lowerCAmelCase						:       Optional[int]  			=     hidden_dropout_prob
       _lowerCAmelCase						:       Tuple  			=     attention_probs_dropout_prob
       _lowerCAmelCase						:       str  			=     drop_path_rate
       _lowerCAmelCase						:       List[str]  			=     hidden_act
       _lowerCAmelCase						:       List[str]  			=     use_absolute_embeddings
       _lowerCAmelCase						:       Optional[int]  			=     layer_norm_eps
       _lowerCAmelCase						:       List[Any]  			=     initializer_range
       _lowerCAmelCase						:       Any  			=     encoder_stride
       # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       _lowerCAmelCase						:       Tuple  			=     int(embed_dim * 2 ** (len(a__  ) - 1)  )
       _lowerCAmelCase						:       Tuple  			=     (0, 0, 0, 0)




