"""simple docstring"""







import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

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


__SCREAMING_SNAKE_CASE    	=logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE    	={
    "facebook/detr-resnet-50": "https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json",
    # See all DETR models at https://huggingface.co/models?filter=detr
}







class        UpperCamelCase		(			lowercase_  ):
   lowercase							=  'detr'
   lowercase							=  ['past_key_values']
   lowercase							=  {
       'hidden_size': 'd_model',
       'num_attention_heads': 'encoder_attention_heads',
   }
   def __init__(			self     ,__UpperCamelCase=True     ,__UpperCamelCase=None     ,__UpperCamelCase=3     ,__UpperCamelCase=100     ,__UpperCamelCase=6     ,__UpperCamelCase=2048     ,__UpperCamelCase=8     ,__UpperCamelCase=6     ,__UpperCamelCase=2048     ,__UpperCamelCase=8     ,__UpperCamelCase=0.0     ,__UpperCamelCase=0.0     ,__UpperCamelCase=True     ,__UpperCamelCase="relu"     ,__UpperCamelCase=256     ,__UpperCamelCase=0.1     ,__UpperCamelCase=0.0     ,__UpperCamelCase=0.0     ,__UpperCamelCase=0.02     ,__UpperCamelCase=1.0     ,__UpperCamelCase=False     ,__UpperCamelCase="sine"     ,__UpperCamelCase="resnet50"     ,__UpperCamelCase=True     ,__UpperCamelCase=False     ,__UpperCamelCase=1     ,__UpperCamelCase=5     ,__UpperCamelCase=2     ,__UpperCamelCase=1     ,__UpperCamelCase=1     ,__UpperCamelCase=5     ,__UpperCamelCase=2     ,__UpperCamelCase=0.1     ,**__UpperCamelCase     ,)	->      Any:


        '''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.'			)
                  lowercase_	:				List[Any]													=	CONFIG_MAPPING['resnet'](out_features=['stage4']			)
             elif isinstance(__UpperCamelCase     ,__UpperCamelCase			):
                  lowercase_	:				int													=	backbone_config.get('model_type'			)
                  lowercase_	:				Optional[Any]													=	CONFIG_MAPPING[backbone_model_type]
                  lowercase_	:				int													=	config_class.from_dict(__UpperCamelCase			)
             # set timm attributes to None
             lowercase_							,  lowercase_							,  lowercase_	:				str													=	None, None, None

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


        '''simple docstring'''


        return self.encoder_attention_heads
   @property
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        return self.d_model
   @classmethod
   def 			_UpperCAmelCase   (			cls     ,__UpperCamelCase     ,**__UpperCamelCase			)	->      str:


        '''simple docstring'''


        return cls(backbone_config=__UpperCamelCase     ,**__UpperCamelCase			)







   def 			_UpperCAmelCase   (			self			)	->      Dict[str, any]:


        '''simple docstring'''


        lowercase_	:				int													=	copy.deepcopy(self.__dict__			)
        if output["backbone_config"] is not None:
             lowercase_	:				Tuple													=	self.backbone_config.to_dict()
        lowercase_	:				Union[str, Any]													=	self.__class__.model_type
        return output







class        UpperCamelCase		(			lowercase_  ):
   lowercase							=  version.parse('1.11'  )
   @property
   def 			_UpperCAmelCase   (			self			)	->      Mapping[str, Mapping[int, str]]:


        '''simple docstring'''


        return OrderedDict(
            [
                ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                ('pixel_mask', {0: 'batch'}),
            ]			)
   @property
   def 			_UpperCAmelCase   (			self			)	->      float:


        '''simple docstring'''


        return 1e-5







   @property
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        return 12


"""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        UpperCamelCase		:
          @staticmethod
          def 			_UpperCAmelCase   (			*__UpperCamelCase     ,**__UpperCamelCase			)	->      Union[str, Any]:


               '''simple docstring'''


               pass







@is_pipeline_test
@require_vision
@require_torch
class        UpperCamelCase		(			unittest.TestCase  ):
   lowercase							=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


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

        lowercase_	:				Optional[int]													=	[
            {
                'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                'candidate_labels': ['cat', 'remote', 'couch'],
            }
        ]
        return object_detector, examples
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      str:


        '''simple docstring'''


        lowercase_	:				Tuple													=	object_detector(examples[0]     ,threshold=0.0			)

        lowercase_	:				Any													=	len(__UpperCamelCase			)
        self.assertGreater(__UpperCamelCase     ,0			)
        self.assertEqual(
            __UpperCamelCase     ,[
                {
                    'score': ANY(__UpperCamelCase			),
                    'label': ANY(__UpperCamelCase			),
                    'box': {'xmin': ANY(__UpperCamelCase			), 'ymin': ANY(__UpperCamelCase			), 'xmax': ANY(__UpperCamelCase			), 'ymax': ANY(__UpperCamelCase			)},
                }
                for i in range(__UpperCamelCase			)
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        pass
   @require_torch
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


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

        lowercase_	:				Optional[int]													=	object_detector(
            './tests/fixtures/tests_samples/COCO/000000039769.png'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                }
            ]     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                    {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                    {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                ]
            ]     ,)
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[str]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	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(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        pass
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	0.2
        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				str													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
            ]     ,)







   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	2
        lowercase_	:				Optional[Any]													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[Any]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,top_k=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
            ]     ,)

