

"""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 VideoMAEImageProcessor





class 				lowerCamelCase  (unittest.TestCase     ):

			def __init__(   self			:  Any      ,			__UpperCAmelCase			:  Any      ,			__UpperCAmelCase			:  str=7      ,			__UpperCAmelCase			:  Optional[Any]=3      ,			__UpperCAmelCase			:  Union[str, Any]=1_0      ,			__UpperCAmelCase			:  str=1_8      ,			__UpperCAmelCase			:  Optional[Any]=3_0      ,			__UpperCAmelCase			:  int=4_0_0      ,			__UpperCAmelCase			:  Union[str, Any]=True      ,			__UpperCAmelCase			:  int=None      ,			__UpperCAmelCase			:  Optional[int]=True      ,			__UpperCAmelCase			:  Tuple=[0.5, 0.5, 0.5]      ,			__UpperCAmelCase			:  Union[str, Any]=[0.5, 0.5, 0.5]      ,			__UpperCAmelCase			:  Optional[Any]=None      ,			)				->      Dict:
						SCREAMING_SNAKE_CASE__									= size if size is not None else {"""shortest_edge""": 1_8}
						SCREAMING_SNAKE_CASE__									= crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}

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





			def    SCREAMING_SNAKE_CASE			(   self			:  List[Any]    )				->      Optional[Any]:
						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 				lowerCamelCase  (A__     ,unittest.TestCase     ):
			lowerCamelCase__				:						Any   						=							VideoMAEImageProcessor if is_vision_available() else None

			def    SCREAMING_SNAKE_CASE			(   self			:  List[str]    )				->      Optional[Any]:
						SCREAMING_SNAKE_CASE__									= VideoMAEImageProcessingTester(self    )

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

			def    SCREAMING_SNAKE_CASE			(   self			:  Any    )				->      Union[str, Any]:
						SCREAMING_SNAKE_CASE__									= 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			:  Tuple    )				->      str:
						SCREAMING_SNAKE_CASE__									= 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}    )

						SCREAMING_SNAKE_CASE__									= 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			:  Any    )				->      Any:
						pass

			def    SCREAMING_SNAKE_CASE			(   self			:  List[str]    )				->      Optional[int]:
						# Initialize image_processing
						SCREAMING_SNAKE_CASE__									= self.image_processing_class(**self.image_processor_dict    )
						# create random PIL videos
						SCREAMING_SNAKE_CASE__									= 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
						SCREAMING_SNAKE_CASE__									= 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__									= 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			:  int    )				->      Union[str, Any]:
						# Initialize image_processing
						SCREAMING_SNAKE_CASE__									= self.image_processing_class(**self.image_processor_dict    )
						# create random numpy tensors
						SCREAMING_SNAKE_CASE__									= 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
						SCREAMING_SNAKE_CASE__									= 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__									= 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			:  Optional[Any]    )				->      List[Any]:
						# Initialize image_processing
						SCREAMING_SNAKE_CASE__									= self.image_processing_class(**self.image_processor_dict    )
						# create random PyTorch tensors
						SCREAMING_SNAKE_CASE__									= 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
						SCREAMING_SNAKE_CASE__									= 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__									= 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"""




import importlib.metadata
import subprocess
import sys

def 		A    (       snake_case__ = False      ):




			'''simple docstring'''


			SCREAMING_SNAKE_CASE__									= False
			if "IPython" in sys.modules:
						SCREAMING_SNAKE_CASE__									= """google.colab""" in str(sys.modules["""IPython"""].get_ipython()      )

			if in_colab:
						if upgrade:
									SCREAMING_SNAKE_CASE__									= ["""pip""", """install""", """-U""", """torch"""]
									subprocess.run(snake_case__						,			check=snake_case__      )
						# get the current version of torch
						SCREAMING_SNAKE_CASE__									= importlib.metadata.version("""torch"""      )
						SCREAMING_SNAKE_CASE__									= torch_version[: torch_version.rindex("""."""      )]
						SCREAMING_SNAKE_CASE__									= f"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
						SCREAMING_SNAKE_CASE__									= ["""pip""", """install""", xla_wheel]
						subprocess.run(snake_case__						,			check=snake_case__      )
			else:
						raise RuntimeError("""`install_xla` utility works only on google colab."""      )


