




'''simple docstring'''



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


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

# General docstring
UpperCAmelCase_							:	Union[str, Any]	     =		'''UperNetConfig'''





class      lowerCAmelCase (			nn.Module):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = 0     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = 1     ,	)   ->	None:



            '''simple docstring'''




            super().__init__()
            __snake_case            =	nn.Convad(
                in_channels=__SCREAMING_SNAKE_CASE     ,	out_channels=__SCREAMING_SNAKE_CASE     ,	kernel_size=__SCREAMING_SNAKE_CASE     ,	padding=__SCREAMING_SNAKE_CASE     ,	bias=__SCREAMING_SNAKE_CASE     ,	dilation=__SCREAMING_SNAKE_CASE     ,	)
            __snake_case            =	nn.BatchNormad(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	nn.ReLU()




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	torch.Tensor:



            '''simple docstring'''




            __snake_case            =	self.conv(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.batch_norm(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.activation(__SCREAMING_SNAKE_CASE	)

            return output





class      lowerCAmelCase (			nn.Module):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	None:



            '''simple docstring'''




            super().__init__()
            __snake_case            =	[
                nn.AdaptiveAvgPoolad(__SCREAMING_SNAKE_CASE	),
                UperNetConvModule(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	kernel_size=1	),
            ]
            for i, layer in enumerate(self.layers	):
                 self.add_module(str(__SCREAMING_SNAKE_CASE	)     ,	__SCREAMING_SNAKE_CASE	)




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	torch.Tensor:



            '''simple docstring'''




            __snake_case            =	input
            for layer in self.layers:
                 __snake_case            =	layer(__SCREAMING_SNAKE_CASE	)
            return hidden_state





class      lowerCAmelCase (			nn.Module):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	None:



            '''simple docstring'''




            super().__init__()
            __snake_case            =	pool_scales
            __snake_case            =	align_corners
            __snake_case            =	in_channels
            __snake_case            =	channels
            __snake_case            =	[]
            for i, pool_scale in enumerate(__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	UperNetPyramidPoolingBlock(pool_scale=__SCREAMING_SNAKE_CASE     ,	in_channels=__SCREAMING_SNAKE_CASE     ,	channels=__SCREAMING_SNAKE_CASE	)
                 self.blocks.append(__SCREAMING_SNAKE_CASE	)
                 self.add_module(str(__SCREAMING_SNAKE_CASE	)     ,	__SCREAMING_SNAKE_CASE	)




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	List[torch.Tensor]:



            '''simple docstring'''




            __snake_case            =	[]
            for ppm in self.blocks:
                 __snake_case            =	ppm(__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	nn.functional.interpolate(
                     __SCREAMING_SNAKE_CASE     ,	size=x.size()[2:]     ,	mode='''bilinear'''     ,	align_corners=self.align_corners	)
                 ppm_outs.append(__SCREAMING_SNAKE_CASE	)
            return ppm_outs





class      lowerCAmelCase (			nn.Module):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




            super().__init__()

            __snake_case            =	config
            __snake_case            =	config.pool_scales  # e.g. (1, 2, 3, 6)
            __snake_case            =	in_channels
            __snake_case            =	config.hidden_size
            __snake_case            =	False
            __snake_case            =	nn.Convad(self.channels     ,	config.num_labels     ,	kernel_size=1	)

            # PSP Module
            __snake_case            =	UperNetPyramidPoolingModule(
                self.pool_scales     ,	self.in_channels[-1]     ,	self.channels     ,	align_corners=self.align_corners     ,	)
            __snake_case            =	UperNetConvModule(
                self.in_channels[-1] + len(self.pool_scales	) * self.channels     ,	self.channels     ,	kernel_size=3     ,	padding=1     ,	)
            # FPN Module
            __snake_case            =	nn.ModuleList()
            __snake_case            =	nn.ModuleList()
            for in_channels in self.in_channels[:-1]:  # skip the top layer
                 __snake_case            =	UperNetConvModule(__SCREAMING_SNAKE_CASE     ,	self.channels     ,	kernel_size=1	)
                 __snake_case            =	UperNetConvModule(self.channels     ,	self.channels     ,	kernel_size=3     ,	padding=1	)
                 self.lateral_convs.append(__SCREAMING_SNAKE_CASE	)
                 self.fpn_convs.append(__SCREAMING_SNAKE_CASE	)

            __snake_case            =	UperNetConvModule(
                len(self.in_channels	) * self.channels     ,	self.channels     ,	kernel_size=3     ,	padding=1     ,	)


       def    lowerCAmelCase   ( self	)   ->	Optional[Any]:



            '''simple docstring'''




            self.apply(self._init_weights	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




            if isinstance(__SCREAMING_SNAKE_CASE     ,	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    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            __snake_case            =	inputs[-1]
            __snake_case            =	[x]
            psp_outs.extend(self.psp_modules(__SCREAMING_SNAKE_CASE	)	)
            __snake_case            =	torch.cat(__SCREAMING_SNAKE_CASE     ,	dim=1	)
            __snake_case            =	self.bottleneck(__SCREAMING_SNAKE_CASE	)

            return output




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	torch.Tensor:



            '''simple docstring'''




            __snake_case            =	[lateral_conv(encoder_hidden_states[i]	) for i, lateral_conv in enumerate(self.lateral_convs	)]

            laterals.append(self.psp_forward(__SCREAMING_SNAKE_CASE	)	)

            # build top-down path
            __snake_case            =	len(__SCREAMING_SNAKE_CASE	)
            for i in range(used_backbone_levels - 1     ,	0     ,	-1	):
                 __snake_case            =	laterals[i - 1].shape[2:]
                 __snake_case            =	laterals[i - 1] + nn.functional.interpolate(
                     laterals[i]     ,	size=__SCREAMING_SNAKE_CASE     ,	mode='''bilinear'''     ,	align_corners=self.align_corners	)

            # build outputs
            __snake_case            =	[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	):
                 __snake_case            =	nn.functional.interpolate(
                     fpn_outs[i]     ,	size=fpn_outs[0].shape[2:]     ,	mode='''bilinear'''     ,	align_corners=self.align_corners	)
            __snake_case            =	torch.cat(__SCREAMING_SNAKE_CASE     ,	dim=1	)
            __snake_case            =	self.fpn_bottleneck(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.classifier(__SCREAMING_SNAKE_CASE	)

            return output





class      lowerCAmelCase (			nn.Module):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = 2     ,	__SCREAMING_SNAKE_CASE = 3     ,	__SCREAMING_SNAKE_CASE = 1	)   ->	None:



            '''simple docstring'''




            super().__init__()

            __snake_case            =	config
            __snake_case            =	config.auxiliary_in_channels
            __snake_case            =	config.auxiliary_channels
            __snake_case            =	config.auxiliary_num_convs
            __snake_case            =	config.auxiliary_concat_input
            __snake_case            =	in_index

            __snake_case            =	(kernel_size // 2) * dilation
            __snake_case            =	[]
            convs.append(
                UperNetConvModule(
                    self.in_channels     ,	self.channels     ,	kernel_size=__SCREAMING_SNAKE_CASE     ,	padding=__SCREAMING_SNAKE_CASE     ,	dilation=__SCREAMING_SNAKE_CASE	)	)
            for i in range(self.num_convs - 1	):
                 convs.append(
                     UperNetConvModule(
                         self.channels     ,	self.channels     ,	kernel_size=__SCREAMING_SNAKE_CASE     ,	padding=__SCREAMING_SNAKE_CASE     ,	dilation=__SCREAMING_SNAKE_CASE	)	)
            if self.num_convs == 0:
                 __snake_case            =	nn.Identity()
            else:
                 __snake_case            =	nn.Sequential(*__SCREAMING_SNAKE_CASE	)
            if self.concat_input:
                 __snake_case            =	UperNetConvModule(
                     self.in_channels + self.channels     ,	self.channels     ,	kernel_size=__SCREAMING_SNAKE_CASE     ,	padding=kernel_size // 2	)

            __snake_case            =	nn.Convad(self.channels     ,	config.num_labels     ,	kernel_size=1	)


       def    lowerCAmelCase   ( self	)   ->	List[Any]:



            '''simple docstring'''




            self.apply(self._init_weights	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            if isinstance(__SCREAMING_SNAKE_CASE     ,	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    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	torch.Tensor:



            '''simple docstring'''




            __snake_case            =	encoder_hidden_states[self.in_index]
            __snake_case            =	self.convs(__SCREAMING_SNAKE_CASE	)
            if self.concat_input:
                 __snake_case            =	self.conv_cat(torch.cat([hidden_states, output]     ,	dim=1	)	)
            __snake_case            =	self.classifier(__SCREAMING_SNAKE_CASE	)
            return output





class      lowerCAmelCase (			__lowerCAmelCase):
       __lowercase	:   int										=      UperNetConfig
       __lowercase	:   Optional[int]										=      '''pixel_values'''
       __lowercase	:   Tuple										=      True


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




            if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                 module.backbone.init_weights()
                 module.decode_head.init_weights()
                 module.auxiliary_head.init_weights()


       def    lowerCAmelCase   ( self	)   ->	int:



            '''simple docstring'''




            self.backbone.init_weights()
            self.decode_head.init_weights()
            self.auxiliary_head.init_weights()




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=False	)   ->	str:



            '''simple docstring'''




            if isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	value





UpperCAmelCase_							:	Union[str, Any]	     =		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.
'''

UpperCAmelCase_							:	int	     =		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      lowerCAmelCase (			__lowerCAmelCase):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            super().__init__(__SCREAMING_SNAKE_CASE	)

            __snake_case            =	AutoBackbone.from_config(config.backbone_config	)

            # Semantic segmentation head(s)
            __snake_case            =	UperNetHead(__SCREAMING_SNAKE_CASE     ,	in_channels=self.backbone.channels	)
            __snake_case            =	UperNetFCNHead(__SCREAMING_SNAKE_CASE	) 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=__SCREAMING_SNAKE_CASE     ,	config_class=_CONFIG_FOR_DOC	)
       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Union[tuple, SemanticSegmenterOutput]:



            '''simple docstring'''




            __snake_case            =	return_dict if return_dict is not None else self.config.use_return_dict
            __snake_case            =	(
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            __snake_case            =	output_attentions if output_attentions is not None else self.config.output_attentions

            __snake_case            =	self.backbone.forward_with_filtered_kwargs(
                __SCREAMING_SNAKE_CASE     ,	output_hidden_states=__SCREAMING_SNAKE_CASE     ,	output_attentions=__SCREAMING_SNAKE_CASE	)
            __snake_case            =	outputs.feature_maps

            __snake_case            =	self.decode_head(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	nn.functional.interpolate(__SCREAMING_SNAKE_CASE     ,	size=pixel_values.shape[2:]     ,	mode='''bilinear'''     ,	align_corners=__SCREAMING_SNAKE_CASE	)

            __snake_case            =	None
            if self.auxiliary_head is not None:
                 __snake_case            =	self.auxiliary_head(__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	nn.functional.interpolate(
                     __SCREAMING_SNAKE_CASE     ,	size=pixel_values.shape[2:]     ,	mode='''bilinear'''     ,	align_corners=__SCREAMING_SNAKE_CASE	)

            __snake_case            =	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
                      __snake_case            =	CrossEntropyLoss(ignore_index=self.config.loss_ignore_index	)
                      __snake_case            =	loss_fct(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
                      __snake_case            =	loss_fct(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
                      __snake_case            =	main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

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












'''simple docstring'''



import inspect
import warnings
from typing import Any, Dict, Optional, Union

from packaging import version







def       _UpperCamelCase			(*_lowerCamelCase				:  str ,						_lowerCamelCase				:  Optional[Union[Dict, Any]] = None ,						_lowerCamelCase				:  List[Any]=True ,						_lowerCamelCase				:  str=2						)-> str:




     '''simple docstring'''





     from .. import __version__

     __snake_case            =	take_from
     __snake_case            =	()
     if not isinstance(args[0] ,						_lowerCamelCase						):
          __snake_case            =	(args,)

     for attribute, version_name, message in args:
          if version.parse(version.parse(_lowerCamelCase						).base_version						) >= version.parse(_lowerCamelCase						):
               raise ValueError(
                   f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\''''
                   f''' version {__version__} is >= {version_name}'''						)

          __snake_case            =	None
          if isinstance(_lowerCamelCase ,						_lowerCamelCase						) and attribute in deprecated_kwargs:
               values += (deprecated_kwargs.pop(_lowerCamelCase						),)
               __snake_case            =	f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.'''
          elif hasattr(_lowerCamelCase ,						_lowerCamelCase						):
               values += (getattr(_lowerCamelCase ,						_lowerCamelCase						),)
               __snake_case            =	f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.'''
          elif deprecated_kwargs is None:
               __snake_case            =	f'''`{attribute}` is deprecated and will be removed in version {version_name}.'''

          if warning is not None:
               __snake_case            =	warning + ''' ''' if standard_warn else ''''''
               warnings.warn(warning + message ,						_lowerCamelCase ,						stacklevel=_lowerCamelCase						)

     if isinstance(_lowerCamelCase ,						_lowerCamelCase						) and len(_lowerCamelCase						) > 0:
          __snake_case            =	inspect.getouterframes(inspect.currentframe()						)[1]
          __snake_case            =	call_frame.filename
          __snake_case            =	call_frame.lineno
          __snake_case            =	call_frame.function
          __snake_case       ,     __snake_case            =	next(iter(deprecated_kwargs.items()						)						)
          raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`'''						)

     if len(_lowerCamelCase						) == 0:
          return
     elif len(_lowerCamelCase						) == 1:
          return values[0]
     return values






