Adding Dropout to ResNet50

bryan123: <p>My goal is to use MC Dropout to ResNet50 architecture. Despite this, it has no dropout layers. What is the correct way to put dropout layers into ResNet50?</p>

amy2: <p>Did you find an answer to that ?</p>

J_Johnson: <p>See here for the PyTorch ResNet implementation:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/main/torchvision/models/resnet.py</a></h4>


      <pre><code class="lang-py">from functools import partial
from typing import Any, Callable, List, Optional, Type, Union

import torch
import torch.nn as nn
from torch import Tensor

from ..transforms._presets import ImageClassification
from ..utils import _log_api_usage_once
from ._api import register_model, Weights, WeightsEnum
from ._meta import _IMAGENET_CATEGORIES
from ._utils import _ovewrite_named_param, handle_legacy_interface


__all__ = [
    "ResNet",
    "ResNet18_Weights",
    "ResNet34_Weights",
    "ResNet50_Weights",
    "ResNet101_Weights",
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>You would need to define a dropout layer in the <code>init</code> (line 167) and rewrite the <code>_forward_impl</code>(line 266). Then instantiate your custom model and load weights.</p>

Bibhabasu_Mohapatra: <p>it might be true as Resnet 50 not have a dropout but you can easily add a Dropout at after maxpooling in the final fully connected layer. I have added the ref of the timm resnet he has added similarly:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/resnet.py">
  <header class="source">

      <a href="https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/resnet.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/resnet.py" target="_blank" rel="noopener nofollow ugc">huggingface/pytorch-image-models/blob/main/timm/models/resnet.py</a></h4>


      <pre><code class="lang-py">"""PyTorch ResNet

This started as a copy of https://github.com/pytorch/vision 'resnet.py' (BSD-3-Clause) with
additional dropout and dynamic global avg/max pool.

ResNeXt, SE-ResNeXt, SENet, and MXNet Gluon stem/downsample variants, tiered stems added by Ross Wightman

Copyright 2019, Ross Wightman
"""
import math
from functools import partial
from typing import Any, Dict, List, Optional, Tuple, Type, Union

import torch
import torch.nn as nn
import torch.nn.functional as F

from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.layers import DropBlock2d, DropPath, AvgPool2dSame, BlurPool2d, GroupNorm, LayerType, create_attn, \
    get_attn, get_act_layer, get_norm_layer, create_classifier
</code></pre>



  This file has been truncated. <a href="https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/resnet.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>