Adding Dropout in Pre-trained Vgg16

Pre-trained VGG16 architecture does not contain a dropout layer except in the last classifier layer. How can I add dropout layer after every convolution layer in VGG 16 if I want to keep the value of VGG16 parameters (weights/Biases)? (FYI, I wanted to add dropout layer between the convolutional layers in order to quantify MC-Dropout uncertainty during prediction).

To do so, you need to modify VGG.features. It is a nn.Sequential object and so all you need to do is modify the layers this specific object, you don’t need to modify the forward. Something of the following:

model = torchvision.model.vgg19(pretrained=True)
feats_list = list(model.features)
new_feats_list = []
for feat in feats_list:
    new_feats_list.append(feat)
    if isinstance(feat, nn.Conv2d):
        new_feats_list.append(nn.Dropout(p=0.5, inplace=True))

# modify convolution layers
model.features = nn.Sequential(*new_feats_list)
3 Likes

@aguennecjacq Oh thank you so much. This is so helpful. It worked!