


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



    """simple docstring"""






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






    def _a					(			self					,   **A_    )  ->       Union[str, Any]:
         return AutoProcessor.from_pretrained(self.tmpdirname					,   **A_    ).image_processor






    def _a					(			self    )  ->       Any:
         shutil.rmtree(self.tmpdirname    )






    def _a					(			self    )  ->       Tuple:
         __UpperCamelCase    							=[np.random.randint(255					,   size=(3, 30, 400)					,   dtype=np.uinta    )]

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

         return image_inputs






    def _a					(			self    )  ->       List[str]:
         __UpperCamelCase    							=SamProcessor(image_processor=self.get_image_processor()    )
         processor.save_pretrained(self.tmpdirname    )

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

         __UpperCamelCase    							=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 _a					(			self    )  ->       str:
         __UpperCamelCase    							=self.get_image_processor()

         __UpperCamelCase    							=SamProcessor(image_processor=A_    )

         __UpperCamelCase    							=self.prepare_image_inputs()

         __UpperCamelCase    							=image_processor(A_					,   return_tensors='np'    )
         __UpperCamelCase    							=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 _a					(			self    )  ->       Optional[int]:
         __UpperCamelCase    							=self.get_image_processor()

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

         __UpperCamelCase    							=[[1764, 2646]]

         __UpperCamelCase    							=[[683, 1024]]
         __UpperCamelCase    							=processor.post_process_masks(A_					,   A_					,   A_    )
         self.assertEqual(masks[0].shape					,   (1, 3, 1764, 2646)    )

         __UpperCamelCase    							=processor.post_process_masks(
             A_					,   torch.tensor(A_    )					,   torch.tensor(A_    )    )
         self.assertEqual(masks[0].shape					,   (1, 3, 1764, 2646)    )

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

         self.assertEqual(masks[0].shape					,   (1, 3, 1764, 2646)    )

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






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



    """simple docstring"""






    def _a					(			self    )  ->       Any:
         __UpperCamelCase    							=tempfile.mkdtemp()
         __UpperCamelCase    							=SamImageProcessor()
         __UpperCamelCase    							=SamProcessor(A_    )
         processor.save_pretrained(self.tmpdirname    )






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






    def _a					(			self    )  ->       Tuple:
         shutil.rmtree(self.tmpdirname    )






    def _a					(			self    )  ->       Any:
         __UpperCamelCase    							=[np.random.randint(255					,   size=(3, 30, 400)					,   dtype=np.uinta    )]

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

         return image_inputs






    def _a					(			self    )  ->       Optional[int]:
         __UpperCamelCase    							=SamProcessor(image_processor=self.get_image_processor()    )
         processor.save_pretrained(self.tmpdirname    )

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

         __UpperCamelCase    							=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 _a					(			self    )  ->       Optional[Any]:
         __UpperCamelCase    							=self.get_image_processor()

         __UpperCamelCase    							=SamProcessor(image_processor=A_    )

         __UpperCamelCase    							=self.prepare_image_inputs()

         __UpperCamelCase    							=image_processor(A_					,   return_tensors='np'    )
         __UpperCamelCase    							=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 _a					(			self    )  ->       str:
         __UpperCamelCase    							=self.get_image_processor()

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

         __UpperCamelCase    							=[[1764, 2646]]

         __UpperCamelCase    							=[[683, 1024]]
         __UpperCamelCase    							=processor.post_process_masks(A_					,   A_					,   A_					,   return_tensors='tf'    )
         self.assertEqual(masks[0].shape					,   (1, 3, 1764, 2646)    )

         __UpperCamelCase    							=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, 1764, 2646)    )

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

         self.assertEqual(masks[0].shape					,   (1, 3, 1764, 2646)    )

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






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



    """simple docstring"""






    def _a					(			self    )  ->       Any:
         __UpperCamelCase    							=tempfile.mkdtemp()
         __UpperCamelCase    							=SamImageProcessor()
         __UpperCamelCase    							=SamProcessor(A_    )
         processor.save_pretrained(self.tmpdirname    )






    def _a					(			self					,   **A_    )  ->       Optional[int]:
         return AutoProcessor.from_pretrained(self.tmpdirname					,   **A_    ).image_processor






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






    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=[np.random.randint(255					,   size=(3, 30, 400)					,   dtype=np.uinta    )]

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

         return image_inputs






    @is_pt_tf_cross_test
    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=self.get_image_processor()

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

         __UpperCamelCase    							=[[1764, 2646]]

         __UpperCamelCase    							=[[683, 1024]]
         __UpperCamelCase    							=processor.post_process_masks(
             A_					,   A_					,   A_					,   return_tensors='tf'    )
         __UpperCamelCase    							=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 _a					(			self    )  ->       Union[str, Any]:
         __UpperCamelCase    							=self.get_image_processor()

         __UpperCamelCase    							=SamProcessor(image_processor=A_    )

         __UpperCamelCase    							=self.prepare_image_inputs()

         __UpperCamelCase    							=image_processor(A_					,   return_tensors='pt'    )['pixel_values'].numpy()
         __UpperCamelCase    							=processor(images=A_					,   return_tensors='pt'    )['pixel_values'].numpy()

         __UpperCamelCase    							=image_processor(A_					,   return_tensors='tf'    )['pixel_values'].numpy()
         __UpperCamelCase    							=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_    )    )









from typing import TYPE_CHECKING

from ...utils import _LazyModule


_A		       =						{'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']}


if TYPE_CHECKING:
   from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer
else:
   import sys

   _A		       =						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





