

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			(				__a       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    ['''image_processor''']
						__UpperCAmelCase			     =    '''SamImageProcessor'''







						def __init__(							self					:      Any			,			snake_case_					:      List[str]  ):



								'''simple docstring'''



								super().__init__(snake_case__  )
								snake_case__						:       Union[str, Any]       						=  self.image_processor
								snake_case__						:       Optional[int]       						=  -1_0
								snake_case__						:       List[str]       						=  self.image_processor.size['''longest_edge''']







						def __call__(							self					:      int			,			snake_case_					:      Tuple=None			,			snake_case_					:      Union[str, Any]=None			,			snake_case_					:      Optional[Any]=None			,			snake_case_					:      Union[str, Any]=None			,			snake_case_					:      Optional[Union[str, TensorType]] = None			,			**snake_case_					:      int			,			):



								'''simple docstring'''



								snake_case__						:       Dict       						=  self.image_processor(
								    snake_case__			,			return_tensors=snake_case__			,			**snake_case__			,			)

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

								if hasattr(snake_case__			,			'''numpy'''  ):  # Checks if Torch or TF tensor
										snake_case__						:       Optional[int]       						=  original_sizes.numpy()

								snake_case__						,       snake_case__						,       snake_case__						:       int       						=  self._check_and_preprocess_points(
								    input_points=snake_case__			,			input_labels=snake_case__			,			input_boxes=snake_case__			,			)

								snake_case__						:       Union[str, Any]       						=  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       __magic_name__							(							self					:      Optional[Any]			,			snake_case_					:      List[Any]			,			snake_case_					:      int			,			snake_case_					:      str=None			,			snake_case_					:      Optional[int]=None			,			snake_case_					:      Tuple=None			,			snake_case_					:      Tuple="pt"			,			):



								'''simple docstring'''



								if input_points is not None:
										if len(snake_case__  ) != len(snake_case__  ):
												snake_case__						:       Any       						=  [
												    self._normalize_coordinates(self.target_size			,			snake_case__			,			original_sizes[0]  ) for point in input_points
												]
										else:
												snake_case__						:       str       						=  [
												    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:
														snake_case__						,       snake_case__						:       List[Any]       						=  self._pad_points_and_labels(snake_case__			,			snake_case__  )

										snake_case__						:       List[Any]       						=  np.array(snake_case__  )

								if input_labels is not None:
										snake_case__						:       int       						=  np.array(snake_case__  )

								if input_boxes is not None:
										if len(snake_case__  ) != len(snake_case__  ):
												snake_case__						:       Union[str, Any]       						=  [
												    self._normalize_coordinates(self.target_size			,			snake_case__			,			original_sizes[0]			,			is_bounding_box=snake_case__  )
												    for box in input_boxes
												]
										else:
												snake_case__						:       Optional[Any]       						=  [
												    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__  )
												]
										snake_case__						:       Union[str, Any]       						=  np.array(snake_case__  )

								if input_boxes is not None:
										if return_tensors == "pt":
												snake_case__						:       Optional[Any]       						=  torch.from_numpy(snake_case__  )
												# boxes batch size of 1 by default
												snake_case__						:       Optional[Any]       						=  input_boxes.unsqueeze(1  ) if len(input_boxes.shape  ) != 3 else input_boxes
										elif return_tensors == "tf":
												snake_case__						:       int       						=  tf.convert_to_tensor(snake_case__  )
												# boxes batch size of 1 by default
												snake_case__						:       str       						=  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":
												snake_case__						:       List[str]       						=  torch.from_numpy(snake_case__  )
												# point batch size of 1 by default
												snake_case__						:       Dict       						=  input_points.unsqueeze(1  ) if len(input_points.shape  ) != 4 else input_points
										elif return_tensors == "tf":
												snake_case__						:       Union[str, Any]       						=  tf.convert_to_tensor(snake_case__  )
												# point batch size of 1 by default
												snake_case__						:       str       						=  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":
												snake_case__						:       List[Any]       						=  torch.from_numpy(snake_case__  )
												# point batch size of 1 by default
												snake_case__						:       str       						=  input_labels.unsqueeze(1  ) if len(input_labels.shape  ) != 3 else input_labels
										elif return_tensors == "tf":
												snake_case__						:       Any       						=  tf.convert_to_tensor(snake_case__  )
												# point batch size of 1 by default
												snake_case__						:       int       						=  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       __magic_name__							(							self					:      str			,			snake_case_					:      Optional[Any]			,			snake_case_					:      Union[str, Any]  ):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      List[str]			,			snake_case_					:      int			,			snake_case_					:      np.ndarray			,			snake_case_					:      Optional[int]			,			snake_case_					:      Optional[Any]=False  ):



								'''simple docstring'''



								snake_case__						,       snake_case__						:       Optional[int]       						=  original_size
								snake_case__						,       snake_case__						:       Any       						=  self.image_processor._get_preprocess_shape(snake_case__			,			longest_edge=snake_case__  )
								snake_case__						:       Optional[Any]       						=  deepcopy(snake_case__  ).astype(snake_case__  )

								if is_bounding_box:
										snake_case__						:       List[Any]       						=  coords.reshape(-1			,			2			,			2  )

								snake_case__						:       Optional[int]       						=  coords[..., 0] * (new_w / old_w)
								snake_case__						:       Tuple       						=  coords[..., 1] * (new_h / old_h)

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

								return coords







						def       __magic_name__							(							self					:      Optional[Any]			,			snake_case_					:      Tuple=None			,			snake_case_					:      Optional[int]=None			,			snake_case_					:      Dict=None			,			):



								'''simple docstring'''



								if input_points is not None:
										if hasattr(snake_case__			,			'''numpy'''  ):  # Checks for TF or Torch tensor
												snake_case__						:       List[str]       						=  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.'''  )
										snake_case__						:       int       						=  [np.array(snake_case__  ) for input_point in input_points]
								else:
										snake_case__						:       Union[str, Any]       						=  None

								if input_labels is not None:
										if hasattr(snake_case__			,			'''numpy'''  ):
												snake_case__						:       List[Any]       						=  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.'''  )
										snake_case__						:       Tuple       						=  [np.array(snake_case__  ) for label in input_labels]
								else:
										snake_case__						:       Any       						=  None

								if input_boxes is not None:
										if hasattr(snake_case__			,			'''numpy'''  ):
												snake_case__						:       Union[str, Any]       						=  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.'''  )
										snake_case__						:       List[Any]       						=  [np.array(snake_case__  ).astype(np.floataa  ) for box in input_boxes]
								else:
										snake_case__						:       Any       						=  None

								return input_points, input_labels, input_boxes







						@property
						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



								snake_case__						:       Tuple       						=  self.image_processor.model_input_names
								return list(dict.fromkeys(snake_case__  )  )







						def       __magic_name__							(							self					:      Any			,			*snake_case_					:      Optional[int]			,			**snake_case_					:      Optional[Any]  ):



								'''simple docstring'''



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






'''simple docstring'''


import unittest

from diffusers import FlaxAutoencoderKL
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax

from .test_modeling_common_flax import FlaxModelTesterMixin


if is_flax_available():
					import jax




@require_flax
class     a			(				SCREAMING_SNAKE_CASE			,			unittest.TestCase       ):






						"""simple docstring"""




						__UpperCAmelCase			     =    FlaxAutoencoderKL







						@property
						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  4
								snake_case__						:       Optional[Any]       						=  3
								snake_case__						:       Optional[int]       						=  (3_2, 3_2)

								snake_case__						:       Optional[int]       						=  jax.random.PRNGKey(0  )
								snake_case__						:       Union[str, Any]       						=  jax.random.uniform(snake_case_			,			((batch_size, num_channels) + sizes)  )

								return {"sample": image, "prng_key": prng_key}







						def       __magic_name__							(							self					:      Union[str, Any]  ):



								'''simple docstring'''



								snake_case__						:       Any       						=  {
								    '''block_out_channels''': [3_2, 6_4],
								    '''in_channels''': 3,
								    '''out_channels''': 3,
								    '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
								    '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
								    '''latent_channels''': 4,
								}
								snake_case__						:       Dict       						=  self.dummy_input
								return init_dict, inputs_dict



