






"""simple docstring"""
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
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCAmelCase         =							logging.get_logger(__name__)

_lowerCAmelCase         =							{
    "microsoft/swin-tiny-patch4-window7-224": (
        "https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json"
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}



class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE       ,       _SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				Optional[Any]    =				"""swin"""

	_A       :				Union[str, Any]    =				{
	    """num_attention_heads""": """num_heads""",
	    """num_hidden_layers""": """num_layers""",
	}





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

		A_		:						Optional[int]	    =   image_size
		A_		:						Union[str, Any]	    =   patch_size
		A_		:						Tuple	    =   num_channels
		A_		:						List[Any]	    =   embed_dim
		A_		:						List[Any]	    =   depths
		A_		:						Any	    =   len(lowerCAmelCase_							)
		A_		:						List[Any]	    =   num_heads
		A_		:						List[str]	    =   window_size
		A_		:						str	    =   mlp_ratio
		A_		:						List[str]	    =   qkv_bias
		A_		:						Any	    =   hidden_dropout_prob
		A_		:						Optional[int]	    =   attention_probs_dropout_prob
		A_		:						Optional[Any]	    =   drop_path_rate
		A_		:						List[str]	    =   hidden_act
		A_		:						Any	    =   use_absolute_embeddings
		A_		:						Union[str, Any]	    =   layer_norm_eps
		A_		:						int	    =   initializer_range
		A_		:						int	    =   encoder_stride
		# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
		# this indicates the channel dimension after the last stage of the model
		A_		:						List[str]	    =   int(embed_dim * 2 ** (len(lowerCAmelCase_							) - 1)							)
		A_		:						str	    =   ["""stem"""] + [f"""stage{idx}""" for idx in range(1     ,     len(lowerCAmelCase_							) + 1							)]
		A_    ,     A_		:						Dict	    =   get_aligned_output_features_output_indices(
		    out_features=lowerCAmelCase_     ,     out_indices=lowerCAmelCase_     ,     stage_names=self.stage_names							)






class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				List[Any]    =				version.parse("""1.11"""						)





	@property
	def    lowerCamelCase(self							):
		return OrderedDict(
		    [
		        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
		    ]							)





	@property
	def    lowerCamelCase(self							):
		return 1e-4









"""simple docstring"""
import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
							from PIL import Image
else:



							class      SCREAMING_SNAKE_CASE  :





								"""simple docstring"""




								@staticmethod
								def    lowerCamelCase(*lowerCAmelCase_     ,     **lowerCAmelCase_							):
									pass



@is_pipeline_test
@require_torch
@require_vision
class      SCREAMING_SNAKE_CASE  (				unittest.TestCase						):





	"""simple docstring"""




	_A       :				List[Any]    =				MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Union[str, Any]	    =   pipeline("""visual-question-answering"""     ,     model="""hf-internal-testing/tiny-vilt-random-vqa"""							)
		A_		:						Union[str, Any]	    =   [
		    {
		        """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							),
		        """question""": """How many cats are there?""",
		    },
		    {
		        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
		        """question""": """How many cats are there?""",
		    },
		]
		return vqa_pipeline, examples





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						str	    =   vqa_pipeline(lowerCAmelCase_     ,     top_k=1							)
		self.assertEqual(
		    lowerCAmelCase_     ,     [
		        [{"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}],
		        [{"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}],
		    ]     ,     )





	@require_torch
	def    lowerCamelCase(self							):
		A_		:						Tuple	    =   pipeline("""visual-question-answering"""     ,     model="""hf-internal-testing/tiny-vilt-random-vqa"""							)
		A_		:						Any	    =   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
		A_		:						Any	    =   """How many cats are there?"""

		A_		:						Optional[int]	    =   vqa_pipeline(image=lowerCAmelCase_     ,     question="""How many cats are there?"""     ,     top_k=2							)
		self.assertEqual(
		    lowerCAmelCase_     ,     [{"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}, {"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}]							)

		A_		:						Any	    =   vqa_pipeline({"""image""": image, """question""": question}     ,     top_k=2							)
		self.assertEqual(
		    lowerCAmelCase_     ,     [{"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}, {"""score""": ANY(lowerCAmelCase_							), """answer""": ANY(lowerCAmelCase_							)}]							)





	@slow
	@require_torch
	def    lowerCamelCase(self							):
		A_		:						int	    =   pipeline("""visual-question-answering"""     ,     model="""dandelin/vilt-b32-finetuned-vqa"""							)
		A_		:						Tuple	    =   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
		A_		:						int	    =   """How many cats are there?"""

		A_		:						List[Any]	    =   vqa_pipeline(image=lowerCAmelCase_     ,     question=lowerCAmelCase_     ,     top_k=2							)
		self.assertEqual(
		    nested_simplify(lowerCAmelCase_     ,     decimals=4							)     ,     [{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]							)

		A_		:						Any	    =   vqa_pipeline({"""image""": image, """question""": question}     ,     top_k=2							)
		self.assertEqual(
		    nested_simplify(lowerCAmelCase_     ,     decimals=4							)     ,     [{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]							)

		A_		:						Tuple	    =   vqa_pipeline(
		    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]     ,     top_k=2							)
		self.assertEqual(
		    nested_simplify(lowerCAmelCase_     ,     decimals=4							)     ,     [[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]] * 2     ,     )





	@require_tf
	@unittest.skip("""Visual question answering not implemented in TF"""							)
	def    lowerCamelCase(self							):
		pass

