




import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import UperNetForSemanticSegmentation
     from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor




class    __lowerCAmelCase       :






       def __init__(   self:					int       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					List[str]=13       ,					_lowerCAmelCase:					Dict=32       ,					_lowerCAmelCase:					Dict=3       ,					_lowerCAmelCase:					Optional[Any]=4       ,					_lowerCAmelCase:					Dict=[10, 20, 30, 40]       ,					_lowerCAmelCase:					int=[2, 2, 3, 2]       ,					_lowerCAmelCase:					Union[str, Any]=True       ,					_lowerCAmelCase:					Union[str, Any]=True       ,					_lowerCAmelCase:					str=37       ,					_lowerCAmelCase:					Optional[int]="gelu"       ,					_lowerCAmelCase:					str=10       ,					_lowerCAmelCase:					Optional[int]=0.02       ,					_lowerCAmelCase:					Union[str, Any]=["stage2", "stage3", "stage4"]       ,					_lowerCAmelCase:					str=3       ,					_lowerCAmelCase:					Union[str, Any]=None       ,					):
         lowercase    :int      	=						parent
         lowercase    :List[Any]      	=						batch_size
         lowercase    :int      	=						image_size
         lowercase    :Dict      	=						num_channels
         lowercase    :Tuple      	=						num_stages
         lowercase    :List[Any]      	=						hidden_sizes
         lowercase    :Union[str, Any]      	=						depths
         lowercase    :Union[str, Any]      	=						is_training
         lowercase    :Union[str, Any]      	=						use_labels
         lowercase    :Any      	=						intermediate_size
         lowercase    :Optional[Any]      	=						hidden_act
         lowercase    :str      	=						type_sequence_label_size
         lowercase    :List[Any]      	=						initializer_range
         lowercase    :str      	=						out_features
         lowercase    :List[Any]      	=						num_labels
         lowercase    :Union[str, Any]      	=						scope
         lowercase    :Optional[int]      	=						num_stages






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :Optional[Any]      	=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

         lowercase    :List[str]      	=						None
         if self.use_labels:
           lowercase    :Any      	=						ids_tensor([self.batch_size]       ,					self.type_sequence_label_size				)

         lowercase    :List[Any]      	=						self.get_config()

         return config, pixel_values, labels






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         return ConvNextConfig(
             num_channels=self.num_channels       ,					num_stages=self.num_stages       ,					hidden_sizes=self.hidden_sizes       ,					depths=self.depths       ,					is_training=self.is_training       ,					intermediate_size=self.intermediate_size       ,					hidden_act=self.hidden_act       ,					out_features=self.out_features       ,					)






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         return UperNetConfig(
             backbone_config=self.get_backbone_config()       ,					hidden_size=5_12       ,					pool_scales=[1, 2, 3, 6]       ,					use_auxiliary_head=_lowerCAmelCase       ,					auxiliary_loss_weight=0.4       ,					auxiliary_in_channels=40       ,					auxiliary_channels=2_56       ,					auxiliary_num_convs=1       ,					auxiliary_concat_input=_lowerCAmelCase       ,					loss_ignore_index=2_55       ,					num_labels=self.num_labels       ,					)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]       ,					_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					List[str]				):
         lowercase    :List[Any]      	=						UperNetForSemanticSegmentation(config=_lowerCAmelCase				)
         model.to(_lowerCAmelCase				)
         model.eval()
         lowercase    :List[str]      	=						model(_lowerCAmelCase				)
         self.parent.assertEqual(
             result.logits.shape       ,					(self.batch_size, self.num_labels, self.image_size, self.image_size)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str				):
         lowercase    :str      	=						self.prepare_config_and_inputs()
         (
             (
             lowercase
         )	,    (
             lowercase
         )	,    (
             lowercase
         )	,    
         )    :Tuple      	=						config_and_inputs
         lowercase    :Union[str, Any]      	=						{"pixel_values": pixel_values}
         return config, inputs_dict







@require_torch
class    __lowerCAmelCase       (							lowerCAmelCase      , lowerCAmelCase      , unittest.TestCase):
       _a 					=						(UperNetForSemanticSegmentation,) if is_torch_available() else ()
       _a 					=						{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
       _a 					=						False
       _a 					=						False
       _a 					=						False
       _a 					=						False
       _a 					=						False
       _a 					=						False






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         lowercase    :List[str]      	=						UperNetModelTester(self				)
         lowercase    :Union[str, Any]      	=						ConfigTester(self       ,					config_class=_lowerCAmelCase       ,					has_text_modality=_lowerCAmelCase       ,					hidden_size=37				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         return






       def 					SCREAMING_SNAKE_CASE    (   self:					str				):
         lowercase	,    lowercase    :str      	=						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowercase    :Optional[Any]      	=						model_class(_lowerCAmelCase				)
           lowercase    :str      	=						inspect.signature(model.forward				)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase    :int      	=						[*signature.parameters.keys()]

           lowercase    :int      	=						["pixel_values"]
           self.assertListEqual(arg_names[:1]       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :int      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCAmelCase				)






       @unittest.skip(reason="UperNet does not use inputs_embeds"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         pass






       @unittest.skip(reason="UperNet does not support input and output embeddings"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         pass






       @unittest.skip(reason="UperNet does not have a base model"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         pass






       @unittest.skip(reason="UperNet does not have a base model"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         pass






       @require_torch_multi_gpu
       @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         pass






       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         pass






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         def check_hidden_states_output(_lowerCAmelCase:					Tuple       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Dict				):
           lowercase    :List[Any]      	=						model_class(_lowerCAmelCase				)
           model.to(_lowerCAmelCase				)
           model.eval()

           with torch.no_grad():
             lowercase    :str      	=						model(**self._prepare_for_class(_lowerCAmelCase       ,					_lowerCAmelCase				)				)

           lowercase    :Tuple      	=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           lowercase    :Tuple      	=						self.model_tester.num_stages
           self.assertEqual(len(_lowerCAmelCase				)       ,					expected_num_stages + 1				)

           # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
           self.assertListEqual(
               list(hidden_states[0].shape[-2:]				)       ,					[self.model_tester.image_size // 4, self.model_tester.image_size // 4]       ,					)

         lowercase	,    lowercase    :Tuple      	=						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           lowercase    :Any      	=						True
           check_hidden_states_output(_lowerCAmelCase       ,					_lowerCAmelCase       ,					_lowerCAmelCase				)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           lowercase    :List[str]      	=						True

           check_hidden_states_output(_lowerCAmelCase       ,					_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Any				):
         lowercase	,    lowercase    :Any      	=						self.model_tester.prepare_config_and_inputs_for_common()

         lowercase    :Any      	=						_config_zero_init(_lowerCAmelCase				)
         lowercase    :Optional[int]      	=						_config_zero_init(configs_no_init.backbone_config				)
         for model_class in self.all_model_classes:
           lowercase    :Optional[Any]      	=						model_class(config=_lowerCAmelCase				)
           for name, param in model.named_parameters():
             if param.requires_grad:
               self.assertIn(
                   ((param.data.mean() * 1e9).round() / 1e9).item()       ,					[0.0, 1.0]       ,					msg=F"Parameter {name} of model {model_class} seems not properly initialized"       ,					)






       @unittest.skip(reason="UperNet does not have tied weights"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         pass






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase    :int      	=						UperNetForSemanticSegmentation.from_pretrained(_lowerCAmelCase				)
           self.assertIsNotNone(_lowerCAmelCase				)







def       UpperCAmelCase__  (     ):
  lowercase    :Union[str, Any]      	=						hf_hub_download(
      repo_id="hf-internal-testing/fixtures_ade20k",			repo_type="dataset",			filename="ADE_val_00000001.jpg"   )
  lowercase    :Tuple      	=						Image.open(lowerCamelCase   ).convert("RGB"   )
  return image







@require_torch
@require_vision
@slow
class    __lowerCAmelCase       (							unittest.TestCase):






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         lowercase    :Tuple      	=						AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"				)
         lowercase    :int      	=						UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"				).to(_lowerCAmelCase				)

         lowercase    :List[Any]      	=						prepare_img()
         lowercase    :Any      	=						processor(images=_lowerCAmelCase       ,					return_tensors="pt"				).to(_lowerCAmelCase				)

         with torch.no_grad():
           lowercase    :str      	=						model(**_lowerCAmelCase				)

         lowercase    :Any      	=						torch.Size((1, model.config.num_labels, 5_12, 5_12)				)
         self.assertEqual(outputs.logits.shape       ,					_lowerCAmelCase				)

         lowercase    :str      	=						torch.tensor(
             [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]				).to(_lowerCAmelCase				)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,					_lowerCAmelCase       ,					atol=1e-4				)				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :List[str]      	=						AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"				)
         lowercase    :Union[str, Any]      	=						UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"				).to(_lowerCAmelCase				)

         lowercase    :int      	=						prepare_img()
         lowercase    :List[Any]      	=						processor(images=_lowerCAmelCase       ,					return_tensors="pt"				).to(_lowerCAmelCase				)

         with torch.no_grad():
           lowercase    :Dict      	=						model(**_lowerCAmelCase				)

         lowercase    :int      	=						torch.Size((1, model.config.num_labels, 5_12, 5_12)				)
         self.assertEqual(outputs.logits.shape       ,					_lowerCAmelCase				)

         lowercase    :Tuple      	=						torch.tensor(
             [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]				).to(_lowerCAmelCase				)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,					_lowerCAmelCase       ,					atol=1e-4				)				)









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_UpperCAmelCase	:						List[Any]			=     {
    "configuration_swinv2": ["SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Swinv2Config"],
}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCAmelCase	:						Union[str, Any]			=     [
         "SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST",
         "Swinv2ForImageClassification",
         "Swinv2ForMaskedImageModeling",
         "Swinv2Model",
         "Swinv2PreTrainedModel",
     ]


if TYPE_CHECKING:
     from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_swinva import (
              SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST,
              SwinvaForImageClassification,
              SwinvaForMaskedImageModeling,
              SwinvaModel,
              SwinvaPreTrainedModel,
          )


else:
     import sys

     _UpperCAmelCase	:						Tuple			=     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



