Using an trained model to finetune, and error occured when loading dict

Two model:Net_D,Net_C
Net_D

class Net_D(nn.Module):
    def __init__(self):
        super(Net_D, self).__init__()

        model = models.resnet50(pretrained=False)
        model.fc = nn.Linear(hidden_dim, num_D_class)
        model = remove_last(model)
        self.resnet = model
........

class Net_C(nn.Module):
def init(self):
super(Net_C, self).init()

    model = self.load_state_dict(torch.load("best.mdl")
    model.fc = nn.Linear(hidden_dim, num_C_class)
    model = remove_last(model)

    self.resnet = model


Two training code: train_D.py, train_C.py
part code for train_D.py
 torch.save(model.state_dict(), 'best.mdl')

part code for train_C.py
    model = Net_C()
    model.to(device)
    print('Model is built ...')
。。。。。。

I run the train_D.py successfuly and I got the best.mdl file.
But when I try to run train_C.py to pretrain the best model, I got the following error info:
RuntimeError: Error(s) in loading state_dict for Fusion_Net_C:
	Unexpected key(s) in state_dict: "resnet.0.weight", "resnet.1.weight", "resnet.1.bias",......
But I found there are actually such keys above.
Any help, thanks.

Your Net_C doesn’t seem to initialize the resnet50 model, which was used in Net_D.
Could you explain your use case a bit, e.g. why you need a new model architecture?

Thank you very much for your reply.
My work is to train two tasks, which are related but different. In fact,I can say the relation between the two tasks is hierarchical. I planed to train taskone( which is task D) firstly and save the model, and then do some modification to the model D to train task two(which is task C).

and the code
“self.resnet = model” is what I try to init the resnet.

The state_dict will contain all parameters and buffers of the model.
In the case of Net_D it will contain the parameters/buffers of the internal resnet50 as well as your custom linear layer. I’m not sure, what remove_last does, but if it removes the last linear layer, your custom one will most likely be gone.

In Net_C you are calling self.load_state_dict immediately in the __init__ method, which won’t work, as your model does not have any parameters or buffers yet.
If you just want to initialize the linear layer and load its state_dict, you could use this approach.