How to add dropout to a squeezenet?

Here’s what I normally do:

    model = models.inception_v3(pretrained=True)
    num_ftrs = model.fc.in_features
    model.fc = nn.Sequential(
      nn.Dropout(0.5),
      nn.Linear(num_ftrs, 5))

However this does not work at all with the squeezenet, it does not have an in_features

Hi,

Yes, because squeezenet does not use Linear in its definition so there is no need to define the structure like ResNet or Inception. But you can get the structure of model using print(model) and extract layers that you want.
Based on your example code, I think you looking for dropping final classifier and train your own linear classifier for it.

Original classfier of squeezenet:

(classifier): Sequential(
    (0): Dropout(p=0.5, inplace=False)
    (1): Conv2d(512, 1000, kernel_size=(1, 1), stride=(1, 1))
    (2): ReLU(inplace=True)
    (3): AdaptiveAvgPool2d(output_size=(1, 1))
  )

To change it the way you want, this snippet may help, although you can modify it in anyway you like:

x = torch.randn(1, 3, 512, 512)

model = torchvision.models.squeezenet.squeezenet1_0(pretrained=True)
model.classifier = nn.Sequential(
    nn.AdaptiveAvgPool2d(output_size=(1, 1)),
    nn.Dropout(0.5),
    nn.Flatten(),
    nn.Linear(in_features=512, out_features=5),
    )
model(x)

Bests

Works great thank you!