


import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


logging.set_verbosity_info()
__lowerCamelCase        =     logging.get_logger(__name__)



def  _a			(						__UpperCamelCase  ,  __UpperCamelCase=False ):
     a_    :     Optional[Any]										=    []
     for i in range(config.num_hidden_layers ):
          # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
          rename_keys.append((F'''blocks.{i}.norm1.weight''', F'''vit.encoder.layer.{i}.layernorm_before.weight''') )
          rename_keys.append((F'''blocks.{i}.norm1.bias''', F'''vit.encoder.layer.{i}.layernorm_before.bias''') )
          rename_keys.append((F'''blocks.{i}.attn.proj.weight''', F'''vit.encoder.layer.{i}.attention.output.dense.weight''') )
          rename_keys.append((F'''blocks.{i}.attn.proj.bias''', F'''vit.encoder.layer.{i}.attention.output.dense.bias''') )
          rename_keys.append((F'''blocks.{i}.norm2.weight''', F'''vit.encoder.layer.{i}.layernorm_after.weight''') )
          rename_keys.append((F'''blocks.{i}.norm2.bias''', F'''vit.encoder.layer.{i}.layernorm_after.bias''') )
          rename_keys.append((F'''blocks.{i}.mlp.fc1.weight''', F'''vit.encoder.layer.{i}.intermediate.dense.weight''') )
          rename_keys.append((F'''blocks.{i}.mlp.fc1.bias''', F'''vit.encoder.layer.{i}.intermediate.dense.bias''') )
          rename_keys.append((F'''blocks.{i}.mlp.fc2.weight''', F'''vit.encoder.layer.{i}.output.dense.weight''') )
          rename_keys.append((F'''blocks.{i}.mlp.fc2.bias''', F'''vit.encoder.layer.{i}.output.dense.bias''') )

     # projection layer + position embeddings
     rename_keys.extend(
         [
             ("""cls_token""", """vit.embeddings.cls_token"""),
             ("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight"""),
             ("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias"""),
             ("""pos_embed""", """vit.embeddings.position_embeddings"""),
         ] )

     if base_model:
          # layernorm + pooler
          rename_keys.extend(
              [
                  ("""norm.weight""", """layernorm.weight"""),
                  ("""norm.bias""", """layernorm.bias"""),
              ] )

          # if just the base model, we should remove "vit" from all keys that start with "vit"
          a_    :     str										=    [(pair[0], pair[1][4:]) if pair[1].startswith("""vit""" ) else pair for pair in rename_keys]
     else:
          # layernorm + classification head
          rename_keys.extend(
              [
                  ("""norm.weight""", """vit.layernorm.weight"""),
                  ("""norm.bias""", """vit.layernorm.bias"""),
                  ("""head.weight""", """classifier.weight"""),
                  ("""head.bias""", """classifier.bias"""),
              ] )

     return rename_keys



def  _a			(						__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase=False ):







     for i in range(config.num_hidden_layers ):
          if base_model:
               a_    :     Dict										=    """"""
          else:
               a_    :     Union[str, Any]										=    """vit."""
          # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
          a_    :     Optional[int]										=    state_dict.pop(F'''blocks.{i}.attn.qkv.weight''' )
          a_    :     Union[str, Any]										=    state_dict.pop(F'''blocks.{i}.attn.qkv.bias''' )
          # next, add query, keys and values (in that order) to the state dict
          a_    :     int										=    in_proj_weight[
              : config.hidden_size, :
          ]
          a_    :     Optional[int]										=    in_proj_bias[: config.hidden_size]
          a_    :     List[Any]										=    in_proj_weight[
              config.hidden_size : config.hidden_size * 2, :
          ]
          a_    :     str										=    in_proj_bias[
              config.hidden_size : config.hidden_size * 2
          ]
          a_    :     Any										=    in_proj_weight[
              -config.hidden_size :, :
          ]
          a_    :     Dict										=    in_proj_bias[-config.hidden_size :]



def  _a			(						__UpperCamelCase ):
     a_    :     Tuple										=    ["""head.weight""", """head.bias"""]
     for k in ignore_keys:
          state_dict.pop(__UpperCamelCase  ,  __UpperCamelCase )



def  _a			(						__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase ):
     a_    :     int										=    dct.pop(__UpperCamelCase )
     a_    :     List[str]										=    val



def  _a			(						):
     a_    :     int										=    """http://images.cocodataset.org/val2017/000000039769.jpg"""
     a_    :     Optional[Any]										=    Image.open(requests.get(__UpperCamelCase  ,  stream=__UpperCamelCase ).raw )
     return im



@torch.no_grad()
def  _a			(						__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase=True ):
     a_    :     int										=    ViTConfig()
     # patch_size
     if model_name[-1] == "8":
          a_    :     Dict										=    8
     # set labels if required
     if not base_model:
          a_    :     List[Any]										=    1_0_0_0
          a_    :     int										=    """huggingface/label-files"""
          a_    :     List[Any]										=    """imagenet-1k-id2label.json"""
          a_    :     Optional[int]										=    json.load(open(hf_hub_download(__UpperCamelCase  ,  __UpperCamelCase  ,  repo_type="""dataset""" )  ,  """r""" ) )
          a_    :     Optional[int]										=    {int(__UpperCamelCase ): v for k, v in idalabel.items()}
          a_    :     Dict										=    idalabel
          a_    :     int										=    {v: k for k, v in idalabel.items()}
     # size of the architecture
     if model_name in ["dino_vits8", "dino_vits16"]:
          a_    :     List[str]										=    3_8_4
          a_    :     List[Any]										=    1_5_3_6
          a_    :     List[Any]										=    1_2
          a_    :     Tuple										=    6

     # load original model from torch hub
     a_    :     Dict										=    torch.hub.load("""facebookresearch/dino:main"""  ,  __UpperCamelCase )
     original_model.eval()

     # load state_dict of original model, remove and rename some keys
     a_    :     Optional[Any]										=    original_model.state_dict()
     if base_model:
          remove_classification_head_(__UpperCamelCase )
     a_    :     Any										=    create_rename_keys(__UpperCamelCase  ,  base_model=__UpperCamelCase )
     for src, dest in rename_keys:
          rename_key(__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase )
     read_in_q_k_v(__UpperCamelCase  ,  __UpperCamelCase  ,  __UpperCamelCase )

     # load HuggingFace model
     if base_model:
          a_    :     Union[str, Any]										=    ViTModel(__UpperCamelCase  ,  add_pooling_layer=__UpperCamelCase ).eval()
     else:
          a_    :     Tuple										=    ViTForImageClassification(__UpperCamelCase ).eval()
     model.load_state_dict(__UpperCamelCase )

     # Check outputs on an image, prepared by ViTImageProcessor
     a_    :     List[str]										=    ViTImageProcessor()
     a_    :     Tuple										=    image_processor(images=prepare_img()  ,  return_tensors="""pt""" )
     a_    :     Any										=    encoding["""pixel_values"""]
     a_    :     Any										=    model(__UpperCamelCase )

     if base_model:
          a_    :     Dict										=    original_model(__UpperCamelCase )
          assert torch.allclose(__UpperCamelCase  ,  outputs.last_hidden_state[:, 0, :]  ,  atol=1E-1 )
     else:
          a_    :     Optional[Any]										=    original_model(__UpperCamelCase )
          assert logits.shape == outputs.logits.shape
          assert torch.allclose(__UpperCamelCase  ,  outputs.logits  ,  atol=1E-3 )

     Path(__UpperCamelCase ).mkdir(exist_ok=__UpperCamelCase )
     print(F'''Saving model {model_name} to {pytorch_dump_folder_path}''' )
     model.save_pretrained(__UpperCamelCase )
     print(F'''Saving image processor to {pytorch_dump_folder_path}''' )
     image_processor.save_pretrained(__UpperCamelCase )


if __name__ == "__main__":
       __lowerCamelCase        =     argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '''--model_name''',
           default='''dino_vitb16''',
           type=str,
           help='''Name of the model trained with DINO you\'d like to convert.''',
       )
       parser.add_argument(
           '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
       )
       parser.add_argument(
           '''--base_model''',
           action='''store_true''',
           help='''Whether to only convert the base model (no projection head weights).''',
       )

       parser.set_defaults(base_model=True)
       __lowerCamelCase        =     parser.parse_args()
       convert_vit_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.base_model)







from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


__lowerCamelCase        =     [
    '''openmmlab/upernet-convnext-tiny''',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
__lowerCamelCase        =     '''UperNetConfig'''


class   a__	(						nn.Module    ):

       def __init__(   self      :     Any      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]]      ,							lowerCamelCase_      :     Union[int, Tuple[int, int], str] = 0      ,							lowerCamelCase_      :     bool = False      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]] = 1      ,							):
            super().__init__()
            a_    :     str										=    nn.Convad(
                in_channels=lowerCamelCase_      ,							out_channels=lowerCamelCase_      ,							kernel_size=lowerCamelCase_      ,							padding=lowerCamelCase_      ,							bias=lowerCamelCase_      ,							dilation=lowerCamelCase_      ,							)
            a_    :     Dict										=    nn.BatchNormad(lowerCamelCase_    )
            a_    :     Union[str, Any]										=    nn.ReLU()

       def 						UpperCAmelCase(   self      :     str      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     Tuple										=    self.conv(lowerCamelCase_    )
            a_    :     int										=    self.batch_norm(lowerCamelCase_    )
            a_    :     Optional[int]										=    self.activation(lowerCamelCase_    )

            return output


class   a__	(						nn.Module    ):

       def __init__(   self      :     List[str]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int    ):
            super().__init__()
            a_    :     Union[str, Any]										=    [
                nn.AdaptiveAvgPoolad(lowerCamelCase_    ),
                UperNetConvModule(lowerCamelCase_      ,							lowerCamelCase_      ,							kernel_size=1    ),
            ]
            for i, layer in enumerate(self.layers    ):
                 self.add_module(str(lowerCamelCase_    )      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[Any]      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     Optional[int]										=    input
            for layer in self.layers:
                 a_    :     Dict										=    layer(lowerCamelCase_    )
            return hidden_state


class   a__	(						nn.Module    ):

       def __init__(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Tuple[int, ...]      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     int      ,							lowerCamelCase_      :     bool    ):
            super().__init__()
            a_    :     Optional[int]										=    pool_scales
            a_    :     List[Any]										=    align_corners
            a_    :     Union[str, Any]										=    in_channels
            a_    :     List[str]										=    channels
            a_    :     Optional[int]										=    []
            for i, pool_scale in enumerate(lowerCamelCase_    ):
                 a_    :     Optional[int]										=    UperNetPyramidPoolingBlock(pool_scale=lowerCamelCase_      ,							in_channels=lowerCamelCase_      ,							channels=lowerCamelCase_    )
                 self.blocks.append(lowerCamelCase_    )
                 self.add_module(str(lowerCamelCase_    )      ,							lowerCamelCase_    )

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     torch.Tensor    ):
            a_    :     List[str]										=    []
            for ppm in self.blocks:
                 a_    :     Any										=    ppm(lowerCamelCase_    )
                 a_    :     str										=    nn.functional.interpolate(
                     lowerCamelCase_      ,							size=x.size()[2:]      ,							mode="""bilinear"""      ,							align_corners=self.align_corners    )
                 ppm_outs.append(lowerCamelCase_    )
            return ppm_outs


class   a__	(						nn.Module    ):

       def __init__(   self      :     Any      ,							lowerCamelCase_      :     Union[str, Any]      ,							lowerCamelCase_      :     int    ):
            super().__init__()

            a_    :     Tuple										=    config
            a_    :     int										=    config.pool_scales  # e.g. (1, 2, 3, 6)
            a_    :     str										=    in_channels
            a_    :     str										=    config.hidden_size
            a_    :     int										=    False
            a_    :     int										=    nn.Convad(self.channels      ,							config.num_labels      ,							kernel_size=1    )

            # PSP Module
            a_    :     Optional[int]										=    UperNetPyramidPoolingModule(
                self.pool_scales      ,							self.in_channels[-1]      ,							self.channels      ,							align_corners=self.align_corners      ,							)
            a_    :     int										=    UperNetConvModule(
                self.in_channels[-1] + len(self.pool_scales    ) * self.channels      ,							self.channels      ,							kernel_size=3      ,							padding=1      ,							)
            # FPN Module
            a_    :     List[Any]										=    nn.ModuleList()
            a_    :     Optional[int]										=    nn.ModuleList()
            for in_channels in self.in_channels[:-1]:  # skip the top layer
                 a_    :     List[str]										=    UperNetConvModule(lowerCamelCase_      ,							self.channels      ,							kernel_size=1    )
                 a_    :     Union[str, Any]										=    UperNetConvModule(self.channels      ,							self.channels      ,							kernel_size=3      ,							padding=1    )
                 self.lateral_convs.append(lowerCamelCase_    )
                 self.fpn_convs.append(lowerCamelCase_    )

            a_    :     str										=    UperNetConvModule(
                len(self.in_channels    ) * self.channels      ,							self.channels      ,							kernel_size=3      ,							padding=1      ,							)

       def 						UpperCAmelCase(   self      :     str    ):
            self.apply(self._init_weights    )

       def 						UpperCAmelCase(   self      :     Any      ,							lowerCamelCase_      :     Union[str, Any]    ):
            if isinstance(lowerCamelCase_      ,							nn.Convad    ):
                 module.weight.data.normal_(mean=0.0      ,							std=self.config.initializer_range    )
                 if module.bias is not None:
                      module.bias.data.zero_()

       def 						UpperCAmelCase(   self      :     Tuple      ,							lowerCamelCase_      :     Optional[int]    ):
            a_    :     Optional[int]										=    inputs[-1]
            a_    :     List[str]										=    [x]
            psp_outs.extend(self.psp_modules(lowerCamelCase_    )    )
            a_    :     int										=    torch.cat(lowerCamelCase_      ,							dim=1    )
            a_    :     Tuple										=    self.bottleneck(lowerCamelCase_    )

            return output

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     torch.Tensor    ):
            # build laterals
            a_    :     List[Any]										=    [lateral_conv(encoder_hidden_states[i]    ) for i, lateral_conv in enumerate(self.lateral_convs    )]

            laterals.append(self.psp_forward(lowerCamelCase_    )    )

            # build top-down path
            a_    :     Tuple										=    len(lowerCamelCase_    )
            for i in range(used_backbone_levels - 1      ,							0      ,							-1    ):
                 a_    :     List[Any]										=    laterals[i - 1].shape[2:]
                 a_    :     int										=    laterals[i - 1] + nn.functional.interpolate(
                     laterals[i]      ,							size=lowerCamelCase_      ,							mode="""bilinear"""      ,							align_corners=self.align_corners    )

            # build outputs
            a_    :     int										=    [self.fpn_convs[i](laterals[i]    ) for i in range(used_backbone_levels - 1    )]
            # append psp feature
            fpn_outs.append(laterals[-1]    )

            for i in range(used_backbone_levels - 1      ,							0      ,							-1    ):
                 a_    :     Union[str, Any]										=    nn.functional.interpolate(
                     fpn_outs[i]      ,							size=fpn_outs[0].shape[2:]      ,							mode="""bilinear"""      ,							align_corners=self.align_corners    )
            a_    :     Optional[Any]										=    torch.cat(lowerCamelCase_      ,							dim=1    )
            a_    :     Tuple										=    self.fpn_bottleneck(lowerCamelCase_    )
            a_    :     str										=    self.classifier(lowerCamelCase_    )

            return output


class   a__	(						nn.Module    ):

       def __init__(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[Any]      ,							lowerCamelCase_      :     int = 2      ,							lowerCamelCase_      :     int = 3      ,							lowerCamelCase_      :     Union[int, Tuple[int, int]] = 1    ):
            super().__init__()

            a_    :     Union[str, Any]										=    config
            a_    :     str										=    config.auxiliary_in_channels
            a_    :     List[str]										=    config.auxiliary_channels
            a_    :     List[Any]										=    config.auxiliary_num_convs
            a_    :     Optional[Any]										=    config.auxiliary_concat_input
            a_    :     str										=    in_index

            a_    :     str										=    (kernel_size // 2) * dilation
            a_    :     Union[str, Any]										=    []
            convs.append(
                UperNetConvModule(
                    self.in_channels      ,							self.channels      ,							kernel_size=lowerCamelCase_      ,							padding=lowerCamelCase_      ,							dilation=lowerCamelCase_    )    )
            for i in range(self.num_convs - 1    ):
                 convs.append(
                     UperNetConvModule(
                         self.channels      ,							self.channels      ,							kernel_size=lowerCamelCase_      ,							padding=lowerCamelCase_      ,							dilation=lowerCamelCase_    )    )
            if self.num_convs == 0:
                 a_    :     Dict										=    nn.Identity()
            else:
                 a_    :     Optional[Any]										=    nn.Sequential(*lowerCamelCase_    )
            if self.concat_input:
                 a_    :     List[str]										=    UperNetConvModule(
                     self.in_channels + self.channels      ,							self.channels      ,							kernel_size=lowerCamelCase_      ,							padding=kernel_size // 2    )

            a_    :     str										=    nn.Convad(self.channels      ,							config.num_labels      ,							kernel_size=1    )

       def 						UpperCAmelCase(   self      :     Optional[int]    ):
            self.apply(self._init_weights    )

       def 						UpperCAmelCase(   self      :     List[str]      ,							lowerCamelCase_      :     Dict    ):
            if isinstance(lowerCamelCase_      ,							nn.Convad    ):
                 module.weight.data.normal_(mean=0.0      ,							std=self.config.initializer_range    )
                 if module.bias is not None:
                      module.bias.data.zero_()

       def 						UpperCAmelCase(   self      :     Any      ,							lowerCamelCase_      :     torch.Tensor    ):
            # just take the relevant feature maps
            a_    :     List[Any]										=    encoder_hidden_states[self.in_index]
            a_    :     Any										=    self.convs(lowerCamelCase_    )
            if self.concat_input:
                 a_    :     Optional[Any]										=    self.conv_cat(torch.cat([hidden_states, output]      ,							dim=1    )    )
            a_    :     Union[str, Any]										=    self.classifier(lowerCamelCase_    )
            return output


class   a__	(						lowerCAmelCase_    ):
       lowerCamelCase__:     Tuple							    =     UperNetConfig
       lowerCamelCase__:     Tuple							    =     """pixel_values"""
       lowerCamelCase__:     Optional[Any]							    =     True

       def 						UpperCAmelCase(   self      :     Optional[int]      ,							lowerCamelCase_      :     str    ):
            if isinstance(lowerCamelCase_      ,							lowerCamelCase_    ):
                 module.backbone.init_weights()
                 module.decode_head.init_weights()
                 module.auxiliary_head.init_weights()

       def 						UpperCAmelCase(   self      :     List[str]    ):
            self.backbone.init_weights()
            self.decode_head.init_weights()
            self.auxiliary_head.init_weights()

       def 						UpperCAmelCase(   self      :     Optional[Any]      ,							lowerCamelCase_      :     List[Any]      ,							lowerCamelCase_      :     str=False    ):
            if isinstance(lowerCamelCase_      ,							lowerCamelCase_    ):
                 a_    :     List[Any]										=    value

__lowerCamelCase        =     R'''
    Parameters:
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.
        config ([`UperNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

__lowerCamelCase        =     R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See
            `attentions` under returned tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under
            returned tensors for more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
'''


@add_start_docstrings(
    """UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes."""						,       lowerCAmelCase_						,       )
class   a__	(						lowerCAmelCase_    ):

       def __init__(   self      :     Optional[int]      ,							lowerCamelCase_      :     Optional[Any]    ):
            super().__init__(lowerCamelCase_    )

            a_    :     str										=    AutoBackbone.from_config(config.backbone_config    )

            # Semantic segmentation head(s)
            a_    :     List[Any]										=    UperNetHead(lowerCamelCase_      ,							in_channels=self.backbone.channels    )
            a_    :     List[str]										=    UperNetFCNHead(lowerCamelCase_    ) if config.use_auxiliary_head else None

            # Initialize weights and apply final processing
            self.post_init()

       @add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format("""batch_size, sequence_length"""    )    )
       @replace_return_docstrings(output_type=lowerCamelCase_      ,							config_class=_CONFIG_FOR_DOC    )
       def 						UpperCAmelCase(   self      :     Union[str, Any]      ,							lowerCamelCase_      :     Optional[torch.Tensor] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							lowerCamelCase_      :     Optional[torch.Tensor] = None      ,							lowerCamelCase_      :     Optional[bool] = None      ,							):
            a_    :     List[str]										=    return_dict if return_dict is not None else self.config.use_return_dict
            a_    :     Any										=    (
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            a_    :     Tuple										=    output_attentions if output_attentions is not None else self.config.output_attentions

            a_    :     List[Any]										=    self.backbone.forward_with_filtered_kwargs(
                lowerCamelCase_      ,							output_hidden_states=lowerCamelCase_      ,							output_attentions=lowerCamelCase_    )
            a_    :     str										=    outputs.feature_maps

            a_    :     Tuple										=    self.decode_head(lowerCamelCase_    )
            a_    :     Optional[Any]										=    nn.functional.interpolate(lowerCamelCase_      ,							size=pixel_values.shape[2:]      ,							mode="""bilinear"""      ,							align_corners=lowerCamelCase_    )

            a_    :     List[Any]										=    None
            if self.auxiliary_head is not None:
                 a_    :     Dict										=    self.auxiliary_head(lowerCamelCase_    )
                 a_    :     List[Any]										=    nn.functional.interpolate(
                     lowerCamelCase_      ,							size=pixel_values.shape[2:]      ,							mode="""bilinear"""      ,							align_corners=lowerCamelCase_    )

            a_    :     Dict										=    None
            if labels is not None:
                 if self.config.num_labels == 1:
                      raise ValueError("""The number of labels should be greater than one"""    )
                 else:
                      # compute weighted loss
                      a_    :     int										=    CrossEntropyLoss(ignore_index=self.config.loss_ignore_index    )
                      a_    :     List[Any]										=    loss_fct(lowerCamelCase_      ,							lowerCamelCase_    )
                      a_    :     Optional[int]										=    loss_fct(lowerCamelCase_      ,							lowerCamelCase_    )
                      a_    :     Tuple										=    main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

            if not return_dict:
                 if output_hidden_states:
                      a_    :     str										=    (logits,) + outputs[1:]
                 else:
                      a_    :     Optional[int]										=    (logits,) + outputs[2:]
                 return ((loss,) + output) if loss is not None else output

            return SemanticSegmenterOutput(
                loss=lowerCamelCase_      ,							logits=lowerCamelCase_      ,							hidden_states=outputs.hidden_states      ,							attentions=outputs.attentions      ,							)



