






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 			__UpperCAmelCase		(						self						)->						str:
           __lowerCAmelCase             =       tempfile.mkdtemp()
           __lowerCAmelCase             =       SamImageProcessor()
           __lowerCAmelCase             =       SamProcessor(__UpperCamelCase						)
           processor.save_pretrained(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self       ,    **__UpperCamelCase						)->						Tuple:
           return AutoProcessor.from_pretrained(self.tmpdirname       ,    **__UpperCamelCase						).image_processor


      def 			__UpperCAmelCase		(						self						)->						Union[str, Any]:
           shutil.rmtree(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase             =       [np.random.randint(2_5_5       ,    size=(3, 3_0, 4_0_0)       ,    dtype=np.uinta						)]

           __lowerCAmelCase             =       [Image.fromarray(np.moveaxis(__UpperCamelCase       ,    0       ,    -1						)						) for x in image_inputs]

           return image_inputs


      def 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase             =       SamProcessor(image_processor=self.get_image_processor()						)
           processor.save_pretrained(self.tmpdirname						)

           __lowerCAmelCase             =       self.get_image_processor(do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

           __lowerCAmelCase             =       SamProcessor.from_pretrained(self.tmpdirname       ,    do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

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


      def 			__UpperCAmelCase		(						self						)->						str:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)

           __lowerCAmelCase             =       self.prepare_image_inputs()

           __lowerCAmelCase             =       image_processor(__UpperCamelCase       ,    return_tensors="np"						)
           __lowerCAmelCase             =       processor(images=__UpperCamelCase       ,    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 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)
           __lowerCAmelCase             =       [torch.ones((1, 3, 5, 5)						)]

           __lowerCAmelCase             =       [[1_7_6_4, 2_6_4_6]]

           __lowerCAmelCase             =       [[6_8_3, 1_0_2_4]]
           __lowerCAmelCase             =       processor.post_process_masks(__UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)
           self.assertEqual(masks[0].shape       ,    (1, 3, 1_7_6_4, 2_6_4_6)						)

           __lowerCAmelCase             =       processor.post_process_masks(
               __UpperCamelCase       ,    torch.tensor(__UpperCamelCase						)       ,    torch.tensor(__UpperCamelCase						)						)
           self.assertEqual(masks[0].shape       ,    (1, 3, 1_7_6_4, 2_6_4_6)						)

           # should also work with np
           __lowerCAmelCase             =       [np.ones((1, 3, 5, 5)						)]
           __lowerCAmelCase             =       processor.post_process_masks(__UpperCamelCase       ,    np.array(__UpperCamelCase						)       ,    np.array(__UpperCamelCase						)						)

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

           __lowerCAmelCase             =       [[1, 0], [0, 1]]
           with self.assertRaises(__UpperCamelCase						):
                __lowerCAmelCase             =       processor.post_process_masks(__UpperCamelCase       ,    np.array(__UpperCamelCase						)       ,    np.array(__UpperCamelCase						)						)



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


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       tempfile.mkdtemp()
           __lowerCAmelCase             =       SamImageProcessor()
           __lowerCAmelCase             =       SamProcessor(__UpperCamelCase						)
           processor.save_pretrained(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self       ,    **__UpperCamelCase						)->						Any:
           return AutoProcessor.from_pretrained(self.tmpdirname       ,    **__UpperCamelCase						).image_processor


      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           shutil.rmtree(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self						)->						Union[str, Any]:
           __lowerCAmelCase             =       [np.random.randint(2_5_5       ,    size=(3, 3_0, 4_0_0)       ,    dtype=np.uinta						)]

           __lowerCAmelCase             =       [Image.fromarray(np.moveaxis(__UpperCamelCase       ,    0       ,    -1						)						) for x in image_inputs]

           return image_inputs


      def 			__UpperCAmelCase		(						self						)->						Tuple:
           __lowerCAmelCase             =       SamProcessor(image_processor=self.get_image_processor()						)
           processor.save_pretrained(self.tmpdirname						)

           __lowerCAmelCase             =       self.get_image_processor(do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

           __lowerCAmelCase             =       SamProcessor.from_pretrained(self.tmpdirname       ,    do_normalize=__UpperCamelCase       ,    padding_value=1.0						)

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


      def 			__UpperCAmelCase		(						self						)->						List[str]:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)

           __lowerCAmelCase             =       self.prepare_image_inputs()

           __lowerCAmelCase             =       image_processor(__UpperCamelCase       ,    return_tensors="np"						)
           __lowerCAmelCase             =       processor(images=__UpperCamelCase       ,    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 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)
           __lowerCAmelCase             =       [tf.ones((1, 3, 5, 5)						)]

           __lowerCAmelCase             =       [[1_7_6_4, 2_6_4_6]]

           __lowerCAmelCase             =       [[6_8_3, 1_0_2_4]]
           __lowerCAmelCase             =       processor.post_process_masks(__UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    return_tensors="tf"						)
           self.assertEqual(masks[0].shape       ,    (1, 3, 1_7_6_4, 2_6_4_6)						)

           __lowerCAmelCase             =       processor.post_process_masks(
               __UpperCamelCase       ,    tf.convert_to_tensor(__UpperCamelCase						)       ,    tf.convert_to_tensor(__UpperCamelCase						)       ,    return_tensors="tf"       ,    )
           self.assertEqual(masks[0].shape       ,    (1, 3, 1_7_6_4, 2_6_4_6)						)

           # should also work with np
           __lowerCAmelCase             =       [np.ones((1, 3, 5, 5)						)]
           __lowerCAmelCase             =       processor.post_process_masks(
               __UpperCamelCase       ,    np.array(__UpperCamelCase						)       ,    np.array(__UpperCamelCase						)       ,    return_tensors="tf"						)

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

           __lowerCAmelCase             =       [[1, 0], [0, 1]]
           with self.assertRaises(tf.errors.InvalidArgumentError						):
                __lowerCAmelCase             =       processor.post_process_masks(
                    __UpperCamelCase       ,    np.array(__UpperCamelCase						)       ,    np.array(__UpperCamelCase						)       ,    return_tensors="tf"						)



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


      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           __lowerCAmelCase             =       tempfile.mkdtemp()
           __lowerCAmelCase             =       SamImageProcessor()
           __lowerCAmelCase             =       SamProcessor(__UpperCamelCase						)
           processor.save_pretrained(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self       ,    **__UpperCamelCase						)->						Tuple:
           return AutoProcessor.from_pretrained(self.tmpdirname       ,    **__UpperCamelCase						).image_processor


      def 			__UpperCAmelCase		(						self						)->						Optional[Any]:
           shutil.rmtree(self.tmpdirname						)


      def 			__UpperCAmelCase		(						self						)->						int:
           __lowerCAmelCase             =       [np.random.randint(2_5_5       ,    size=(3, 3_0, 4_0_0)       ,    dtype=np.uinta						)]

           __lowerCAmelCase             =       [Image.fromarray(np.moveaxis(__UpperCamelCase       ,    0       ,    -1						)						) for x in image_inputs]

           return image_inputs


      @is_pt_tf_cross_test
      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)
           __lowerCAmelCase             =       np.random.randint(0       ,    2       ,    size=(1, 3, 5, 5)						).astype(np.floataa						)
           __lowerCAmelCase             =       [tf.convert_to_tensor(__UpperCamelCase						)]
           __lowerCAmelCase             =       [torch.tensor(__UpperCamelCase						)]

           __lowerCAmelCase             =       [[1_7_6_4, 2_6_4_6]]

           __lowerCAmelCase             =       [[6_8_3, 1_0_2_4]]
           __lowerCAmelCase             =       processor.post_process_masks(
               __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    return_tensors="tf"						)
           __lowerCAmelCase             =       processor.post_process_masks(
               __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    return_tensors="pt"						)

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


      @is_pt_tf_cross_test
      def 			__UpperCAmelCase		(						self						)->						str:
           __lowerCAmelCase             =       self.get_image_processor()

           __lowerCAmelCase             =       SamProcessor(image_processor=__UpperCamelCase						)

           __lowerCAmelCase             =       self.prepare_image_inputs()

           __lowerCAmelCase             =       image_processor(__UpperCamelCase       ,    return_tensors="pt"						)["pixel_values"].numpy()
           __lowerCAmelCase             =       processor(images=__UpperCamelCase       ,    return_tensors="pt"						)["pixel_values"].numpy()

           __lowerCAmelCase             =       image_processor(__UpperCamelCase       ,    return_tensors="tf"						)["pixel_values"].numpy()
           __lowerCAmelCase             =       processor(images=__UpperCamelCase       ,    return_tensors="tf"						)["pixel_values"].numpy()

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














# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowerCamelCase      :       Dict    =						abspath(join(dirname(dirname(__file__)), '''src'''))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='''ignore''', category=FutureWarning)





def    __lowerCAmelCase   (    __snake_case							):
     from diffusers.utils.testing_utils import pytest_addoption_shared

     pytest_addoption_shared(__snake_case							)





def    __lowerCAmelCase   (    __snake_case							):
     from diffusers.utils.testing_utils import pytest_terminal_summary_main

     __lowerCAmelCase             =       terminalreporter.config.getoption("--make-reports"							)
     if make_reports:
          pytest_terminal_summary_main(__snake_case       ,			id=__snake_case							)






