How to add a second classifier to a pre-trained model and train

Hi, I am trying to take a pre-traied ResNet, keep the original classifier, but also add a new classifier (randomly initialized) on top of the last CONV layer such that the CONV layer part of the pre-trained network simultaneously feed both classifiers (original with pre-trained weights and new with random weights). Then train the entire network by adding up the losses.
I don’t know how to add the new classifier to an existing pre-trained network (or basically how to write a model with two classifiers). Can anybody please help me with a hint?
Thanks so much.

Well I copied this from Dilated ResNet but the idea is the same.
1st of all since you are modifiying the architecture, when you train to load pretrained weights it will raise up an error.

def drn_c_26(pretrained=False, **kwargs):
    model = DRN(BasicBlock, [1, 1, 2, 2, 2, 2, 1, 1], arch='C', **kwargs)
    if pretrained:
    return model

This is cos pretrained is looking for the exact architecture. when u use model.load_state_dict set strict=False

For setting both classifiers (I imagine a fully connected with softmax ), you just have to create them in the forward function.

output1 = classifier1(x)
output2 = classifier2(x)
return output1, output2

Then you just have to create a new loss which sums both classifier’s loses (in the main file) and that’s all

1 Like