




"""simple docstring"""



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 MobileNetVaImageProcessor




class      UpperCAmelCase_  (						unittest.TestCase							):




    def __init__(   self						:	Tuple     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	Optional[Any]=7     ,			snake_case_						:	Tuple=3     ,			snake_case_						:	List[Any]=18     ,			snake_case_						:	Optional[Any]=30     ,			snake_case_						:	Any=400     ,			snake_case_						:	List[str]=True     ,			snake_case_						:	Optional[Any]=None     ,			snake_case_						:	List[Any]=True     ,			snake_case_						:	str=None     ,			)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    size if size is not None else {"shortest_edge": 20}
     A__	=    crop_size if crop_size is not None else {"height": 18, "width": 18}
     A__	=    parent
     A__	=    batch_size
     A__	=    num_channels
     A__	=    image_size
     A__	=    min_resolution
     A__	=    max_resolution
     A__	=    do_resize
     A__	=    size
     A__	=    do_center_crop
     A__	=    crop_size




    def 							__magic_name__							(   self						:	Any				)				->							Tuple:




     '''simple docstring'''

     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
     }






@require_torch
@require_vision
class      UpperCAmelCase_  (						A_,       unittest.TestCase							):
    lowercase__						 =				MobileNetVaImageProcessor if is_vision_available() else None




    def 							__magic_name__							(   self						:	str				)				->							int:




     '''simple docstring'''

     A__	=    MobileNetVaImageProcessingTester(self				)




    @property
    def 							__magic_name__							(   self						:	str				)				->							Union[str, Any]:




     '''simple docstring'''

     return self.image_processor_tester.prepare_image_processor_dict()




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							List[str]:




     '''simple docstring'''

     A__	=    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_     ,			"crop_size"				)				)




    def 							__magic_name__							(   self						:	int				)				->							int:




     '''simple docstring'''

     A__	=    self.image_processing_class.from_dict(self.image_processor_dict				)
     self.assertEqual(image_processor.size     ,			{"shortest_edge": 20}				)
     self.assertEqual(image_processor.crop_size     ,			{"height": 18, "width": 18}				)

     A__	=    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 							__magic_name__							(   self						:	List[str]				)				->							Optional[int]:




     '''simple docstring'''

     pass




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




     '''simple docstring'''

     A__	=    self.image_processing_class(**self.image_processor_dict				)
     # create random PIL images
     A__	=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=snake_case_				)
     for image in image_inputs:
      self.assertIsInstance(snake_case_     ,			Image.Image				)

     # Test not batched input
     A__	=    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
     A__	=    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 							__magic_name__							(   self						:	List[Any]				)				->							Optional[int]:




     '''simple docstring'''

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

     # Test not batched input
     A__	=    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
     A__	=    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 							__magic_name__							(   self						:	Tuple				)				->							Dict:




     '''simple docstring'''

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

     # Test not batched input
     A__	=    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
     A__	=    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"],
         )     ,			)











"""simple docstring"""



import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
      from transformers import TvltImageProcessor

if is_speech_available():
      from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class      UpperCAmelCase_  (						unittest.TestCase							):




    def 							__magic_name__							(   self						:	str				)				->							Dict:




     '''simple docstring'''

     A__	=    "ZinengTang/tvlt-base"
     A__	=    tempfile.mkdtemp()




    def 							__magic_name__							(   self						:	int     ,			**snake_case_						:	Dict				)				->							Union[str, Any]:




     '''simple docstring'''

     return TvltImageProcessor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]     ,			**snake_case_						:	str				)				->							List[str]:




     '''simple docstring'''

     return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,			**snake_case_				)




    def 							__magic_name__							(   self						:	Optional[int]				)				->							str:




     '''simple docstring'''

     shutil.rmtree(self.tmpdirname				)




    def 							__magic_name__							(   self						:	Tuple				)				->							Any:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)
     processor.save_pretrained(self.tmpdirname				)
     A__	=    TvltProcessor.from_pretrained(self.tmpdirname				)

     self.assertIsInstance(processor.feature_extractor     ,			snake_case_				)
     self.assertIsInstance(processor.image_processor     ,			snake_case_				)




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




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)

     A__	=    feature_extractor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(audio=snake_case_     ,			return_tensors="np"				)

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




    def 							__magic_name__							(   self						:	Tuple				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([3, 224, 224]				)

     A__	=    image_processor(snake_case_     ,			return_tensors="np"				)
     A__	=    processor(images=snake_case_     ,			return_tensors="np"				)

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




    def 							__magic_name__							(   self						:	Tuple				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     A__	=    np.ones([12_000]				)
     A__	=    np.ones([3, 224, 224]				)

     A__	=    processor(audio=snake_case_     ,			images=snake_case_				)

     self.assertListEqual(list(inputs.keys()				)     ,			["audio_values", "audio_mask", "pixel_values", "pixel_mask"]				)

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




    def 							__magic_name__							(   self						:	Optional[Any]				)				->							Tuple:




     '''simple docstring'''

     A__	=    self.get_image_processor()
     A__	=    self.get_feature_extractor()

     A__	=    TvltProcessor(image_processor=snake_case_     ,			feature_extractor=snake_case_				)

     self.assertListEqual(
         processor.model_input_names     ,			image_processor.model_input_names + feature_extractor.model_input_names     ,			msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"     ,			)





