


from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block


@dataclass
class     lowercase__  (					__SCREAMING_SNAKE_CASE		):
  A__=  42




class     lowercase__  (					__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE		):






  @register_to_config
  def __init__(					self      :					Dict		,						_lowercase      :					int = 6_55_36		,						_lowercase      :					Optional[int] = None		,						_lowercase      :					int = 2		,						_lowercase      :					int = 2		,						_lowercase      :					int = 0		,						_lowercase      :					str = "fourier"		,						_lowercase      :					bool = True		,						_lowercase      :					bool = False		,						_lowercase      :					float = 0.0		,						_lowercase      :					Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")		,						_lowercase      :					Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")		,						_lowercase      :					Tuple[str] = "UNetMidBlock1D"		,						_lowercase      :					str = None		,						_lowercase      :					Tuple[int] = (32, 32, 64)		,						_lowercase      :					str = None		,						_lowercase      :					int = 8		,						_lowercase      :					int = 1		,						_lowercase      :					bool = False		,						):




         """simple docstring"""







         super().__init__()
         UpperCAmelCase__										=     sample_size

         # time
         if time_embedding_type == "fourier":
                UpperCAmelCase__										=     GaussianFourierProjection(
                    embedding_size=8		,						set_W_to_weight=_lowercase		,						log=_lowercase		,						flip_sin_to_cos=_lowercase )
                UpperCAmelCase__										=     2 * block_out_channels[0]
         elif time_embedding_type == "positional":
                UpperCAmelCase__										=     Timesteps(
                    block_out_channels[0]		,						flip_sin_to_cos=_lowercase		,						downscale_freq_shift=_lowercase )
                UpperCAmelCase__										=     block_out_channels[0]

         if use_timestep_embedding:
                UpperCAmelCase__										=     block_out_channels[0] * 4
                UpperCAmelCase__										=     TimestepEmbedding(
                    in_channels=_lowercase		,						time_embed_dim=_lowercase		,						act_fn=_lowercase		,						out_dim=block_out_channels[0]		,						)

         UpperCAmelCase__										=     nn.ModuleList([] )
         UpperCAmelCase__										=     None
         UpperCAmelCase__										=     nn.ModuleList([] )
         UpperCAmelCase__										=     None

         # down
         UpperCAmelCase__										=     in_channels
         for i, down_block_type in enumerate(_lowercase ):
                UpperCAmelCase__										=     output_channel
                UpperCAmelCase__										=     block_out_channels[i]

                if i == 0:
                       input_channel += extra_in_channels

                UpperCAmelCase__										=     i == len(_lowercase ) - 1

                UpperCAmelCase__										=     get_down_block(
                    _lowercase		,						num_layers=_lowercase		,						in_channels=_lowercase		,						out_channels=_lowercase		,						temb_channels=block_out_channels[0]		,						add_downsample=not is_final_block or downsample_each_block		,						)
                self.down_blocks.append(_lowercase )

         # mid
         UpperCAmelCase__										=     get_mid_block(
             _lowercase		,						in_channels=block_out_channels[-1]		,						mid_channels=block_out_channels[-1]		,						out_channels=block_out_channels[-1]		,						embed_dim=block_out_channels[0]		,						num_layers=_lowercase		,						add_downsample=_lowercase		,						)

         # up
         UpperCAmelCase__										=     list(reversed(_lowercase ) )
         UpperCAmelCase__										=     reversed_block_out_channels[0]
         if out_block_type is None:
                UpperCAmelCase__										=     out_channels
         else:
                UpperCAmelCase__										=     block_out_channels[0]

         for i, up_block_type in enumerate(_lowercase ):
                UpperCAmelCase__										=     output_channel
                UpperCAmelCase__										=     (
                    reversed_block_out_channels[i + 1] if i < len(_lowercase ) - 1 else final_upsample_channels
                )

                UpperCAmelCase__										=     i == len(_lowercase ) - 1

                UpperCAmelCase__										=     get_up_block(
                    _lowercase		,						num_layers=_lowercase		,						in_channels=_lowercase		,						out_channels=_lowercase		,						temb_channels=block_out_channels[0]		,						add_upsample=not is_final_block		,						)
                self.up_blocks.append(_lowercase )
                UpperCAmelCase__										=     output_channel

         # out
         UpperCAmelCase__										=     norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4		,						32 )
         UpperCAmelCase__										=     get_out_block(
             out_block_type=_lowercase		,						num_groups_out=_lowercase		,						embed_dim=block_out_channels[0]		,						out_channels=_lowercase		,						act_fn=_lowercase		,						fc_dim=block_out_channels[-1] // 4		,						)






  def 			_UpperCAmelCase  (					self      :					List[Any]		,						_lowercase      :					torch.FloatTensor		,						_lowercase      :					Union[torch.Tensor, float, int]		,						_lowercase      :					bool = True		,						):




         """simple docstring"""







         UpperCAmelCase__										=     timestep
         if not torch.is_tensor(_lowercase ):
                UpperCAmelCase__										=     torch.tensor([timesteps]		,						dtype=torch.long		,						device=sample.device )
         elif torch.is_tensor(_lowercase ) and len(timesteps.shape ) == 0:
                UpperCAmelCase__										=     timesteps[None].to(sample.device )

         UpperCAmelCase__										=     self.time_proj(_lowercase )
         if self.config.use_timestep_embedding:
                UpperCAmelCase__										=     self.time_mlp(_lowercase )
         else:
                UpperCAmelCase__										=     timestep_embed[..., None]
                UpperCAmelCase__										=     timestep_embed.repeat([1, 1, sample.shape[2]] ).to(sample.dtype )
                UpperCAmelCase__										=     timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:]) )

         # 2. down
         UpperCAmelCase__										=     ()
         for downsample_block in self.down_blocks:
                UpperCAmelCase__						,			UpperCAmelCase__										=     downsample_block(hidden_states=_lowercase		,						temb=_lowercase )
                down_block_res_samples += res_samples

         # 3. mid
         if self.mid_block:
                UpperCAmelCase__										=     self.mid_block(_lowercase		,						_lowercase )

         # 4. up
         for i, upsample_block in enumerate(self.up_blocks ):
                UpperCAmelCase__										=     down_block_res_samples[-1:]
                UpperCAmelCase__										=     down_block_res_samples[:-1]
                UpperCAmelCase__										=     upsample_block(_lowercase		,						res_hidden_states_tuple=_lowercase		,						temb=_lowercase )

         # 5. post-process
         if self.out_block:
                UpperCAmelCase__										=     self.out_block(_lowercase		,						_lowercase )

         if not return_dict:
                return (sample,)

         return UNetaDOutput(sample=_lowercase )









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     lowercase__  (					__SCREAMING_SNAKE_CASE		):
  A__=  ['image_processor']
  A__=  'SamImageProcessor'






  def __init__(					self      :					int		,						_lowercase      :					List[Any] ):




         """simple docstring"""







         super().__init__(_lowercase )
         UpperCAmelCase__										=     self.image_processor
         UpperCAmelCase__										=     -10
         UpperCAmelCase__										=     self.image_processor.size["longest_edge"]






  def __call__(					self      :					Union[str, Any]		,						_lowercase      :					str=None		,						_lowercase      :					Union[str, Any]=None		,						_lowercase      :					List[Any]=None		,						_lowercase      :					Dict=None		,						_lowercase      :					Optional[Union[str, TensorType]] = None		,						**_lowercase      :					Union[str, Any]		,						):




         """simple docstring"""







         UpperCAmelCase__										=     self.image_processor(
             _lowercase		,						return_tensors=_lowercase		,						**_lowercase		,						)

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

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

         UpperCAmelCase__						,			UpperCAmelCase__						,			UpperCAmelCase__										=     self._check_and_preprocess_points(
             input_points=_lowercase		,						input_labels=_lowercase		,						input_boxes=_lowercase		,						)

         UpperCAmelCase__										=     self._normalize_and_convert(
             _lowercase		,						_lowercase		,						input_points=_lowercase		,						input_labels=_lowercase		,						input_boxes=_lowercase		,						return_tensors=_lowercase		,						)

         return encoding_image_processor






  def 			_UpperCAmelCase  (					self      :					Dict		,						_lowercase      :					int		,						_lowercase      :					Dict		,						_lowercase      :					Optional[Any]=None		,						_lowercase      :					Any=None		,						_lowercase      :					Union[str, Any]=None		,						_lowercase      :					List[Any]="pt"		,						):




         """simple docstring"""







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

                UpperCAmelCase__										=     np.array(_lowercase )

         if input_labels is not None:
                UpperCAmelCase__										=     np.array(_lowercase )

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

         if input_boxes is not None:
                if return_tensors == "pt":
                       UpperCAmelCase__										=     torch.from_numpy(_lowercase )
                       # 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(_lowercase )
                       # boxes batch size of 1 by default
                       UpperCAmelCase__										=     tf.expand_dims(_lowercase		,						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(_lowercase )
                       # 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(_lowercase )
                       # point batch size of 1 by default
                       UpperCAmelCase__										=     tf.expand_dims(_lowercase		,						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(_lowercase )
                       # 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(_lowercase )
                       # point batch size of 1 by default
                       UpperCAmelCase__										=     tf.expand_dims(_lowercase		,						1 ) if len(input_labels.shape ) != 3 else input_labels
                encoding_image_processor.update({"input_labels": input_labels} )

         return encoding_image_processor






  def 			_UpperCAmelCase  (					self      :					Union[str, Any]		,						_lowercase      :					Any		,						_lowercase      :					Optional[Any] ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Any		,						_lowercase      :					int		,						_lowercase      :					np.ndarray		,						_lowercase      :					Optional[Any]		,						_lowercase      :					Any=False ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     original_size
         UpperCAmelCase__						,			UpperCAmelCase__										=     self.image_processor._get_preprocess_shape(_lowercase		,						longest_edge=_lowercase )
         UpperCAmelCase__										=     deepcopy(_lowercase ).astype(_lowercase )

         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 			_UpperCAmelCase  (					self      :					int		,						_lowercase      :					Tuple=None		,						_lowercase      :					Optional[Any]=None		,						_lowercase      :					List[Any]=None		,						):




         """simple docstring"""







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

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

         if input_labels is not None:
                if hasattr(_lowercase		,						"numpy" ):
                       UpperCAmelCase__										=     input_labels.numpy().tolist()

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

         if input_boxes is not None:
                if hasattr(_lowercase		,						"numpy" ):
                       UpperCAmelCase__										=     input_boxes.numpy().tolist()

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

         return input_points, input_labels, input_boxes






  @property
  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.image_processor.model_input_names
         return list(dict.fromkeys(_lowercase ) )






  def 			_UpperCAmelCase  (					self      :					int		,						*_lowercase      :					List[str]		,						**_lowercase      :					Any ):




         """simple docstring"""







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





