Adding dropout layer in pretrained resnet3D

I want to add drop out layer before final layer of pretrained resnet3D model, can someone please verify my approach, am I doing it correctly?

model = torchvision.models.video.r3d_18(pretrained=True)
model.fc = nn.Sequential(
     nn.Dropout(0.5),
     nn.Linear(model.fc.in_features, num_classes)
)

Thankyou.

Hi,

Yes, you are managing it correctly. Just for clarification, this is the two last layer of the model:

(avgpool): AdaptiveAvgPool3d(output_size=(1, 1, 1))
(fc): Linear(in_features=512, out_features=400, bias=True)

So by your code it will be converted to:

(avgpool): AdaptiveAvgPool3d(output_size=(1, 1, 1))
(fc): Sequential(
    (0): Dropout(p=0.5, inplace=False)
    (1): Linear(in_features=512, out_features=#num_classes, bias=True)
  )

Bests

2 Likes

ahh got it, thanks! :slight_smile: