






"""simple docstring"""







import unittest

import numpy as np
import requests

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_image_inputs


if is_torch_available():
	import torch

	from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
	__a       :	int             = False

if is_vision_available():
	from PIL import Image

	from transformers import PixaStructImageProcessor







class        _SCREAMING_SNAKE_CASE						(  unittest.TestCase							):





							"""simple docstring"""





							def __init__(   self:     Dict				,		__A:     Dict				,		__A:     Tuple=7				,		__A:     Optional[int]=3				,		__A:     List[Any]=18				,		__A:     Any=30				,		__A:     List[Any]=400				,		__A:     Dict=None				,		__A:     str=True				,		__A:     List[Any]=True				,		__A:     List[str]=None				,		):

												'''simple docstring'''

												a__          =  size if size is not None else {'''height''': 20, '''width''': 20}
												a__          =  parent
												a__          =  batch_size
												a__          =  num_channels
												a__          =  image_size
												a__          =  min_resolution
												a__          =  max_resolution
												a__          =  size
												a__          =  do_normalize
												a__          =  do_convert_rgb
												a__          =  [512, 1024, 2048, 4096]
												a__          =  patch_size if patch_size is not None else {'''height''': 16, '''width''': 16}




							def  lowercase		(   self:     Union[str, Any]    ):

												'''simple docstring'''

												return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





							def  lowercase		(   self:     Dict    ):

												'''simple docstring'''

												a__          =  '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'''
												a__          =  Image.open(requests.get(__A				,		stream=__A    ).raw    ).convert('''RGB'''    )
												return raw_image







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11       , reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'       , )
@require_torch
@require_vision
class        _SCREAMING_SNAKE_CASE						(  __snake_case       , unittest.TestCase							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    =PixaStructImageProcessor if is_vision_available() else None




							def  lowercase		(   self:     Dict    ):

												'''simple docstring'''

												a__          =  PixaStructImageProcessingTester(self    )




							@property
							def  lowercase		(   self:     Union[str, Any]    ):

												'''simple docstring'''

												return self.image_processor_tester.prepare_image_processor_dict()




							def  lowercase		(   self:     int    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												self.assertTrue(hasattr(__A				,		'''do_normalize'''    )    )
												self.assertTrue(hasattr(__A				,		'''do_convert_rgb'''    )    )




							def  lowercase		(   self:     int    ):

												'''simple docstring'''

												a__          =  self.image_processor_tester.prepare_dummy_image()

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												a__          =  2048

												a__          =  image_processor(__A				,		return_tensors='''pt'''				,		max_patches=__A    )
												self.assertTrue(torch.allclose(inputs.flattened_patches.mean()				,		torch.tensor(0.0_6_0_6    )				,		atol=1e-3				,		rtol=1e-3    )    )




							def  lowercase		(   self:     List[Any]    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												# create random PIL images
												a__          =  prepare_image_inputs(self.image_processor_tester				,		equal_resolution=__A    )
												for image in image_inputs:
																	self.assertIsInstance(__A				,		Image.Image    )

												# Test not batched input
												a__          =  (
												    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
												    * self.image_processor_tester.num_channels
												) + 2

												for max_patch in self.image_processor_tester.max_patches:
																	# Test not batched input
																	a__          =  image_processor(
																	    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

																	# Test batched
																	a__          =  image_processor(
																	    __A				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)




							def  lowercase		(   self:     Union[str, Any]    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												# create random PIL images
												a__          =  prepare_image_inputs(self.image_processor_tester				,		equal_resolution=__A    )
												for image in image_inputs:
																	self.assertIsInstance(__A				,		Image.Image    )

												# Test not batched input
												a__          =  (
												    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
												    * self.image_processor_tester.num_channels
												) + 2

												a__          =  True

												for max_patch in self.image_processor_tester.max_patches:
																	# Test not batched input
																	with self.assertRaises(__A    ):
																						a__          =  image_processor(
																						    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches

																	a__          =  '''Hello'''

																	a__          =  image_processor(
																	    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A				,		header_text=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

																	# Test batched
																	a__          =  image_processor(
																	    __A				,		return_tensors='''pt'''				,		max_patches=__A				,		header_text=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)




							def  lowercase		(   self:     Optional[int]    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												# create random numpy tensors
												a__          =  prepare_image_inputs(self.image_processor_tester				,		equal_resolution=__A				,		numpify=__A    )
												for image in image_inputs:
																	self.assertIsInstance(__A				,		np.ndarray    )

												a__          =  (
												    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
												    * self.image_processor_tester.num_channels
												) + 2

												for max_patch in self.image_processor_tester.max_patches:
																	# Test not batched input
																	a__          =  image_processor(
																	    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

																	# Test batched
																	a__          =  image_processor(
																	    __A				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)





							def  lowercase		(   self:     str    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												# create random PyTorch tensors
												a__          =  prepare_image_inputs(self.image_processor_tester				,		equal_resolution=__A				,		torchify=__A    )
												for image in image_inputs:
																	self.assertIsInstance(__A				,		torch.Tensor    )

												# Test not batched input
												a__          =  (
												    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
												    * self.image_processor_tester.num_channels
												) + 2

												for max_patch in self.image_processor_tester.max_patches:
																	# Test not batched input
																	a__          =  image_processor(
																	    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

																	# Test batched
																	a__          =  image_processor(
																	    __A				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11       , reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'       , )
@require_torch
@require_vision
class        _SCREAMING_SNAKE_CASE						(  __snake_case       , unittest.TestCase							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    =PixaStructImageProcessor if is_vision_available() else None




							def  lowercase		(   self:     List[Any]    ):

												'''simple docstring'''

												a__          =  PixaStructImageProcessingTester(self				,		num_channels=4    )
												a__          =  3




							@property
							def  lowercase		(   self:     Optional[Any]    ):

												'''simple docstring'''

												return self.image_processor_tester.prepare_image_processor_dict()




							def  lowercase		(   self:     Any    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												self.assertTrue(hasattr(__A				,		'''do_normalize'''    )    )
												self.assertTrue(hasattr(__A				,		'''do_convert_rgb'''    )    )





							def  lowercase		(   self:     Union[str, Any]    ):

												'''simple docstring'''

												a__          =  self.image_processing_class(**self.image_processor_dict    )
												# create random PIL images
												a__          =  prepare_image_inputs(self.image_processor_tester				,		equal_resolution=__A    )
												for image in image_inputs:
																	self.assertIsInstance(__A				,		Image.Image    )

												# Test not batched input
												a__          =  (
												    (self.image_processor_tester.patch_size['''height'''] * self.image_processor_tester.patch_size['''width'''])
												    * (self.image_processor_tester.num_channels - 1)
												) + 2

												for max_patch in self.image_processor_tester.max_patches:
																	# Test not batched input
																	a__          =  image_processor(
																	    image_inputs[0]				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

																	# Test batched
																	a__          =  image_processor(
																	    __A				,		return_tensors='''pt'''				,		max_patches=__A    ).flattened_patches
																	self.assertEqual(
																	    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)










"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a       :	Any             = logging.get_logger(__name__)

__a       :	Union[str, Any]             = {
    'caidas/swin2sr-classicalsr-x2-64': (
        'https://huggingface.co/caidas/swin2sr-classicalsr-x2-64/resolve/main/config.json'
    ),
}







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='swin2sr'

							_SCREAMING_SNAKE_CASE    ={
							    'hidden_size': 'embed_dim',
							    'num_attention_heads': 'num_heads',
							    'num_hidden_layers': 'num_layers',
							}




							def __init__(   self:     Union[str, Any]				,		__A:     List[Any]=64				,		__A:     int=1				,		__A:     Dict=3				,		__A:     List[Any]=180				,		__A:     int=[6, 6, 6, 6, 6, 6]				,		__A:     Tuple=[6, 6, 6, 6, 6, 6]				,		__A:     int=8				,		__A:     Optional[int]=2.0				,		__A:     Optional[int]=True				,		__A:     int=0.0				,		__A:     Any=0.0				,		__A:     Optional[Any]=0.1				,		__A:     Optional[Any]="gelu"				,		__A:     Dict=False				,		__A:     List[Any]=0.0_2				,		__A:     List[Any]=1e-5				,		__A:     List[str]=2				,		__A:     int=1.0				,		__A:     Dict="1conv"				,		__A:     Optional[Any]="pixelshuffle"				,		**__A:     Dict				,		):

												'''simple docstring'''

												super().__init__(**__A    )

												a__          =  image_size
												a__          =  patch_size
												a__          =  num_channels
												a__          =  embed_dim
												a__          =  depths
												a__          =  len(__A    )
												a__          =  num_heads
												a__          =  window_size
												a__          =  mlp_ratio
												a__          =  qkv_bias
												a__          =  hidden_dropout_prob
												a__          =  attention_probs_dropout_prob
												a__          =  drop_path_rate
												a__          =  hidden_act
												a__          =  use_absolute_embeddings
												a__          =  layer_norm_eps
												a__          =  initializer_range
												a__          =  upscale
												a__          =  img_range
												a__          =  resi_connection
												a__          =  upsampler


