






'''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


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

# General docstring
A:      int  				=				'UperNetConfig'






class 							__magic_name__     (	nn.Module				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase = 0  ,      _lowercase = False  ,      _lowercase = 1  ,      )						->  None:
							super().__init__()
							lowercase_      :   Union[str, Any]        =			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  ,      )
							lowercase_      :   Optional[Any]        =			nn.BatchNormad(_SCREAMING_SNAKE_CASE       )
							lowercase_      :   Tuple        =			nn.ReLU()







	def        snake_case__		(							self  ,      _lowercase       )						->  torch.Tensor:
							lowercase_      :   Optional[Any]        =			self.conv(_SCREAMING_SNAKE_CASE       )
							lowercase_      :   Union[str, Any]        =			self.batch_norm(_SCREAMING_SNAKE_CASE       )
							lowercase_      :   Optional[Any]        =			self.activation(_SCREAMING_SNAKE_CASE       )

							return output






class 							__magic_name__     (	nn.Module				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  None:
							super().__init__()
							lowercase_      :   Any        =			[
							    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        snake_case__		(							self  ,      _lowercase       )						->  torch.Tensor:
							lowercase_      :   Any        =			input
							for layer in self.layers:
													lowercase_      :   int        =			layer(_SCREAMING_SNAKE_CASE       )
							return hidden_state






class 							__magic_name__     (	nn.Module				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase       )						->  None:
							super().__init__()
							lowercase_      :   Dict        =			pool_scales
							lowercase_      :   Tuple        =			align_corners
							lowercase_      :   List[Any]        =			in_channels
							lowercase_      :   Tuple        =			channels
							lowercase_      :   Dict        =			[]
							for i, pool_scale in enumerate(_SCREAMING_SNAKE_CASE       ):
													lowercase_      :   Any        =			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        snake_case__		(							self  ,      _lowercase       )						->  List[torch.Tensor]:
							lowercase_      :   Any        =			[]
							for ppm in self.blocks:
													lowercase_      :   Optional[Any]        =			ppm(_SCREAMING_SNAKE_CASE       )
													lowercase_      :   int        =			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 							__magic_name__     (	nn.Module				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase       )						->  Dict:
							super().__init__()

							lowercase_      :   List[Any]        =			config
							lowercase_      :   Optional[Any]        =			config.pool_scales  # e.g. (1, 2, 3, 6)
							lowercase_      :   Dict        =			in_channels
							lowercase_      :   str        =			config.hidden_size
							lowercase_      :   int        =			False
							lowercase_      :   Optional[int]        =			nn.Convad(self.channels  ,      config.num_labels  ,      kernel_size=1       )

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

							lowercase_      :   str        =			UperNetConvModule(
							    len(self.in_channels       ) * self.channels  ,      self.channels  ,      kernel_size=3  ,      padding=1  ,      )
	def        snake_case__		(							self       )						->  List[str]:
							self.apply(self._init_weights       )
	def        snake_case__		(							self  ,      _lowercase       )						->  Dict:
							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        snake_case__		(							self  ,      _lowercase       )						->  List[str]:
							lowercase_      :   Optional[Any]        =			inputs[-1]
							lowercase_      :   List[str]        =			[x]
							psp_outs.extend(self.psp_modules(_SCREAMING_SNAKE_CASE       )       )
							lowercase_      :   Union[str, Any]        =			torch.cat(_SCREAMING_SNAKE_CASE  ,      dim=1       )
							lowercase_      :   Union[str, Any]        =			self.bottleneck(_SCREAMING_SNAKE_CASE       )

							return output







	def        snake_case__		(							self  ,      _lowercase       )						->  torch.Tensor:
							# build laterals
							lowercase_      :   List[Any]        =			[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
							lowercase_      :   Any        =			len(_SCREAMING_SNAKE_CASE       )
							for i in range(used_backbone_levels - 1  ,      0  ,      -1       ):
													lowercase_      :   int        =			laterals[i - 1].shape[2:]
													lowercase_      :   Dict        =			laterals[i - 1] + nn.functional.interpolate(
													    laterals[i]  ,      size=_SCREAMING_SNAKE_CASE  ,      mode='bilinear'  ,      align_corners=self.align_corners       )

							# build outputs
							lowercase_      :   Optional[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       ):
													lowercase_      :   List[str]        =			nn.functional.interpolate(
													    fpn_outs[i]  ,      size=fpn_outs[0].shape[2:]  ,      mode='bilinear'  ,      align_corners=self.align_corners       )
							lowercase_      :   Optional[int]        =			torch.cat(_SCREAMING_SNAKE_CASE  ,      dim=1       )
							lowercase_      :   int        =			self.fpn_bottleneck(_SCREAMING_SNAKE_CASE       )
							lowercase_      :   Union[str, Any]        =			self.classifier(_SCREAMING_SNAKE_CASE       )

							return output






class 							__magic_name__     (	nn.Module				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase  ,      _lowercase = 2  ,      _lowercase = 3  ,      _lowercase = 1       )						->  None:
							super().__init__()

							lowercase_      :   str        =			config
							lowercase_      :   Optional[Any]        =			config.auxiliary_in_channels
							lowercase_      :   Tuple        =			config.auxiliary_channels
							lowercase_      :   List[str]        =			config.auxiliary_num_convs
							lowercase_      :   str        =			config.auxiliary_concat_input
							lowercase_      :   Any        =			in_index

							lowercase_      :   Any        =			(kernel_size // 2) * dilation
							lowercase_      :   Optional[Any]        =			[]
							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:
													lowercase_      :   Optional[int]        =			nn.Identity()
							else:
													lowercase_      :   List[Any]        =			nn.Sequential(*_SCREAMING_SNAKE_CASE       )
							if self.concat_input:
													lowercase_      :   List[str]        =			UperNetConvModule(
													    self.in_channels + self.channels  ,      self.channels  ,      kernel_size=_SCREAMING_SNAKE_CASE  ,      padding=kernel_size // 2       )

							lowercase_      :   List[str]        =			nn.Convad(self.channels  ,      config.num_labels  ,      kernel_size=1       )
	def        snake_case__		(							self       )						->  str:
							self.apply(self._init_weights       )
	def        snake_case__		(							self  ,      _lowercase       )						->  Optional[Any]:
							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        snake_case__		(							self  ,      _lowercase       )						->  torch.Tensor:
							# just take the relevant feature maps
							lowercase_      :   Optional[Any]        =			encoder_hidden_states[self.in_index]
							lowercase_      :   List[str]        =			self.convs(_SCREAMING_SNAKE_CASE       )
							if self.concat_input:
													lowercase_      :   Union[str, Any]        =			self.conv_cat(torch.cat([hidden_states, output]  ,      dim=1       )       )
							lowercase_      :   List[Any]        =			self.classifier(_SCREAMING_SNAKE_CASE       )
							return output






class 							__magic_name__     (	lowerCAmelCase__				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Tuple        =  UperNetConfig
	SCREAMING_SNAKE_CASE_					:      Tuple        =  "pixel_values"
	SCREAMING_SNAKE_CASE_					:      Tuple        =  True
	def        snake_case__		(							self  ,      _lowercase       )						->  Tuple:
							if isinstance(_SCREAMING_SNAKE_CASE  ,      _SCREAMING_SNAKE_CASE       ):
													module.backbone.init_weights()
													module.decode_head.init_weights()
													module.auxiliary_head.init_weights()
	def        snake_case__		(							self       )						->  Union[str, Any]:
							self.backbone.init_weights()
							self.decode_head.init_weights()
							self.auxiliary_head.init_weights()







	def        snake_case__		(							self  ,      _lowercase  ,      _lowercase=False       )						->  Optional[int]:
							if isinstance(_SCREAMING_SNAKE_CASE  ,      _SCREAMING_SNAKE_CASE       ):
													lowercase_      :   int        =			value


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

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






@add_start_docstrings(
    'UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.',						lowerCAmelCase__,						)
class 							__magic_name__     (	lowerCAmelCase__				):







	"""simple docstring"""



	def __init__(							self  ,      _lowercase       )						->  Dict:
							super().__init__(_SCREAMING_SNAKE_CASE       )

							lowercase_      :   Tuple        =			AutoBackbone.from_config(config.backbone_config       )

							# Semantic segmentation head(s)
							lowercase_      :   Union[str, Any]        =			UperNetHead(_SCREAMING_SNAKE_CASE  ,      in_channels=self.backbone.channels       )
							lowercase_      :   Dict        =			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        snake_case__		(							self  ,      _lowercase = None  ,      _lowercase = None  ,      _lowercase = None  ,      _lowercase = None  ,      _lowercase = None  ,      )						->  Union[tuple, SemanticSegmenterOutput]:
							lowercase_      :   Union[str, Any]        =			return_dict if return_dict is not None else self.config.use_return_dict
							lowercase_      :   str        =			(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowercase_      :   str        =			output_attentions if output_attentions is not None else self.config.output_attentions

							lowercase_      :   str        =			self.backbone.forward_with_filtered_kwargs(
							    _SCREAMING_SNAKE_CASE  ,      output_hidden_states=_SCREAMING_SNAKE_CASE  ,      output_attentions=_SCREAMING_SNAKE_CASE       )
							lowercase_      :   Dict        =			outputs.feature_maps

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

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

							lowercase_      :   Any        =			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
																			lowercase_      :   Optional[Any]        =			CrossEntropyLoss(ignore_index=self.config.loss_ignore_index       )
																			lowercase_      :   Optional[int]        =			loss_fct(_SCREAMING_SNAKE_CASE  ,      _SCREAMING_SNAKE_CASE       )
																			lowercase_      :   List[Any]        =			loss_fct(_SCREAMING_SNAKE_CASE  ,      _SCREAMING_SNAKE_CASE       )
																			lowercase_      :   Optional[Any]        =			main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

							if not return_dict:
													if output_hidden_states:
																			lowercase_      :   Union[str, Any]        =			(logits,) + outputs[1:]
													else:
																			lowercase_      :   Dict        =			(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'''

from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url






class 							__magic_name__     (	UpperCAmelCase_				):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_					:      Union[str, Any]        =  ''
	SCREAMING_SNAKE_CASE_					:      Union[str, Any]        =  'hf-legacy'  # "hf://"" is reserved for hffs
	def __init__(							self  ,      _lowercase = None  ,      _lowercase = None  ,      **_lowercase  ,      )						->  Optional[Any]:
							super().__init__(self  ,      **_lowercase       )
							lowercase_      :   int        =			repo_info
							lowercase_      :   List[Any]        =			token
							lowercase_      :   Union[str, Any]        =			None
	def        lowerCamelCase__		(							self       )						->  Optional[Any]:
							if self.dir_cache is None:
													lowercase_      :   Optional[Any]        =			{}
													for hf_file in self.repo_info.siblings:
																			# TODO(QL): add sizes
																			lowercase_      :   str        =			{
																			    'name': hf_file.rfilename,
																			    'size': None,
																			    'type': 'file',
																			}
																			self.dir_cache.update(
																			    {
																			        str(_lowercase       ): {'name': str(_lowercase       ), 'size': None, 'type': 'directory'}
																			        for d in list(PurePosixPath(hf_file.rfilename       ).parents       )[:-1]
																			    }       )
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase = "rb"  ,      **_lowercase  ,      )						->  Dict:
							if not isinstance(self.repo_info  ,      _lowercase       ):
													raise NotImplementedError(f"Open is only implemented for dataset repositories, but got {self.repo_info}"       )
							lowercase_      :   Optional[int]        =			hf_hub_url(self.repo_info.id  ,      _lowercase  ,      revision=self.repo_info.sha       )
							return fsspec.open(
							    _lowercase  ,      mode=_lowercase  ,      headers=get_authentication_headers_for_url(_lowercase  ,      use_auth_token=self.token       )  ,      client_kwargs={'trust_env': True}  ,      ).open()
	def        lowerCamelCase__		(							self  ,      _lowercase  ,      **_lowercase       )						->  Tuple:
							self._get_dirs()
							lowercase_      :   str        =			self._strip_protocol(_lowercase       )
							if path in self.dir_cache:
													return self.dir_cache[path]
							else:
													raise FileNotFoundError(_lowercase       )







	def        lowerCamelCase__		(							self  ,      _lowercase  ,      _lowercase=False  ,      **_lowercase       )						->  List[str]:
							self._get_dirs()
							lowercase_      :   List[str]        =			PurePosixPath(path.strip('/'       )       )
							lowercase_      :   List[str]        =			{}
							for p, f in self.dir_cache.items():
													lowercase_      :   Tuple        =			PurePosixPath(p.strip('/'       )       )
													lowercase_      :   Optional[int]        =			p.parent
													if root == path:
																			lowercase_      :   List[str]        =			f
							lowercase_      :   List[str]        =			list(paths.values()       )
							if detail:
													return out
							else:
													return sorted(f['name'] for f in out       )
