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










from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_A     =			logging.get_logger(__name__)

_A     =			{
    '''google/vit-base-patch16-224''': '''https://huggingface.co/vit-base-patch16-224/resolve/main/config.json''',
    # See all ViT models at https://huggingface.co/models?filter=vit
}


class  A		(  __UpperCAmelCase     ):
		__snake_case									=   'vit'



		def __init__(     self,	UpperCamelCase__=768,	UpperCamelCase__=12,	UpperCamelCase__=12,	UpperCamelCase__=3072,	UpperCamelCase__="gelu",	UpperCamelCase__=0.0,	UpperCamelCase__=0.0,	UpperCamelCase__=0.02,	UpperCamelCase__=1E-12,	UpperCamelCase__=224,	UpperCamelCase__=16,	UpperCamelCase__=3,	UpperCamelCase__=True,	UpperCamelCase__=16,	**UpperCamelCase__,	):







								"""simple docstring"""



								super().__init__(**UpperCamelCase__   )

								lowerCAmelCase_      			=  hidden_size
								lowerCAmelCase_      			=  num_hidden_layers
								lowerCAmelCase_      			=  num_attention_heads
								lowerCAmelCase_      			=  intermediate_size
								lowerCAmelCase_      			=  hidden_act
								lowerCAmelCase_      			=  hidden_dropout_prob
								lowerCAmelCase_      			=  attention_probs_dropout_prob
								lowerCAmelCase_      			=  initializer_range
								lowerCAmelCase_      			=  layer_norm_eps
								lowerCAmelCase_      			=  image_size
								lowerCAmelCase_      			=  patch_size
								lowerCAmelCase_      			=  num_channels
								lowerCAmelCase_      			=  qkv_bias
								lowerCAmelCase_      			=  encoder_stride







class  A		(  __UpperCAmelCase     ):
		__snake_case									=   version.parse('1.11'     )



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return OrderedDict(
								    [
								        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
								    ]   )







		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return 1E-4




