


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

       '''simple docstring'''







       @staticmethod
       def   _snake_case    (  *_lowerCamelCase  : Tuple       ,  **_lowerCamelCase  : Any							):






             '''simple docstring'''



             pass



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

   '''simple docstring'''







   a_				:							Tuple    = MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

   def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						pipeline(
             """zero-shot-object-detection"""       ,  model="""hf-internal-testing/tiny-random-owlvit-object-detection"""							)

         __lowerCamelCase     :      Any       						=						[
             {
                 """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                 """candidate_labels""": ["""cat""", """remote""", """couch"""],
             }
         ]
         return object_detector, examples

   def   _snake_case    (  self  : Optional[Any]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						object_detector(examples[0]       ,  threshold=0.0							)

         __lowerCamelCase     :      int       						=						len(_lowerCamelCase							)
         self.assertGreater(_lowerCamelCase       ,  0							)
         self.assertEqual(
             _lowerCamelCase       ,  [
                 {
                     """score""": ANY(_lowerCamelCase							),
                     """label""": ANY(_lowerCamelCase							),
                     """box""": {"""xmin""": ANY(_lowerCamelCase							), """ymin""": ANY(_lowerCamelCase							), """xmax""": ANY(_lowerCamelCase							), """ymax""": ANY(_lowerCamelCase							)},
                 }
                 for i in range(_lowerCamelCase							)
             ]       ,  )

   @require_tf
   @unittest.skip("""Zero Shot Object Detection not implemented in TF"""							)
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         pass

   @require_torch
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



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

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

         self.assertEqual(
             nested_simplify(_lowerCamelCase       ,  decimals=4							)       ,  [
                 {"""score""": 0.7_235, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                 {"""score""": 0.7_218, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                 {"""score""": 0.7_184, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                 {"""score""": 0.6_748, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                 {"""score""": 0.6_656, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                 {"""score""": 0.6_614, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                 {"""score""": 0.6_456, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
                 {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
                 {"""score""": 0.6_419, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
             ]       ,  )

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

         self.assertEqual(
             nested_simplify(_lowerCamelCase       ,  decimals=4							)       ,  [
                 [
                     {"""score""": 0.7_235, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                     {"""score""": 0.7_218, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                     {"""score""": 0.7_184, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
                     {"""score""": 0.6_748, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                     {"""score""": 0.6_656, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                     {"""score""": 0.6_614, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
                     {"""score""": 0.6_456, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
                     {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
                     {"""score""": 0.6_419, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
                 ]
             ]       ,  )

   @require_torch
   @slow
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						pipeline("""zero-shot-object-detection"""							)

         __lowerCamelCase     :      str       						=						object_detector(
             """http://images.cocodataset.org/val2017/000000039769.jpg"""       ,  candidate_labels=["""cat""", """remote""", """couch"""]       ,  )
         self.assertEqual(
             nested_simplify(_lowerCamelCase       ,  decimals=4							)       ,  [
                 {"""score""": 0.2_868, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                 {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                 {"""score""": 0.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                 {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                 {"""score""": 0.1_208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
             ]       ,  )

         __lowerCamelCase     :      str       						=						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(_lowerCamelCase       ,  decimals=4							)       ,  [
                 [
                     {"""score""": 0.2_868, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                     {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                     {"""score""": 0.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                     {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                     {"""score""": 0.1_208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
                 ],
                 [
                     {"""score""": 0.2_868, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                     {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                     {"""score""": 0.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
                     {"""score""": 0.1_474, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
                     {"""score""": 0.1_208, """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   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         pass

   @require_torch
   @slow
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						0.2
         __lowerCamelCase     :      int       						=						pipeline("""zero-shot-object-detection"""							)

         __lowerCamelCase     :      Optional[Any]       						=						object_detector(
             """http://images.cocodataset.org/val2017/000000039769.jpg"""       ,  candidate_labels=["""cat""", """remote""", """couch"""]       ,  threshold=_lowerCamelCase       ,  )
         self.assertEqual(
             nested_simplify(_lowerCamelCase       ,  decimals=4							)       ,  [
                 {"""score""": 0.2_868, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
                 {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
                 {"""score""": 0.2_537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
             ]       ,  )


   @require_torch
   @slow
   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						2
         __lowerCamelCase     :      Any       						=						pipeline("""zero-shot-object-detection"""							)

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







import copy
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 ..auto import CONFIG_MAPPING


__UpperCamelCase    :						Optional[int]     =							logging.get_logger(__name__)

__UpperCamelCase    :						Any     =							{
    'microsoft/conditional-detr-resnet-50': (
        'https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json'
    ),
}



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Optional[int]    = "conditional_detr"
   a_				:							Union[str, Any]    = ["past_key_values"]
   a_				:							str    = {
       "hidden_size": "d_model",
       "num_attention_heads": "encoder_attention_heads",
   }

   def __init__(  self  : Dict       ,  _lowerCamelCase  : Optional[int]=True       ,  _lowerCamelCase  : int=None       ,  _lowerCamelCase  : Optional[int]=3       ,  _lowerCamelCase  : Tuple=3_0_0       ,  _lowerCamelCase  : Any=6       ,  _lowerCamelCase  : Union[str, Any]=2_0_4_8       ,  _lowerCamelCase  : List[str]=8       ,  _lowerCamelCase  : Any=6       ,  _lowerCamelCase  : Any=2_0_4_8       ,  _lowerCamelCase  : List[Any]=8       ,  _lowerCamelCase  : Optional[int]=0.0       ,  _lowerCamelCase  : Any=0.0       ,  _lowerCamelCase  : Tuple=True       ,  _lowerCamelCase  : Union[str, Any]="relu"       ,  _lowerCamelCase  : str=2_5_6       ,  _lowerCamelCase  : int=0.1       ,  _lowerCamelCase  : List[str]=0.0       ,  _lowerCamelCase  : Tuple=0.0       ,  _lowerCamelCase  : List[str]=0.02       ,  _lowerCamelCase  : Union[str, Any]=1.0       ,  _lowerCamelCase  : Optional[int]=False       ,  _lowerCamelCase  : List[str]="sine"       ,  _lowerCamelCase  : Optional[Any]="resnet50"       ,  _lowerCamelCase  : List[str]=True       ,  _lowerCamelCase  : Dict=False       ,  _lowerCamelCase  : Optional[int]=2       ,  _lowerCamelCase  : Dict=5       ,  _lowerCamelCase  : List[Any]=2       ,  _lowerCamelCase  : Any=1       ,  _lowerCamelCase  : Any=1       ,  _lowerCamelCase  : Dict=2       ,  _lowerCamelCase  : Dict=5       ,  _lowerCamelCase  : Dict=2       ,  _lowerCamelCase  : Union[str, Any]=0.25       ,  **_lowerCamelCase  : Tuple       ,  ):






         '''simple docstring'''



         if backbone_config is not None and use_timm_backbone:
               raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""							)

         if not use_timm_backbone:
               if backbone_config is None:
                     logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""							)
                     __lowerCamelCase     :      Any       						=						CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]							)
               elif isinstance(_lowerCamelCase       ,  _lowerCamelCase							):
                     __lowerCamelCase     :      str       						=						backbone_config.get("""model_type"""							)
                     __lowerCamelCase     :      Optional[Any]       						=						CONFIG_MAPPING[backbone_model_type]
                     __lowerCamelCase     :      Optional[Any]       						=						config_class.from_dict(_lowerCamelCase							)

         __lowerCamelCase     :      Optional[int]       						=						use_timm_backbone
         __lowerCamelCase     :      Union[str, Any]       						=						backbone_config
         __lowerCamelCase     :      List[Any]       						=						num_channels
         __lowerCamelCase     :      str       						=						num_queries
         __lowerCamelCase     :      Dict       						=						d_model
         __lowerCamelCase     :      List[Any]       						=						encoder_ffn_dim
         __lowerCamelCase     :      Optional[Any]       						=						encoder_layers
         __lowerCamelCase     :      Any       						=						encoder_attention_heads
         __lowerCamelCase     :      Dict       						=						decoder_ffn_dim
         __lowerCamelCase     :      List[str]       						=						decoder_layers
         __lowerCamelCase     :      Tuple       						=						decoder_attention_heads
         __lowerCamelCase     :      List[Any]       						=						dropout
         __lowerCamelCase     :      Any       						=						attention_dropout
         __lowerCamelCase     :      Optional[Any]       						=						activation_dropout
         __lowerCamelCase     :      List[str]       						=						activation_function
         __lowerCamelCase     :      Optional[int]       						=						init_std
         __lowerCamelCase     :      Union[str, Any]       						=						init_xavier_std
         __lowerCamelCase     :      Union[str, Any]       						=						encoder_layerdrop
         __lowerCamelCase     :      int       						=						decoder_layerdrop
         __lowerCamelCase     :      Dict       						=						encoder_layers
         __lowerCamelCase     :      Tuple       						=						auxiliary_loss
         __lowerCamelCase     :      Any       						=						position_embedding_type
         __lowerCamelCase     :      Tuple       						=						backbone
         __lowerCamelCase     :      int       						=						use_pretrained_backbone
         __lowerCamelCase     :      Tuple       						=						dilation
         # Hungarian matcher
         __lowerCamelCase     :      Dict       						=						class_cost
         __lowerCamelCase     :      Optional[Any]       						=						bbox_cost
         __lowerCamelCase     :      Any       						=						giou_cost
         # Loss coefficients
         __lowerCamelCase     :      Union[str, Any]       						=						mask_loss_coefficient
         __lowerCamelCase     :      List[Any]       						=						dice_loss_coefficient
         __lowerCamelCase     :      Any       						=						cls_loss_coefficient
         __lowerCamelCase     :      str       						=						bbox_loss_coefficient
         __lowerCamelCase     :      Dict       						=						giou_loss_coefficient
         __lowerCamelCase     :      List[str]       						=						focal_alpha
         super().__init__(is_encoder_decoder=_lowerCamelCase       ,  **_lowerCamelCase							)

   @property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return self.encoder_attention_heads

   @property
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         return self.d_model


   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						copy.deepcopy(self.__dict__							)
         if self.backbone_config is not None:
               __lowerCamelCase     :      Dict       						=						self.backbone_config.to_dict()
         __lowerCamelCase     :      int       						=						self.__class__.model_type
         return output



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Dict    = version.parse("1.11"						)

   @property
   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



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

   @property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return 1E-5


   @property
   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         return 1_2



