


'''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__		:					List[str]												=  [
    """openmmlab/upernet-convnext-tiny""",
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
A__		:					Union[str, Any]												=  """UperNetConfig"""





class    UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    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:
         super().__init__()
         __lowerCamelCase   :	Optional[int]								=		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_			,     )
         __lowerCamelCase   :	List[str]								=		nn.BatchNormad(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	str								=		nn.ReLU()







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     torch.Tensor:
         __lowerCamelCase   :	List[Any]								=		self.conv(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Dict								=		self.batch_norm(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Any								=		self.activation(SCREAMING_SNAKE_CASE_	)

         return output







class    UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     None:
         super().__init__()
         __lowerCamelCase   :	str								=		[
             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       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     torch.Tensor:
         __lowerCamelCase   :	Union[str, Any]								=		input
         for layer in self.layers:
              __lowerCamelCase   :	str								=		layer(SCREAMING_SNAKE_CASE_	)
         return hidden_state







class    UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






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





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     int:
         super().__init__()

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

         # PSP Module
         __lowerCamelCase   :	List[str]								=		UperNetPyramidPoolingModule(
             self.pool_scales			,     self.in_channels[-1]			,     self.channels			,     align_corners=self.align_corners			,     )
         __lowerCamelCase   :	Optional[Any]								=		UperNetConvModule(
             self.in_channels[-1] + len(self.pool_scales	) * self.channels			,     self.channels			,     kernel_size=3			,     padding=1			,     )
         # FPN Module
         __lowerCamelCase   :	List[Any]								=		nn.ModuleList()
         __lowerCamelCase   :	Dict								=		nn.ModuleList()
         for in_channels in self.in_channels[:-1]:  # skip the top layer
              __lowerCamelCase   :	int								=		UperNetConvModule(SCREAMING_SNAKE_CASE_			,     self.channels			,     kernel_size=1	)
              __lowerCamelCase   :	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_	)

         __lowerCamelCase   :	List[Any]								=		UperNetConvModule(
             len(self.in_channels	) * self.channels			,     self.channels			,     kernel_size=3			,     padding=1			,     )







    def       lowercase_				(							self	)				->     List[Any]:
         self.apply(self._init_weights	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     List[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       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     Optional[int]:
         __lowerCamelCase   :	int								=		inputs[-1]
         __lowerCamelCase   :	List[Any]								=		[x]
         psp_outs.extend(self.psp_modules(SCREAMING_SNAKE_CASE_	)	)
         __lowerCamelCase   :	str								=		torch.cat(SCREAMING_SNAKE_CASE_			,     dim=1	)
         __lowerCamelCase   :	Tuple								=		self.bottleneck(SCREAMING_SNAKE_CASE_	)

         return output







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

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

         return output







class    UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






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

         __lowerCamelCase   :	List[Any]								=		config
         __lowerCamelCase   :	Dict								=		config.auxiliary_in_channels
         __lowerCamelCase   :	List[str]								=		config.auxiliary_channels
         __lowerCamelCase   :	List[Any]								=		config.auxiliary_num_convs
         __lowerCamelCase   :	List[Any]								=		config.auxiliary_concat_input
         __lowerCamelCase   :	Any								=		in_index

         __lowerCamelCase   :	Dict								=		(kernel_size // 2) * dilation
         __lowerCamelCase   :	Union[str, 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:
              __lowerCamelCase   :	Any								=		nn.Identity()
         else:
              __lowerCamelCase   :	Any								=		nn.Sequential(*SCREAMING_SNAKE_CASE_	)
         if self.concat_input:
              __lowerCamelCase   :	Any								=		UperNetConvModule(
                  self.in_channels + self.channels			,     self.channels			,     kernel_size=SCREAMING_SNAKE_CASE_			,     padding=kernel_size // 2	)

         __lowerCamelCase   :	Union[str, Any]								=		nn.Convad(self.channels			,     config.num_labels			,     kernel_size=1	)







    def       lowercase_				(							self	)				->     int:
         self.apply(self._init_weights	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     str:
         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       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     torch.Tensor:
         # just take the relevant feature maps
         __lowerCamelCase   :	Optional[int]								=		encoder_hidden_states[self.in_index]
         __lowerCamelCase   :	int								=		self.convs(SCREAMING_SNAKE_CASE_	)
         if self.concat_input:
              __lowerCamelCase   :	Optional[int]								=		self.conv_cat(torch.cat([hidden_states, output]			,     dim=1	)	)
         __lowerCamelCase   :	Optional[Any]								=		self.classifier(SCREAMING_SNAKE_CASE_	)
         return output







class    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    lowerCamelCase  :					int							     =			UperNetConfig
    lowerCamelCase  :					List[Any]							     =			'pixel_values'
    lowerCamelCase  :					Dict							     =			True







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     Optional[int]:
         if isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	):
              module.backbone.init_weights()
              module.decode_head.init_weights()
              module.auxiliary_head.init_weights()







    def       lowercase_				(							self	)				->     Optional[int]:
         self.backbone.init_weights()
         self.decode_head.init_weights()
         self.auxiliary_head.init_weights()







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_=False	)				->     Optional[int]:
         if isinstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	):
              __lowerCamelCase   :	List[str]								=		value







A__		:					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.
"""

A__		:					List[Any]												=  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.'					,		_UpperCAmelCase					,		)
class    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_	)				->     int:
         super().__init__(SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	int								=		AutoBackbone.from_config(config.backbone_config	)

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

         __lowerCamelCase   :	str								=		self.backbone.forward_with_filtered_kwargs(
             SCREAMING_SNAKE_CASE_			,     output_hidden_states=SCREAMING_SNAKE_CASE_			,     output_attentions=SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Union[str, Any]								=		outputs.feature_maps

         __lowerCamelCase   :	Optional[int]								=		self.decode_head(SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Any								=		nn.functional.interpolate(SCREAMING_SNAKE_CASE_			,     size=pixel_values.shape[2:]			,     mode='bilinear'			,     align_corners=SCREAMING_SNAKE_CASE_	)

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

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

         if not return_dict:
              if output_hidden_states:
                   __lowerCamelCase   :	Optional[int]								=		(logits,) + outputs[1:]
              else:
                   __lowerCamelCase   :	List[Any]								=		(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 numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey





class    UpperCAmelCase_  :





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)				->     int:
         if dst_width < 0 or dst_height < 0:
              raise ValueError('Destination width/height should be > 0'	)

         __lowerCamelCase   :	Dict								=		img
         __lowerCamelCase   :	Any								=		img.shape[1]
         __lowerCamelCase   :	Optional[int]								=		img.shape[0]
         __lowerCamelCase   :	Dict								=		dst_width
         __lowerCamelCase   :	str								=		dst_height

         __lowerCamelCase   :	Dict								=		self.src_w / self.dst_w
         __lowerCamelCase   :	List[Any]								=		self.src_h / self.dst_h

         __lowerCamelCase   :	Optional[int]								=		(
             np.ones((self.dst_h, self.dst_w, 3)			,     np.uinta	) * 2_55
         )







    def       lowercase_				(							self	)				->     List[Any]:
         for i in range(self.dst_h	):
              for j in range(self.dst_w	):
                   __lowerCamelCase   :	Union[str, Any]								=		self.img[self.get_y(SCREAMING_SNAKE_CASE_	)][self.get_x(SCREAMING_SNAKE_CASE_	)]







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     int:
         return int(self.ratio_x * x	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     int:
         return int(self.ratio_y * y	)


if __name__ == "__main__":
      A__						,					A__		:					Optional[Any]												=  800, 600
      A__		:					List[str]												=  imread("""image_data/lena.jpg""", 1)
      A__		:					List[Any]												=  NearestNeighbour(im, dst_w, dst_h)
      n.process()

      imshow(
          f'''Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}''', n.output
      )
      waitKey(0)
      destroyAllWindows()






