




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
							from PIL import Image

							from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast


@require_vision
class 							__SCREAMING_SNAKE_CASE				(						unittest.TestCase    ):

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						int				  =				tempfile.mkdtemp()

						lowerCamelCase_				:						List[Any]				  =				CLIPImageProcessor()
						lowerCamelCase_				:						Union[str, Any]				  =				BertTokenizer.from_pretrained(
						    'hf-internal-testing/tiny-random-BertModel'					,			model_input_names=['input_ids', 'attention_mask']   )

						lowerCamelCase_				:						Tuple				  =				GitProcessor(__lowercase					,			__lowercase   )

						processor.save_pretrained(self.tmpdirname   )

					def       lowercase_				(							self					,			**__lowercase   ):







						'''simple docstring'''




						return AutoProcessor.from_pretrained(self.tmpdirname					,			**__lowercase   ).tokenizer

					def       lowercase_				(							self					,			**__lowercase   ):







						'''simple docstring'''




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

					def       lowercase_				(							self   ):







						'''simple docstring'''




						shutil.rmtree(self.tmpdirname   )

					def       lowercase_				(							self   ):







						'''simple docstring'''




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

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

						return image_inputs

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Tuple				  =				GitProcessor(tokenizer=self.get_tokenizer()					,			image_processor=self.get_image_processor()   )
						processor.save_pretrained(self.tmpdirname   )

						lowerCamelCase_				:						str				  =				self.get_tokenizer(bos_token='(BOS)'					,			eos_token='(EOS)'   )
						lowerCamelCase_				:						Any				  =				self.get_image_processor(do_normalize=__lowercase					,			padding_value=1.0   )

						lowerCamelCase_				:						Optional[Any]				  =				GitProcessor.from_pretrained(
						    self.tmpdirname					,			bos_token='(BOS)'					,			eos_token='(EOS)'					,			do_normalize=__lowercase					,			padding_value=1.0   )

						self.assertEqual(processor.tokenizer.get_vocab()					,			tokenizer_add_kwargs.get_vocab()   )
						self.assertIsInstance(processor.tokenizer					,			__lowercase   )

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

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						int				  =				self.get_image_processor()
						lowerCamelCase_				:						Any				  =				self.get_tokenizer()

						lowerCamelCase_				:						str				  =				GitProcessor(tokenizer=__lowercase					,			image_processor=__lowercase   )

						lowerCamelCase_				:						Tuple				  =				self.prepare_image_inputs()

						lowerCamelCase_				:						Union[str, Any]				  =				image_processor(__lowercase					,			return_tensors='np'   )
						lowerCamelCase_				:						Optional[int]				  =				processor(images=__lowercase					,			return_tensors='np'   )

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

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						str				  =				self.get_image_processor()
						lowerCamelCase_				:						List[Any]				  =				self.get_tokenizer()

						lowerCamelCase_				:						Optional[int]				  =				GitProcessor(tokenizer=__lowercase					,			image_processor=__lowercase   )

						lowerCamelCase_				:						Optional[int]				  =				'lower newer'

						lowerCamelCase_				:						Optional[Any]				  =				processor(text=__lowercase   )

						lowerCamelCase_				:						int				  =				tokenizer(__lowercase					,			return_token_type_ids=__lowercase   )

						for key in encoded_tok.keys():
							self.assertListEqual(encoded_tok[key]					,			encoded_processor[key]   )

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						List[str]				  =				self.get_image_processor()
						lowerCamelCase_				:						str				  =				self.get_tokenizer()

						lowerCamelCase_				:						str				  =				GitProcessor(tokenizer=__lowercase					,			image_processor=__lowercase   )

						lowerCamelCase_				:						Optional[Any]				  =				'lower newer'
						lowerCamelCase_				:						Tuple				  =				self.prepare_image_inputs()

						lowerCamelCase_				:						List[str]				  =				processor(text=__lowercase					,			images=__lowercase   )

						self.assertListEqual(list(inputs.keys()   )					,			['input_ids', 'attention_mask', 'pixel_values']   )

						# test if it raises when no input is passed
						with pytest.raises(__lowercase   ):
							processor()

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						List[str]				  =				self.get_image_processor()
						lowerCamelCase_				:						str				  =				self.get_tokenizer()

						lowerCamelCase_				:						Dict				  =				GitProcessor(tokenizer=__lowercase					,			image_processor=__lowercase   )

						lowerCamelCase_				:						str				  =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						lowerCamelCase_				:						Any				  =				processor.batch_decode(__lowercase   )
						lowerCamelCase_				:						Optional[int]				  =				tokenizer.batch_decode(__lowercase   )

						self.assertListEqual(__lowercase					,			__lowercase   )





					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Union[str, Any]				  =				self.get_image_processor()
						lowerCamelCase_				:						List[str]				  =				self.get_tokenizer()

						lowerCamelCase_				:						Optional[Any]				  =				GitProcessor(tokenizer=__lowercase					,			image_processor=__lowercase   )

						lowerCamelCase_				:						Optional[Any]				  =				'lower newer'
						lowerCamelCase_				:						Dict				  =				self.prepare_image_inputs()

						lowerCamelCase_				:						List[str]				  =				processor(text=__lowercase					,			images=__lowercase   )

						# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
						self.assertListEqual(list(inputs.keys()   )					,			['input_ids', 'attention_mask', 'pixel_values']   )



import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import ConvNextImageProcessor





class A__     (			unittest.TestCase			):



 """simple docstring"""







 def __init__(		self			:						Optional[Any]  ,    A_			:						Union[str, Any]  ,    A_			:						List[Any]=7  ,    A_			:						Optional[Any]=3  ,    A_			:						Optional[int]=1_8  ,    A_			:						Dict=3_0  ,    A_			:						Dict=4_0_0  ,    A_			:						List[Any]=True  ,    A_			:						int=None  ,    A_			:						List[Any]=0.875  ,    A_			:						List[str]=True  ,    A_			:						List[Any]=[0.5, 0.5, 0.5]  ,    A_			:						Optional[Any]=[0.5, 0.5, 0.5]  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     size if size is not None else {"shortest_edge": 2_0}
      _lowerCAmelCase :       Union[str, Any]	   =     parent
      _lowerCAmelCase :       int	   =     batch_size
      _lowerCAmelCase :       Any	   =     num_channels
      _lowerCAmelCase :       str	   =     image_size
      _lowerCAmelCase :       List[Any]	   =     min_resolution
      _lowerCAmelCase :       Tuple	   =     max_resolution
      _lowerCAmelCase :       Any	   =     do_resize
      _lowerCAmelCase :       List[str]	   =     size
      _lowerCAmelCase :       List[str]	   =     crop_pct
      _lowerCAmelCase :       Tuple	   =     do_normalize
      _lowerCAmelCase :       int	   =     image_mean
      _lowerCAmelCase :       Dict	   =     image_std


 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
          "crop_pct": self.crop_pct,
      }





@require_torch
@require_vision
class A__     (			A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       int  			=    ConvNextImageProcessor if is_vision_available() else None

 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     ConvNextImageProcessingTester(self							)

 @property
 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      return self.image_processor_tester.prepare_image_processor_dict()

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






      '''simple docstring'''




      _lowerCAmelCase :       int	   =     self.image_processing_class(**self.image_processor_dict							)
      self.assertTrue(hasattr(A_  ,    "do_resize"							)							)
      self.assertTrue(hasattr(A_  ,    "size"							)							)
      self.assertTrue(hasattr(A_  ,    "crop_pct"							)							)
      self.assertTrue(hasattr(A_  ,    "do_normalize"							)							)
      self.assertTrue(hasattr(A_  ,    "image_mean"							)							)
      self.assertTrue(hasattr(A_  ,    "image_std"							)							)

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     self.image_processing_class.from_dict(self.image_processor_dict							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 2_0}							)

      _lowerCAmelCase :       Optional[int]	   =     self.image_processing_class.from_dict(self.image_processor_dict  ,    size=4_2							)
      self.assertEqual(image_processor.size  ,    {"shortest_edge": 4_2}							)

 def  __magic_name__							(		self			:						List[str]							):






      '''simple docstring'''




      pass

 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[int]	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PIL images
      _lowerCAmelCase :       Tuple	   =     prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    Image.Image							)

      # Test not batched input
      _lowerCAmelCase :       List[Any]	   =     image_processing(image_inputs[0]  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )

      # Test batched
      _lowerCAmelCase :       Any	   =     image_processing(A_  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Dict	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random numpy tensors
      _lowerCAmelCase :       str	   =     prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=A_  ,    numpify=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    np.ndarray							)

      # Test not batched input
      _lowerCAmelCase :       str	   =     image_processing(image_inputs[0]  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )

      # Test batched
      _lowerCAmelCase :       Optional[Any]	   =     image_processing(A_  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )


 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     self.image_processing_class(**self.image_processor_dict							)
      # create random PyTorch tensors
      _lowerCAmelCase :       List[str]	   =     prepare_image_inputs(self.image_processor_tester  ,    equal_resolution=A_  ,    torchify=A_							)
      for image in image_inputs:
           self.assertIsInstance(A_  ,    torch.Tensor							)

      # Test not batched input
      _lowerCAmelCase :       Any	   =     image_processing(image_inputs[0]  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )

      # Test batched
      _lowerCAmelCase :       Union[str, Any]	   =     image_processing(A_  ,    return_tensors="pt"							).pixel_values
      self.assertEqual(
          encoded_images.shape  ,    (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["shortest_edge"],
              self.image_processor_tester.size["shortest_edge"],
          )  ,    )






