

from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_tf_available():
      import tensorflow as tf

      from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
      from ..tf_utils import stable_softmax

if is_torch_available():
      from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

lowerCAmelCase__       = logging.get_logger(__name__)






@add_end_docstrings(_snake_case    )
class 						__magic_name__    (       _snake_case    ):


 def __init__(						self		: str			,						*lowerCAmelCase__		: Any			,						**lowerCAmelCase__		: int    )    ->      Any:
   super().__init__(*lowerCAmelCase__			,						**lowerCAmelCase__    )
   requires_backends(self			,						"vision"    )
   self.check_model_type(
       TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
       if self.framework == "tf"
       else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING    )


 def _UpperCamelCase	(						self		: Dict			,						lowerCAmelCase__		: Union[str, Any]=None    )    ->      int:
   UpperCAmelCase					      =  {}
   if top_k is not None:
     UpperCAmelCase					      =  top_k
   return {}, {}, postprocess_params


 def __call__(						self		: Tuple			,						lowerCAmelCase__		: Union[str, List[str], "Image.Image", List["Image.Image"]]			,						**lowerCAmelCase__		: str    )    ->      Union[str, Any]:
   return super().__call__(lowerCAmelCase__			,						**lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[Any]			,						lowerCAmelCase__		: Any    )    ->      Optional[Any]:
   UpperCAmelCase					      =  load_image(lowerCAmelCase__    )
   UpperCAmelCase					      =  self.image_processor(images=lowerCAmelCase__			,						return_tensors=self.framework    )
   return model_inputs


 def _UpperCamelCase	(						self		: Any			,						lowerCAmelCase__		: Any    )    ->      Union[str, Any]:
   UpperCAmelCase					      =  self.model(**lowerCAmelCase__    )
   return model_outputs


 def _UpperCamelCase	(						self		: List[Any]			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Tuple=5    )    ->      Union[str, Any]:
   if top_k > self.model.config.num_labels:
     UpperCAmelCase					      =  self.model.config.num_labels

   if self.framework == "pt":
     UpperCAmelCase					      =  model_outputs.logits.softmax(-1    )[0]
     UpperCAmelCase      ,	UpperCAmelCase					      =  probs.topk(lowerCAmelCase__    )
   elif self.framework == "tf":
     UpperCAmelCase					      =  stable_softmax(model_outputs.logits			,						axis=-1    )[0]
     UpperCAmelCase					      =  tf.math.top_k(lowerCAmelCase__			,						k=lowerCAmelCase__    )
     UpperCAmelCase      ,	UpperCAmelCase					      =  topk.values.numpy(), topk.indices.numpy()
   else:
     raise ValueError(f"Unsupported framework: {self.framework}"    )

   UpperCAmelCase					      =  scores.tolist()
   UpperCAmelCase					      =  ids.tolist()
   return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCAmelCase__			,						lowerCAmelCase__    )]






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""lxmert"""
 UpperCAmelCase							    =						{}


 def __init__(						self		: int			,						lowerCAmelCase__		: Any=3_0_5_2_2			,						lowerCAmelCase__		: List[str]=7_6_8			,						lowerCAmelCase__		: Union[str, Any]=1_2			,						lowerCAmelCase__		: List[Any]=9_5_0_0			,						lowerCAmelCase__		: Any=1_6_0_0			,						lowerCAmelCase__		: Union[str, Any]=4_0_0			,						lowerCAmelCase__		: Tuple=3_0_7_2			,						lowerCAmelCase__		: Dict="gelu"			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: Tuple=0.1			,						lowerCAmelCase__		: int=5_1_2			,						lowerCAmelCase__		: List[str]=2			,						lowerCAmelCase__		: List[str]=0.02			,						lowerCAmelCase__		: str=1e-1_2			,						lowerCAmelCase__		: str=9			,						lowerCAmelCase__		: int=5			,						lowerCAmelCase__		: Optional[int]=5			,						lowerCAmelCase__		: List[Any]=2_0_4_8			,						lowerCAmelCase__		: Any=4			,						lowerCAmelCase__		: Dict=6.67			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Union[str, Any]=True			,						lowerCAmelCase__		: Any=True			,						lowerCAmelCase__		: Tuple=True			,						lowerCAmelCase__		: Optional[Any]=True			,						lowerCAmelCase__		: Optional[int]=True			,						lowerCAmelCase__		: Tuple=True			,						**lowerCAmelCase__		: List[Any]			,						)    ->      Dict:
   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  hidden_size
   UpperCAmelCase					      =  num_attention_heads
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  intermediate_size
   UpperCAmelCase					      =  hidden_dropout_prob
   UpperCAmelCase					      =  attention_probs_dropout_prob
   UpperCAmelCase					      =  max_position_embeddings
   UpperCAmelCase					      =  type_vocab_size
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  num_qa_labels
   UpperCAmelCase					      =  num_object_labels
   UpperCAmelCase					      =  num_attr_labels
   UpperCAmelCase					      =  l_layers
   UpperCAmelCase					      =  x_layers
   UpperCAmelCase					      =  r_layers
   UpperCAmelCase					      =  visual_feat_dim
   UpperCAmelCase					      =  visual_pos_dim
   UpperCAmelCase					      =  visual_loss_normalizer
   UpperCAmelCase					      =  task_matched
   UpperCAmelCase					      =  task_mask_lm
   UpperCAmelCase					      =  task_obj_predict
   UpperCAmelCase					      =  task_qa
   UpperCAmelCase					      =  visual_obj_loss
   UpperCAmelCase					      =  visual_attr_loss
   UpperCAmelCase					      =  visual_feat_loss
   UpperCAmelCase					      =  {"vision": r_layers, "cross_encoder": x_layers, "language": l_layers}
   super().__init__(**lowerCAmelCase__    )



