


from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


UpperCAmelCase   :    Union[str, Any]     =		logging.get_logger(__name__)

# General docstring
UpperCAmelCase   :    Optional[Any]     =		"ResNetConfig"

# Base docstring
UpperCAmelCase   :    Any     =		"microsoft/resnet-50"
UpperCAmelCase   :    str     =		[1, 20_48, 7, 7]

# Image classification docstring
UpperCAmelCase   :    Tuple     =		"microsoft/resnet-50"
UpperCAmelCase   :    Union[str, Any]     =		"tiger cat"

UpperCAmelCase   :    Optional[Any]     =		[
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

		def __init__( self		,      A		,      A		,      A = 3		,      A = 1		,      A = "relu"	)  -> List[Any]:







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  nn.Convad(
					    A		,      A		,      kernel_size=A		,      stride=A		,      padding=kernel_size // 2		,      bias=A	)
					lowerCamelCase    		=  nn.BatchNormad(A	)
					lowerCamelCase    		=  ACTaFN[activation] if activation is not None else nn.Identity()







		def 					__A   ( self		,      A	)  -> Tensor:







					'''simple docstring'''







					lowerCamelCase    		=  self.convolution(A	)
					lowerCamelCase    		=  self.normalization(A	)
					lowerCamelCase    		=  self.activation(A	)
					return hidden_state







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

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







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  ResNetConvLayer(
					    config.num_channels		,      config.embedding_size		,      kernel_size=7		,      stride=2		,      activation=config.hidden_act	)
					lowerCamelCase    		=  nn.MaxPoolad(kernel_size=3		,      stride=2		,      padding=1	)
					lowerCamelCase    		=  config.num_channels







		def 					__A   ( self		,      A	)  -> Tensor:







					'''simple docstring'''







					lowerCamelCase    		=  pixel_values.shape[1]
					if num_channels != self.num_channels:
								raise ValueError(
								    """Make sure that the channel dimension of the pixel values match with the one set in the configuration."""	)
					lowerCamelCase    		=  self.embedder(A	)
					lowerCamelCase    		=  self.pooler(A	)
					return embedding







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

		def __init__( self		,      A		,      A		,      A = 2	)  -> Optional[int]:







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  nn.Convad(A		,      A		,      kernel_size=1		,      stride=A		,      bias=A	)
					lowerCamelCase    		=  nn.BatchNormad(A	)







		def 					__A   ( self		,      A	)  -> Tensor:







					'''simple docstring'''







					lowerCamelCase    		=  self.convolution(A	)
					lowerCamelCase    		=  self.normalization(A	)
					return hidden_state







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

		def __init__( self		,      A		,      A		,      A = 1		,      A = "relu"	)  -> Optional[int]:







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  in_channels != out_channels or stride != 1
					lowerCamelCase    		=  (
					    ResNetShortCut(A		,      A		,      stride=A	) if should_apply_shortcut else nn.Identity()
					)
					lowerCamelCase    		=  nn.Sequential(
					    ResNetConvLayer(A		,      A		,      stride=A	)		,      ResNetConvLayer(A		,      A		,      activation=A	)		,      )
					lowerCamelCase    		=  ACTaFN[activation]







		def 					__A   ( self		,      A	)  -> Tuple:







					'''simple docstring'''







					lowerCamelCase    		=  hidden_state
					lowerCamelCase    		=  self.layer(A	)
					lowerCamelCase    		=  self.shortcut(A	)
					hidden_state += residual
					lowerCamelCase    		=  self.activation(A	)
					return hidden_state







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

		def __init__( self		,      A		,      A		,      A = 1		,      A = "relu"		,      A = 4	)  -> Optional[int]:







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  in_channels != out_channels or stride != 1
					lowerCamelCase    		=  out_channels // reduction
					lowerCamelCase    		=  (
					    ResNetShortCut(A		,      A		,      stride=A	) if should_apply_shortcut else nn.Identity()
					)
					lowerCamelCase    		=  nn.Sequential(
					    ResNetConvLayer(A		,      A		,      kernel_size=1	)		,      ResNetConvLayer(A		,      A		,      stride=A	)		,      ResNetConvLayer(A		,      A		,      kernel_size=1		,      activation=A	)		,      )
					lowerCamelCase    		=  ACTaFN[activation]







		def 					__A   ( self		,      A	)  -> str:







					'''simple docstring'''







					lowerCamelCase    		=  hidden_state
					lowerCamelCase    		=  self.layer(A	)
					lowerCamelCase    		=  self.shortcut(A	)
					hidden_state += residual
					lowerCamelCase    		=  self.activation(A	)
					return hidden_state







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

		def __init__( self		,      A		,      A		,      A		,      A = 2		,      A = 2		,      )  -> Optional[Any]:







					'''simple docstring'''







					super().__init__()

					lowerCamelCase    		=  ResNetBottleNeckLayer if config.layer_type == """bottleneck""" else ResNetBasicLayer

					lowerCamelCase    		=  nn.Sequential(
					    # downsampling is done in the first layer with stride of 2
					    layer(A		,      A		,      stride=A		,      activation=config.hidden_act	)		,      *[layer(A		,      A		,      activation=config.hidden_act	) for _ in range(depth - 1	)]		,      )







		def 					__A   ( self		,      A	)  -> Tensor:







					'''simple docstring'''







					lowerCamelCase    		=  input
					for layer in self.layers:
								lowerCamelCase    		=  layer(A	)
					return hidden_state







class        __lowercase       (							nn.Module  ):



		"""simple docstring"""

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







					'''simple docstring'''







					super().__init__()
					lowerCamelCase    		=  nn.ModuleList([]	)
					# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
					self.stages.append(
					    ResNetStage(
					        A		,      config.embedding_size		,      config.hidden_sizes[0]		,      stride=2 if config.downsample_in_first_stage else 1		,      depth=config.depths[0]		,      )	)
					lowerCamelCase    		=  zip(config.hidden_sizes		,      config.hidden_sizes[1:]	)
					for (in_channels, out_channels), depth in zip(A		,      config.depths[1:]	):
								self.stages.append(ResNetStage(A		,      A		,      A		,      depth=A	)	)







		def 					__A   ( self		,      A		,      A = False		,      A = True	)  -> BaseModelOutputWithNoAttention:







					'''simple docstring'''







					lowerCamelCase    		=  () if output_hidden_states else None

					for stage_module in self.stages:
								if output_hidden_states:
											lowerCamelCase    		=  hidden_states + (hidden_state,)

								lowerCamelCase    		=  stage_module(A	)

					if output_hidden_states:
								lowerCamelCase    		=  hidden_states + (hidden_state,)

					if not return_dict:
								return tuple(v for v in [hidden_state, hidden_states] if v is not None	)

					return BaseModelOutputWithNoAttention(
					    last_hidden_state=A		,      hidden_states=A		,      )







class        __lowercase       (							a_  ):



		"""simple docstring"""

		UpperCamelCase     :					List[str]			       =		ResNetConfig
		UpperCamelCase     :					Any			       =		"resnet"
		UpperCamelCase     :					List[Any]			       =		"pixel_values"
		UpperCamelCase     :					Any			       =		True







		def 					__A   ( self		,      A	)  -> Optional[Any]:







					'''simple docstring'''







					if isinstance(A		,      nn.Convad	):
								nn.init.kaiming_normal_(module.weight		,      mode="""fan_out"""		,      nonlinearity="""relu"""	)
					elif isinstance(A		,      (nn.BatchNormad, nn.GroupNorm)	):
								nn.init.constant_(module.weight		,      1	)
								nn.init.constant_(module.bias		,      0	)







		def 					__A   ( self		,      A		,      A=False	)  -> Optional[int]:







					'''simple docstring'''







					if isinstance(A		,      A	):
								lowerCamelCase    		=  value






UpperCAmelCase   :    int     =		r"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`ResNetConfig`]): 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   :    Union[str, Any]     =		r"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            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(
    "The bare ResNet model outputting raw features without any specific head on top."			,							a_			,							)
class        __lowercase       (							a_  ):



		"""simple docstring"""

		def __init__( self		,      A	)  -> int:







					'''simple docstring'''







					super().__init__(A	)
					lowerCamelCase    		=  config
					lowerCamelCase    		=  ResNetEmbeddings(A	)
					lowerCamelCase    		=  ResNetEncoder(A	)
					lowerCamelCase    		=  nn.AdaptiveAvgPoolad((1, 1)	)
					# Initialize weights and apply final processing
					self.post_init()







		@add_start_docstrings_to_model_forward(A	)
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC		,      output_type=A		,      config_class=_CONFIG_FOR_DOC		,      modality="""vision"""		,      expected_output=_EXPECTED_OUTPUT_SHAPE		,      )
		def 					__A   ( self		,      A		,      A = None		,      A = None	)  -> BaseModelOutputWithPoolingAndNoAttention:







					'''simple docstring'''







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

					lowerCamelCase    		=  self.embedder(A	)

					lowerCamelCase    		=  self.encoder(
					    A		,      output_hidden_states=A		,      return_dict=A	)

					lowerCamelCase    		=  encoder_outputs[0]

					lowerCamelCase    		=  self.pooler(A	)

					if not return_dict:
								return (last_hidden_state, pooled_output) + encoder_outputs[1:]

					return BaseModelOutputWithPoolingAndNoAttention(
					    last_hidden_state=A		,      pooler_output=A		,      hidden_states=encoder_outputs.hidden_states		,      )







@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "			,							a_			,							)
class        __lowercase       (							a_  ):



		"""simple docstring"""

		def __init__( self		,      A	)  -> str:







					'''simple docstring'''







					super().__init__(A	)
					lowerCamelCase    		=  config.num_labels
					lowerCamelCase    		=  ResNetModel(A	)
					# classification head
					lowerCamelCase    		=  nn.Sequential(
					    nn.Flatten()		,      nn.Linear(config.hidden_sizes[-1]		,      config.num_labels	) if config.num_labels > 0 else nn.Identity()		,      )
					# initialize weights and apply final processing
					self.post_init()







		@add_start_docstrings_to_model_forward(A	)
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT		,      output_type=A		,      config_class=_CONFIG_FOR_DOC		,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,      )
		def 					__A   ( self		,      A = None		,      A = None		,      A = None		,      A = None		,      )  -> ImageClassifierOutputWithNoAttention:







					'''simple docstring'''







					lowerCamelCase    		=  return_dict if return_dict is not None else self.config.use_return_dict

					lowerCamelCase    		=  self.resnet(A		,      output_hidden_states=A		,      return_dict=A	)

					lowerCamelCase    		=  outputs.pooler_output if return_dict else outputs[1]

					lowerCamelCase    		=  self.classifier(A	)

					lowerCamelCase    		=  None

					if labels is not None:
								if self.config.problem_type is None:
											if self.num_labels == 1:
														lowerCamelCase    		=  """regression"""
											elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
														lowerCamelCase    		=  """single_label_classification"""
											else:
														lowerCamelCase    		=  """multi_label_classification"""
								if self.config.problem_type == "regression":
											lowerCamelCase    		=  MSELoss()
											if self.num_labels == 1:
														lowerCamelCase    		=  loss_fct(logits.squeeze()		,      labels.squeeze()	)
											else:
														lowerCamelCase    		=  loss_fct(A		,      A	)
								elif self.config.problem_type == "single_label_classification":
											lowerCamelCase    		=  CrossEntropyLoss()
											lowerCamelCase    		=  loss_fct(logits.view(-1		,      self.num_labels	)		,      labels.view(-1	)	)
								elif self.config.problem_type == "multi_label_classification":
											lowerCamelCase    		=  BCEWithLogitsLoss()
											lowerCamelCase    		=  loss_fct(A		,      A	)

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

					return ImageClassifierOutputWithNoAttention(loss=A		,      logits=A		,      hidden_states=outputs.hidden_states	)







@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "			,							a_			,							)
class        __lowercase       (							a_			,							a_  ):



		"""simple docstring"""

		def __init__( self		,      A	)  -> int:







					'''simple docstring'''







					super().__init__(A	)
					super()._init_backbone(A	)

					lowerCamelCase    		=  [config.embedding_size] + config.hidden_sizes
					lowerCamelCase    		=  ResNetEmbeddings(A	)
					lowerCamelCase    		=  ResNetEncoder(A	)

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







		@add_start_docstrings_to_model_forward(A	)
		@replace_return_docstrings(output_type=A		,      config_class=_CONFIG_FOR_DOC	)
		def 					__A   ( self		,      A		,      A = None		,      A = None	)  -> BackboneOutput:







					'''simple docstring'''







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

					lowerCamelCase    		=  self.embedder(A	)

					lowerCamelCase    		=  self.encoder(A		,      output_hidden_states=A		,      return_dict=A	)

					lowerCamelCase    		=  outputs.hidden_states

					lowerCamelCase    		=  ()
					for idx, stage in enumerate(self.stage_names	):
								if stage in self.out_features:
											feature_maps += (hidden_states[idx],)

					if not return_dict:
								lowerCamelCase    		=  (feature_maps,)
								if output_hidden_states:
											output += (outputs.hidden_states,)
								return output

					return BackboneOutput(
					    feature_maps=A		,      hidden_states=outputs.hidden_states if output_hidden_states else None		,      attentions=A		,      )





from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


UpperCAmelCase   :    Any     =		logging.get_logger(__name__)







class        __lowercase       (							a_  ):



		"""simple docstring"""

		UpperCamelCase     :					List[Any]			       =		["audio_values", "audio_mask"]







		def __init__( self		,      A=20_48		,      A=1		,      A=[16, 16]		,      A=1_28		,      A=4_41_00		,      A=86		,      A=20_48		,      A=0.0		,      **A		,      )  -> Dict:







					'''simple docstring'''







					super().__init__(
					    feature_size=A		,      sampling_rate=A		,      padding_value=A		,      **A		,      )

					lowerCamelCase    		=  spectrogram_length
					lowerCamelCase    		=  num_channels
					lowerCamelCase    		=  patch_size
					lowerCamelCase    		=  feature_size // self.patch_size[1]
					lowerCamelCase    		=  n_fft
					lowerCamelCase    		=  sampling_rate // hop_length_to_sampling_rate
					lowerCamelCase    		=  sampling_rate
					lowerCamelCase    		=  padding_value
					lowerCamelCase    		=  mel_filter_bank(
					    num_frequency_bins=1 + n_fft // 2		,      num_mel_filters=A		,      min_frequency=0.0		,      max_frequency=22050.0		,      sampling_rate=A		,      norm="""slaney"""		,      mel_scale="""slaney"""		,      ).T







		def 					__A   ( self		,      A	)  -> np.ndarray:







					'''simple docstring'''







					lowerCamelCase    		=  spectrogram(
					    A		,      window_function(self.n_fft		,      """hann"""	)		,      frame_length=self.n_fft		,      hop_length=self.hop_length		,      power=2.0		,      mel_filters=self.mel_filters.T		,      log_mel="""dB"""		,      db_range=80.0		,      )
					lowerCamelCase    		=  log_spec[:, :-1]
					lowerCamelCase    		=  log_spec - 20.0
					lowerCamelCase    		=  np.clip(log_spec / 40.0		,      -2.0		,      0.0	) + 1.0
					return log_spec







		def __call__( self		,      A		,      A = None		,      A = True		,      A = None		,      A = False		,      A = False		,      **A		,      )  -> BatchFeature:







					'''simple docstring'''







					if sampling_rate is not None:
								if sampling_rate != self.sampling_rate:
											raise ValueError(
											    """This feature extractor is set to support sampling rate"""
											    F' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'
											    F' with {self.sampling_rate} and not {sampling_rate}.'	)
					else:
								logger.warning(
								    """It is strongly recommended to pass the `sampling_rate` argument to this function. """
								    """Failing to do so can result in silent errors that might be hard to debug."""	)

					lowerCamelCase    		=  isinstance(A		,      np.ndarray	) and len(raw_speech.shape	) > 1
					if is_batched_numpy and len(raw_speech.shape	) > 2:
								raise ValueError(F'Only mono-channel audio is supported for input to {self}'	)
					lowerCamelCase    		=  is_batched_numpy or (
					    isinstance(A		,      (list, tuple)	) and (isinstance(raw_speech[0]		,      (np.ndarray, tuple, list)	))
					)
					if is_batched:
								lowerCamelCase    		=  [np.asarray([speech]		,      dtype=np.floataa	).T for speech in raw_speech]
					elif not is_batched and not isinstance(A		,      np.ndarray	):
								lowerCamelCase    		=  np.asarray(A		,      dtype=np.floataa	)
					elif isinstance(A		,      np.ndarray	) and raw_speech.dtype is np.dtype(np.floataa	):
								lowerCamelCase    		=  raw_speech.astype(np.floataa	)
					# always return batch
					if not is_batched:
								lowerCamelCase    		=  [np.asarray([raw_speech]	).T]

					# Convert audio signals to log mel spectrograms, truncate by time axis
					lowerCamelCase    		=  [
					    self._np_extract_fbank_features(waveform.squeeze()	).T[: self.spectrogram_length] for waveform in raw_speech
					]
					if isinstance(audio_features[0]		,      A	):
								lowerCamelCase    		=  [np.asarray(A		,      dtype=np.floataa	) for feature in audio_features]

					# Create audio attention mask
					lowerCamelCase    		=  max(
					    [ceil(feature.shape[0] / self.patch_size[0]	) * self.freq_len for feature in audio_features]	)  # The maximum number of audio patches in a batch
					if return_attention_mask:
								lowerCamelCase    		=  [
								    (ceil(feature.shape[0] / self.patch_size[0]	) * self.freq_len) * [1]
								    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]	) * self.freq_len) * [0]
								    for feature in audio_features
								]
								lowerCamelCase    		=  np.array(A	).astype(np.floataa	)

					# convert into correct format for padding
					lowerCamelCase    		=  max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
					lowerCamelCase    		=  np.ones([len(A	), 1, max_time_len, self.feature_size]	).astype(np.floataa	)
					lowerCamelCase    		=  padded_audio_features * self.padding_value
					for i in range(len(A	)	):
								lowerCamelCase    		=  audio_features[i]
								lowerCamelCase    		=  feature

					# return as BatchFeature
					if return_attention_mask:
								lowerCamelCase    		=  {"""audio_values""": padded_audio_features, """audio_mask""": audio_mask}
					else:
								lowerCamelCase    		=  {"""audio_values""": padded_audio_features}

					lowerCamelCase    		=  BatchFeature(data=A		,      tensor_type=A	)
					return encoded_inputs

