Get features of pretrained model when forwarding

I use resnet50 and change fc layer to classifying, like this:
resnet50.fc = nn.Sequential(
nn.Linear(fc_inputs, 256),
nn.ReLU(),
nn.Dropout(0.4),
nn.Linear(256, num_classes),
nn.LogSoftmax(dim=1)
)
I want to get both outputs and the features of nn.Linear(fc_inputs, 256) when forwarding, it looks like:
features, outputs = resnet50(data)
Thanks for all your supporting. (Sorry for my bad English)

I think it would be easier to not use the Sequential API in this case, but rather define each layer in the .__init__() method of your ResNet network, and call them in order in the .forward() method, this way you can have intermediate variables for features and outputs.

1 Like

Thanks for your suggestion, it work for me.

1 Like