





'''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 					__snake_case  (					unittest.TestCase							):






   '''simple docstring'''





   def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE=7	,		__SCREAMING_SNAKE_CASE=3	,		__SCREAMING_SNAKE_CASE=1_0	,		__SCREAMING_SNAKE_CASE=1_8	,		__SCREAMING_SNAKE_CASE=3_0	,		__SCREAMING_SNAKE_CASE=4_0_0	,		__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	,		):
    snake_case__     :					Union[str, Any]    =  size if size is not None else {"""shortest_edge""": 1_8}
    snake_case__     :					Tuple    =  crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}

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


   def 		__UpperCamelCase						(							self       ):
    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							):






   '''simple docstring'''





   lowerCamelCase__    							=      VivitImageProcessor if is_vision_available() else None

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					List[Any]    =  VivitImageProcessingTester(self       )

   @property
   def 		__UpperCamelCase						(							self       ):
    return self.image_processor_tester.prepare_image_processor_dict()

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Union[str, Any]    =  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       ):
    snake_case__     :					Optional[Any]    =  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}       )

    snake_case__     :					Dict    =  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 		__UpperCamelCase						(							self       ):
    # Initialize image_processing
    snake_case__     :					str    =  self.image_processing_class(**self.image_processor_dict       )
    # create random PIL videos
    snake_case__     :					str    =  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
    snake_case__     :					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
    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       ):
    # Initialize image_processing
    snake_case__     :					int    =  self.image_processing_class(**self.image_processor_dict       )
    # create random numpy tensors
    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
    snake_case__     :					Optional[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
    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       ):
    # Initialize image_processing
    snake_case__     :					Any    =  self.image_processing_class(**self.image_processor_dict       )
    # create random PyTorch tensors
    snake_case__     :					int    =  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
    snake_case__     :					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
    snake_case__     :					Tuple    =  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"""],
        )	,		)








from typing import Optional

from .. import Features, NamedSplit
from ..packaged_modules.text.text import Text
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader



class 				A       (			UpperCAmelCase__				):
					'''simple docstring'''






					def __init__(self       :		Any ,   _UpperCAmelCase       :		NestedDataStructureLike[PathLike] ,   _UpperCAmelCase       :		Optional[NamedSplit] = None ,   _UpperCAmelCase       :		Optional[Features] = None ,   _UpperCAmelCase       :		str = None ,   _UpperCAmelCase       :		bool = False ,   _UpperCAmelCase       :		bool = False ,   _UpperCAmelCase       :		Optional[int] = None ,   **_UpperCAmelCase       :		Optional[int] ,   )		->  List[str]:




							"""simple docstring"""


							super().__init__(
							    _UpperCAmelCase ,   split=_UpperCAmelCase ,   features=_UpperCAmelCase ,   cache_dir=_UpperCAmelCase ,   keep_in_memory=_UpperCAmelCase ,   streaming=_UpperCAmelCase ,   num_proc=_UpperCAmelCase ,   **_UpperCAmelCase ,   )
							lowercase__	     =						path_or_paths if isinstance(_UpperCAmelCase ,   _UpperCAmelCase						) else {self.split: path_or_paths}
							lowercase__	     =						Text(
							    cache_dir=_UpperCAmelCase ,   data_files=_UpperCAmelCase ,   features=_UpperCAmelCase ,   **_UpperCAmelCase ,   )


					def     lowerCamelCase__	(self       :		Tuple						)		->  Any:




							"""simple docstring"""


							if self.streaming:
									lowercase__	     =						self.builder.as_streaming_dataset(split=self.split						)
							# Build regular (map-style) dataset
							else:
									lowercase__	     =						None
									lowercase__	     =						None
									lowercase__	     =						None
									lowercase__	     =						None

									self.builder.download_and_prepare(
									    download_config=_UpperCAmelCase ,   download_mode=_UpperCAmelCase ,   verification_mode=_UpperCAmelCase ,   base_path=_UpperCAmelCase ,   num_proc=self.num_proc ,   )
									lowercase__	     =						self.builder.as_dataset(
									    split=self.split ,   verification_mode=_UpperCAmelCase ,   in_memory=self.keep_in_memory						)
							return dataset

