



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




         '''simple docstring'''






         @staticmethod
         def       _lowerCAmelCase (	*lowerCamelCase__,		**lowerCamelCase__		):
             pass






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




    '''simple docstring'''






    __lowerCamelCase      :			List[Any] =				MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
        A			:	str										=       pipeline(
            """zero-shot-object-detection""",		model="""hf-internal-testing/tiny-random-owlvit-object-detection"""		)

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






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        A			:	Any										=       object_detector(examples[0],		threshold=0.0		)

        A			:	Union[str, Any]										=       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       _lowerCAmelCase (	self		):
        pass






    @require_torch
    def       _lowerCAmelCase (	self		):
        A			:	Union[str, Any]										=       pipeline(
            """zero-shot-object-detection""",		model="""hf-internal-testing/tiny-random-owlvit-object-detection"""		)

        A			:	int										=       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.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}},
            ],		)

        A			:	List[Any]										=       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.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       _lowerCAmelCase (	self		):
        A			:	List[str]										=       pipeline("""zero-shot-object-detection"""		)

        A			:	Optional[int]										=       object_detector(
            """http://images.cocodataset.org/val2017/000000039769.jpg""",		candidate_labels=["""cat""", """remote""", """couch"""],		)
        self.assertEqual(
            nested_simplify(lowerCamelCase__,		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}},
            ],		)

        A			:	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(lowerCamelCase__,		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       _lowerCAmelCase (	self		):
        pass






    @require_torch
    @slow
    def       _lowerCAmelCase (	self		):
        A			:	List[str]										=       0.2
        A			:	Dict										=       pipeline("""zero-shot-object-detection"""		)

        A			:	str										=       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.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       _lowerCAmelCase (	self		):
        A			:	int										=       2
        A			:	int										=       pipeline("""zero-shot-object-detection"""		)

        A			:	int										=       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.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}},
            ],		)







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


SCREAMING_SNAKE_CASE_:Optional[Any]					=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:Union[str, Any]					=						{
    """microsoft/conditional-detr-resnet-50""": (
        """https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"""
    ),
}






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Dict =				"conditional_detr"
    __lowerCamelCase      :			str =				["past_key_values"]
    __lowerCamelCase      :			str =				{
        "hidden_size": "d_model",
        "num_attention_heads": "encoder_attention_heads",
    }






    def __init__(	self,		lowerCamelCase__=True,		lowerCamelCase__=None,		lowerCamelCase__=3,		lowerCamelCase__=300,		lowerCamelCase__=6,		lowerCamelCase__=2048,		lowerCamelCase__=8,		lowerCamelCase__=6,		lowerCamelCase__=2048,		lowerCamelCase__=8,		lowerCamelCase__=0.0,		lowerCamelCase__=0.0,		lowerCamelCase__=True,		lowerCamelCase__="relu",		lowerCamelCase__=256,		lowerCamelCase__=0.1,		lowerCamelCase__=0.0,		lowerCamelCase__=0.0,		lowerCamelCase__=0.02,		lowerCamelCase__=1.0,		lowerCamelCase__=False,		lowerCamelCase__="sine",		lowerCamelCase__="resnet50",		lowerCamelCase__=True,		lowerCamelCase__=False,		lowerCamelCase__=2,		lowerCamelCase__=5,		lowerCamelCase__=2,		lowerCamelCase__=1,		lowerCamelCase__=1,		lowerCamelCase__=2,		lowerCamelCase__=5,		lowerCamelCase__=2,		lowerCamelCase__=0.25,		**lowerCamelCase__,		):
        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."""		)
                A			:	List[Any]										=       CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]		)
            elif isinstance(lowerCamelCase__,		lowerCamelCase__		):
                A			:	Any										=       backbone_config.get("""model_type"""		)
                A			:	Optional[Any]										=       CONFIG_MAPPING[backbone_model_type]
                A			:	Tuple										=       config_class.from_dict(lowerCamelCase__		)

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






    @property
    def       _lowerCAmelCase (	self		):
        return self.encoder_attention_heads






    @property
    def       _lowerCAmelCase (	self		):
        return self.d_model






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       copy.deepcopy(self.__dict__		)
        if self.backbone_config is not None:
            A			:	List[Any]										=       self.backbone_config.to_dict()
        A			:	List[str]										=       self.__class__.model_type
        return output






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Tuple =				version.parse("1.11"   )






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






    @property
    def       _lowerCAmelCase (	self		):
        return 1e-5






    @property
    def       _lowerCAmelCase (	self		):
        return 12


