



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_video_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import VivitImageProcessor


class 	__snake_case   ( unittest.TestCase ):
		def __init__(							self    ,a_    ,a_=7    ,a_=3    ,a_=10    ,a_=18    ,a_=30    ,a_=400    ,a_=True    ,a_=None    ,a_=True    ,a_=[0.5, 0.5, 0.5]    ,a_=[0.5, 0.5, 0.5]    ,a_=None    ,):







						"""simple docstring"""
						lowerCAmelCase__		  =  size if size is not None else {'shortest_edge': 18}
						lowerCAmelCase__		  =  crop_size if crop_size is not None else {'height': 18, 'width': 18}

						lowerCAmelCase__		  =  parent
						lowerCAmelCase__		  =  batch_size
						lowerCAmelCase__		  =  num_channels
						lowerCAmelCase__		  =  num_frames
						lowerCAmelCase__		  =  image_size
						lowerCAmelCase__		  =  min_resolution
						lowerCAmelCase__		  =  max_resolution
						lowerCAmelCase__		  =  do_resize
						lowerCAmelCase__		  =  size
						lowerCAmelCase__		  =  do_normalize
						lowerCAmelCase__		  =  image_mean
						lowerCAmelCase__		  =  image_std
						lowerCAmelCase__		  =  crop_size



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""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_size": self.crop_size,
						}



@require_torch
@require_vision
class 	__snake_case   ( SCREAMING_SNAKE_CASE							, unittest.TestCase ):
		SCREAMING_SNAKE_CASE__								=	VivitImageProcessor if is_vision_available() else None
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VivitImageProcessingTester(self						)
		@property
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						return self.image_processor_tester.prepare_image_processor_dict()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.image_processing_class(**self.image_processor_dict						)
						self.assertTrue(hasattr(a_    ,'image_mean'						)						)
						self.assertTrue(hasattr(a_    ,'image_std'						)						)
						self.assertTrue(hasattr(a_    ,'do_normalize'						)						)
						self.assertTrue(hasattr(a_    ,'do_resize'						)						)
						self.assertTrue(hasattr(a_    ,'do_center_crop'						)						)
						self.assertTrue(hasattr(a_    ,'size'						)						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.image_processing_class.from_dict(self.image_processor_dict						)
						self.assertEqual(image_processor.size    ,{'shortest_edge': 18}						)
						self.assertEqual(image_processor.crop_size    ,{'height': 18, 'width': 18}						)

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







						"""simple docstring"""
						# Initialize image_processing
						lowerCAmelCase__		  =  self.image_processing_class(**self.image_processor_dict						)
						# create random PIL videos
						lowerCAmelCase__		  =  prepare_video_inputs(self.image_processor_tester    ,equal_resolution=a_						)
						for video in video_inputs:
										self.assertIsInstance(a_    ,a_						)
										self.assertIsInstance(video[0]    ,Image.Image						)

						# Test not batched input
						lowerCAmelCase__		  =  image_processing(video_inputs[0]    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)

						# Test batched
						lowerCAmelCase__		  =  image_processing(a_    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# Initialize image_processing
						lowerCAmelCase__		  =  self.image_processing_class(**self.image_processor_dict						)
						# create random numpy tensors
						lowerCAmelCase__		  =  prepare_video_inputs(self.image_processor_tester    ,equal_resolution=a_    ,numpify=a_						)
						for video in video_inputs:
										self.assertIsInstance(a_    ,a_						)
										self.assertIsInstance(video[0]    ,np.ndarray						)

						# Test not batched input
						lowerCAmelCase__		  =  image_processing(video_inputs[0]    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)

						# Test batched
						lowerCAmelCase__		  =  image_processing(a_    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						# Initialize image_processing
						lowerCAmelCase__		  =  self.image_processing_class(**self.image_processor_dict						)
						# create random PyTorch tensors
						lowerCAmelCase__		  =  prepare_video_inputs(self.image_processor_tester    ,equal_resolution=a_    ,torchify=a_						)
						for video in video_inputs:
										self.assertIsInstance(a_    ,a_						)
										self.assertIsInstance(video[0]    ,torch.Tensor						)

						# Test not batched input
						lowerCAmelCase__		  =  image_processing(video_inputs[0]    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        1,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)

						# Test batched
						lowerCAmelCase__		  =  image_processing(a_    ,return_tensors='pt'						).pixel_values
						self.assertEqual(
						    encoded_videos.shape    ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_frames,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['height'],
						        self.image_processor_tester.crop_size['width'],
						    )    ,)





import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor


@require_tokenizers
@require_vision
class 	__snake_case   ( unittest.TestCase ):
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  tempfile.mkdtemp()

						# fmt: off
						lowerCAmelCase__		  =  ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
						# fmt: on
						lowerCAmelCase__		  =  os.path.join(self.tmpdirname    ,VOCAB_FILES_NAMES['vocab_file']						)
						with open(self.vocab_file    ,'w'    ,encoding='utf-8'						) as vocab_writer:
										vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]						)						)

						lowerCAmelCase__		  =  {
						    'do_resize': True,
						    'size': {'height': 18, 'width': 18},
						    'do_normalize': True,
						    'image_mean': [0.5, 0.5, 0.5],
						    'image_std': [0.5, 0.5, 0.5],
						}
						lowerCAmelCase__		  =  os.path.join(self.tmpdirname    ,a_						)
						with open(self.image_processor_file    ,'w'    ,encoding='utf-8'						) as fp:
										json.dump(a_    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return BertTokenizer.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return ViTImageProcessor.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						shutil.rmtree(self.tmpdirname						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  [np.random.randint(255    ,size=(3, 30, 400)    ,dtype=np.uinta						)]

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

						return image_inputs
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_tokenizer()
						lowerCAmelCase__		  =  self.get_image_processor()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						processor.save_pretrained(self.tmpdirname						)
						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname						)

						self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer.get_vocab()						)
						self.assertIsInstance(processor.tokenizer    ,(BertTokenizer, BertTokenizerFast)						)

						self.assertEqual(processor.image_processor.to_json_string()    ,image_processor.to_json_string()						)
						self.assertIsInstance(processor.image_processor    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(
						    tokenizer=self.get_tokenizer()    ,image_processor=self.get_image_processor()						)
						processor.save_pretrained(self.tmpdirname						)

						lowerCAmelCase__		  =  self.get_tokenizer(bos_token='(BOS)'    ,eos_token='(EOS)'						)
						lowerCAmelCase__		  =  self.get_image_processor(do_normalize=a_    ,padding_value=1.0						)

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor.from_pretrained(
						    self.tmpdirname    ,bos_token='(BOS)'    ,eos_token='(EOS)'    ,do_normalize=a_    ,padding_value=1.0						)

						self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer_add_kwargs.get_vocab()						)
						self.assertIsInstance(processor.tokenizer    ,(BertTokenizer, BertTokenizerFast)						)

						self.assertEqual(processor.image_processor.to_json_string()    ,image_processor_add_kwargs.to_json_string()						)
						self.assertIsInstance(processor.image_processor    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  image_processor(a_    ,return_tensors='np'						)
						lowerCAmelCase__		  =  processor(images=a_    ,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 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'

						lowerCAmelCase__		  =  processor(text=a_						)

						lowerCAmelCase__		  =  tokenizer(a_						)

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







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

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

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

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

						# test if it raises when no input is passed
						with self.assertRaises(a_						):
										processor()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

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

						lowerCAmelCase__		  =  processor.batch_decode(a_						)
						lowerCAmelCase__		  =  tokenizer.batch_decode(a_						)

						self.assertListEqual(a_    ,a_						)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

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

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

						self.assertListEqual(list(inputs.keys()						)    ,processor.model_input_names						)
