



"""simple docstring"""







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        __A       (	A_    ):







		'''simple docstring'''





		lowerCAmelCase			:					Any   	=				"lxmert"
		lowerCAmelCase			:					Union[str, Any]   	=				{}







		def __init__(	self						:  List[str]			,_snake_case						:  Any=30_522			,_snake_case						:  Optional[Any]=768			,_snake_case						:  Any=12			,_snake_case						:  Optional[int]=9_500			,_snake_case						:  Optional[Any]=1_600			,_snake_case						:  Union[str, Any]=400			,_snake_case						:  Optional[int]=3_072			,_snake_case						:  List[Any]="gelu"			,_snake_case						:  Dict=0.1			,_snake_case						:  Union[str, Any]=0.1			,_snake_case						:  Optional[int]=512			,_snake_case						:  Any=2			,_snake_case						:  Tuple=0.02			,_snake_case						:  Any=1e-12			,_snake_case						:  str=9			,_snake_case						:  Optional[int]=5			,_snake_case						:  List[str]=5			,_snake_case						:  int=2_048			,_snake_case						:  Optional[int]=4			,_snake_case						:  str=6.67			,_snake_case						:  Optional[int]=True			,_snake_case						:  Optional[Any]=True			,_snake_case						:  str=True			,_snake_case						:  List[str]=True			,_snake_case						:  int=True			,_snake_case						:  Any=True			,_snake_case						:  Any=True			,**_snake_case						:  Optional[int]			,)					->		Dict:






				"""simple docstring"""



				lowercase__   :    Tuple				=			vocab_size
				lowercase__   :    str				=			hidden_size
				lowercase__   :    List[Any]				=			num_attention_heads
				lowercase__   :    Optional[Any]				=			hidden_act
				lowercase__   :    Optional[int]				=			intermediate_size
				lowercase__   :    List[str]				=			hidden_dropout_prob
				lowercase__   :    Dict				=			attention_probs_dropout_prob
				lowercase__   :    List[str]				=			max_position_embeddings
				lowercase__   :    Optional[Any]				=			type_vocab_size
				lowercase__   :    List[Any]				=			initializer_range
				lowercase__   :    List[Any]				=			layer_norm_eps
				lowercase__   :    Optional[int]				=			num_qa_labels
				lowercase__   :    List[Any]				=			num_object_labels
				lowercase__   :    int				=			num_attr_labels
				lowercase__   :    Optional[int]				=			l_layers
				lowercase__   :    Any				=			x_layers
				lowercase__   :    Optional[Any]				=			r_layers
				lowercase__   :    int				=			visual_feat_dim
				lowercase__   :    Optional[Any]				=			visual_pos_dim
				lowercase__   :    Optional[int]				=			visual_loss_normalizer
				lowercase__   :    List[str]				=			task_matched
				lowercase__   :    Optional[Any]				=			task_mask_lm
				lowercase__   :    Tuple				=			task_obj_predict
				lowercase__   :    int				=			task_qa
				lowercase__   :    Any				=			visual_obj_loss
				lowercase__   :    Optional[Any]				=			visual_attr_loss
				lowercase__   :    Tuple				=			visual_feat_loss
				lowercase__   :    str				=			{'''vision''': r_layers, '''cross_encoder''': x_layers, '''language''': l_layers}
				super().__init__(**_snake_case						)





"""simple docstring"""







from collections import UserDict
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_torch_available():
							from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
							from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
							from ..tf_utils import stable_softmax

lowerCAmelCase_   						=   logging.get_logger(__name__)
@add_end_docstrings(A_    )
class        __A       (	A_    ):







		'''simple docstring'''





		def __init__(	self						:  List[str]			,**_snake_case						:  Dict						)					->		List[Any]:






				"""simple docstring"""



				super().__init__(**_snake_case						)

				requires_backends(self			,'''vision'''						)
				self.check_model_type(
				    TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
				    if self.framework == '''tf'''
				    else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING						)







		def __call__(	self						:  Optional[int]			,_snake_case						:  Union[str, List[str], "Image", List["Image"]]			,**_snake_case						:  int						)					->		Optional[Any]:






				"""simple docstring"""



				return super().__call__(_snake_case			,**_snake_case						)







		def 					UpperCAmelCase    (	self						:  Dict			,**_snake_case						:  Optional[int]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			{}
				if "candidate_labels" in kwargs:
						lowercase__   :    Any				=			kwargs['''candidate_labels''']
				if "hypothesis_template" in kwargs:
						lowercase__   :    Optional[Any]				=			kwargs['''hypothesis_template''']

				return preprocess_params, {}, {}







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Optional[int]			,_snake_case						:  Dict=None			,_snake_case						:  Union[str, Any]="This is a photo of {}."						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			load_image(_snake_case						)
				lowercase__   :    int				=			self.image_processor(images=[image]			,return_tensors=self.framework						)
				lowercase__   :    str				=			candidate_labels
				lowercase__   :    Dict				=			[hypothesis_template.format(_snake_case						) for x in candidate_labels]
				lowercase__   :    Any				=			self.tokenizer(_snake_case			,return_tensors=self.framework			,padding=_snake_case						)
				lowercase__   :    Optional[int]				=			[text_inputs]
				return inputs







		def 					UpperCAmelCase    (	self						:  Optional[Any]			,_snake_case						:  Optional[int]						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			model_inputs.pop('''candidate_labels'''						)
				lowercase__   :    Union[str, Any]				=			model_inputs.pop('''text_inputs'''						)
				if isinstance(text_inputs[0]			,_snake_case						):
						lowercase__   :    List[str]				=			text_inputs[0]
				else:
						# Batching case.
						lowercase__   :    int				=			text_inputs[0][0]

				lowercase__   :    Tuple				=			self.model(**_snake_case			,**_snake_case						)

				lowercase__   :    Union[str, Any]				=			{
				    '''candidate_labels''': candidate_labels,
				    '''logits''': outputs.logits_per_image,
				}
				return model_outputs







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Dict				=			model_outputs.pop('''candidate_labels'''						)
				lowercase__   :    Optional[Any]				=			model_outputs['''logits'''][0]
				if self.framework == "pt":
						lowercase__   :    Optional[int]				=			logits.softmax(dim=-1						).squeeze(-1						)
						lowercase__   :    Tuple				=			probs.tolist()
						if not isinstance(_snake_case			,_snake_case						):
								lowercase__   :    Any				=			[scores]
				elif self.framework == "tf":
						lowercase__   :    List[str]				=			stable_softmax(_snake_case			,axis=-1						)
						lowercase__   :    Optional[Any]				=			probs.numpy().tolist()
				else:
						raise ValueError(f"""Unsupported framework: {self.framework}"""						)

				lowercase__   :    Union[str, Any]				=			[
				    {'''score''': score, '''label''': candidate_label}
				    for score, candidate_label in sorted(zip(_snake_case			,_snake_case						)			,key=lambda _snake_case						: -x[0]						)
				]
				return result
