

'''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_video_inputs


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import VivitImageProcessor






class     lowercase_			(unittest.TestCase   ):




  """simple docstring"""




  def __init__(   self     :  Tuple      ,lowercase__     :  List[str]      ,lowercase__     :  Any=7      ,lowercase__     :  Optional[Any]=3      ,lowercase__     :  Optional[Any]=1_0      ,lowercase__     :  Any=1_8      ,lowercase__     :  Optional[int]=3_0      ,lowercase__     :  List[str]=4_0_0      ,lowercase__     :  Optional[int]=True      ,lowercase__     :  str=None      ,lowercase__     :  Dict=True      ,lowercase__     :  int=[0.5, 0.5, 0.5]      ,lowercase__     :  List[str]=[0.5, 0.5, 0.5]      ,lowercase__     :  Tuple=None      ,):
   __lowercase													=				size if size is not None else {'shortest_edge': 1_8}
   __lowercase													=				crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}

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






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   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     lowercase_			(__lowerCamelCase       , unittest.TestCase   ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE					:						Optional[Any]           =    VivitImageProcessor if is_vision_available() else None






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				VivitImageProcessingTester(self    )






  @property
  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   return self.image_processor_tester.prepare_image_processor_dict()






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   __lowercase													=				self.image_processing_class(**self.image_processor_dict    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''image_mean'''    )    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''image_std'''    )    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''do_normalize'''    )    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''do_resize'''    )    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''do_center_crop'''    )    )
   self.assertTrue(hasattr(UpperCAmelCase_      ,'''size'''    )    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   __lowercase													=				self.image_processing_class.from_dict(self.image_processor_dict    )
   self.assertEqual(image_processor.size      ,{'''shortest_edge''': 1_8}    )
   self.assertEqual(image_processor.crop_size      ,{'''height''': 1_8, '''width''': 1_8}    )

   __lowercase													=				self.image_processing_class.from_dict(self.image_processor_dict      ,size=4_2      ,crop_size=8_4    )
   self.assertEqual(image_processor.size      ,{'''shortest_edge''': 4_2}    )
   self.assertEqual(image_processor.crop_size      ,{'''height''': 8_4, '''width''': 8_4}    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   # Initialize image_processing
   __lowercase													=				self.image_processing_class(**self.image_processor_dict    )
   # create random PIL videos
   __lowercase													=				prepare_video_inputs(self.image_processor_tester      ,equal_resolution=UpperCAmelCase_    )
   for video in video_inputs:
    self.assertIsInstance(UpperCAmelCase_      ,UpperCAmelCase_    )
    self.assertIsInstance(video[0]      ,Image.Image    )

   # Test not batched input
   __lowercase													=				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
   __lowercase													=				image_processing(UpperCAmelCase_      ,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     :  Union[str, Any]    ):
   # Initialize image_processing
   __lowercase													=				self.image_processing_class(**self.image_processor_dict    )
   # create random numpy tensors
   __lowercase													=				prepare_video_inputs(self.image_processor_tester      ,equal_resolution=UpperCAmelCase_      ,numpify=UpperCAmelCase_    )
   for video in video_inputs:
    self.assertIsInstance(UpperCAmelCase_      ,UpperCAmelCase_    )
    self.assertIsInstance(video[0]      ,np.ndarray    )

   # Test not batched input
   __lowercase													=				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
   __lowercase													=				image_processing(UpperCAmelCase_      ,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     :  Tuple    ):
   # Initialize image_processing
   __lowercase													=				self.image_processing_class(**self.image_processor_dict    )
   # create random PyTorch tensors
   __lowercase													=				prepare_video_inputs(self.image_processor_tester      ,equal_resolution=UpperCAmelCase_      ,torchify=UpperCAmelCase_    )
   for video in video_inputs:
    self.assertIsInstance(UpperCAmelCase_      ,UpperCAmelCase_    )
    self.assertIsInstance(video[0]      ,torch.Tensor    )

   # Test not batched input
   __lowercase													=				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
   __lowercase													=				image_processing(UpperCAmelCase_      ,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'''],
       )      ,)








'''simple docstring'''






from argparse import ArgumentParser

from . import BaseTransformersCLICommand


def 	_A  (					A__   ):






 """simple docstring"""



 return DownloadCommand(args.model  ,       args.cache_dir  ,       args.force  ,       args.trust_remote_code   )






class     lowercase_			(lowerCamelCase__   ):




  """simple docstring"""




  @staticmethod
  def       SCREAMING_SNAKE_CASE   (   lowercase__     :  ArgumentParser    ):
   __lowercase													=				parser.add_parser('''download'''    )
   download_parser.add_argument(
       '''--cache-dir'''      ,type=lowercase__      ,default=lowercase__      ,help='''Path to location to store the models'''    )
   download_parser.add_argument(
       '''--force'''      ,action='''store_true'''      ,help='''Force the model to be download even if already in cache-dir'''    )
   download_parser.add_argument(
       '''--trust-remote-code'''      ,action='''store_true'''      ,help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine'''      ,)
   download_parser.add_argument('''model'''      ,type=lowercase__      ,help='''Name of the model to download'''    )
   download_parser.set_defaults(func=lowercase__    )






  def __init__(   self     :  str      ,lowercase__     :  str      ,lowercase__     :  str      ,lowercase__     :  bool      ,lowercase__     :  bool    ):
   __lowercase													=				model
   __lowercase													=				cache
   __lowercase													=				force
   __lowercase													=				trust_remote_code






  def       SCREAMING_SNAKE_CASE   (   self     :  Any    ):
   from ..models.auto import AutoModel, AutoTokenizer

   AutoModel.from_pretrained(
       self._model      ,cache_dir=self._cache      ,force_download=self._force      ,trust_remote_code=self._trust_remote_code    )
   AutoTokenizer.from_pretrained(
       self._model      ,cache_dir=self._cache      ,force_download=self._force      ,trust_remote_code=self._trust_remote_code    )

