






import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available





class 			__a  (    A__       ):
 _lowerCAmelCase       :       Any        = ['''image_processor''', '''tokenizer''']
 _lowerCAmelCase       :       Union[str, Any]        = '''OwlViTImageProcessor'''
 _lowerCAmelCase       :       List[str]        = ('''CLIPTokenizer''', '''CLIPTokenizerFast''')







 def __init__(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=None							,   SCREAMING_SNAKE_CASE					:   str=None							,   **SCREAMING_SNAKE_CASE					:   Dict						):


      '''simple docstring'''






      UpperCamelCase__			:   List[str]       				=  None
      if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."							,   SCREAMING_SNAKE_CASE							,   )
           UpperCamelCase__			:   Tuple       				=  kwargs.pop("feature_extractor"						)

      UpperCamelCase__			:   Optional[int]       				=  image_processor if image_processor is not None else feature_extractor
      if image_processor is None:
           raise ValueError("You need to specify an `image_processor`."						)
      if tokenizer is None:
           raise ValueError("You need to specify a `tokenizer`."						)

      super().__init__(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						)







 def __call__(		self					:   str							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=None							,   SCREAMING_SNAKE_CASE					:   Tuple=None							,   SCREAMING_SNAKE_CASE					:   str=None							,   SCREAMING_SNAKE_CASE					:   List[str]="max_length"							,   SCREAMING_SNAKE_CASE					:   Optional[Any]="np"							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






      if text is None and query_images is None and images is None:
           raise ValueError(
               "You have to specify at least one text or query image or image. All three cannot be none."						)

      if text is not None:
           if isinstance(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						) or (isinstance(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						) and not isinstance(text[0]							,   SCREAMING_SNAKE_CASE						)):
                UpperCamelCase__			:   Tuple       				=  [self.tokenizer(SCREAMING_SNAKE_CASE							,   padding=SCREAMING_SNAKE_CASE							,   return_tensors=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)]

           elif isinstance(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						) and isinstance(text[0]							,   SCREAMING_SNAKE_CASE						):
                UpperCamelCase__			:   Optional[Any]       				=  []

                # Maximum number of queries across batch
                UpperCamelCase__			:   Union[str, Any]       				=  max([len(SCREAMING_SNAKE_CASE						) for t in text]						)

                # Pad all batch samples to max number of text queries
                for t in text:
                     if len(SCREAMING_SNAKE_CASE						) != max_num_queries:
                          UpperCamelCase__			:   Union[str, Any]       				=  t + [" "] * (max_num_queries - len(SCREAMING_SNAKE_CASE						))

                     UpperCamelCase__			:   Optional[Any]       				=  self.tokenizer(SCREAMING_SNAKE_CASE							,   padding=SCREAMING_SNAKE_CASE							,   return_tensors=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)
                     encodings.append(SCREAMING_SNAKE_CASE						)
           else:
                raise TypeError("Input text should be a string, a list of strings or a nested list of strings"						)

           if return_tensors == "np":
                UpperCamelCase__			:   int       				=  np.concatenate([encoding["input_ids"] for encoding in encodings]							,   axis=0						)
                UpperCamelCase__			:   Optional[Any]       				=  np.concatenate([encoding["attention_mask"] for encoding in encodings]							,   axis=0						)

           elif return_tensors == "jax" and is_flax_available():
                import jax.numpy as jnp

                UpperCamelCase__			:   Optional[int]       				=  jnp.concatenate([encoding["input_ids"] for encoding in encodings]							,   axis=0						)
                UpperCamelCase__			:   Union[str, Any]       				=  jnp.concatenate([encoding["attention_mask"] for encoding in encodings]							,   axis=0						)

           elif return_tensors == "pt" and is_torch_available():
                import torch

                UpperCamelCase__			:   Union[str, Any]       				=  torch.cat([encoding["input_ids"] for encoding in encodings]							,   dim=0						)
                UpperCamelCase__			:   List[Any]       				=  torch.cat([encoding["attention_mask"] for encoding in encodings]							,   dim=0						)

           elif return_tensors == "tf" and is_tf_available():
                import tensorflow as tf

                UpperCamelCase__			:   str       				=  tf.stack([encoding["input_ids"] for encoding in encodings]							,   axis=0						)
                UpperCamelCase__			:   Optional[Any]       				=  tf.stack([encoding["attention_mask"] for encoding in encodings]							,   axis=0						)

           else:
                raise ValueError("Target return tensor type could not be returned"						)

           UpperCamelCase__			:   Optional[int]       				=  BatchEncoding()
           UpperCamelCase__			:   List[str]       				=  input_ids
           UpperCamelCase__			:   List[str]       				=  attention_mask

      if query_images is not None:
           UpperCamelCase__			:   Optional[int]       				=  BatchEncoding()
           UpperCamelCase__			:   Dict       				=  self.image_processor(
               SCREAMING_SNAKE_CASE							,   return_tensors=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						).pixel_values
           UpperCamelCase__			:   Dict       				=  query_pixel_values

      if images is not None:
           UpperCamelCase__			:   str       				=  self.image_processor(SCREAMING_SNAKE_CASE							,   return_tensors=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      if text is not None and images is not None:
           UpperCamelCase__			:   Union[str, Any]       				=  image_features.pixel_values
           return encoding
      elif query_images is not None and images is not None:
           UpperCamelCase__			:   List[Any]       				=  image_features.pixel_values
           return encoding
      elif text is not None or query_images is not None:
           return encoding
      else:
           return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE						)							,   tensor_type=SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[str]							,   *SCREAMING_SNAKE_CASE					:   Optional[Any]							,   **SCREAMING_SNAKE_CASE					:   List[str]						):


      '''simple docstring'''






      return self.image_processor.post_process(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[str]							,   *SCREAMING_SNAKE_CASE					:   List[Any]							,   **SCREAMING_SNAKE_CASE					:   str						):


      '''simple docstring'''






      return self.image_processor.post_process_object_detection(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   int							,   *SCREAMING_SNAKE_CASE					:   List[Any]							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






      return self.image_processor.post_process_image_guided_detection(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Tuple							,   *SCREAMING_SNAKE_CASE					:   Optional[int]							,   **SCREAMING_SNAKE_CASE					:   Tuple						):


      '''simple docstring'''






      return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   List[str]							,   *SCREAMING_SNAKE_CASE					:   int							,   **SCREAMING_SNAKE_CASE					:   Tuple						):


      '''simple docstring'''






      return self.tokenizer.decode(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 @property
 def 					__lowercase					(		self					:   Dict						):


      '''simple docstring'''






      warnings.warn(
          "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."							,   SCREAMING_SNAKE_CASE							,   )
      return self.image_processor_class







 @property
 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      warnings.warn(
          "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."							,   SCREAMING_SNAKE_CASE							,   )
      return self.image_processor







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


lowerCamelCase	:							Tuple				=logging.get_logger(__name__)

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





class 			__a  (    A__       ):
 _lowerCAmelCase       :       Optional[Any]        = '''conditional_detr'''
 _lowerCAmelCase       :       List[Any]        = ['''past_key_values''']
 _lowerCAmelCase       :       List[str]        = {
     '''hidden_size''': '''d_model''',
     '''num_attention_heads''': '''encoder_attention_heads''',
 }







 def __init__(		self					:   str							,   SCREAMING_SNAKE_CASE					:   Tuple=True							,   SCREAMING_SNAKE_CASE					:   Optional[int]=None							,   SCREAMING_SNAKE_CASE					:   Tuple=3							,   SCREAMING_SNAKE_CASE					:   Any=3_00							,   SCREAMING_SNAKE_CASE					:   Tuple=6							,   SCREAMING_SNAKE_CASE					:   int=20_48							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=8							,   SCREAMING_SNAKE_CASE					:   int=6							,   SCREAMING_SNAKE_CASE					:   Dict=20_48							,   SCREAMING_SNAKE_CASE					:   Optional[int]=8							,   SCREAMING_SNAKE_CASE					:   Dict=0.0							,   SCREAMING_SNAKE_CASE					:   List[Any]=0.0							,   SCREAMING_SNAKE_CASE					:   int=True							,   SCREAMING_SNAKE_CASE					:   int="relu"							,   SCREAMING_SNAKE_CASE					:   Any=2_56							,   SCREAMING_SNAKE_CASE					:   int=0.1							,   SCREAMING_SNAKE_CASE					:   int=0.0							,   SCREAMING_SNAKE_CASE					:   Optional[int]=0.0							,   SCREAMING_SNAKE_CASE					:   List[str]=0.0_2							,   SCREAMING_SNAKE_CASE					:   Optional[int]=1.0							,   SCREAMING_SNAKE_CASE					:   List[str]=False							,   SCREAMING_SNAKE_CASE					:   int="sine"							,   SCREAMING_SNAKE_CASE					:   str="resnet50"							,   SCREAMING_SNAKE_CASE					:   int=True							,   SCREAMING_SNAKE_CASE					:   Tuple=False							,   SCREAMING_SNAKE_CASE					:   Tuple=2							,   SCREAMING_SNAKE_CASE					:   Tuple=5							,   SCREAMING_SNAKE_CASE					:   List[str]=2							,   SCREAMING_SNAKE_CASE					:   List[str]=1							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=1							,   SCREAMING_SNAKE_CASE					:   str=2							,   SCREAMING_SNAKE_CASE					:   Any=5							,   SCREAMING_SNAKE_CASE					:   int=2							,   SCREAMING_SNAKE_CASE					:   List[Any]=0.2_5							,   **SCREAMING_SNAKE_CASE					:   Union[str, 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."						)
                UpperCamelCase__			:   Tuple       				=  CONFIG_MAPPING["resnet"](out_features=["stage4"]						)
           elif isinstance(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE						):
                UpperCamelCase__			:   Union[str, Any]       				=  backbone_config.get("model_type"						)
                UpperCamelCase__			:   Tuple       				=  CONFIG_MAPPING[backbone_model_type]
                UpperCamelCase__			:   Any       				=  config_class.from_dict(SCREAMING_SNAKE_CASE						)

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







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


      '''simple docstring'''






      return self.encoder_attention_heads







 @property
 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      return self.d_model







 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






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







class 			__a  (    A__       ):
 _lowerCAmelCase       :       Tuple        = version.parse('''1.11'''       )







 @property
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






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







 @property
 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      return 1e-5







 @property
 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      return 12