





'''simple docstring'''


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


_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

# General docstring
_SCREAMING_SNAKE_CASE	 =							"ResNetConfig"

# Base docstring
_SCREAMING_SNAKE_CASE	 =							"microsoft/resnet-50"
_SCREAMING_SNAKE_CASE	 =							[1, 20_48, 7, 7]

# Image classification docstring
_SCREAMING_SNAKE_CASE	 =							"microsoft/resnet-50"
_SCREAMING_SNAKE_CASE	 =							"tiger cat"

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

class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 3			,     _lowerCAmelCase = 1			,     _lowerCAmelCase = "relu"		)    ->							int:
     super().__init__()
     _lowerCAmelCase				=      nn.Convad(
         _lowerCAmelCase			,     _lowerCAmelCase			,     kernel_size=_lowerCAmelCase			,     stride=_lowerCAmelCase			,     padding=kernel_size // 2			,     bias=_lowerCAmelCase		)
     _lowerCAmelCase				=      nn.BatchNormad(_lowerCAmelCase		)
     _lowerCAmelCase				=      ACTaFN[activation] if activation is not None else nn.Identity()





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Tensor:
     _lowerCAmelCase				=      self.convolution(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.normalization(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.activation(_lowerCAmelCase		)
     return hidden_state







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase		)    ->							Tuple:
     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 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Tensor:
     _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(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.pooler(_lowerCAmelCase		)
     return embedding







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 2		)    ->							List[str]:
     super().__init__()
     _lowerCAmelCase				=      nn.Convad(_lowerCAmelCase			,     _lowerCAmelCase			,     kernel_size=1			,     stride=_lowerCAmelCase			,     bias=_lowerCAmelCase		)
     _lowerCAmelCase				=      nn.BatchNormad(_lowerCAmelCase		)





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Tensor:
     _lowerCAmelCase				=      self.convolution(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.normalization(_lowerCAmelCase		)
     return hidden_state







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 1			,     _lowerCAmelCase = "relu"		)    ->							int:
     super().__init__()
     _lowerCAmelCase				=      in_channels != out_channels or stride != 1
     _lowerCAmelCase				=      (
         ResNetShortCut(_lowerCAmelCase			,     _lowerCAmelCase			,     stride=_lowerCAmelCase		) if should_apply_shortcut else nn.Identity()
     )
     _lowerCAmelCase				=      nn.Sequential(
         ResNetConvLayer(_lowerCAmelCase			,     _lowerCAmelCase			,     stride=_lowerCAmelCase		)			,     ResNetConvLayer(_lowerCAmelCase			,     _lowerCAmelCase			,     activation=_lowerCAmelCase		)			,     )
     _lowerCAmelCase				=      ACTaFN[activation]





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      hidden_state
     _lowerCAmelCase				=      self.layer(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.shortcut(_lowerCAmelCase		)
     hidden_state += residual
     _lowerCAmelCase				=      self.activation(_lowerCAmelCase		)
     return hidden_state







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 1			,     _lowerCAmelCase = "relu"			,     _lowerCAmelCase = 4		)    ->							Tuple:
     super().__init__()
     _lowerCAmelCase				=      in_channels != out_channels or stride != 1
     _lowerCAmelCase				=      out_channels // reduction
     _lowerCAmelCase				=      (
         ResNetShortCut(_lowerCAmelCase			,     _lowerCAmelCase			,     stride=_lowerCAmelCase		) if should_apply_shortcut else nn.Identity()
     )
     _lowerCAmelCase				=      nn.Sequential(
         ResNetConvLayer(_lowerCAmelCase			,     _lowerCAmelCase			,     kernel_size=1		)			,     ResNetConvLayer(_lowerCAmelCase			,     _lowerCAmelCase			,     stride=_lowerCAmelCase		)			,     ResNetConvLayer(_lowerCAmelCase			,     _lowerCAmelCase			,     kernel_size=1			,     activation=_lowerCAmelCase		)			,     )
     _lowerCAmelCase				=      ACTaFN[activation]





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							str:
     _lowerCAmelCase				=      hidden_state
     _lowerCAmelCase				=      self.layer(_lowerCAmelCase		)
     _lowerCAmelCase				=      self.shortcut(_lowerCAmelCase		)
     hidden_state += residual
     _lowerCAmelCase				=      self.activation(_lowerCAmelCase		)
     return hidden_state







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase = 2			,     _lowerCAmelCase = 2			,     )    ->							Dict:
     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(_lowerCAmelCase			,     _lowerCAmelCase			,     stride=_lowerCAmelCase			,     activation=config.hidden_act		)			,     *[layer(_lowerCAmelCase			,     _lowerCAmelCase			,     activation=config.hidden_act		) for _ in range(depth - 1		)]			,     )





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Tensor:
     _lowerCAmelCase				=      input
     for layer in self.layers:
         _lowerCAmelCase				=      layer(_lowerCAmelCase		)
     return hidden_state







class    lowerCAmelCase_		(		nn.Module		):




 def __init__(	self			,     _lowerCAmelCase		)    ->							List[Any]:
     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(
             _lowerCAmelCase			,     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(_lowerCAmelCase			,     config.depths[1:]		):
         self.stages.append(ResNetStage(_lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase			,     depth=_lowerCAmelCase		)		)





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = False			,     _lowerCAmelCase = True		)    ->							BaseModelOutputWithNoAttention:
     _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(_lowerCAmelCase		)

     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=_lowerCAmelCase			,     hidden_states=_lowerCAmelCase			,     )







class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							Tuple         =					ResNetConfig
 __lowerCamelCase						:							int         =					"resnet"
 __lowerCamelCase						:							int         =					"pixel_values"
 __lowerCamelCase						:							List[str]         =					True




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							List[str]:
     if isinstance(_lowerCAmelCase			,     nn.Convad		):
         nn.init.kaiming_normal_(module.weight			,     mode="fan_out"			,     nonlinearity="relu"		)
     elif isinstance(_lowerCAmelCase			,     (nn.BatchNormad, nn.GroupNorm)		):
         nn.init.constant_(module.weight			,     1		)
         nn.init.constant_(module.bias			,     0		)





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase=False		)    ->							Optional[Any]:
     if isinstance(_lowerCAmelCase			,     _lowerCAmelCase		):
         _lowerCAmelCase				=      value







_SCREAMING_SNAKE_CASE	 =							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"

_SCREAMING_SNAKE_CASE	 =							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."						,__magic_name__						,)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     _lowerCAmelCase		)    ->							str:
     super().__init__(_lowerCAmelCase		)
     _lowerCAmelCase				=      config
     _lowerCAmelCase				=      ResNetEmbeddings(_lowerCAmelCase		)
     _lowerCAmelCase				=      ResNetEncoder(_lowerCAmelCase		)
     _lowerCAmelCase				=      nn.AdaptiveAvgPoolad((1, 1)		)
     # Initialize weights and apply final processing
     self.post_init()





 @add_start_docstrings_to_model_forward(_lowerCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC			,     output_type=_lowerCAmelCase			,     config_class=_CONFIG_FOR_DOC			,     modality="vision"			,     expected_output=_EXPECTED_OUTPUT_SHAPE			,     )
 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None		)    ->							BaseModelOutputWithPoolingAndNoAttention:
     _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(_lowerCAmelCase		)

     _lowerCAmelCase				=      self.encoder(
         _lowerCAmelCase			,     output_hidden_states=_lowerCAmelCase			,     return_dict=_lowerCAmelCase		)

     _lowerCAmelCase				=      encoder_outputs[0]

     _lowerCAmelCase				=      self.pooler(_lowerCAmelCase		)

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

     return BaseModelOutputWithPoolingAndNoAttention(
         last_hidden_state=_lowerCAmelCase			,     pooler_output=_lowerCAmelCase			,     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    "						,__magic_name__						,)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     _lowerCAmelCase		)    ->							List[str]:
     super().__init__(_lowerCAmelCase		)
     _lowerCAmelCase				=      config.num_labels
     _lowerCAmelCase				=      ResNetModel(_lowerCAmelCase		)
     # 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(_lowerCAmelCase		)
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT			,     output_type=_lowerCAmelCase			,     config_class=_CONFIG_FOR_DOC			,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,     )
 def 							_snake_case   (	self			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None			,     )    ->							ImageClassifierOutputWithNoAttention:
     _lowerCAmelCase				=      return_dict if return_dict is not None else self.config.use_return_dict

     _lowerCAmelCase				=      self.resnet(_lowerCAmelCase			,     output_hidden_states=_lowerCAmelCase			,     return_dict=_lowerCAmelCase		)

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

     _lowerCAmelCase				=      self.classifier(_lowerCAmelCase		)

     _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(_lowerCAmelCase			,     _lowerCAmelCase		)
         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(_lowerCAmelCase			,     _lowerCAmelCase		)

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

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







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




 def __init__(	self			,     _lowerCAmelCase		)    ->							Tuple:
     super().__init__(_lowerCAmelCase		)
     super()._init_backbone(_lowerCAmelCase		)

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

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





 @add_start_docstrings_to_model_forward(_lowerCAmelCase		)
 @replace_return_docstrings(output_type=_lowerCAmelCase			,     config_class=_CONFIG_FOR_DOC		)
 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase = None			,     _lowerCAmelCase = None		)    ->							BackboneOutput:
     _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(_lowerCAmelCase		)

     _lowerCAmelCase				=      self.encoder(_lowerCAmelCase			,     output_hidden_states=_lowerCAmelCase			,     return_dict=_lowerCAmelCase		)

     _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=_lowerCAmelCase			,     hidden_states=outputs.hidden_states if output_hidden_states else None			,     attentions=_lowerCAmelCase			,     )









'''simple docstring'''


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
       from transformers import TvltImageProcessor

if is_speech_available():
       from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      "ZinengTang/tvlt-base"
     _lowerCAmelCase				=      tempfile.mkdtemp()




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Any:
     return TvltImageProcessor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							str:
     return TvltFeatureExtractor.from_pretrained(self.checkpoint			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)
     processor.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      TvltProcessor.from_pretrained(self.tmpdirname		)

     self.assertIsInstance(processor.feature_extractor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)

     _lowerCAmelCase				=      feature_extractor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     return_tensors="np"		)

     for key in audio_dict.keys():
         self.assertAlmostEqual(audio_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

     for key in image_dict.keys():
         self.assertAlmostEqual(image_dict[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     _lowerCAmelCase				=      np.ones([12000]		)
     _lowerCAmelCase				=      np.ones([3, 224, 224]		)

     _lowerCAmelCase				=      processor(audio=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()





 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_feature_extractor()

     _lowerCAmelCase				=      TvltProcessor(image_processor=_lowerCAmelCase			,     feature_extractor=_lowerCAmelCase		)

     self.assertListEqual(
         processor.model_input_names			,     image_processor.model_input_names + feature_extractor.model_input_names			,     msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"			,     )


