






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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor






def  lowerCAmelCase__		(					a_  :     Tuple			,						a_  :     str=1_0			,						a_  :     str=1_0			,						a_  :     Any=False			,						a_  :     List[Any]=False			)   -> Tuple:
       UpperCAmelCase__	:   Optional[int]												=	[]
       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
              UpperCAmelCase__	:   int												=	[Image.fromarray(np.moveaxis(a_			,						0			,						-1			)			) for frame in video]

       if torchify:
              UpperCAmelCase__	:   int												=	[torch.from_numpy(a_			) for frame in video]

       return video






def  lowerCAmelCase__		(					a_  :     Union[str, Any]			,						a_  :     Dict=False			,						a_  :     Union[str, Any]=False			,						a_  :     int=False			)   -> Any:
       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       UpperCAmelCase__	:   List[str]												=	[]
       for i in range(image_processor_tester.batch_size			):
              if equal_resolution:
                     UpperCAmelCase__	:   Dict												=	image_processor_tester.max_resolution
              else:
                     UpperCAmelCase__							,							UpperCAmelCase__	:   Dict												=	np.random.choice(
                         np.arange(image_processor_tester.min_resolution			,						image_processor_tester.max_resolution			)			,						2			)
                     UpperCAmelCase__	:   Any												=	prepare_video(
                         image_processor_tester=a_			,						width=a_			,						height=a_			,						numpify=a_			,						torchify=a_			,						)
              video_inputs.append(a_			)

       return video_inputs




class 					__UpperCAmelCase (		unittest.TestCase	):

      '''simple docstring'''





      def __init__(					self  ,		_UpperCAmelCase  ,		_UpperCAmelCase=7  ,		_UpperCAmelCase=3  ,		_UpperCAmelCase=4  ,		_UpperCAmelCase=18  ,		_UpperCAmelCase=30  ,		_UpperCAmelCase=400  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=None  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=[0.5, 0.5, 0.5]  ,		_UpperCAmelCase=[0.5, 0.5, 0.5]  ,		_UpperCAmelCase=True  ,		_UpperCAmelCase=None  ,		):
             UpperCAmelCase__	:   List[Any]												=	size if size is not None else {'''shortest_edge''': 18}
             UpperCAmelCase__	:   Dict												=	crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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




      def 							lowerCamelCase     (					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,
                 "do_center_crop": self.do_center_crop,
                 "crop_size": self.crop_size,
             }




@require_torch
@require_vision
class 					__UpperCAmelCase (		UpperCamelCase__      ,				unittest.TestCase	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    Tuple				       =					TvltImageProcessor if is_vision_available() else None




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   List[str]												=	TvltImageProcessorTester(self						)




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




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







'''simple docstring'''







import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
  import torch

  from transformers import CamembertModel




@require_torch
@require_sentencepiece
@require_tokenizers
class 					__UpperCAmelCase (		unittest.TestCase	):

      '''simple docstring'''





      @slow
      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Optional[int]												=	CamembertModel.from_pretrained('''camembert-base'''						)
             model.to(_UpperCAmelCase						)

             UpperCAmelCase__	:   Optional[int]												=	torch.tensor(
                 [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]]  ,		device=_UpperCAmelCase  ,		dtype=torch.long  ,		)  # J'aime le camembert !
             with torch.no_grad():
                    UpperCAmelCase__	:   Tuple												=	model(_UpperCAmelCase						)['''last_hidden_state''']
             UpperCAmelCase__	:   List[str]												=	torch.Size((1, 10, 768)						)
             self.assertEqual(output.shape  ,		_UpperCAmelCase						)
             # compare the actual values for a slice.
             UpperCAmelCase__	:   List[Any]												=	torch.tensor(
                 [[[-0.02_54, 0.02_35, 0.10_27], [0.06_06, -0.18_11, -0.04_18], [-0.15_61, -0.11_27, 0.26_87]]]  ,		device=_UpperCAmelCase  ,		dtype=torch.float  ,		)
             # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
             # camembert.eval()
             # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

             self.assertTrue(torch.allclose(output[:, :3, :3]  ,		_UpperCAmelCase  ,		atol=1E-4						)						)