






import tensorflow as tf

from ...tf_utils import shape_list



class   lowerCamelCase				(tf.keras.layers.Layer      ):







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase=1    ,		_UpperCamelCase=False    ,		**_UpperCamelCase       )     ->							Union[str, Any]:
							super().__init__(**_UpperCamelCase       )

							UpperCAmelCase_   :				Any						=						vocab_size
							UpperCAmelCase_   :				str						=						d_embed
							UpperCAmelCase_   :				Any						=						d_proj

							UpperCAmelCase_   :				Any						=						cutoffs + [vocab_size]
							UpperCAmelCase_   :				Tuple						=						[0] + self.cutoffs
							UpperCAmelCase_   :				Optional[int]						=						div_val

							UpperCAmelCase_   :				int						=						self.cutoffs[0]
							UpperCAmelCase_   :				Dict						=						len(self.cutoffs       ) - 1
							UpperCAmelCase_   :				Tuple						=						self.shortlist_size + self.n_clusters
							UpperCAmelCase_   :				List[Any]						=						keep_order

							UpperCAmelCase_   :				List[str]						=						[]
							UpperCAmelCase_   :				Dict						=						[]



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							if self.n_clusters > 0:
												UpperCAmelCase_   :				List[str]						=						self.add_weight(
												    shape=(self.n_clusters, self.d_embed)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name='cluster_weight'       )
												UpperCAmelCase_   :				List[Any]						=						self.add_weight(
												    shape=(self.n_clusters,)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name='cluster_bias'       )

							if self.div_val == 1:
												for i in range(len(self.cutoffs       )       ):
																	if self.d_proj != self.d_embed:
																						UpperCAmelCase_   :				List[Any]						=						self.add_weight(
																						    shape=(self.d_embed, self.d_proj)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_projs_._{i}"    ,		)
																						self.out_projs.append(_UpperCamelCase       )
																	else:
																						self.out_projs.append(_UpperCamelCase       )
																	UpperCAmelCase_   :				Union[str, Any]						=						self.add_weight(
																	    shape=(self.vocab_size, self.d_embed)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_layers_._{i}_._weight"    ,		)
																	UpperCAmelCase_   :				Dict						=						self.add_weight(
																	    shape=(self.vocab_size,)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_layers_._{i}_._bias"    ,		)
																	self.out_layers.append((weight, bias)       )
							else:
												for i in range(len(self.cutoffs       )       ):
																	UpperCAmelCase_	,  UpperCAmelCase_   :				Optional[int]						=						self.cutoff_ends[i], self.cutoff_ends[i + 1]
																	UpperCAmelCase_   :				Optional[Any]						=						self.d_embed // (self.div_val**i)

																	UpperCAmelCase_   :				List[Any]						=						self.add_weight(
																	    shape=(d_emb_i, self.d_proj)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_projs_._{i}"       )
																	self.out_projs.append(_UpperCamelCase       )
																	UpperCAmelCase_   :				Optional[Any]						=						self.add_weight(
																	    shape=(r_idx - l_idx, d_emb_i)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_layers_._{i}_._weight"    ,		)
																	UpperCAmelCase_   :				Dict						=						self.add_weight(
																	    shape=(r_idx - l_idx,)    ,		initializer='zeros'    ,		trainable=_UpperCamelCase    ,		name=f"out_layers_._{i}_._bias"    ,		)
																	self.out_layers.append((weight, bias)       )
							super().build(_UpperCamelCase       )



		@staticmethod
		def  __UpperCAmelCase      (							_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase=None       )     ->							Any:
							UpperCAmelCase_   :				Optional[Any]						=						x
							if proj is not None:
												UpperCAmelCase_   :				Tuple						=						tf.einsum('ibd,ed->ibe'    ,		_UpperCamelCase    ,		_UpperCamelCase       )
							return tf.einsum('ibd,nd->ibn'    ,		_UpperCamelCase    ,		_UpperCamelCase       ) + b



		@staticmethod
		def  __UpperCAmelCase      (							_UpperCamelCase    ,		_UpperCamelCase       )     ->							Optional[Any]:
							UpperCAmelCase_   :				Dict						=						shape_list(_UpperCamelCase       )
							UpperCAmelCase_   :				List[Any]						=						tf.range(lp_size[0]    ,		dtype=target.dtype       )
							UpperCAmelCase_   :				Optional[Any]						=						tf.stack([r, target]    ,		1       )
							return tf.gather_nd(_UpperCamelCase    ,		_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase=True    ,		_UpperCamelCase=False       )     ->							List[str]:
							UpperCAmelCase_   :				Optional[int]						=						0
							if self.n_clusters == 0:
												UpperCAmelCase_   :				List[Any]						=						self._logit(_UpperCamelCase    ,		self.out_layers[0][0]    ,		self.out_layers[0][1]    ,		self.out_projs[0]       )
												if target is not None:
																	UpperCAmelCase_   :				Tuple						=						tf.nn.sparse_softmax_cross_entropy_with_logits(labels=_UpperCamelCase    ,		logits=_UpperCamelCase       )
												UpperCAmelCase_   :				Any						=						tf.nn.log_softmax(_UpperCamelCase    ,		axis=-1       )
							else:
												UpperCAmelCase_   :				Optional[int]						=						shape_list(_UpperCamelCase       )
												UpperCAmelCase_   :				Optional[int]						=						[]
												UpperCAmelCase_   :				Tuple						=						tf.zeros(hidden_sizes[:2]       )
												for i in range(len(self.cutoffs       )       ):
																	UpperCAmelCase_	,  UpperCAmelCase_   :				List[str]						=						self.cutoff_ends[i], self.cutoff_ends[i + 1]
																	if target is not None:
																						UpperCAmelCase_   :				Union[str, Any]						=						(target >= l_idx) & (target < r_idx)
																						UpperCAmelCase_   :				Any						=						tf.where(_UpperCamelCase       )
																						UpperCAmelCase_   :				str						=						tf.boolean_mask(_UpperCamelCase    ,		_UpperCamelCase       ) - l_idx

																	if self.div_val == 1:
																						UpperCAmelCase_   :				str						=						self.out_layers[0][0][l_idx:r_idx]
																						UpperCAmelCase_   :				Dict						=						self.out_layers[0][1][l_idx:r_idx]
																	else:
																						UpperCAmelCase_   :				List[Any]						=						self.out_layers[i][0]
																						UpperCAmelCase_   :				Optional[Any]						=						self.out_layers[i][1]

																	if i == 0:
																						UpperCAmelCase_   :				Any						=						tf.concat([cur_W, self.cluster_weight]    ,		0       )
																						UpperCAmelCase_   :				Dict						=						tf.concat([cur_b, self.cluster_bias]    ,		0       )

																						UpperCAmelCase_   :				str						=						self._logit(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		self.out_projs[0]       )
																						UpperCAmelCase_   :				Tuple						=						tf.nn.log_softmax(_UpperCamelCase       )
																						out.append(head_logprob[..., : self.cutoffs[0]]       )
																						if target is not None:
																											UpperCAmelCase_   :				Optional[Any]						=						tf.boolean_mask(_UpperCamelCase    ,		_UpperCamelCase       )
																											UpperCAmelCase_   :				str						=						self._gather_logprob(_UpperCamelCase    ,		_UpperCamelCase       )
																	else:
																						UpperCAmelCase_   :				List[str]						=						self._logit(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		self.out_projs[i]       )
																						UpperCAmelCase_   :				Union[str, Any]						=						tf.nn.log_softmax(_UpperCamelCase       )
																						UpperCAmelCase_   :				str						=						self.cutoffs[0] + i - 1  # No probability for the head cluster
																						UpperCAmelCase_   :				Optional[Any]						=						head_logprob[..., cluster_prob_idx, None] + tail_logprob
																						out.append(_UpperCamelCase       )
																						if target is not None:
																											UpperCAmelCase_   :				str						=						tf.boolean_mask(_UpperCamelCase    ,		_UpperCamelCase       )
																											UpperCAmelCase_   :				str						=						tf.boolean_mask(_UpperCamelCase    ,		_UpperCamelCase       )
																											UpperCAmelCase_   :				Optional[int]						=						self._gather_logprob(_UpperCamelCase    ,		_UpperCamelCase       )
																											cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
																	if target is not None:
																						loss += tf.scatter_nd(_UpperCamelCase    ,		-cur_logprob    ,		shape_list(_UpperCamelCase       )       )
												UpperCAmelCase_   :				List[str]						=						tf.concat(_UpperCamelCase    ,		axis=-1       )

							if target is not None:
												if return_mean:
																	UpperCAmelCase_   :				List[str]						=						tf.reduce_mean(_UpperCamelCase       )
												# Add the training-time loss value to the layer using `self.add_loss()`.
												self.add_loss(_UpperCamelCase       )

												# Log the loss as a metric (we could log arbitrary metrics,
												# including different metrics for training and inference.
												self.add_metric(_UpperCamelCase    ,		name=self.name    ,		aggregation='mean' if return_mean else ''       )

							return out














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, Pipeline


if is_vision_available():
		from ..image_utils import load_image


if is_torch_available():
		import torch

		from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

__UpperCAmelCase     							=						logging.get_logger(__name__)


__UpperCAmelCase     							=						Dict[str, Any]
__UpperCAmelCase     							=						List[Prediction]



@add_end_docstrings(_snake_case      )
class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def __init__(							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Optional[int]:
							super().__init__(*_UpperCamelCase    ,		**_UpperCamelCase       )

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

							requires_backends(self    ,		'vision'       )
							self.check_model_type(
							    dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()       )       )



		def  __UpperCAmelCase      (							self    ,		**_UpperCamelCase       )     ->							Tuple:
							UpperCAmelCase_   :				str						=						{}
							if "threshold" in kwargs:
												UpperCAmelCase_   :				Tuple						=						kwargs['threshold']
							return {}, {}, postprocess_kwargs



		def __call__(							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Union[Predictions, List[Prediction]]:
							return super().__call__(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Dict:
							UpperCAmelCase_   :				Any						=						load_image(_UpperCamelCase       )
							UpperCAmelCase_   :				Any						=						torch.IntTensor([[image.height, image.width]]       )
							UpperCAmelCase_   :				str						=						self.image_processor(images=[image]    ,		return_tensors='pt'       )
							if self.tokenizer is not None:
												UpperCAmelCase_   :				List[str]						=						self.tokenizer(text=inputs['words']    ,		boxes=inputs['boxes']    ,		return_tensors='pt'       )
							UpperCAmelCase_   :				Any						=						target_size
							return inputs



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							List[Any]:
							UpperCAmelCase_   :				Any						=						model_inputs.pop('target_size'       )
							UpperCAmelCase_   :				Optional[Any]						=						self.model(**_UpperCamelCase       )
							UpperCAmelCase_   :				Dict						=						outputs.__class__({'target_size': target_size, **outputs}       )
							if self.tokenizer is not None:
												UpperCAmelCase_   :				List[str]						=						model_inputs['bbox']
							return model_outputs



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase=0.9       )     ->							List[str]:
							UpperCAmelCase_   :				List[Any]						=						model_outputs['target_size']
							if self.tokenizer is not None:
												# This is a LayoutLMForTokenClassification variant.
												# The OCR got the boxes and the model classified the words.
												UpperCAmelCase_	,  UpperCAmelCase_   :				str						=						target_size[0].tolist()

												def unnormalize(_UpperCamelCase       ):
																	return self._get_bounding_box(
																	    torch.Tensor(
																	        [
																	            (width * bbox[0] / 1_0_0_0),
																	            (height * bbox[1] / 1_0_0_0),
																	            (width * bbox[2] / 1_0_0_0),
																	            (height * bbox[3] / 1_0_0_0),
																	        ]       )       )

												UpperCAmelCase_	,  UpperCAmelCase_   :				List[Any]						=						model_outputs['logits'].squeeze(0       ).softmax(dim=-1       ).max(dim=-1       )
												UpperCAmelCase_   :				Union[str, Any]						=						[self.model.config.idalabel[prediction] for prediction in classes.tolist()]
												UpperCAmelCase_   :				Tuple						=						[unnormalize(_UpperCamelCase       ) for bbox in model_outputs['bbox'].squeeze(0       )]
												UpperCAmelCase_   :				List[str]						=						['score', 'label', 'box']
												UpperCAmelCase_   :				Any						=						[dict(zip(_UpperCamelCase    ,		_UpperCamelCase       )       ) for vals in zip(scores.tolist()    ,		_UpperCamelCase    ,		_UpperCamelCase       ) if vals[0] > threshold]
							else:
												# This is a regular ForObjectDetectionModel
												UpperCAmelCase_   :				Union[str, Any]						=						self.image_processor.post_process_object_detection(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )
												UpperCAmelCase_   :				str						=						raw_annotations[0]
												UpperCAmelCase_   :				str						=						raw_annotation['scores']
												UpperCAmelCase_   :				Tuple						=						raw_annotation['labels']
												UpperCAmelCase_   :				List[Any]						=						raw_annotation['boxes']

												UpperCAmelCase_   :				Union[str, Any]						=						scores.tolist()
												UpperCAmelCase_   :				int						=						[self.model.config.idalabel[label.item()] for label in labels]
												UpperCAmelCase_   :				Any						=						[self._get_bounding_box(_UpperCamelCase       ) for box in boxes]

												# {"scores": [...], ...} --> [{"score":x, ...}, ...]
												UpperCAmelCase_   :				int						=						['score', 'label', 'box']
												UpperCAmelCase_   :				Dict						=						[
												    dict(zip(_UpperCamelCase    ,		_UpperCamelCase       )       )
												    for vals in zip(raw_annotation['scores']    ,		raw_annotation['labels']    ,		raw_annotation['boxes']       )
												]

							return annotation



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Dict[str, int]:
							if self.framework != "pt":
												raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.'       )
							UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :				Dict						=						box.int().tolist()
							UpperCAmelCase_   :				str						=						{
							    'xmin': xmin,
							    'ymin': ymin,
							    'xmax': xmax,
							    'ymax': ymax,
							}
							return bbox






