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     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	Optional[int]						=["image_processor"]
  SCREAMING_SNAKE_CASE_	:	Optional[Any]						="SamImageProcessor"






  def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      List[Any]:





       '''simple docstring'''





       super().__init__(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      self.image_processor
       UpperCamelCase__										=      -10
       UpperCamelCase__										=      self.image_processor.size['''longest_edge''']






  def __call__(self   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_ = None   ,				**SCREAMING_SNAKE_CASE_   ,				)		->      BatchEncoding:





       '''simple docstring'''





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

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

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

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

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

       return encoding_image_processor






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_="pt"   ,				)		->      Any:





       '''simple docstring'''





       if input_points is not None:
            if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
                 UpperCamelCase__										=      [
                     self._normalize_coordinates(self.target_size   ,				SCREAMING_SNAKE_CASE_   ,				original_sizes[0]	) for point in input_points
                 ]
            else:
                 UpperCamelCase__										=      [
                     self._normalize_coordinates(self.target_size   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)
                     for point, original_size in zip(SCREAMING_SNAKE_CASE_   ,				SCREAMING_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(SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)

            UpperCamelCase__										=      np.array(SCREAMING_SNAKE_CASE_	)

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

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

       if input_boxes is not None:
            if return_tensors == "pt":
                 UpperCamelCase__										=      torch.from_numpy(SCREAMING_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(SCREAMING_SNAKE_CASE_	)
                 # boxes batch size of 1 by default
                 UpperCamelCase__										=      tf.expand_dims(SCREAMING_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(SCREAMING_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(SCREAMING_SNAKE_CASE_	)
                 # point batch size of 1 by default
                 UpperCamelCase__										=      tf.expand_dims(SCREAMING_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(SCREAMING_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(SCREAMING_SNAKE_CASE_	)
                 # point batch size of 1 by default
                 UpperCamelCase__										=      tf.expand_dims(SCREAMING_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 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)		->      Dict:





       '''simple docstring'''





       UpperCamelCase__										=      max([point.shape[0] for point in input_points]	)
       UpperCamelCase__										=      []
       for i, point in enumerate(SCREAMING_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(SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      processed_input_points
       return input_points, input_labels






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=False	)		->      np.ndarray:





       '''simple docstring'''





       UpperCamelCase__						,       UpperCamelCase__										=      original_size
       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor._get_preprocess_shape(SCREAMING_SNAKE_CASE_   ,				longest_edge=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      deepcopy(SCREAMING_SNAKE_CASE_	).astype(SCREAMING_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 		_a					(self   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=None   ,				)		->      Tuple:





       '''simple docstring'''





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

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

       if input_labels is not None:
            if hasattr(SCREAMING_SNAKE_CASE_   ,				'''numpy'''	):
                 UpperCamelCase__										=      input_labels.numpy().tolist()

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

       if input_boxes is not None:
            if hasattr(SCREAMING_SNAKE_CASE_   ,				'''numpy'''	):
                 UpperCamelCase__										=      input_boxes.numpy().tolist()

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

       return input_points, input_labels, input_boxes






  @property
  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





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






  def 		_a					(self   ,				*SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_	)		->      int:





       '''simple docstring'''





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







from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin



class     _A   (    __UpperCamelCase	, __UpperCamelCase							):






  @register_to_config
  def __init__(self   ,				SCREAMING_SNAKE_CASE_ = 768   ,				)		->      Tuple:





       '''simple docstring'''





       super().__init__()

       UpperCamelCase__										=      nn.Parameter(torch.zeros(1   ,				SCREAMING_SNAKE_CASE_	)	)
       UpperCamelCase__										=      nn.Parameter(torch.ones(1   ,				SCREAMING_SNAKE_CASE_	)	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      nn.Parameter(self.mean.to(SCREAMING_SNAKE_CASE_	).to(SCREAMING_SNAKE_CASE_	)	)
       UpperCamelCase__										=      nn.Parameter(self.std.to(SCREAMING_SNAKE_CASE_	).to(SCREAMING_SNAKE_CASE_	)	)
       return self






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      (embeds - self.mean) * 1.0 / self.std
       return embeds






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      (embeds * self.std) + self.mean
       return embeds



