"""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 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	tempfile.mkdtemp()
        lowercase_	:				Union[str, Any]													=	SamImageProcessor()
        lowercase_	:				Any													=	SamProcessor(__UpperCamelCase			)
        processor.save_pretrained(self.tmpdirname			)
   def 			_UpperCAmelCase   (			self     ,**__UpperCamelCase			)	->      Optional[Any]:


        '''simple docstring'''


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


        '''simple docstring'''


        shutil.rmtree(self.tmpdirname			)
   def 			_UpperCAmelCase   (			self			)	->      List[str]:


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	[np.random.randint(255     ,size=(3, 30, 400)     ,dtype=np.uinta			)]

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

        return image_inputs
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


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

        lowercase_	:				List[str]													=	self.get_image_processor(do_normalize=__UpperCamelCase     ,padding_value=1.0			)

        lowercase_	:				List[Any]													=	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[Any]:


        '''simple docstring'''


        lowercase_	:				int													=	self.get_image_processor()

        lowercase_	:				Dict													=	SamProcessor(image_processor=__UpperCamelCase			)

        lowercase_	:				str													=	self.prepare_image_inputs()

        lowercase_	:				Optional[int]													=	image_processor(__UpperCamelCase     ,return_tensors='np'			)
        lowercase_	:				List[str]													=	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			)	->      Optional[int]:


        '''simple docstring'''


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

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

        lowercase_	:				Optional[int]													=	[[1764, 2646]]

        lowercase_	:				Optional[Any]													=	[[683, 1024]]
        lowercase_	:				Optional[int]													=	processor.post_process_masks(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)
        self.assertEqual(masks[0].shape     ,(1, 3, 1764, 2646)			)

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

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

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

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







@require_vision
@require_tf
class        UpperCamelCase		(			unittest.TestCase  ):
   def 			_UpperCAmelCase   (			self			)	->      List[str]:


        '''simple docstring'''


        lowercase_	:				Dict													=	tempfile.mkdtemp()
        lowercase_	:				List[str]													=	SamImageProcessor()
        lowercase_	:				List[str]													=	SamProcessor(__UpperCamelCase			)
        processor.save_pretrained(self.tmpdirname			)
   def 			_UpperCAmelCase   (			self     ,**__UpperCamelCase			)	->      Union[str, Any]:


        '''simple docstring'''


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


        '''simple docstring'''


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


        '''simple docstring'''


        lowercase_	:				Optional[int]													=	[np.random.randint(255     ,size=(3, 30, 400)     ,dtype=np.uinta			)]

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

        return image_inputs
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


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

        lowercase_	:				List[Any]													=	self.get_image_processor(do_normalize=__UpperCamelCase     ,padding_value=1.0			)

        lowercase_	:				Optional[int]													=	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:


        '''simple docstring'''


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

        lowercase_	:				List[Any]													=	SamProcessor(image_processor=__UpperCamelCase			)

        lowercase_	:				str													=	self.prepare_image_inputs()

        lowercase_	:				Optional[int]													=	image_processor(__UpperCamelCase     ,return_tensors='np'			)
        lowercase_	:				Tuple													=	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			)	->      int:


        '''simple docstring'''


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

        lowercase_	:				Optional[int]													=	SamProcessor(image_processor=__UpperCamelCase			)
        lowercase_	:				Union[str, Any]													=	[tf.ones((1, 3, 5, 5)			)]

        lowercase_	:				Tuple													=	[[1764, 2646]]

        lowercase_	:				Optional[Any]													=	[[683, 1024]]
        lowercase_	:				Union[str, Any]													=	processor.post_process_masks(__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,return_tensors='tf'			)
        self.assertEqual(masks[0].shape     ,(1, 3, 1764, 2646)			)

        lowercase_	:				Optional[Any]													=	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, 1764, 2646)			)

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

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

        lowercase_	:				List[str]													=	[[1, 0], [0, 1]]
        with self.assertRaises(tf.errors.InvalidArgumentError			):
             lowercase_	:				Tuple													=	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			)	->      Union[str, Any]:


        '''simple docstring'''


        lowercase_	:				Dict													=	tempfile.mkdtemp()
        lowercase_	:				List[Any]													=	SamImageProcessor()
        lowercase_	:				Optional[int]													=	SamProcessor(__UpperCamelCase			)
        processor.save_pretrained(self.tmpdirname			)
   def 			_UpperCAmelCase   (			self     ,**__UpperCamelCase			)	->      Dict:


        '''simple docstring'''


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


        '''simple docstring'''


        shutil.rmtree(self.tmpdirname			)
   def 			_UpperCAmelCase   (			self			)	->      List[Any]:


        '''simple docstring'''


        lowercase_	:				Any													=	[np.random.randint(255     ,size=(3, 30, 400)     ,dtype=np.uinta			)]

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

        return image_inputs
   @is_pt_tf_cross_test
   def 			_UpperCAmelCase   (			self			)	->      Dict:


        '''simple docstring'''


        lowercase_	:				int													=	self.get_image_processor()

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

        lowercase_	:				int													=	[[1764, 2646]]

        lowercase_	:				Dict													=	[[683, 1024]]
        lowercase_	:				List[str]													=	processor.post_process_masks(
            __UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase     ,return_tensors='tf'			)
        lowercase_	:				List[Any]													=	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			)	->      List[Any]:


        '''simple docstring'''


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

        lowercase_	:				Optional[Any]													=	SamProcessor(image_processor=__UpperCamelCase			)

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

        lowercase_	:				Optional[int]													=	image_processor(__UpperCamelCase     ,return_tensors='pt'			)['pixel_values'].numpy()
        lowercase_	:				Union[str, Any]													=	processor(images=__UpperCamelCase     ,return_tensors='pt'			)['pixel_values'].numpy()

        lowercase_	:				List[Any]													=	image_processor(__UpperCamelCase     ,return_tensors='tf'			)['pixel_values'].numpy()
        lowercase_	:				Optional[int]													=	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			)			)


"""simple docstring"""







from __future__ import annotations



def 			lowercase__(       __SCREAMING_SNAKE_CASE							:     list      ):
     if not nums:
          raise ValueError('List is empty'      )
     return sum(__SCREAMING_SNAKE_CASE      ) / len(__SCREAMING_SNAKE_CASE      )


if __name__ == "__main__":
       import doctest

       doctest.testmod()

