
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 BlipImageProcessor



class       a_   ( unittest.TestCase						):






			def __init__(			self							, __UpperCamelCase							, __UpperCamelCase=7							, __UpperCamelCase=3							, __UpperCamelCase=18							, __UpperCamelCase=30							, __UpperCamelCase=400							, __UpperCamelCase=True							, __UpperCamelCase=None							, __UpperCamelCase=True							, __UpperCamelCase=False							, __UpperCamelCase=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]							, __UpperCamelCase=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]							, __UpperCamelCase=True							, ):
									_lowercase									=				size if size is not None else {"""height""": 20, """width""": 20}
									_lowercase									=				parent
									_lowercase									=				batch_size
									_lowercase									=				num_channels
									_lowercase									=				image_size
									_lowercase									=				min_resolution
									_lowercase									=				max_resolution
									_lowercase									=				do_resize
									_lowercase									=				size
									_lowercase									=				do_normalize
									_lowercase									=				image_mean
									_lowercase									=				image_std
									_lowercase									=				do_pad
									_lowercase									=				do_convert_rgb






			def       UpperCamelCase_							(			self     ):
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_convert_rgb": self.do_convert_rgb,
									    "do_pad": self.do_pad,
									}






			def       UpperCamelCase_							(			self							, __UpperCamelCase=False							, __UpperCamelCase=False							, __UpperCamelCase=False     ):
									assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

									if equal_resolution:
															_lowercase									=				[]
															for i in range(self.batch_size     ):
																					image_inputs.append(
																					    np.random.randint(
																					        255							, size=(self.num_channels, self.max_resolution, self.max_resolution)							, dtype=np.uinta     )     )
									else:
															_lowercase									=				[]
															for i in range(self.batch_size     ):
																					_lowercase					,	_lowercase									=				np.random.choice(np.arange(self.min_resolution							, self.max_resolution     )							, 2     )
																					image_inputs.append(np.random.randint(255							, size=(self.num_channels, width, height)							, dtype=np.uinta     )     )

									if not numpify and not torchify:
															# PIL expects the channel dimension as last dimension
															_lowercase									=				[Image.fromarray(np.moveaxis(__UpperCamelCase							, 0							, -1     )     ) for x in image_inputs]

									if torchify:
															_lowercase									=				[torch.from_numpy(__UpperCamelCase     ) for x in image_inputs]

									return image_inputs







@require_torch
@require_vision
class       a_   ( _a				, unittest.TestCase						):
			a  : Dict				      =       BlipImageProcessor if is_vision_available() else None






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				BlipImageProcessingTester(self     )






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






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_resize"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """size"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_normalize"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """image_mean"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """image_std"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_convert_rgb"""     )     )






			def       UpperCamelCase_							(			self     ):
									pass






			def       UpperCamelCase_							(			self     ):
									# Initialize image_processor
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									# create random PIL images
									_lowercase									=				self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase     )
									for image in image_inputs:
															self.assertIsInstance(__UpperCamelCase							, Image.Image     )

									# Test not batched input
									_lowercase									=				image_processor(image_inputs[0]							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )

									# Test batched
									_lowercase									=				image_processor(__UpperCamelCase							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )






			def       UpperCamelCase_							(			self     ):
									# Initialize image_processor
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									# create random numpy tensors
									_lowercase									=				self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase							, numpify=__UpperCamelCase     )
									for image in image_inputs:
															self.assertIsInstance(__UpperCamelCase							, np.ndarray     )

									# Test not batched input
									_lowercase									=				image_processor(image_inputs[0]							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )

									# Test batched
									_lowercase									=				image_processor(__UpperCamelCase							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )






			def       UpperCamelCase_							(			self     ):
									# Initialize image_processor
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									# create random PyTorch tensors
									_lowercase									=				self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase							, torchify=__UpperCamelCase     )
									for image in image_inputs:
															self.assertIsInstance(__UpperCamelCase							, torch.Tensor     )

									# Test not batched input
									_lowercase									=				image_processor(image_inputs[0]							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )

									# Test batched
									_lowercase									=				image_processor(__UpperCamelCase							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )







@require_torch
@require_vision
class       a_   ( _a				, unittest.TestCase						):
			a  : str				      =       BlipImageProcessor if is_vision_available() else None






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				BlipImageProcessingTester(self							, num_channels=4     )
									_lowercase									=				3






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






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_resize"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """size"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_normalize"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """image_mean"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """image_std"""     )     )
									self.assertTrue(hasattr(__UpperCamelCase							, """do_convert_rgb"""     )     )






			def       UpperCamelCase_							(			self     ):
									pass






			def       UpperCamelCase_							(			self     ):
									# Initialize image_processor
									_lowercase									=				self.image_processing_class(**self.image_processor_dict     )
									# create random PIL images
									_lowercase									=				self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCamelCase     )
									for image in image_inputs:
															self.assertIsInstance(__UpperCamelCase							, Image.Image     )

									# Test not batched input
									_lowercase									=				image_processor(image_inputs[0]							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        1,
									        self.expected_encoded_image_num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )

									# Test batched
									_lowercase									=				image_processor(__UpperCamelCase							, return_tensors="""pt"""     ).pixel_values
									self.assertEqual(
									    encoded_images.shape							, (
									        self.image_processor_tester.batch_size,
									        self.expected_encoded_image_num_channels,
									        self.image_processor_tester.size["""height"""],
									        self.image_processor_tester.size["""width"""],
									    )							, )

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


A		:   int         =    {'''configuration_vilt''': ['''VILT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViltConfig''']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   int         =    ['''ViltFeatureExtractor''']
		A		:   List[Any]         =    ['''ViltImageProcessor''']
		A		:   Dict         =    ['''ViltProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   Tuple         =    [
		    '''VILT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ViltForImageAndTextRetrieval''',
		    '''ViltForImagesAndTextClassification''',
		    '''ViltForTokenClassification''',
		    '''ViltForMaskedLM''',
		    '''ViltForQuestionAnswering''',
		    '''ViltLayer''',
		    '''ViltModel''',
		    '''ViltPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_vilt import ViltFeatureExtractor
				from .image_processing_vilt import ViltImageProcessor
				from .processing_vilt import ViltProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vilt import (
				    VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViltForImageAndTextRetrieval,
				    ViltForImagesAndTextClassification,
				    ViltForMaskedLM,
				    ViltForQuestionAnswering,
				    ViltForTokenClassification,
				    ViltLayer,
				    ViltModel,
				    ViltPreTrainedModel,
				)


else:
		import sys

		A		:   Dict         =    _LazyModule(__name__, globals()['''__file__'''], _import_structure)