

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       __snake_case  (    unittest.TestCase       ):

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
						SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
						SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
						processor.save_pretrained(self.tmpdirname)

		def 						lowerCAmelCase__     (					self						,		**_A):
						return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

		def 						lowerCAmelCase__     (					self):
						shutil.rmtree(self.tmpdirname)

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

						return image_inputs

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=self.get_image_processor())
						processor.save_pretrained(self.tmpdirname)

						SCREAMING_SNAKE_CASE_									=						self.get_image_processor(do_normalize=_A						,		padding_value=1.0)

						SCREAMING_SNAKE_CASE_									=						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):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

						SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

						SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='np')
						SCREAMING_SNAKE_CASE_									=						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):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)
						SCREAMING_SNAKE_CASE_									=						[torch.ones((1, 3, 5, 5))]

						SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

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

						SCREAMING_SNAKE_CASE_									=						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
						SCREAMING_SNAKE_CASE_									=						[np.ones((1, 3, 5, 5))]
						SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(_A						,		np.array(_A)						,		np.array(_A))

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

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






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

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
						SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
						SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
						processor.save_pretrained(self.tmpdirname)

		def 						lowerCAmelCase__     (					self						,		**_A):
						return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

		def 						lowerCAmelCase__     (					self):
						shutil.rmtree(self.tmpdirname)

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

						return image_inputs

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=self.get_image_processor())
						processor.save_pretrained(self.tmpdirname)

						SCREAMING_SNAKE_CASE_									=						self.get_image_processor(do_normalize=_A						,		padding_value=1.0)

						SCREAMING_SNAKE_CASE_									=						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):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

						SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

						SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='np')
						SCREAMING_SNAKE_CASE_									=						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):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)
						SCREAMING_SNAKE_CASE_									=						[tf.ones((1, 3, 5, 5))]

						SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

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

						SCREAMING_SNAKE_CASE_									=						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
						SCREAMING_SNAKE_CASE_									=						[np.ones((1, 3, 5, 5))]
						SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
						    _A						,		np.array(_A)						,		np.array(_A)						,		return_tensors='tf')

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

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






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

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						tempfile.mkdtemp()
						SCREAMING_SNAKE_CASE_									=						SamImageProcessor()
						SCREAMING_SNAKE_CASE_									=						SamProcessor(_A)
						processor.save_pretrained(self.tmpdirname)

		def 						lowerCAmelCase__     (					self						,		**_A):
						return AutoProcessor.from_pretrained(self.tmpdirname						,		**_A).image_processor

		def 						lowerCAmelCase__     (					self):
						shutil.rmtree(self.tmpdirname)

		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						[np.random.randint(255						,		size=(3, 30, 400)						,		dtype=np.uinta)]

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

						return image_inputs

		@is_pt_tf_cross_test
		def 						lowerCAmelCase__     (					self):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

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

						SCREAMING_SNAKE_CASE_									=						[[1764, 2646]]

						SCREAMING_SNAKE_CASE_									=						[[683, 1024]]
						SCREAMING_SNAKE_CASE_									=						processor.post_process_masks(
						    _A						,		_A						,		_A						,		return_tensors='tf')
						SCREAMING_SNAKE_CASE_									=						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):
						SCREAMING_SNAKE_CASE_									=						self.get_image_processor()

						SCREAMING_SNAKE_CASE_									=						SamProcessor(image_processor=_A)

						SCREAMING_SNAKE_CASE_									=						self.prepare_image_inputs()

						SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='pt')['pixel_values'].numpy()
						SCREAMING_SNAKE_CASE_									=						processor(images=_A						,		return_tensors='pt')['pixel_values'].numpy()

						SCREAMING_SNAKE_CASE_									=						image_processor(_A						,		return_tensors='tf')['pixel_values'].numpy()
						SCREAMING_SNAKE_CASE_									=						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 (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


UpperCamelCase__  :      Tuple			=    {
    "configuration_trocr": ["TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP", "TrOCRConfig"],
    "processing_trocr": ["TrOCRProcessor"],
}


try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCamelCase__  :      Tuple			=    [
           "TROCR_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TrOCRForCausalLM",
           "TrOCRPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig
       from .processing_trocr import TrOCRProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel

else:
       import sys

       UpperCamelCase__  :      str			=    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


