PyCIL icon indicating copy to clipboard operation
PyCIL copied to clipboard

Cannot run pre-trained ResNet18. RuntimeError: Error(s) in loading state_dict for ResNet: Missing key(s) in state_dict

Open ElifCerenGokYildirim opened this issue 1 year ago • 1 comments

Hi, I am trying to run pre-trained ResNet18 with CIFAR100 and FOSTER method but I am getting this specific error: RuntimeError: Error(s) in loading state_dict for ResNet: Missing key(s) in state_dict: "conv1.0.weight", "conv1.1.weight", "conv1.1.bias", "conv1.1.running_mean", "conv1.1.running_var". Unexpected key(s) in state_dict: "bn1.running_mean", "bn1.running_var", "bn1.weight", "bn1.bias", "fc.weight", "fc.bias", "conv1.weight". I can run ResNet18 from scratch. Here is the full track of error: ` Traceback (most recent call last): File "C:\PycharmProjects\CL\main.py", line 31, in main()

File "C:\PycharmProjects\CL\main.py", line 12, in main train(args)

File "C:\PycharmProjects\CL\trainer.py", line 18, in train _train(args)

File "C:\PycharmProjects\CL\trainer.py", line 65, in _train model.incremental_train(data_manager)

File "C:\PycharmProjects\CL\models\foster.py", line 46, in incremental_train self._network.update_fc(self._total_classes)

File "C:\PycharmProjects\CL\utils\inc_net.py", line 524, in update_fc self.convnets.append(get_convnet(self.args))

File "C:\PycharmProjects\CL\utils\inc_net.py", line 26, in get_convnet return resnet18(pretrained=pretrained,args=args)

File "C:\PycharmProjects\CL\convs\resnet.py", line 293, in resnet18 return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,

File "C:\PycharmProjects\CL\convs\resnet.py", line 269, in _resnet model.load_state_dict(state_dict)

File "C:\Anaconda3\envs\daddy\lib\site-packages\torch\nn\modules\module.py", line 1497, in load_state_dict raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(

RuntimeError: Error(s) in loading state_dict for ResNet:

    Missing key(s) in state_dict: "conv1.0.weight", "conv1.1.weight", "conv1.1.bias", "conv1.1.running_mean", "conv1.1.running_var".
    Unexpected key(s) in state_dict: "bn1.running_mean", "bn1.running_var", "bn1.weight", "bn1.bias", "fc.weight", "fc.bias", "conv1.weight".`
    

If you could help me with this, I would be very happy. Thank you so much.

ElifCerenGokYildirim avatar Feb 19 '24 16:02 ElifCerenGokYildirim

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

caoshuai888 avatar Feb 19 '24 16:02 caoshuai888

Hi, the problem is caused by the mismatch of the pre-trained weight and the defined model. Here are two solutions.

  1. Use definitely the same network structure as the pre-trained weight and redefine the conv backbone.
  2. When loading the weights from state_dict, choose "strict=False"

zhoudw-zdw avatar Mar 19 '24 06:03 zhoudw-zdw