






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import OwlViTImageProcessor, OwlViTProcessor






@require_vision
class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =tempfile.mkdtemp()

          # fmt: off
          _lowercase              =['', 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
          # fmt: on
          _lowercase              =dict(zip(snake_case,      range(len(snake_case))))
          _lowercase              =['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
          _lowercase              ={'unk_token': '<unk>'}

          _lowercase              =os.path.join(self.tmpdirname,      VOCAB_FILES_NAMES['vocab_file'])
          _lowercase              =os.path.join(self.tmpdirname,      VOCAB_FILES_NAMES['merges_file'])
          with open(self.vocab_file,      'w',      encoding='utf-8') as fp:
             fp.write(json.dumps(snake_case) + '\n')
          with open(self.merges_file,      'w',      encoding='utf-8') as fp:
             fp.write('\n'.join(snake_case))

          _lowercase              ={
              'do_resize': True,
              'size': 20,
              'do_center_crop': True,
              'crop_size': 18,
              'do_normalize': True,
              'image_mean': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
              'image_std': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
          }
          _lowercase              =os.path.join(self.tmpdirname,      snake_case)
          with open(self.image_processor_file,      'w',      encoding='utf-8') as fp:
             json.dump(snake_case,      snake_case)





       def 				UpperCamelCase__						(				self	:Optional[int],      **snake_case	:Tuple):



          """simple docstring"""

          return CLIPTokenizer.from_pretrained(self.tmpdirname,      pad_token='!',      **snake_case)





       def 				UpperCamelCase__						(				self	:int,      **snake_case	:Union[str, Any]):



          """simple docstring"""

          return CLIPTokenizerFast.from_pretrained(self.tmpdirname,      pad_token='!',      **snake_case)





       def 				UpperCamelCase__						(				self	:Union[str, Any],      **snake_case	:Union[str, Any]):



          """simple docstring"""

          return OwlViTImageProcessor.from_pretrained(self.tmpdirname,      **snake_case)





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          shutil.rmtree(self.tmpdirname)





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

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

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

          return image_inputs





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.get_tokenizer()
          _lowercase              =self.get_rust_tokenizer()
          _lowercase              =self.get_image_processor()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)
          processor_slow.save_pretrained(self.tmpdirname)
          _lowercase              =OwlViTProcessor.from_pretrained(self.tmpdirname,      use_fast=snake_case)

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)
          processor_fast.save_pretrained(self.tmpdirname)
          _lowercase              =OwlViTProcessor.from_pretrained(self.tmpdirname)

          self.assertEqual(processor_slow.tokenizer.get_vocab(),      tokenizer_slow.get_vocab())
          self.assertEqual(processor_fast.tokenizer.get_vocab(),      tokenizer_fast.get_vocab())
          self.assertEqual(tokenizer_slow.get_vocab(),      tokenizer_fast.get_vocab())
          self.assertIsInstance(processor_slow.tokenizer,      snake_case)
          self.assertIsInstance(processor_fast.tokenizer,      snake_case)

          self.assertEqual(processor_slow.image_processor.to_json_string(),      image_processor.to_json_string())
          self.assertEqual(processor_fast.image_processor.to_json_string(),      image_processor.to_json_string())
          self.assertIsInstance(processor_slow.image_processor,      snake_case)
          self.assertIsInstance(processor_fast.image_processor,      snake_case)





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =OwlViTProcessor(tokenizer=self.get_tokenizer(),      image_processor=self.get_image_processor())
          processor.save_pretrained(self.tmpdirname)

          _lowercase              =self.get_tokenizer(bos_token='(BOS)',      eos_token='(EOS)')
          _lowercase              =self.get_image_processor(do_normalize=snake_case)

          _lowercase              =OwlViTProcessor.from_pretrained(
              self.tmpdirname,      bos_token='(BOS)',      eos_token='(EOS)',      do_normalize=snake_case)

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

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





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.get_image_processor()
          _lowercase              =self.get_tokenizer()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)

          _lowercase              =self.prepare_image_inputs()

          _lowercase              =image_processor(snake_case,      return_tensors='np')
          _lowercase              =processor(images=snake_case,      return_tensors='np')

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





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.get_image_processor()
          _lowercase              =self.get_tokenizer()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)

          _lowercase              ='lower newer'

          _lowercase              =processor(text=snake_case,      return_tensors='np')

          _lowercase              =tokenizer(snake_case,      return_tensors='np')

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





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.get_image_processor()
          _lowercase              =self.get_tokenizer()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)

          _lowercase              ='lower newer'
          _lowercase              =self.prepare_image_inputs()

          _lowercase              =processor(text=snake_case,      images=snake_case)

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

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





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase              ='google/owlvit-base-patch32'
          _lowercase              =OwlViTProcessor.from_pretrained(snake_case)

          _lowercase              =['cat', 'nasa badge']
          _lowercase              =processor(text=snake_case)

          _lowercase              =16
          self.assertListEqual(list(inputs.keys()),      ['input_ids', 'attention_mask'])
          self.assertEqual(inputs['input_ids'].shape,      (2, seq_length))

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





       def 				UpperCamelCase__						(				self	:Optional[Any]):



          """simple docstring"""

          _lowercase              ='google/owlvit-base-patch32'
          _lowercase              =OwlViTProcessor.from_pretrained(snake_case)

          _lowercase              =[['cat', 'nasa badge'], ['person']]
          _lowercase              =processor(text=snake_case)

          _lowercase              =16
          _lowercase              =len(snake_case)
          _lowercase              =max([len(snake_case) for texts in input_texts])

          self.assertListEqual(list(inputs.keys()),      ['input_ids', 'attention_mask'])
          self.assertEqual(inputs['input_ids'].shape,      (batch_size * num_max_text_queries, seq_length))

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





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



          """simple docstring"""

          _lowercase              ='google/owlvit-base-patch32'
          _lowercase              =OwlViTProcessor.from_pretrained(snake_case)

          _lowercase              =['cat', 'nasa badge']
          _lowercase              =processor(text=snake_case)

          _lowercase              =16
          _lowercase              =inputs['input_ids']
          _lowercase              =[
              [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          ]

          self.assertListEqual(list(inputs.keys()),      ['input_ids', 'attention_mask'])
          self.assertEqual(inputs['input_ids'].shape,      (2, seq_length))
          self.assertListEqual(list(input_ids[0]),      predicted_ids[0])
          self.assertListEqual(list(input_ids[1]),      predicted_ids[1])





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.get_image_processor()
          _lowercase              =self.get_tokenizer()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)

          _lowercase              =self.prepare_image_inputs()
          _lowercase              =self.prepare_image_inputs()

          _lowercase              =processor(images=snake_case,      query_images=snake_case)

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

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





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.get_image_processor()
          _lowercase              =self.get_tokenizer()

          _lowercase              =OwlViTProcessor(tokenizer=snake_case,      image_processor=snake_case)

          _lowercase              =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          _lowercase              =processor.batch_decode(snake_case)
          _lowercase              =tokenizer.batch_decode(snake_case)

          self.assertListEqual(snake_case,      snake_case)










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


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ChineseCLIPImageProcessor






class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       def __init__(				self	:Any,      snake_case	:Optional[int],      snake_case	:Optional[Any]=7,      snake_case	:str=3,      snake_case	:Optional[int]=18,      snake_case	:str=30,      snake_case	:List[Any]=400,      snake_case	:Any=True,      snake_case	:Dict=None,      snake_case	:Any=True,      snake_case	:Dict=None,      snake_case	:List[Any]=True,      snake_case	:Optional[int]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],      snake_case	:Any=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],      snake_case	:int=True,      ):



          """simple docstring"""

          _lowercase              =size if size is not None else {'height': 224, 'width': 224}
          _lowercase              =crop_size if crop_size is not None else {'height': 18, 'width': 18}
          _lowercase              =parent
          _lowercase              =batch_size
          _lowercase              =num_channels
          _lowercase              =image_size
          _lowercase              =min_resolution
          _lowercase              =max_resolution
          _lowercase              =do_resize
          _lowercase              =size
          _lowercase              =do_center_crop
          _lowercase              =crop_size
          _lowercase              =do_normalize
          _lowercase              =image_mean
          _lowercase              =image_std
          _lowercase              =do_convert_rgb





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_convert_rgb": self.do_convert_rgb,
          }





       def 				UpperCamelCase__						(				self	:Dict,      snake_case	:List[Any]=False,      snake_case	:Any=False,      snake_case	:Union[str, Any]=False):



          """simple docstring"""

          assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

          if equal_resolution:
             _lowercase              =[]
             for i in range(self.batch_size):
                image_inputs.append(
                    np.random.randint(
                        255,      size=(self.num_channels, self.max_resolution, self.max_resolution),      dtype=np.uinta))
          else:
             _lowercase              =[]
             for i in range(self.batch_size):
                _lowercase       ,       _lowercase              =np.random.choice(np.arange(self.min_resolution,      self.max_resolution),      2)
                image_inputs.append(np.random.randint(255,      size=(self.num_channels, width, height),      dtype=np.uinta))

          if not numpify and not torchify:
             # PIL expects the channel dimension as last dimension
             _lowercase              =[Image.fromarray(np.moveaxis(snake_case,      0,      -1)) for x in image_inputs]

          if torchify:
             _lowercase              =[torch.from_numpy(snake_case) for x in image_inputs]

          return image_inputs






@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		List[Any]					=ChineseCLIPImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase              =ChineseCLIPImageProcessingTester(self,      do_center_crop=snake_case)





       @property
       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_resize'))
          self.assertTrue(hasattr(snake_case,      'size'))
          self.assertTrue(hasattr(snake_case,      'do_center_crop'))
          self.assertTrue(hasattr(snake_case,      'center_crop'))
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'image_mean'))
          self.assertTrue(hasattr(snake_case,      'image_std'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size,      {'height': 224, 'width': 224})
          self.assertEqual(image_processor.crop_size,      {'height': 18, 'width': 18})

          _lowercase              =self.image_processing_class.from_dict(self.image_processor_dict,      size=42,      crop_size=84)
          self.assertEqual(image_processor.size,      {'shortest_edge': 42})
          self.assertEqual(image_processor.crop_size,      {'height': 84, 'width': 84})





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          pass





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case,      numpify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      np.ndarray)

          # Test not batched input
          _lowercase              =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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case,      torchify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      torch.Tensor)

          # Test not batched input
          _lowercase              =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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      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.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )






@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Optional[Any]					=ChineseCLIPImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase              =ChineseCLIPImageProcessingTester(self,      num_channels=4,      do_center_crop=snake_case)
          _lowercase              =3





       @property
       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_resize'))
          self.assertTrue(hasattr(snake_case,      'size'))
          self.assertTrue(hasattr(snake_case,      'do_center_crop'))
          self.assertTrue(hasattr(snake_case,      'center_crop'))
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'image_mean'))
          self.assertTrue(hasattr(snake_case,      'image_std'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          pass





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =image_processing(image_inputs[0],      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  1,
                  self.expected_encoded_image_num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  self.image_processor_tester.batch_size,
                  self.expected_encoded_image_num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )


