


from copy import deepcopy
from typing import Optional, Union

import numpy as np

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


if is_torch_available():
						import torch

if is_tf_available():
						import tensorflow as tf







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       str 			=		['image_processor']
			_UpperCamelCase		:       List[str] 			=		'SamImageProcessor'




			def __init__(				self   ,    snake_case  ):

						'''simple docstring'''


						super().__init__(snake_case  )
						UpperCamelCase__       =							self.image_processor
						UpperCamelCase__       =							-10
						UpperCamelCase__       =							self.image_processor.size["longest_edge"]




			def __call__(				self   ,    snake_case=None   ,    snake_case=None   ,    snake_case=None   ,    snake_case=None   ,    snake_case = None   ,    **snake_case   ,    ):

						'''simple docstring'''


						UpperCamelCase__       =							self.image_processor(
						    snake_case   ,    return_tensors=snake_case   ,    **snake_case   ,    )

						# pop arguments that are not used in the foward but used nevertheless
						UpperCamelCase__       =							encoding_image_processor["original_sizes"]

						if hasattr(snake_case   ,    "numpy"  ):  # Checks if Torch or TF tensor
									UpperCamelCase__       =							original_sizes.numpy()

						UpperCamelCase__,   UpperCamelCase__,   UpperCamelCase__       =							self._check_and_preprocess_points(
						    input_points=snake_case   ,    input_labels=snake_case   ,    input_boxes=snake_case   ,    )

						UpperCamelCase__       =							self._normalize_and_convert(
						    snake_case   ,    snake_case   ,    input_points=snake_case   ,    input_labels=snake_case   ,    input_boxes=snake_case   ,    return_tensors=snake_case   ,    )

						return encoding_image_processor




			def 					snake_case__					(				self   ,    snake_case   ,    snake_case   ,    snake_case=None   ,    snake_case=None   ,    snake_case=None   ,    snake_case="pt"   ,    ):

						'''simple docstring'''


						if input_points is not None:
									if len(snake_case  ) != len(snake_case  ):
												UpperCamelCase__       =							[
												    self._normalize_coordinates(self.target_size   ,    snake_case   ,    original_sizes[0]  ) for point in input_points
												]
									else:
												UpperCamelCase__       =							[
												    self._normalize_coordinates(self.target_size   ,    snake_case   ,    snake_case  )
												    for point, original_size in zip(snake_case   ,    snake_case  )
												]
									# check that all arrays have the same shape
									if not all(point.shape == input_points[0].shape for point in input_points  ):
												if input_labels is not None:
															UpperCamelCase__,   UpperCamelCase__       =							self._pad_points_and_labels(snake_case   ,    snake_case  )

									UpperCamelCase__       =							np.array(snake_case  )

						if input_labels is not None:
									UpperCamelCase__       =							np.array(snake_case  )

						if input_boxes is not None:
									if len(snake_case  ) != len(snake_case  ):
												UpperCamelCase__       =							[
												    self._normalize_coordinates(self.target_size   ,    snake_case   ,    original_sizes[0]   ,    is_bounding_box=snake_case  )
												    for box in input_boxes
												]
									else:
												UpperCamelCase__       =							[
												    self._normalize_coordinates(self.target_size   ,    snake_case   ,    snake_case   ,    is_bounding_box=snake_case  )
												    for box, original_size in zip(snake_case   ,    snake_case  )
												]
									UpperCamelCase__       =							np.array(snake_case  )

						if input_boxes is not None:
									if return_tensors == "pt":
												UpperCamelCase__       =							torch.from_numpy(snake_case  )
												# boxes batch size of 1 by default
												UpperCamelCase__       =							input_boxes.unsqueeze(1  ) if len(input_boxes.shape  ) != 3 else input_boxes
									elif return_tensors == "tf":
												UpperCamelCase__       =							tf.convert_to_tensor(snake_case  )
												# boxes batch size of 1 by default
												UpperCamelCase__       =							tf.expand_dims(snake_case   ,    1  ) if len(input_boxes.shape  ) != 3 else input_boxes
									encoding_image_processor.update({"input_boxes": input_boxes}  )
						if input_points is not None:
									if return_tensors == "pt":
												UpperCamelCase__       =							torch.from_numpy(snake_case  )
												# point batch size of 1 by default
												UpperCamelCase__       =							input_points.unsqueeze(1  ) if len(input_points.shape  ) != 4 else input_points
									elif return_tensors == "tf":
												UpperCamelCase__       =							tf.convert_to_tensor(snake_case  )
												# point batch size of 1 by default
												UpperCamelCase__       =							tf.expand_dims(snake_case   ,    1  ) if len(input_points.shape  ) != 4 else input_points
									encoding_image_processor.update({"input_points": input_points}  )
						if input_labels is not None:
									if return_tensors == "pt":
												UpperCamelCase__       =							torch.from_numpy(snake_case  )
												# point batch size of 1 by default
												UpperCamelCase__       =							input_labels.unsqueeze(1  ) if len(input_labels.shape  ) != 3 else input_labels
									elif return_tensors == "tf":
												UpperCamelCase__       =							tf.convert_to_tensor(snake_case  )
												# point batch size of 1 by default
												UpperCamelCase__       =							tf.expand_dims(snake_case   ,    1  ) if len(input_labels.shape  ) != 3 else input_labels
									encoding_image_processor.update({"input_labels": input_labels}  )

						return encoding_image_processor




			def 					snake_case__					(				self   ,    snake_case   ,    snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							max([point.shape[0] for point in input_points]  )
						UpperCamelCase__       =							[]
						for i, point in enumerate(snake_case  ):
									if point.shape[0] != expected_nb_points:
												UpperCamelCase__       =							np.concatenate(
												    [point, np.zeros((expected_nb_points - point.shape[0], 2)  ) + self.point_pad_value]   ,    axis=0  )
												UpperCamelCase__       =							np.append(input_labels[i]   ,    [self.point_pad_value]  )
									processed_input_points.append(snake_case  )
						UpperCamelCase__       =							processed_input_points
						return input_points, input_labels




			def 					snake_case__					(				self   ,    snake_case   ,    snake_case   ,    snake_case   ,    snake_case=False  ):

						'''simple docstring'''


						UpperCamelCase__,   UpperCamelCase__       =							original_size
						UpperCamelCase__,   UpperCamelCase__       =							self.image_processor._get_preprocess_shape(snake_case   ,    longest_edge=snake_case  )
						UpperCamelCase__       =							deepcopy(snake_case  ).astype(snake_case  )

						if is_bounding_box:
									UpperCamelCase__       =							coords.reshape(-1   ,    2   ,    2  )

						UpperCamelCase__       =							coords[..., 0] * (new_w / old_w)
						UpperCamelCase__       =							coords[..., 1] * (new_h / old_h)

						if is_bounding_box:
									UpperCamelCase__       =							coords.reshape(-1   ,    4  )

						return coords




			def 					snake_case__					(				self   ,    snake_case=None   ,    snake_case=None   ,    snake_case=None   ,    ):

						'''simple docstring'''


						if input_points is not None:
									if hasattr(snake_case   ,    "numpy"  ):  # Checks for TF or Torch tensor
												UpperCamelCase__       =							input_points.numpy().tolist()

									if not isinstance(snake_case   ,    snake_case  ) or not isinstance(input_points[0]   ,    snake_case  ):
												raise ValueError("Input points must be a list of list of floating points."  )
									UpperCamelCase__       =							[np.array(snake_case  ) for input_point in input_points]
						else:
									UpperCamelCase__       =							None

						if input_labels is not None:
									if hasattr(snake_case   ,    "numpy"  ):
												UpperCamelCase__       =							input_labels.numpy().tolist()

									if not isinstance(snake_case   ,    snake_case  ) or not isinstance(input_labels[0]   ,    snake_case  ):
												raise ValueError("Input labels must be a list of list integers."  )
									UpperCamelCase__       =							[np.array(snake_case  ) for label in input_labels]
						else:
									UpperCamelCase__       =							None

						if input_boxes is not None:
									if hasattr(snake_case   ,    "numpy"  ):
												UpperCamelCase__       =							input_boxes.numpy().tolist()

									if (
									    not isinstance(snake_case   ,    snake_case  )
									    or not isinstance(input_boxes[0]   ,    snake_case  )
									    or not isinstance(input_boxes[0][0]   ,    snake_case  )
									):
												raise ValueError("Input boxes must be a list of list of list of floating points."  )
									UpperCamelCase__       =							[np.array(snake_case  ).astype(np.floataa  ) for box in input_boxes]
						else:
									UpperCamelCase__       =							None

						return input_points, input_labels, input_boxes




			@property
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.image_processor.model_input_names
						return list(dict.fromkeys(snake_case  )  )







			def 					snake_case__					(				self   ,    *snake_case   ,    **snake_case  ):

						'''simple docstring'''


						return self.image_processor.post_process_masks(*snake_case   ,    **snake_case  )






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCamelCase     		=							{
    'configuration_xlm_roberta_xl': [
        'XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'XLMRobertaXLConfig',
        'XLMRobertaXLOnnxConfig',
    ],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__UpperCamelCase     		=							[
						    'XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'XLMRobertaXLForCausalLM',
						    'XLMRobertaXLForMaskedLM',
						    'XLMRobertaXLForMultipleChoice',
						    'XLMRobertaXLForQuestionAnswering',
						    'XLMRobertaXLForSequenceClassification',
						    'XLMRobertaXLForTokenClassification',
						    'XLMRobertaXLModel',
						    'XLMRobertaXLPreTrainedModel',
						]

if TYPE_CHECKING:
						from .configuration_xlm_roberta_xl import (
						    XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    XLMRobertaXLConfig,
						    XLMRobertaXLOnnxConfig,
						)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_xlm_roberta_xl import (
												    XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
												    XLMRobertaXLForCausalLM,
												    XLMRobertaXLForMaskedLM,
												    XLMRobertaXLForMultipleChoice,
												    XLMRobertaXLForQuestionAnswering,
												    XLMRobertaXLForSequenceClassification,
												    XLMRobertaXLForTokenClassification,
												    XLMRobertaXLModel,
												    XLMRobertaXLPreTrainedModel,
												)

else:
						import sys

						__UpperCamelCase     		=							_LazyModule(__name__, globals()['__file__'], _import_structure)


