






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
					from transformers import TvltImageProcessor

if is_speech_available():
					from transformers import TvltFeatureExtractor

from transformers import TvltProcessor


@require_torch
class     __SCREAMING_SNAKE_CASE					(   unittest.TestCase):






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						'ZinengTang/tvlt-base'
												lowerCAmelCase__    						=						tempfile.mkdtemp()






						def       UpperCamelCase__      (	self     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												return TvltImageProcessor.from_pretrained(self.checkpoint     ,						**_UpperCamelCase   )






						def       UpperCamelCase__      (	self     ,						**_UpperCamelCase   ):






												"""simple docstring"""
												return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,						**_UpperCamelCase   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												shutil.rmtree(self.tmpdirname   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_image_processor()
												lowerCAmelCase__    						=						self.get_feature_extractor()

												lowerCAmelCase__    						=						TvltProcessor(image_processor=_UpperCamelCase     ,						feature_extractor=_UpperCamelCase   )
												processor.save_pretrained(self.tmpdirname   )
												lowerCAmelCase__    						=						TvltProcessor.from_pretrained(self.tmpdirname   )

												self.assertIsInstance(processor.feature_extractor     ,						_UpperCamelCase   )
												self.assertIsInstance(processor.image_processor     ,						_UpperCamelCase   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_image_processor()
												lowerCAmelCase__    						=						self.get_feature_extractor()

												lowerCAmelCase__    						=						TvltProcessor(image_processor=_UpperCamelCase     ,						feature_extractor=_UpperCamelCase   )

												lowerCAmelCase__    						=						np.ones([1_20_00]   )

												lowerCAmelCase__    						=						feature_extractor(_UpperCamelCase     ,						return_tensors='np'   )
												lowerCAmelCase__    						=						processor(audio=_UpperCamelCase     ,						return_tensors='np'   )

												for key in audio_dict.keys():
																		self.assertAlmostEqual(audio_dict[key].sum()     ,						input_processor[key].sum()     ,						delta=1E-2   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_image_processor()
												lowerCAmelCase__    						=						self.get_feature_extractor()

												lowerCAmelCase__    						=						TvltProcessor(image_processor=_UpperCamelCase     ,						feature_extractor=_UpperCamelCase   )

												lowerCAmelCase__    						=						np.ones([3, 2_24, 2_24]   )

												lowerCAmelCase__    						=						image_processor(_UpperCamelCase     ,						return_tensors='np'   )
												lowerCAmelCase__    						=						processor(images=_UpperCamelCase     ,						return_tensors='np'   )

												for key in image_dict.keys():
																		self.assertAlmostEqual(image_dict[key].sum()     ,						input_processor[key].sum()     ,						delta=1E-2   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_image_processor()
												lowerCAmelCase__    						=						self.get_feature_extractor()

												lowerCAmelCase__    						=						TvltProcessor(image_processor=_UpperCamelCase     ,						feature_extractor=_UpperCamelCase   )

												lowerCAmelCase__    						=						np.ones([1_20_00]   )
												lowerCAmelCase__    						=						np.ones([3, 2_24, 2_24]   )

												lowerCAmelCase__    						=						processor(audio=_UpperCamelCase     ,						images=_UpperCamelCase   )

												self.assertListEqual(list(inputs.keys()   )     ,						['audio_values', 'audio_mask', 'pixel_values', 'pixel_mask']   )

												# test if it raises when no input is passed
												with pytest.raises(_UpperCamelCase   ):
																		processor()







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_image_processor()
												lowerCAmelCase__    						=						self.get_feature_extractor()

												lowerCAmelCase__    						=						TvltProcessor(image_processor=_UpperCamelCase     ,						feature_extractor=_UpperCamelCase   )

												self.assertListEqual(
												    processor.model_input_names     ,						image_processor.model_input_names + feature_extractor.model_input_names     ,						msg='`processor` and `image_processor`+`feature_extractor` model input names do not match'     ,						)













import numpy as np
from PIL import Image







def 	_UpperCamelCase   (    UpperCamelCase_       :					np.ndarray    ,       UpperCamelCase_       :					int    ,       UpperCamelCase_       :					int		)  ->   np.ndarray:

						"""simple docstring"""



						lowerCAmelCase__    						=						np.array(UpperCamelCase_		)
						if arr.shape[0] != arr.shape[1]:
												raise ValueError('The input array is not a square matrix'		)
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0

						# compute the shape of the output matrix
						lowerCAmelCase__    						=						(arr.shape[0] - size) // stride + 1
						# initialize the output matrix with zeros of shape maxpool_shape
						lowerCAmelCase__    						=						np.zeros((maxpool_shape, maxpool_shape)		)

						while i < arr.shape[0]:
												if i + size > arr.shape[0]:
																		# if the end of the matrix is reached, break
																		break
												while j < arr.shape[1]:
																		# if the end of the matrix is reached, break
																		if j + size > arr.shape[1]:
																								break
																		# compute the maximum of the pooling matrix
																		lowerCAmelCase__    						=						np.max(arr[i : i + size, j : j + size]		)
																		# shift the pooling matrix by stride of column pixels
																		j += stride
																		mat_j += 1

												# shift the pooling matrix by stride of row pixels
												i += stride
												mat_i += 1

												# reset the column index to 0
												lowerCAmelCase__    						=						0
												lowerCAmelCase__    						=						0

						return updated_arr







def 	_UpperCamelCase   (    UpperCamelCase_       :					np.ndarray    ,       UpperCamelCase_       :					int    ,       UpperCamelCase_       :					int		)  ->   np.ndarray:

						"""simple docstring"""



						lowerCAmelCase__    						=						np.array(UpperCamelCase_		)
						if arr.shape[0] != arr.shape[1]:
												raise ValueError('The input array is not a square matrix'		)
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						0

						# compute the shape of the output matrix
						lowerCAmelCase__    						=						(arr.shape[0] - size) // stride + 1
						# initialize the output matrix with zeros of shape avgpool_shape
						lowerCAmelCase__    						=						np.zeros((avgpool_shape, avgpool_shape)		)

						while i < arr.shape[0]:
												# if the end of the matrix is reached, break
												if i + size > arr.shape[0]:
																		break
												while j < arr.shape[1]:
																		# if the end of the matrix is reached, break
																		if j + size > arr.shape[1]:
																								break
																		# compute the average of the pooling matrix
																		lowerCAmelCase__    						=						int(np.average(arr[i : i + size, j : j + size]		)		)
																		# shift the pooling matrix by stride of column pixels
																		j += stride
																		mat_j += 1

												# shift the pooling matrix by stride of row pixels
												i += stride
												mat_i += 1
												# reset the column index to 0
												lowerCAmelCase__    						=						0
												lowerCAmelCase__    						=						0

						return updated_arr


# Main Function
if __name__ == "__main__":
					from doctest import testmod

					testmod(name="""avgpooling""", verbose=True)

					# Loading the image
					__snake_case    :    List[str]         =     Image.open("""path_to_image""")

					# Converting the image to numpy array and maxpooling, displaying the result
					# Ensure that the image is a square matrix

					Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

					# Converting the image to numpy array and averagepooling, displaying the result
					# Ensure that the image is a square matrix

					Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()





