


"""simple docstring"""

import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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    lowerCamelCase__						:
             @staticmethod
             def SCREAMING_SNAKE_CASE_	(		*A_	:	List[Any]      ,       **A_	:	Tuple							):






              '''simple docstring'''


              pass





@is_pipeline_test
@require_vision
@require_torch
class    lowerCamelCase__						(		unittest.TestCase    ):
      a :     int											=		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
      def SCREAMING_SNAKE_CASE_	(		self	:	List[Any]      ,       A_	:	Optional[Any]      ,       A_	:	Optional[int]      ,       A_	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	pipeline(
           """zero-shot-object-detection"""      ,       model="""hf-internal-testing/tiny-random-owlvit-object-detection"""							)

       __lowercase									=	[
           {
               """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
               """candidate_labels""": ["""cat""", """remote""", """couch"""],
           }
       ]
       return object_detector, examples
      def SCREAMING_SNAKE_CASE_	(		self	:	int      ,       A_	:	Any      ,       A_	:	str							):






       '''simple docstring'''


       __lowercase									=	object_detector(examples[0]      ,       threshold=0.0							)

       __lowercase									=	len(A_							)
       self.assertGreater(A_      ,       0							)
       self.assertEqual(
           A_      ,       [
               {
                   """score""": ANY(A_							),
                   """label""": ANY(A_							),
                   """box""": {"""xmin""": ANY(A_							), """ymin""": ANY(A_							), """xmax""": ANY(A_							), """ymax""": ANY(A_							)},
               }
               for i in range(A_							)
           ]      ,       )
      @require_tf
      @unittest.skip("""Zero Shot Object Detection not implemented in TF"""							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       pass
      @require_torch
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	pipeline(
           """zero-shot-object-detection"""      ,       model="""hf-internal-testing/tiny-random-owlvit-object-detection"""							)

       __lowercase									=	object_detector(
           """./tests/fixtures/tests_samples/COCO/000000039769.png"""      ,       candidate_labels=["""cat""", """remote""", """couch"""]      ,       threshold=0.64      ,       )

       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
               {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
               {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
               {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
               {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
               {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
               {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
               {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
               {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
           ]      ,       )

       __lowercase									=	object_detector(
           [
               {
                   """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                   """candidate_labels""": ["""cat""", """remote""", """couch"""],
               }
           ]      ,       threshold=0.64      ,       )

       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               [
                   {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                   {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                   {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                   {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                   {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                   {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                   {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
                   {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
                   {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
               ]
           ]      ,       )
      @require_torch
      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	pipeline("""zero-shot-object-detection"""							)

       __lowercase									=	object_detector(
           """http://images.cocodataset.org/val2017/000000039769.jpg"""      ,       candidate_labels=["""cat""", """remote""", """couch"""]      ,       )
       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
               {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
               {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
               {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
               {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
           ]      ,       )

       __lowercase									=	object_detector(
           [
               {
                   """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
                   """candidate_labels""": ["""cat""", """remote""", """couch"""],
               },
               {
                   """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
                   """candidate_labels""": ["""cat""", """remote""", """couch"""],
               },
           ]      ,       )
       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               [
                   {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                   {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                   {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                   {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                   {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
               ],
               [
                   {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                   {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                   {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                   {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                   {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
               ],
           ]      ,       )
      @require_tf
      @unittest.skip("""Zero Shot Object Detection not implemented in TF"""							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Any							):






       '''simple docstring'''


       pass
      @require_torch
      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	List[Any]							):






       '''simple docstring'''


       __lowercase									=	0.2
       __lowercase									=	pipeline("""zero-shot-object-detection"""							)

       __lowercase									=	object_detector(
           """http://images.cocodataset.org/val2017/000000039769.jpg"""      ,       candidate_labels=["""cat""", """remote""", """couch"""]      ,       threshold=A_      ,       )
       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
               {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
               {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
           ]      ,       )






      @require_torch
      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       __lowercase									=	2
       __lowercase									=	pipeline("""zero-shot-object-detection"""							)

       __lowercase									=	object_detector(
           """http://images.cocodataset.org/val2017/000000039769.jpg"""      ,       candidate_labels=["""cat""", """remote""", """couch"""]      ,       top_k=A_      ,       )
       self.assertEqual(
           nested_simplify(A_      ,       decimals=4							)      ,       [
               {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
               {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
           ]      ,       )




"""simple docstring"""

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


UpperCAmelCase__  							=logging.get_logger(__name__)

UpperCAmelCase__  							={
    "microsoft/swinv2-tiny-patch4-window8-256": (
        "https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json"
    ),
}





class    lowerCamelCase__						(		_a    ):
      a :     List[Any]											=		"""swinv2"""

      a :     Any											=		{
          """num_attention_heads""": """num_heads""",
          """num_hidden_layers""": """num_layers""",
      }
      def __init__(		self	:	List[str]      ,       A_	:	Any=2_2_4      ,       A_	:	int=4      ,       A_	:	Optional[int]=3      ,       A_	:	List[Any]=9_6      ,       A_	:	List[Any]=[2, 2, 6, 2]      ,       A_	:	List[str]=[3, 6, 1_2, 2_4]      ,       A_	:	Union[str, Any]=7      ,       A_	:	int=4.0      ,       A_	:	List[str]=True      ,       A_	:	str=0.0      ,       A_	:	Any=0.0      ,       A_	:	Union[str, Any]=0.1      ,       A_	:	Optional[Any]="gelu"      ,       A_	:	int=False      ,       A_	:	List[Any]=0.02      ,       A_	:	Tuple=1e-5      ,       A_	:	Tuple=3_2      ,       **A_	:	int      ,       ):






       '''simple docstring'''


       super().__init__(**A_							)

       __lowercase									=	image_size
       __lowercase									=	patch_size
       __lowercase									=	num_channels
       __lowercase									=	embed_dim
       __lowercase									=	depths
       __lowercase									=	len(A_							)
       __lowercase									=	num_heads
       __lowercase									=	window_size
       __lowercase									=	mlp_ratio
       __lowercase									=	qkv_bias
       __lowercase									=	hidden_dropout_prob
       __lowercase									=	attention_probs_dropout_prob
       __lowercase									=	drop_path_rate
       __lowercase									=	hidden_act
       __lowercase									=	use_absolute_embeddings
       __lowercase									=	layer_norm_eps
       __lowercase									=	initializer_range
       __lowercase									=	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
       __lowercase									=	int(embed_dim * 2 ** (len(A_							) - 1)							)
       __lowercase									=	(0, 0, 0, 0)
