



'''simple docstring'''






from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_torch_available():
     import torch

     from transformers.modeling_outputs import BaseModelOutput

     from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCAmelCase:					Tuple						       =						logging.get_logger(__name__)

@add_end_docstrings(UpperCAmelCase__	)
class 							a__(					UpperCAmelCase__	):







    def __init__(      self :  Dict						,			**__snake_case :  Dict	):
      super().__init__(**lowerCamelCase__	)

      if self.framework == "tf":
        raise ValueError(F"""The {self.__class__} is only available in PyTorch."""	)

      requires_backends(self						,			'vision'	)
      self.check_model_type(lowerCamelCase__	)







    def __call__(      self :  List[str]						,			__snake_case :  Union[str, "Image.Image", List[Dict[str, Any]]]						,			__snake_case :  Union[str, List[str]] = None						,			**__snake_case :  Optional[int]						,			):







      if "text_queries" in kwargs:
        a						:					Optional[Any]        =     kwargs.pop('text_queries'	)

      if isinstance(lowerCamelCase__						,			(str, Image.Image)	):
        a						:					List[Any]        =     {"image": image, "candidate_labels": candidate_labels}
      else:
        a						:					Dict        =     image
      a						:					Dict        =     super().__call__(lowerCamelCase__						,			**lowerCamelCase__	)
      return results







    def 			lowercase_       (      self :  List[Any]						,			**__snake_case :  Dict	):
      a						:					str        =     {}
      if "threshold" in kwargs:
        a						:					Optional[Any]        =     kwargs["threshold"]
      if "top_k" in kwargs:
        a						:					List[str]        =     kwargs["top_k"]
      return {}, {}, postprocess_params







    def 			lowercase_       (      self :  Optional[int]						,			__snake_case :  str	):
      a						:					Dict        =     load_image(inputs['image']	)
      a						:					Union[str, Any]        =     inputs["candidate_labels"]
      if isinstance(lowerCamelCase__						,			lowerCamelCase__	):
        a						:					Any        =     candidate_labels.split(','	)

      a						:					str        =     torch.tensor([[image.height, image.width]]						,			dtype=torch.intaa	)
      for i, candidate_label in enumerate(lowerCamelCase__	):
        a						:					Tuple        =     self.tokenizer(lowerCamelCase__						,			return_tensors=self.framework	)
        a						:					List[str]        =     self.image_processor(lowerCamelCase__						,			return_tensors=self.framework	)
        yield {
            "is_last": i == len(lowerCamelCase__	) - 1,
            "target_size": target_size,
            "candidate_label": candidate_label,
            **text_inputs,
            **image_features,
        }







    def 			lowercase_       (      self :  Optional[int]						,			__snake_case :  str	):
      a						:					List[Any]        =     model_inputs.pop('target_size'	)
      a						:					Optional[int]        =     model_inputs.pop('candidate_label'	)
      a						:					Union[str, Any]        =     model_inputs.pop('is_last'	)

      a						:					Union[str, Any]        =     self.model(**lowerCamelCase__	)

      a						:					Optional[Any]        =     {"target_size": target_size, "candidate_label": candidate_label, "is_last": is_last, **outputs}
      return model_outputs







    def 			lowercase_       (      self :  Union[str, Any]						,			__snake_case :  Union[str, Any]						,			__snake_case :  List[str]=0.1						,			__snake_case :  Optional[int]=None	):
      a						:					Any        =     []
      for model_output in model_outputs:
        a						:					Union[str, Any]        =     model_output["candidate_label"]
        a						:					Tuple        =     BaseModelOutput(lowerCamelCase__	)
        a						:					Dict        =     self.image_processor.post_process_object_detection(
            outputs=lowerCamelCase__						,			threshold=lowerCamelCase__						,			target_sizes=model_output['target_size']	)[0]

        for index in outputs["scores"].nonzero():
          a						:					int        =     outputs["scores"][index].item()
          a						:					Optional[int]        =     self._get_bounding_box(outputs['boxes'][index][0]	)

          a						:					Tuple        =     {"score": score, "label": label, "box": box}
          results.append(lowerCamelCase__	)

      a						:					List[str]        =     sorted(lowerCamelCase__						,			key=lambda __snake_case	: x["score"]						,			reverse=lowerCamelCase__	)
      if top_k:
        a						:					Tuple        =     results[:top_k]

      return results







    def 			lowercase_       (      self :  str						,			__snake_case :  "torch.Tensor"	):







      if self.framework != "pt":
        raise ValueError('The ZeroShotObjectDetectionPipeline is only available in PyTorch.'	)
      a						:					List[str]        =     box.int().tolist()
      a						:					Tuple        =     {
          "xmin": xmin,
          "ymin": ymin,
          "xmax": xmax,
          "ymax": ymax,
      }
      return bbox



'''simple docstring'''


import copy

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


lowerCamelCase__					=   logging.get_logger(__name__)

lowerCamelCase__					=   {
    'ut/deta': 'https://huggingface.co/ut/deta/resolve/main/config.json',
}






class 		lowerCAmelCase__	(	UpperCAmelCase__		):
			lowerCAmelCase       :		Any				      =   "deta"
			lowerCAmelCase       :		Union[str, Any]				      =   {
			    "hidden_size": "d_model",
			    "num_attention_heads": "encoder_attention_heads",
			}




			def __init__(						self     :   Optional[Any]		,     lowerCamelCase__     :   str=None		,     lowerCamelCase__     :   str=9_00		,     lowerCamelCase__     :   Any=20_48		,     lowerCamelCase__     :   Optional[int]=6		,     lowerCamelCase__     :   str=20_48		,     lowerCamelCase__     :   Dict=8		,     lowerCamelCase__     :   Any=6		,     lowerCamelCase__     :   Union[str, Any]=10_24		,     lowerCamelCase__     :   Optional[int]=8		,     lowerCamelCase__     :   List[Any]=0.0		,     lowerCamelCase__     :   Tuple=True		,     lowerCamelCase__     :   int="relu"		,     lowerCamelCase__     :   str=2_56		,     lowerCamelCase__     :   Dict=0.1		,     lowerCamelCase__     :   Dict=0.0		,     lowerCamelCase__     :   Dict=0.0		,     lowerCamelCase__     :   Optional[int]=0.0_2		,     lowerCamelCase__     :   List[Any]=1.0		,     lowerCamelCase__     :   Optional[int]=True		,     lowerCamelCase__     :   Any=False		,     lowerCamelCase__     :   Any="sine"		,     lowerCamelCase__     :   str=5		,     lowerCamelCase__     :   Optional[int]=4		,     lowerCamelCase__     :   int=4		,     lowerCamelCase__     :   Optional[Any]=True		,     lowerCamelCase__     :   Dict=3_00		,     lowerCamelCase__     :   int=True		,     lowerCamelCase__     :   Dict=True		,     lowerCamelCase__     :   Union[str, Any]=1		,     lowerCamelCase__     :   Any=5		,     lowerCamelCase__     :   List[Any]=2		,     lowerCamelCase__     :   Union[str, Any]=1		,     lowerCamelCase__     :   str=1		,     lowerCamelCase__     :   Union[str, Any]=5		,     lowerCamelCase__     :   str=2		,     lowerCamelCase__     :   Optional[Any]=0.1		,     lowerCamelCase__     :   Union[str, Any]=0.2_5		,     **lowerCamelCase__     :   Optional[Any]		,     )      ->List[str]:



					'''simple docstring'''







					if backbone_config is None:
							logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."       )
							_UpperCAmelCase						:				int	    =					CONFIG_MAPPING["resnet"](out_features=["stage2", "stage3", "stage4"]       )
					else:
							if isinstance(lowerCamelCase__		,     lowerCamelCase__       ):
									_UpperCAmelCase						:				Any	    =					backbone_config.pop("model_type"       )
									_UpperCAmelCase						:				Optional[int]	    =					CONFIG_MAPPING[backbone_model_type]
									_UpperCAmelCase						:				List[str]	    =					config_class.from_dict(lowerCamelCase__       )

					_UpperCAmelCase						:				Optional[Any]	    =					backbone_config
					_UpperCAmelCase						:				Optional[int]	    =					num_queries
					_UpperCAmelCase						:				Union[str, Any]	    =					max_position_embeddings
					_UpperCAmelCase						:				Union[str, Any]	    =					d_model
					_UpperCAmelCase						:				str	    =					encoder_ffn_dim
					_UpperCAmelCase						:				Optional[int]	    =					encoder_layers
					_UpperCAmelCase						:				int	    =					encoder_attention_heads
					_UpperCAmelCase						:				Union[str, Any]	    =					decoder_ffn_dim
					_UpperCAmelCase						:				Tuple	    =					decoder_layers
					_UpperCAmelCase						:				Union[str, Any]	    =					decoder_attention_heads
					_UpperCAmelCase						:				Any	    =					dropout
					_UpperCAmelCase						:				List[str]	    =					attention_dropout
					_UpperCAmelCase						:				Union[str, Any]	    =					activation_dropout
					_UpperCAmelCase						:				Optional[int]	    =					activation_function
					_UpperCAmelCase						:				str	    =					init_std
					_UpperCAmelCase						:				Tuple	    =					init_xavier_std
					_UpperCAmelCase						:				Optional[Any]	    =					encoder_layerdrop
					_UpperCAmelCase						:				int	    =					auxiliary_loss
					_UpperCAmelCase						:				Union[str, Any]	    =					position_embedding_type
					# deformable attributes
					_UpperCAmelCase						:				List[Any]	    =					num_feature_levels
					_UpperCAmelCase						:				List[Any]	    =					encoder_n_points
					_UpperCAmelCase						:				Tuple	    =					decoder_n_points
					_UpperCAmelCase						:				Optional[int]	    =					two_stage
					_UpperCAmelCase						:				Dict	    =					two_stage_num_proposals
					_UpperCAmelCase						:				int	    =					with_box_refine
					_UpperCAmelCase						:				str	    =					assign_first_stage
					if two_stage is True and with_box_refine is False:
							raise ValueError("If two_stage is True, with_box_refine must be True."       )
					# Hungarian matcher
					_UpperCAmelCase						:				Optional[int]	    =					class_cost
					_UpperCAmelCase						:				Dict	    =					bbox_cost
					_UpperCAmelCase						:				int	    =					giou_cost
					# Loss coefficients
					_UpperCAmelCase						:				int	    =					mask_loss_coefficient
					_UpperCAmelCase						:				List[Any]	    =					dice_loss_coefficient
					_UpperCAmelCase						:				Dict	    =					bbox_loss_coefficient
					_UpperCAmelCase						:				int	    =					giou_loss_coefficient
					_UpperCAmelCase						:				Optional[Any]	    =					eos_coefficient
					_UpperCAmelCase						:				int	    =					focal_alpha
					super().__init__(is_encoder_decoder=lowerCamelCase__		,     **lowerCamelCase__       )




			@property
			def 							lowerCAmelCase__			(						self     :   int       )      ->int:



					'''simple docstring'''







					return self.encoder_attention_heads




			@property
			def 							lowerCAmelCase__			(						self     :   List[Any]       )      ->int:



					'''simple docstring'''







					return self.d_model






			def 							lowerCAmelCase__			(						self     :   Union[str, Any]       )      ->int:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[int]	    =					copy.deepcopy(self.__dict__       )
					_UpperCAmelCase						:				Tuple	    =					self.backbone_config.to_dict()
					_UpperCAmelCase						:				List[Any]	    =					self.__class__.model_type
					return output



