


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






  def __init__(					self      :					Any		,						_lowercase      :					Any		,						_lowercase      :					List[str]=13		,						_lowercase      :					List[str]=32		,						_lowercase      :					Tuple=3		,						_lowercase      :					Dict=4		,						_lowercase      :					Optional[int]=[10, 20, 30, 40]		,						_lowercase      :					List[str]=[2, 2, 3, 2]		,						_lowercase      :					Dict=True		,						_lowercase      :					Optional[Any]=True		,						_lowercase      :					List[Any]=37		,						_lowercase      :					str="gelu"		,						_lowercase      :					int=10		,						_lowercase      :					Any=0.0_2		,						_lowercase      :					Union[str, Any]=["stage2", "stage3", "stage4"]		,						_lowercase      :					Any=3		,						_lowercase      :					int=None		,						):




         """simple docstring"""







         UpperCAmelCase__										=     parent
         UpperCAmelCase__										=     batch_size
         UpperCAmelCase__										=     image_size
         UpperCAmelCase__										=     num_channels
         UpperCAmelCase__										=     num_stages
         UpperCAmelCase__										=     hidden_sizes
         UpperCAmelCase__										=     depths
         UpperCAmelCase__										=     is_training
         UpperCAmelCase__										=     use_labels
         UpperCAmelCase__										=     intermediate_size
         UpperCAmelCase__										=     hidden_act
         UpperCAmelCase__										=     type_sequence_label_size
         UpperCAmelCase__										=     initializer_range
         UpperCAmelCase__										=     out_features
         UpperCAmelCase__										=     num_labels
         UpperCAmelCase__										=     scope
         UpperCAmelCase__										=     num_stages






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

         UpperCAmelCase__										=     None
         if self.use_labels:
                UpperCAmelCase__										=     ids_tensor([self.batch_size]		,						self.type_sequence_label_size )

         UpperCAmelCase__										=     self.get_config()

         return config, pixel_values, labels






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """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 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







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






  def 			_UpperCAmelCase  (					self      :					Dict		,						_lowercase      :					List[str]		,						_lowercase      :					Tuple		,						_lowercase      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     UperNetForSemanticSegmentation(config=_lowercase )
         model.to(_lowercase )
         model.eval()
         UpperCAmelCase__										=     model(_lowercase )
         self.parent.assertEqual(
             result.logits.shape		,						(self.batch_size, self.num_labels, self.image_size, self.image_size) )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase__			
         )						,			(
             UpperCAmelCase__			
         )						,			(
             UpperCAmelCase__			
         )						,						
         )										=     config_and_inputs
         UpperCAmelCase__										=     {"pixel_values": pixel_values}
         return config, inputs_dict




@require_torch
class     lowercase__  (					__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE					,							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 			_UpperCAmelCase  (					self      :					List[str] ):




         """simple docstring"""







         UpperCAmelCase__										=     UperNetModelTester(self )
         UpperCAmelCase__										=     ConfigTester(self		,						config_class=_lowercase		,						has_text_modality=_lowercase		,						hidden_size=37 )






  def 			_UpperCAmelCase  (					self      :					List[str] ):




         """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 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         return






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     model_class(_lowercase )
                UpperCAmelCase__										=     inspect.signature(model.forward )
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                UpperCAmelCase__										=     [*signature.parameters.keys()]

                UpperCAmelCase__										=     ["pixel_values"]
                self.assertListEqual(arg_names[:1]		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*_lowercase )






  @unittest.skip(reason="UperNet does not use inputs_embeds" )
  def 			_UpperCAmelCase  (					self      :					Optional[Any] ):




         """simple docstring"""







         pass






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




         """simple docstring"""







         pass






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




         """simple docstring"""







         pass






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




         """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 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         pass






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




         """simple docstring"""







         pass






  def 			_UpperCAmelCase  (					self      :					Any ):




         """simple docstring"""







         def check_hidden_states_output(_lowercase      :					Optional[int]		,						_lowercase      :					Optional[Any]		,						_lowercase      :					int ):
                UpperCAmelCase__										=     model_class(_lowercase )
                model.to(_lowercase )
                model.eval()

                with torch.no_grad():
                       UpperCAmelCase__										=     model(**self._prepare_for_class(_lowercase		,						_lowercase ) )

                UpperCAmelCase__										=     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                UpperCAmelCase__										=     self.model_tester.num_stages
                self.assertEqual(len(_lowercase )		,						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]		,						)

         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     True
                check_hidden_states_output(_lowercase		,						_lowercase		,						_lowercase )

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                UpperCAmelCase__										=     True

                check_hidden_states_output(_lowercase		,						_lowercase		,						_lowercase )






  def 			_UpperCAmelCase  (					self      :					Tuple ):




         """simple docstring"""







         UpperCAmelCase__						,			UpperCAmelCase__										=     self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase__										=     _config_zero_init(_lowercase )
         UpperCAmelCase__										=     _config_zero_init(configs_no_init.backbone_config )
         for model_class in self.all_model_classes:
                UpperCAmelCase__										=     model_class(config=_lowercase )
                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 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         pass






  @slow
  def 			_UpperCAmelCase  (					self      :					int ):




         """simple docstring"""







         for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained(_lowercase )
                self.assertIsNotNone(_lowercase )





def __UpperCAmelCase       (		)						->					Tuple:



       '''simple docstring'''





       UpperCAmelCase__										=     hf_hub_download(
           repo_id="hf-internal-testing/fixtures_ade20k" ,						repo_type="dataset" ,						filename="ADE_val_00000001.jpg"			)
       UpperCAmelCase__										=     Image.open(__A			).convert("RGB"			)
       return image






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






  def 			_UpperCAmelCase  (					self      :					Optional[int] ):




         """simple docstring"""







         UpperCAmelCase__										=     AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny" )
         UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny" ).to(_lowercase )

         UpperCAmelCase__										=     prepare_img()
         UpperCAmelCase__										=     processor(images=_lowercase		,						return_tensors="pt" ).to(_lowercase )

         with torch.no_grad():
                UpperCAmelCase__										=     model(**_lowercase )

         UpperCAmelCase__										=     torch.Size((1, model.config.num_labels, 5_12, 5_12) )
         self.assertEqual(outputs.logits.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]] ).to(_lowercase )
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,						_lowercase		,						atol=1E-4 ) )






  def 			_UpperCAmelCase  (					self      :					List[Any] ):




         """simple docstring"""







         UpperCAmelCase__										=     AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny" )
         UpperCAmelCase__										=     UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny" ).to(_lowercase )

         UpperCAmelCase__										=     prepare_img()
         UpperCAmelCase__										=     processor(images=_lowercase		,						return_tensors="pt" ).to(_lowercase )

         with torch.no_grad():
                UpperCAmelCase__										=     model(**_lowercase )

         UpperCAmelCase__										=     torch.Size((1, model.config.num_labels, 5_12, 5_12) )
         self.assertEqual(outputs.logits.shape		,						_lowercase )

         UpperCAmelCase__										=     torch.tensor(
             [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]] ).to(_lowercase )
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,						_lowercase		,						atol=1E-4 ) )









import warnings

from ...utils import logging
from .image_processing_dpt import DPTImageProcessor


A      = logging.get_logger(__name__)


class     lowercase__  (					__SCREAMING_SNAKE_CASE		):






  def __init__(					self      :					Union[str, Any]		,						*_lowercase      :					Any		,						**_lowercase      :					Optional[int] ):




         """simple docstring"""







         warnings.warn(
             "The class DPTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
             " use DPTImageProcessor instead."		,						_lowercase		,						)
         super().__init__(*_lowercase		,						**_lowercase )





