Replace resnet 50 fc with mlp

Hy guys, I must do “features sharing”. I must pass the features of resnet 50 (2048) to a MLP

from torchvsion.models import resnet50
class pipo(nn.module):
      def __init__(self,n_input, n_output, n_hidden):
            super(pipo, self).__init__()
            self.model = resnet50(pretrained = True)
            self.model.fc = nn.Linear(self.model.fc.in_features, n_input)
            self.fc = nn.Linear(self.model.fc, n_hidden)
   = nn.Linear(n_hidden, n_output)
     def forward(self, x):

is it right in this way?

This looks generally good.
It might be a typo, but self.fc should most likely be defined as:

self.fc = nn.Linear(self.model.fc.out_features, n_hidden)

Can you clarify me the difference between …fc.in_features() and fc.out_features()?

The in_features define the input features of your tensor for this linear layer, while out_features define the output features.
E.g. if you are working with 2 input features for each sample (let’s say height and weight for a dog classifier), you would define in_features=2 in your first linear layer. The number of output features depends on your architecture and you could chose any valid value, which “works”.
Have a look at CS231n to get an example of the weight matrix and the mentioned names.

1 Like