






import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
							import torch

							from transformers import AutoModelForImageClassification

if is_vision_available():
							from transformers import AutoImageProcessor
@require_torch
@require_vision
class  _UpperCAmelCase   (			unittest.TestCase      ):

				"""simple docstring"""




				@slow
				def 							lowercase__  (	self		:       str    ):

								'''simple docstring'''
								lowercase__  					=     AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''    )
								lowercase__  					=     AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''    )
								model.to(lowerCamelCase    )

								from datasets import load_dataset

								lowercase__  					=     load_dataset('''nielsr/rvlcdip-demo'''    )

								lowercase__  					=     dataset['''train'''][0]['''image'''].convert('''RGB'''    )

								lowercase__  					=     image_processor(lowerCamelCase,    return_tensors='''pt'''    ).to(lowerCamelCase    )

								# forward pass
								with torch.no_grad():
												lowercase__  					=     model(**lowerCamelCase    )
												lowercase__  					=     outputs.logits

								lowercase__  					=     torch.Size((1, 16)    )
								self.assertEqual(logits.shape,    lowerCamelCase    )

								lowercase__  					=     torch.tensor(
								    [-0.4158, -0.4092, -0.4347],    device=lowerCamelCase,    dtype=torch.float,    )
								self.assertTrue(torch.allclose(logits[0, :3],    lowerCamelCase,    atol=1E-4    )    )












import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING


A__      :     Tuple       =					logging.get_logger(__name__)
class  _UpperCAmelCase   (			A__      ):

				"""simple docstring"""




				lowercase__											=			"""upernet"""




				def __init__(	self		:       Dict,    lowerCamelCase		:       Union[str, Any]=None,    lowerCamelCase		:       str=512,    lowerCamelCase		:       Optional[Any]=0.02,    lowerCamelCase		:       Optional[Any]=[1, 2, 3, 6],    lowerCamelCase		:       Optional[int]=True,    lowerCamelCase		:       Tuple=0.4,    lowerCamelCase		:       Optional[int]=384,    lowerCamelCase		:       Optional[int]=256,    lowerCamelCase		:       Dict=1,    lowerCamelCase		:       str=False,    lowerCamelCase		:       List[str]=255,    **lowerCamelCase		:       List[Any],    ):

								'''simple docstring'''
								super().__init__(**lowerCamelCase    )

								if backbone_config is None:
												logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''    )
												lowercase__  					=     CONFIG_MAPPING['''resnet'''](out_features=['''stage1''', '''stage2''', '''stage3''', '''stage4''']    )
								elif isinstance(lowerCamelCase,    lowerCamelCase    ):
												lowercase__  					=     backbone_config.get('''model_type'''    )
												lowercase__  					=     CONFIG_MAPPING[backbone_model_type]
												lowercase__  					=     config_class.from_dict(lowerCamelCase    )

								lowercase__  					=     backbone_config
								lowercase__  					=     hidden_size
								lowercase__  					=     initializer_range
								lowercase__  					=     pool_scales
								lowercase__  					=     use_auxiliary_head
								lowercase__  					=     auxiliary_loss_weight
								lowercase__  					=     auxiliary_in_channels
								lowercase__  					=     auxiliary_channels
								lowercase__  					=     auxiliary_num_convs
								lowercase__  					=     auxiliary_concat_input
								lowercase__  					=     loss_ignore_index




				def 							lowercase__  (	self		:       str    ):

								'''simple docstring'''
								lowercase__  					=     copy.deepcopy(self.__dict__    )
								lowercase__  					=     self.backbone_config.to_dict()
								lowercase__  					=     self.__class__.model_type
								return output




