




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 TvltImageProcessor
def 				__UpperCamelCase  (		snake_case    ,	snake_case=1_0    ,	snake_case=1_0    ,	snake_case=False    ,	snake_case=False     )  ->						Any:


   '''simple docstring'''







   __A				 =       []
   for i in range(image_processor_tester.num_frames     ):
      video.append(np.random.randint(2_5_5    ,	size=(image_processor_tester.num_channels, width, height)    ,	dtype=np.uinta     )     )

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

   if torchify:
      __A				 =       [torch.from_numpy(snake_case     ) for frame in video]

   return video
def 				__UpperCamelCase  (		snake_case    ,	snake_case=False    ,	snake_case=False    ,	snake_case=False     )  ->						Tuple:


   '''simple docstring'''







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

   __A				 =       []
   for i in range(image_processor_tester.batch_size     ):
      if equal_resolution:
         __A				 =       __A				 =       image_processor_tester.max_resolution
      else:
         __A ,						__A				 =       np.random.choice(
             np.arange(image_processor_tester.min_resolution    ,	image_processor_tester.max_resolution     )    ,	2     )
         __A				 =       prepare_video(
             image_processor_tester=snake_case    ,	width=snake_case    ,	height=snake_case    ,	numpify=snake_case    ,	torchify=snake_case    ,	)
      video_inputs.append(snake_case     )

   return video_inputs







class 						_lowerCAmelCase(						unittest.TestCase):

  """simple docstring"""







  def __init__( self  ,    UpperCAmelCase  ,    UpperCAmelCase=7  ,    UpperCAmelCase=3  ,    UpperCAmelCase=4  ,    UpperCAmelCase=18  ,    UpperCAmelCase=30  ,    UpperCAmelCase=4_00  ,    UpperCAmelCase=True  ,    UpperCAmelCase=None  ,    UpperCAmelCase=True  ,    UpperCAmelCase=[0.5, 0.5, 0.5]  ,    UpperCAmelCase=[0.5, 0.5, 0.5]  ,    UpperCAmelCase=True  ,    UpperCAmelCase=None  ,    )-> List[str]:
     __A				 =       size if size is not None else {'''shortest_edge''': 18}
     __A				 =       crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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





  def 		SCREAMING_SNAKE_CASE__	( self     )-> str:
     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "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 						_lowerCAmelCase(						_a		,   unittest.TestCase):

  """simple docstring"""







  lowerCamelCase__      					=   TvltImageProcessor if is_vision_available() else None

  def 		SCREAMING_SNAKE_CASE__	( self     )-> Any:
     __A				 =       TvltImageProcessorTester(self     )

  @property
  def 		SCREAMING_SNAKE_CASE__	( self     )-> List[Any]:
     return self.image_processor_tester.prepare_image_processor_dict()

  def 		SCREAMING_SNAKE_CASE__	( self     )-> int:
     __A				 =       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     )-> Dict:
     # Initialize image_processor
     __A				 =       self.image_processing_class(**self.image_processor_dict     )
     # create random PIL videos
     __A				 =       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
     __A				 =       image_processor(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
     __A				 =       image_processor(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     )-> int:
     # Initialize image_processor
     __A				 =       self.image_processing_class(**self.image_processor_dict     )
     # create random numpy tensors
     __A				 =       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
     __A				 =       image_processor(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
     __A				 =       image_processor(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     )-> List[str]:
     # Initialize image_processor
     __A				 =       self.image_processing_class(**self.image_processor_dict     )
     # create random PyTorch tensors
     __A				 =       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
     __A				 =       image_processor(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
     __A				 =       image_processor(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'''],
         )  ,    )








from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase   :							Dict     =	logging.get_logger(__name__)

_UpperCamelCase   :							Optional[int]     =	{
    """MCG-NJU/videomae-base""": """https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json""",
}







class 						_lowerCAmelCase(						_a):

  """simple docstring"""







  lowerCamelCase__      					=   '''videomae'''

  def __init__( self  ,    UpperCAmelCase=2_24  ,    UpperCAmelCase=16  ,    UpperCAmelCase=3  ,    UpperCAmelCase=16  ,    UpperCAmelCase=2  ,    UpperCAmelCase=7_68  ,    UpperCAmelCase=12  ,    UpperCAmelCase=12  ,    UpperCAmelCase=30_72  ,    UpperCAmelCase="gelu"  ,    UpperCAmelCase=0.0  ,    UpperCAmelCase=0.0  ,    UpperCAmelCase=0.02  ,    UpperCAmelCase=1e-12  ,    UpperCAmelCase=True  ,    UpperCAmelCase=True  ,    UpperCAmelCase=6  ,    UpperCAmelCase=3_84  ,    UpperCAmelCase=4  ,    UpperCAmelCase=15_36  ,    UpperCAmelCase=True  ,    **UpperCAmelCase  ,    )-> Any:
     super().__init__(**UpperCAmelCase     )

     __A				 =       image_size
     __A				 =       patch_size
     __A				 =       num_channels
     __A				 =       num_frames
     __A				 =       tubelet_size

     __A				 =       hidden_size
     __A				 =       num_hidden_layers
     __A				 =       num_attention_heads
     __A				 =       intermediate_size
     __A				 =       hidden_act
     __A				 =       hidden_dropout_prob
     __A				 =       attention_probs_dropout_prob
     __A				 =       initializer_range
     __A				 =       layer_norm_eps
     __A				 =       qkv_bias
     __A				 =       use_mean_pooling

     __A				 =       decoder_num_attention_heads
     __A				 =       decoder_hidden_size
     __A				 =       decoder_num_hidden_layers
     __A				 =       decoder_intermediate_size
     __A				 =       norm_pix_loss

