

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

# General docstring
__UpperCAmelCase			=					'UperNetConfig'







class   __lowercase      (     nn.Module       ):

  def __init__(	self		:      int       ,A		:      int       ,A		:      int       ,A		:      Union[int, Tuple[int, int]]       ,A		:      Union[int, Tuple[int, int], str] = 0       ,A		:      bool = False       ,A		:      Union[int, Tuple[int, int]] = 1       ,):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__       :	Any        =       nn.Convad(
       in_channels=A       ,out_channels=A       ,kernel_size=A       ,padding=A       ,bias=A       ,dilation=A       ,)
   UpperCAmelCase__       :	List[Any]        =       nn.BatchNormad(A     )
   UpperCAmelCase__       :	Optional[Any]        =       nn.ReLU()




  def  __lowercase							(	self		:      List[Any]       ,A		:      torch.Tensor     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.conv(A     )
   UpperCAmelCase__       :	Optional[Any]        =       self.batch_norm(A     )
   UpperCAmelCase__       :	Dict        =       self.activation(A     )

   return output







class   __lowercase      (     nn.Module       ):

  def __init__(	self		:      int       ,A		:      int       ,A		:      int       ,A		:      int     ):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__       :	int        =       [
       nn.AdaptiveAvgPoolad(A     ),
       UperNetConvModule(A       ,A       ,kernel_size=1     ),
   ]
   for i, layer in enumerate(self.layers     ):
    self.add_module(str(A     )       ,A     )




  def  __lowercase							(	self		:      Dict       ,A		:      torch.Tensor     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       input
   for layer in self.layers:
    UpperCAmelCase__       :	Optional[int]        =       layer(A     )
   return hidden_state







class   __lowercase      (     nn.Module       ):

  def __init__(	self		:      Union[str, Any]       ,A		:      Tuple[int, ...]       ,A		:      int       ,A		:      int       ,A		:      bool     ):






   '''simple docstring'''
   super().__init__()
   UpperCAmelCase__       :	Dict        =       pool_scales
   UpperCAmelCase__       :	Optional[int]        =       align_corners
   UpperCAmelCase__       :	Optional[int]        =       in_channels
   UpperCAmelCase__       :	List[str]        =       channels
   UpperCAmelCase__       :	Tuple        =       []
   for i, pool_scale in enumerate(A     ):
    UpperCAmelCase__       :	str        =       UperNetPyramidPoolingBlock(pool_scale=A       ,in_channels=A       ,channels=A     )
    self.blocks.append(A     )
    self.add_module(str(A     )       ,A     )




  def  __lowercase							(	self		:      List[Any]       ,A		:      torch.Tensor     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       []
   for ppm in self.blocks:
    UpperCAmelCase__       :	Tuple        =       ppm(A     )
    UpperCAmelCase__       :	List[str]        =       nn.functional.interpolate(
        A       ,size=x.size()[2:]       ,mode="""bilinear"""       ,align_corners=self.align_corners     )
    ppm_outs.append(A     )
   return ppm_outs







class   __lowercase      (     nn.Module       ):

  def __init__(	self		:      Union[str, Any]       ,A		:      str       ,A		:      Optional[Any]     ):






   '''simple docstring'''
   super().__init__()

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

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

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

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   self.apply(self._init_weights     )

  def  __lowercase							(	self		:      Dict       ,A		:      List[Any]     ):






   '''simple docstring'''
   if isinstance(A       ,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		:      str       ,A		:      Tuple     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       inputs[-1]
   UpperCAmelCase__       :	Any        =       [x]
   psp_outs.extend(self.psp_modules(A     )     )
   UpperCAmelCase__       :	Union[str, Any]        =       torch.cat(A       ,dim=1     )
   UpperCAmelCase__       :	Dict        =       self.bottleneck(A     )

   return output




  def  __lowercase							(	self		:      List[Any]       ,A		:      torch.Tensor     ):






   '''simple docstring'''
   # build laterals
   UpperCAmelCase__       :	Optional[int]        =       [lateral_conv(encoder_hidden_states[i]     ) for i, lateral_conv in enumerate(self.lateral_convs     )]

   laterals.append(self.psp_forward(A     )     )

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

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

   return output







class   __lowercase      (     nn.Module       ):

  def __init__(	self		:      Optional[Any]       ,A		:      Tuple       ,A		:      int = 2       ,A		:      int = 3       ,A		:      Union[int, Tuple[int, int]] = 1     ):






   '''simple docstring'''
   super().__init__()

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

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

   UpperCAmelCase__       :	int        =       nn.Convad(self.channels       ,config.num_labels       ,kernel_size=1     )

  def  __lowercase							(	self		:      Optional[int]     ):






   '''simple docstring'''
   self.apply(self._init_weights     )

  def  __lowercase							(	self		:      Any       ,A		:      Any     ):






   '''simple docstring'''
   if isinstance(A       ,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		:      Optional[int]       ,A		:      torch.Tensor     ):






   '''simple docstring'''
   # just take the relevant feature maps
   UpperCAmelCase__       :	int        =       encoder_hidden_states[self.in_index]
   UpperCAmelCase__       :	int        =       self.convs(A     )
   if self.concat_input:
    UpperCAmelCase__       :	str        =       self.conv_cat(torch.cat([hidden_states, output]       ,dim=1     )     )
   UpperCAmelCase__       :	Tuple        =       self.classifier(A     )
   return output







class   __lowercase      (     __lowerCamelCase       ):
  snake_case_												=							UperNetConfig
  snake_case_												=							"""pixel_values"""
  snake_case_												=							True

  def  __lowercase							(	self		:      Dict       ,A		:      str     ):






   '''simple docstring'''
   if isinstance(A       ,A     ):
    module.backbone.init_weights()
    module.decode_head.init_weights()
    module.auxiliary_head.init_weights()

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   self.backbone.init_weights()
   self.decode_head.init_weights()
   self.auxiliary_head.init_weights()




  def  __lowercase							(	self		:      Union[str, Any]       ,A		:      List[Any]       ,A		:      int=False     ):






   '''simple docstring'''
   if isinstance(A       ,A     ):
    UpperCAmelCase__       :	str        =       value







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

__UpperCAmelCase			=					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   __lowercase      (     __lowerCamelCase       ):

  def __init__(	self		:      Dict       ,A		:      Optional[int]     ):






   '''simple docstring'''
   super().__init__(A     )

   UpperCAmelCase__       :	Any        =       AutoBackbone.from_config(config.backbone_config     )

   # Semantic segmentation head(s)
   UpperCAmelCase__       :	Any        =       UperNetHead(A       ,in_channels=self.backbone.channels     )
   UpperCAmelCase__       :	Dict        =       UperNetFCNHead(A     ) 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=A       ,config_class=_CONFIG_FOR_DOC     )
  def  __lowercase							(	self		:      Optional[int]       ,A		:      Optional[torch.Tensor] = None       ,A		:      Optional[bool] = None       ,A		:      Optional[bool] = None       ,A		:      Optional[torch.Tensor] = None       ,A		:      Optional[bool] = None       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	List[str]        =       return_dict if return_dict is not None else self.config.use_return_dict
   UpperCAmelCase__       :	str        =       (
       output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
   )
   UpperCAmelCase__       :	Tuple        =       output_attentions if output_attentions is not None else self.config.output_attentions

   UpperCAmelCase__       :	int        =       self.backbone.forward_with_filtered_kwargs(
       A       ,output_hidden_states=A       ,output_attentions=A     )
   UpperCAmelCase__       :	List[Any]        =       outputs.feature_maps

   UpperCAmelCase__       :	int        =       self.decode_head(A     )
   UpperCAmelCase__       :	Optional[Any]        =       nn.functional.interpolate(A       ,size=pixel_values.shape[2:]       ,mode="""bilinear"""       ,align_corners=A     )

   UpperCAmelCase__       :	int        =       None
   if self.auxiliary_head is not None:
    UpperCAmelCase__       :	Union[str, Any]        =       self.auxiliary_head(A     )
    UpperCAmelCase__       :	Tuple        =       nn.functional.interpolate(
        A       ,size=pixel_values.shape[2:]       ,mode="""bilinear"""       ,align_corners=A     )

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

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

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












import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY


@is_pipeline_test
class       __lowerCAmelCase					(       unittest.TestCase      ):


	'''simple docstring'''





	_A													=				MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
	_A													=				TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

	def    snake_case__(						self:						Optional[int],					lowerCamelCase_:						Tuple,					lowerCamelCase_:						List[Any],					lowerCamelCase_:						Optional[Any]			):
			lowercase__					:      int      =		AudioClassificationPipeline(model=lowerCamelCase_,					feature_extractor=lowerCamelCase_			)

			# test with a raw waveform
			lowercase__					:      Dict      =		np.zeros((34000,)			)
			lowercase__					:      Optional[int]      =		np.zeros((14000,)			)
			return audio_classifier, [audioa, audio]

	def    snake_case__(						self:						Optional[int],					lowerCamelCase_:						Tuple,					lowerCamelCase_:						str			):
			lowercase__   ,				lowercase__					:      List[str]      =		examples
			lowercase__					:      int      =		audio_classifier(lowerCamelCase_			)
			# by default a model is initialized with num_labels=2
			self.assertEqual(
			    lowerCamelCase_,					[
			        {'score': ANY(lowerCamelCase_			), 'label': ANY(lowerCamelCase_			)},
			        {'score': ANY(lowerCamelCase_			), 'label': ANY(lowerCamelCase_			)},
			    ],					)
			lowercase__					:      List[Any]      =		audio_classifier(lowerCamelCase_,					top_k=1			)
			self.assertEqual(
			    lowerCamelCase_,					[
			        {'score': ANY(lowerCamelCase_			), 'label': ANY(lowerCamelCase_			)},
			    ],					)

			self.run_torchaudio(lowerCamelCase_			)

	@require_torchaudio
	def    snake_case__(						self:						Dict,					lowerCamelCase_:						str			):
			import datasets

			# test with a local file
			lowercase__					:      Optional[Any]      =		datasets.load_dataset('hf-internal-testing/librispeech_asr_dummy',					'clean',					split='validation'			)
			lowercase__					:      int      =		dataset[0]['audio']['array']
			lowercase__					:      Tuple      =		audio_classifier(lowerCamelCase_			)
			self.assertEqual(
			    lowerCamelCase_,					[
			        {'score': ANY(lowerCamelCase_			), 'label': ANY(lowerCamelCase_			)},
			        {'score': ANY(lowerCamelCase_			), 'label': ANY(lowerCamelCase_			)},
			    ],					)

	@require_torch
	def    snake_case__(						self:						Optional[int]			):
			lowercase__					:      List[str]      =		'anton-l/wav2vec2-random-tiny-classifier'

			lowercase__					:      Tuple      =		pipeline('audio-classification',					model=lowerCamelCase_			)

			lowercase__					:      List[str]      =		np.ones((8000,)			)
			lowercase__					:      List[Any]      =		audio_classifier(lowerCamelCase_,					top_k=4			)

			lowercase__					:      List[str]      =		[
			    {'score': 0.0_8_4_2, 'label': 'no'},
			    {'score': 0.0_8_3_8, 'label': 'up'},
			    {'score': 0.0_8_3_7, 'label': 'go'},
			    {'score': 0.0_8_3_4, 'label': 'right'},
			]
			lowercase__					:      Optional[int]      =		[
			    {'score': 0.0_8_4_5, 'label': 'stop'},
			    {'score': 0.0_8_4_4, 'label': 'on'},
			    {'score': 0.0_8_4_1, 'label': 'right'},
			    {'score': 0.0_8_3_4, 'label': 'left'},
			]
			self.assertIn(nested_simplify(lowerCamelCase_,					decimals=4			),					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]			)

			lowercase__					:      Tuple      =		{'array': np.ones((8000,)			), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
			lowercase__					:      Any      =		audio_classifier(lowerCamelCase_,					top_k=4			)
			self.assertIn(nested_simplify(lowerCamelCase_,					decimals=4			),					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]			)

	@require_torch
	@slow
	def    snake_case__(						self:						Optional[int]			):
			import datasets

			lowercase__					:      str      =		'superb/wav2vec2-base-superb-ks'

			lowercase__					:      Optional[int]      =		pipeline('audio-classification',					model=lowerCamelCase_			)
			lowercase__					:      List[Any]      =		datasets.load_dataset('anton-l/superb_dummy',					'ks',					split='test'			)

			lowercase__					:      Optional[Any]      =		np.array(dataset[3]['speech'],					dtype=np.floataa			)
			lowercase__					:      Optional[int]      =		audio_classifier(lowerCamelCase_,					top_k=4			)
			self.assertEqual(
			    nested_simplify(lowerCamelCase_,					decimals=3			),					[
			        {'score': 0.9_8_1, 'label': 'go'},
			        {'score': 0.0_0_7, 'label': 'up'},
			        {'score': 0.0_0_6, 'label': '_unknown_'},
			        {'score': 0.0_0_1, 'label': 'down'},
			    ],					)





	@require_tf
	@unittest.skip('Audio classification is not implemented for TF'			)
	def    snake_case__(						self:						int			):
			pass


