


'''simple docstring'''


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 							__magic_name__	:

     """simple docstring"""






     def __init__(   self			,     lowerCamelCase			,     lowerCamelCase=13			,     lowerCamelCase=32			,     lowerCamelCase=3			,     lowerCamelCase=4			,     lowerCamelCase=[10, 20, 30, 40]			,     lowerCamelCase=[2, 2, 3, 2]			,     lowerCamelCase=True			,     lowerCamelCase=True			,     lowerCamelCase=37			,     lowerCamelCase="gelu"			,     lowerCamelCase=10			,     lowerCamelCase=0.02			,     lowerCamelCase=["stage2", "stage3", "stage4"]			,     lowerCamelCase=3			,     lowerCamelCase=None			,     ):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							parent
          __A	:       int      =							batch_size
          __A	:       Tuple      =							image_size
          __A	:       Optional[int]      =							num_channels
          __A	:       Tuple      =							num_stages
          __A	:       List[str]      =							hidden_sizes
          __A	:       int      =							depths
          __A	:       Dict      =							is_training
          __A	:       Union[str, Any]      =							use_labels
          __A	:       Optional[int]      =							intermediate_size
          __A	:       Optional[int]      =							hidden_act
          __A	:       Optional[Any]      =							type_sequence_label_size
          __A	:       Any      =							initializer_range
          __A	:       Union[str, Any]      =							out_features
          __A	:       Union[str, Any]      =							num_labels
          __A	:       Optional[int]      =							scope
          __A	:       Any      =							num_stages


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Dict      =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

          __A	:       Optional[int]      =							self.get_config()

          return config, pixel_values, labels


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          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 lowerCAmelCase__  (   self		):


          '''simple docstring'''





          return UperNetConfig(
              backbone_config=self.get_backbone_config()			,     hidden_size=512			,     pool_scales=[1, 2, 3, 6]			,     use_auxiliary_head=lowerCamelCase			,     auxiliary_loss_weight=0.4			,     auxiliary_in_channels=40			,     auxiliary_channels=256			,     auxiliary_num_convs=1			,     auxiliary_concat_input=lowerCamelCase			,     loss_ignore_index=255			,     num_labels=self.num_labels			,     )


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Optional[Any]      =							UperNetForSemanticSegmentation(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Optional[int]      =							model(lowerCamelCase		)
          self.parent.assertEqual(
              result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size, self.image_size)		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							self.prepare_config_and_inputs()
          (
              (
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,
          )	:       Any      =							config_and_inputs
          __A	:       Any      =							{"pixel_values": pixel_values}
          return config, inputs_dict







@require_torch
class 							__magic_name__	(       lowerCAmelCase    ,		lowerCAmelCase    ,		unittest.TestCase ):

     """simple docstring"""






     lowerCamelCase__       						=				(UperNetForSemanticSegmentation,) if is_torch_available() else ()
     lowerCamelCase__       						=				{'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False
     lowerCamelCase__       						=				False


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							UperNetModelTester(self		)
          __A	:       Any      =							ConfigTester(self			,     config_class=lowerCamelCase			,     has_text_modality=lowerCamelCase			,     hidden_size=37		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          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 lowerCAmelCase__  (   self		):


          '''simple docstring'''





          return


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A  ,__A	:       List[Any]      =							self.model_tester.prepare_config_and_inputs_for_common()

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

               __A	:       Dict      =							["pixel_values"]
               self.assertListEqual(arg_names[:1]			,     lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[Any]      =							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 lowerCAmelCase__  (   self		):


          '''simple docstring'''





          pass


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


          '''simple docstring'''





          pass


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


          '''simple docstring'''





          pass


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


          '''simple docstring'''





          pass


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


          '''simple docstring'''





          pass


     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          pass


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          def check_hidden_states_output(lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):
               __A	:       str      =							model_class(lowerCamelCase		)
               model.to(lowerCamelCase		)
               model.eval()

               with torch.no_grad():
                    __A	:       Tuple      =							model(**self._prepare_for_class(lowerCamelCase			,     lowerCamelCase		)		)

               __A	:       Optional[Any]      =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

               __A	:       Dict      =							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]			,     )

          __A  ,__A	:       List[str]      =							self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               __A	:       Union[str, Any]      =							True
               check_hidden_states_output(lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __A	:       Union[str, Any]      =							True

               check_hidden_states_output(lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A  ,__A	:       int      =							self.model_tester.prepare_config_and_inputs_for_common()

          __A	:       List[str]      =							_config_zero_init(lowerCamelCase		)
          __A	:       int      =							_config_zero_init(configs_no_init.backbone_config		)
          for model_class in self.all_model_classes:
               __A	:       List[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 lowerCAmelCase__  (   self		):


          '''simple docstring'''





          pass


     @slow
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __A	:       List[Any]      =							UperNetForSemanticSegmentation.from_pretrained(lowerCamelCase		)
               self.assertIsNotNone(lowerCamelCase		)


def 		_lowercase ():




     '''simple docstring'''





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







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

     """simple docstring"""






     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"		)
          __A	:       Dict      =							UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"		).to(lowerCamelCase		)

          __A	:       Dict      =							prepare_img()
          __A	:       Any      =							processor(images=lowerCamelCase			,     return_tensors="pt"		).to(lowerCamelCase		)

          with torch.no_grad():
               __A	:       int      =							model(**lowerCamelCase		)

          __A	:       int      =							torch.Size((1, model.config.num_labels, 512, 512)		)
          self.assertEqual(outputs.logits.shape			,     lowerCamelCase		)

          __A	:       List[str]      =							torch.tensor(
              [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]		).to(lowerCamelCase		)
          self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,     lowerCamelCase			,     atol=1E-4		)		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"		)
          __A	:       str      =							UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"		).to(lowerCamelCase		)

          __A	:       Any      =							prepare_img()
          __A	:       Any      =							processor(images=lowerCamelCase			,     return_tensors="pt"		).to(lowerCamelCase		)

          with torch.no_grad():
               __A	:       int      =							model(**lowerCamelCase		)

          __A	:       Any      =							torch.Size((1, model.config.num_labels, 512, 512)		)
          self.assertEqual(outputs.logits.shape			,     lowerCamelCase		)

          __A	:       Union[str, Any]      =							torch.tensor(
              [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]		).to(lowerCamelCase		)
          self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]			,     lowerCamelCase			,     atol=1E-4		)		)





'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


_UpperCamelCase 							=  {"""configuration_vit""": ["""VIT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """ViTConfig""", """ViTOnnxConfig"""]}

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  ["""ViTFeatureExtractor"""]
      _UpperCamelCase 							=  ["""ViTImageProcessor"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """VIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """ViTForImageClassification""",
          """ViTForMaskedImageModeling""",
          """ViTModel""",
          """ViTPreTrainedModel""",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """TFViTForImageClassification""",
          """TFViTModel""",
          """TFViTPreTrainedModel""",
      ]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _UpperCamelCase 							=  [
          """FlaxViTForImageClassification""",
          """FlaxViTModel""",
          """FlaxViTPreTrainedModel""",
      ]

if TYPE_CHECKING:
      from .configuration_vit import VIT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTConfig, ViTOnnxConfig

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_vit import ViTFeatureExtractor
            from .image_processing_vit import ViTImageProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_vit import (
                VIT_PRETRAINED_MODEL_ARCHIVE_LIST,
                ViTForImageClassification,
                ViTForMaskedImageModeling,
                ViTModel,
                ViTPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_vit import TFViTForImageClassification, TFViTModel, TFViTPreTrainedModel

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel, FlaxViTPreTrainedModel


else:
      import sys

      _UpperCamelCase 							=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

