





"""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  _SCREAMING_SNAKE_CASE(       unittest.TestCase  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=10		,SCREAMING_SNAKE_CASE__=18		,SCREAMING_SNAKE_CASE__=30		,SCREAMING_SNAKE_CASE__=4_00		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]		,SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]		,SCREAMING_SNAKE_CASE__=None		,)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  size if size is not None else {'''shortest_edge''': 18}
             __SCREAMING_SNAKE_CASE       :Tuple		    =  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

             __SCREAMING_SNAKE_CASE       :List[Any]		    =  parent
             __SCREAMING_SNAKE_CASE       :List[str]		    =  batch_size
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  num_channels
             __SCREAMING_SNAKE_CASE       :Any		    =  num_frames
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  image_size
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  min_resolution
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  max_resolution
             __SCREAMING_SNAKE_CASE       :Dict		    =  do_resize
             __SCREAMING_SNAKE_CASE       :str		    =  size
             __SCREAMING_SNAKE_CASE       :Dict		    =  do_normalize
             __SCREAMING_SNAKE_CASE       :Dict		    =  image_mean
             __SCREAMING_SNAKE_CASE       :List[str]		    =  image_std
             __SCREAMING_SNAKE_CASE       :str		    =  crop_size




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """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  _SCREAMING_SNAKE_CASE(       A   ,     unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_       :				Dict     						=  VivitImageProcessor if is_vision_available() else None




       def    _UpperCamelCase					(						self							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  VivitImageProcessingTester(self							)




       @property
       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             return self.image_processor_tester.prepare_image_processor_dict()




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.image_processing_class(**self.image_processor_dict							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''image_mean'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''image_std'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_normalize'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_resize'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_center_crop'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''size'''							)							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  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}							)

             __SCREAMING_SNAKE_CASE       :List[str]		    =  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"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.image_processing_class(**self.image_processor_dict							)
             # create random PIL videos
             __SCREAMING_SNAKE_CASE       :Any		    =  prepare_video_inputs(self.image_processor_tester		,equal_resolution=SCREAMING_SNAKE_CASE__							)
             for video in video_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(video[0]		,Image.Image							)

             # Test not batched input
             __SCREAMING_SNAKE_CASE       :int		    =  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
             __SCREAMING_SNAKE_CASE       :List[str]		    =  image_processing(SCREAMING_SNAKE_CASE__		,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    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.image_processing_class(**self.image_processor_dict							)
             # create random numpy tensors
             __SCREAMING_SNAKE_CASE       :int		    =  prepare_video_inputs(self.image_processor_tester		,equal_resolution=SCREAMING_SNAKE_CASE__		,numpify=SCREAMING_SNAKE_CASE__							)
             for video in video_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(video[0]		,np.ndarray							)

             # Test not batched input
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  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
             __SCREAMING_SNAKE_CASE       :Any		    =  image_processing(SCREAMING_SNAKE_CASE__		,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    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.image_processing_class(**self.image_processor_dict							)
             # create random PyTorch tensors
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  prepare_video_inputs(self.image_processor_tester		,equal_resolution=SCREAMING_SNAKE_CASE__		,torchify=SCREAMING_SNAKE_CASE__							)
             for video in video_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(video[0]		,torch.Tensor							)

             # Test not batched input
             __SCREAMING_SNAKE_CASE       :Dict		    =  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
             __SCREAMING_SNAKE_CASE       :Dict		    =  image_processing(SCREAMING_SNAKE_CASE__		,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 torch import nn






class  _SCREAMING_SNAKE_CASE(       nn.Module  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






             super().__init__()
             __SCREAMING_SNAKE_CASE       :Tuple		    =  class_size
             __SCREAMING_SNAKE_CASE       :str		    =  embed_size
             # self.mlp1 = nn.Linear(embed_size, embed_size)
             # self.mlp2 = (nn.Linear(embed_size, class_size))
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  nn.Linear(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.mlp(SCREAMING_SNAKE_CASE__							)
             return logits