






'''simple docstring'''
import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
 import torch

if is_tf_available():
 import tensorflow as tf


@require_vision
@require_torchvision
class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  tempfile.mkdtemp()
       lowercase_						:     int							     =  SamImageProcessor()
       lowercase_						:     Tuple							     =  SamProcessor(a      )
       processor.save_pretrained(self.tmpdirname      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]	,  **a	:							Dict      ):






       '''simple docstring'''






       return AutoProcessor.from_pretrained(self.tmpdirname	,  **a      ).image_processor




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       shutil.rmtree(self.tmpdirname      )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  [np.random.randint(2_5_5	,  size=(3, 3_0, 4_0_0)	,  dtype=np.uinta      )]

       lowercase_						:     List[str]							     =  [Image.fromarray(np.moveaxis(a	,  0	,  -1      )      ) for x in image_inputs]

       return image_inputs




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     Tuple							     =  SamProcessor(image_processor=self.get_image_processor()      )
       processor.save_pretrained(self.tmpdirname      )

       lowercase_						:     int							     =  self.get_image_processor(do_normalize=a	,  padding_value=1.0      )

       lowercase_						:     Tuple							     =  SamProcessor.from_pretrained(self.tmpdirname	,  do_normalize=a	,  padding_value=1.0      )

       self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()      )
       self.assertIsInstance(processor.image_processor	,  a      )




    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  self.get_image_processor()

       lowercase_						:     Dict							     =  SamProcessor(image_processor=a      )

       lowercase_						:     Any							     =  self.prepare_image_inputs()

       lowercase_						:     Tuple							     =  image_processor(a	,  return_tensors="np"      )
       lowercase_						:     Dict							     =  processor(images=a	,  return_tensors="np"      )

       input_feat_extract.pop("original_sizes"      )  # pop original_sizes as it is popped in the processor
       input_feat_extract.pop("reshaped_input_sizes"      )  # pop original_sizes as it is popped in the processor

       for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()	,  input_processor[key].sum()	,  delta=1e-2      )





    @require_torch
    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.get_image_processor()

       lowercase_						:     Optional[Any]							     =  SamProcessor(image_processor=a      )
       lowercase_						:     Tuple							     =  [torch.ones((1, 3, 5, 5)      )]

       lowercase_						:     Optional[int]							     =  [[1_7_6_4, 2_6_4_6]]

       lowercase_						:     int							     =  [[6_8_3, 1_0_2_4]]
       lowercase_						:     Optional[int]							     =  processor.post_process_masks(a	,  a	,  a      )
       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       lowercase_						:     List[str]							     =  processor.post_process_masks(
           a	,  torch.tensor(a      )	,  torch.tensor(a      )      )
       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       # should also work with np
       lowercase_						:     Optional[Any]							     =  [np.ones((1, 3, 5, 5)      )]
       lowercase_						:     Optional[int]							     =  processor.post_process_masks(a	,  np.array(a      )	,  np.array(a      )      )

       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       lowercase_						:     Optional[Any]							     =  [[1, 0], [0, 1]]
       with self.assertRaises(a      ):
          lowercase_						:     Optional[int]							     =  processor.post_process_masks(a	,  np.array(a      )	,  np.array(a      )      )


@require_vision
@require_tf
class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  tempfile.mkdtemp()
       lowercase_						:     Any							     =  SamImageProcessor()
       lowercase_						:     List[Any]							     =  SamProcessor(a      )
       processor.save_pretrained(self.tmpdirname      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]	,  **a	:							Any      ):






       '''simple docstring'''






       return AutoProcessor.from_pretrained(self.tmpdirname	,  **a      ).image_processor




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       shutil.rmtree(self.tmpdirname      )




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  [np.random.randint(2_5_5	,  size=(3, 3_0, 4_0_0)	,  dtype=np.uinta      )]

       lowercase_						:     List[str]							     =  [Image.fromarray(np.moveaxis(a	,  0	,  -1      )      ) for x in image_inputs]

       return image_inputs




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  SamProcessor(image_processor=self.get_image_processor()      )
       processor.save_pretrained(self.tmpdirname      )

       lowercase_						:     int							     =  self.get_image_processor(do_normalize=a	,  padding_value=1.0      )

       lowercase_						:     str							     =  SamProcessor.from_pretrained(self.tmpdirname	,  do_normalize=a	,  padding_value=1.0      )

       self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()      )
       self.assertIsInstance(processor.image_processor	,  a      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  self.get_image_processor()

       lowercase_						:     Union[str, Any]							     =  SamProcessor(image_processor=a      )

       lowercase_						:     Optional[int]							     =  self.prepare_image_inputs()

       lowercase_						:     List[Any]							     =  image_processor(a	,  return_tensors="np"      )
       lowercase_						:     int							     =  processor(images=a	,  return_tensors="np"      )

       input_feat_extract.pop("original_sizes"      )  # pop original_sizes as it is popped in the processor
       input_feat_extract.pop("reshaped_input_sizes"      )  # pop reshaped_input_sizes as it is popped in the processor

       for key in input_feat_extract.keys():
          self.assertAlmostEqual(input_feat_extract[key].sum()	,  input_processor[key].sum()	,  delta=1e-2      )





    @require_tf
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     Tuple							     =  self.get_image_processor()

       lowercase_						:     List[Any]							     =  SamProcessor(image_processor=a      )
       lowercase_						:     Optional[Any]							     =  [tf.ones((1, 3, 5, 5)      )]

       lowercase_						:     int							     =  [[1_7_6_4, 2_6_4_6]]

       lowercase_						:     List[str]							     =  [[6_8_3, 1_0_2_4]]
       lowercase_						:     Dict							     =  processor.post_process_masks(a	,  a	,  a	,  return_tensors="tf"      )
       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       lowercase_						:     Optional[Any]							     =  processor.post_process_masks(
           a	,  tf.convert_to_tensor(a      )	,  tf.convert_to_tensor(a      )	,  return_tensors="tf"	,  )
       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       # should also work with np
       lowercase_						:     Union[str, Any]							     =  [np.ones((1, 3, 5, 5)      )]
       lowercase_						:     List[Any]							     =  processor.post_process_masks(
           a	,  np.array(a      )	,  np.array(a      )	,  return_tensors="tf"      )

       self.assertEqual(masks[0].shape	,  (1, 3, 1_7_6_4, 2_6_4_6)      )

       lowercase_						:     List[Any]							     =  [[1, 0], [0, 1]]
       with self.assertRaises(tf.errors.InvalidArgumentError      ):
          lowercase_						:     List[str]							     =  processor.post_process_masks(
              a	,  np.array(a      )	,  np.array(a      )	,  return_tensors="tf"      )


@require_vision
@require_torchvision
class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  tempfile.mkdtemp()
       lowercase_						:     Tuple							     =  SamImageProcessor()
       lowercase_						:     Optional[Any]							     =  SamProcessor(a      )
       processor.save_pretrained(self.tmpdirname      )




    def 					lowerCAmelCase__					(      self	:							Tuple	,  **a	:							Dict      ):






       '''simple docstring'''






       return AutoProcessor.from_pretrained(self.tmpdirname	,  **a      ).image_processor




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       shutil.rmtree(self.tmpdirname      )




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






       '''simple docstring'''






       lowercase_						:     Dict							     =  [np.random.randint(2_5_5	,  size=(3, 3_0, 4_0_0)	,  dtype=np.uinta      )]

       lowercase_						:     Dict							     =  [Image.fromarray(np.moveaxis(a	,  0	,  -1      )      ) for x in image_inputs]

       return image_inputs




    @is_pt_tf_cross_test
    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  self.get_image_processor()

       lowercase_						:     Union[str, Any]							     =  SamProcessor(image_processor=a      )
       lowercase_						:     int							     =  np.random.randint(0	,  2	,  size=(1, 3, 5, 5)      ).astype(np.floataa      )
       lowercase_						:     int							     =  [tf.convert_to_tensor(a      )]
       lowercase_						:     List[Any]							     =  [torch.tensor(a      )]

       lowercase_						:     Dict							     =  [[1_7_6_4, 2_6_4_6]]

       lowercase_						:     Dict							     =  [[6_8_3, 1_0_2_4]]
       lowercase_						:     Dict							     =  processor.post_process_masks(
           a	,  a	,  a	,  return_tensors="tf"      )
       lowercase_						:     Optional[Any]							     =  processor.post_process_masks(
           a	,  a	,  a	,  return_tensors="pt"      )

       self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()      )      )





    @is_pt_tf_cross_test
    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  self.get_image_processor()

       lowercase_						:     Tuple							     =  SamProcessor(image_processor=a      )

       lowercase_						:     List[Any]							     =  self.prepare_image_inputs()

       lowercase_						:     int							     =  image_processor(a	,  return_tensors="pt"      )["pixel_values"].numpy()
       lowercase_						:     Dict							     =  processor(images=a	,  return_tensors="pt"      )["pixel_values"].numpy()

       lowercase_						:     Dict							     =  image_processor(a	,  return_tensors="tf"      )["pixel_values"].numpy()
       lowercase_						:     Tuple							     =  processor(images=a	,  return_tensors="tf"      )["pixel_values"].numpy()

       self.assertTrue(np.allclose(a	,  a      )      )
       self.assertTrue(np.allclose(a	,  a      )      )
       self.assertTrue(np.allclose(a	,  a      )      )












'''simple docstring'''
import warnings

from diffusers import StableDiffusionInpaintPipeline as StableDiffusionInpaintPipeline  # noqa F401


warnings.warn(
    'The `inpainting.py` script is outdated. Please use directly `from diffusers import'
    ' StableDiffusionInpaintPipeline` instead.'
)




