






'''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							:




       @staticmethod
       def 			__UpperCamelCase				(	*lowerCamelCase			, **lowerCamelCase   )				->Union[str, Any]:



          '''simple docstring'''
          pass
@is_pipeline_test
@require_torch
@require_vision
class 				__SCREAMING_SNAKE_CASE							(				unittest.TestCase  ):
      __a	       =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase   )				->Union[str, Any]:



         '''simple docstring'''
         __a					  =     pipeline('visual-question-answering'			, model='hf-internal-testing/tiny-vilt-random-vqa'   )
         __a					  =     [
             {
                 """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 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase   )				->Any:



         '''simple docstring'''
         __a					  =     vqa_pipeline(_A			, top_k=1   )
         self.assertEqual(
             _A			, [
                 [{'score': ANY(_A   ), 'answer': ANY(_A   )}],
                 [{'score': ANY(_A   ), 'answer': ANY(_A   )}],
             ]			, )




      @require_torch
      def 			__UpperCamelCase				(	self   )				->str:



         '''simple docstring'''
         __a					  =     pipeline('visual-question-answering'			, model='hf-internal-testing/tiny-vilt-random-vqa'   )
         __a					  =     """./tests/fixtures/tests_samples/COCO/000000039769.png"""
         __a					  =     """How many cats are there?"""

         __a					  =     vqa_pipeline(image=_A			, question='How many cats are there?'			, top_k=2   )
         self.assertEqual(
             _A			, [{'score': ANY(_A   ), 'answer': ANY(_A   )}, {'score': ANY(_A   ), 'answer': ANY(_A   )}]   )

         __a					  =     vqa_pipeline({'image': image, 'question': question}			, top_k=2   )
         self.assertEqual(
             _A			, [{'score': ANY(_A   ), 'answer': ANY(_A   )}, {'score': ANY(_A   ), 'answer': ANY(_A   )}]   )




      @slow
      @require_torch
      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         __a					  =     pipeline('visual-question-answering'			, model='dandelin/vilt-b32-finetuned-vqa'   )
         __a					  =     """./tests/fixtures/tests_samples/COCO/000000039769.png"""
         __a					  =     """How many cats are there?"""

         __a					  =     vqa_pipeline(image=_A			, question=_A			, top_k=2   )
         self.assertEqual(
             nested_simplify(_A			, decimals=4   )			, [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]   )

         __a					  =     vqa_pipeline({'image': image, 'question': question}			, top_k=2   )
         self.assertEqual(
             nested_simplify(_A			, decimals=4   )			, [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]   )

         __a					  =     vqa_pipeline(
             [{'image': image, 'question': question}, {'image': image, 'question': question}]			, top_k=2   )
         self.assertEqual(
             nested_simplify(_A			, 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 			__UpperCamelCase				(	self   )				->Any:



         '''simple docstring'''
         pass






"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__magic_name__       :					Any						=			logging.get_logger(__name__)

__magic_name__       :					int						=			{
    """facebook/convnextv2-tiny-1k-224""": """https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json""",
}






class 							lowercase__			(   __SCREAMING_SNAKE_CASE					,     __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""
			__lowerCAmelCase     :				str										=  """convnextv2"""







			def __init__(      self						,   _A=3						,   _A=4						,   _A=4						,   _A=None						,   _A=None						,   _A="gelu"						,   _A=0.02						,   _A=1e-1_2						,   _A=0.0						,   _A=2_2_4						,   _A=None						,   _A=None						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(**_A    )

									UpperCamelCase				:		Optional[Any]				   =			num_channels
									UpperCamelCase				:		int				   =			patch_size
									UpperCamelCase				:		Dict				   =			num_stages
									UpperCamelCase				:		Tuple				   =			[9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
									UpperCamelCase				:		str				   =			[3, 3, 9, 3] if depths is None else depths
									UpperCamelCase				:		List[str]				   =			hidden_act
									UpperCamelCase				:		List[str]				   =			initializer_range
									UpperCamelCase				:		List[Any]				   =			layer_norm_eps
									UpperCamelCase				:		Any				   =			drop_path_rate
									UpperCamelCase				:		Any				   =			image_size
									UpperCamelCase				:		Union[str, Any]				   =			["""stem"""] + [f"""stage{idx}""" for idx in range(1						,   len(self.depths    ) + 1    )]
									UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			get_aligned_output_features_output_indices(
									    out_features=_A						,   out_indices=_A						,   stage_names=self.stage_names    )



