KeyError: 'unexpected key "module.densenet121.features.denseblock1.denselayer1.norm.1.weight" in state_dict

I am using the generic saved model DenseNet121 (file name: model.pth.tar).

While loading the above model I am getting the following error:

KeyError: ‘unexpected key “module.densenet121.features.denseblock1.denselayer1.norm.1.weight” in state_dict’

Complete Error:
File “/usr/local/lib/python3.6/site-packages/torch/nn/modules/”, line 522, in load_state_dict
KeyError: ‘unexpected key “module.densenet121.features.denseblock1.denselayer1.norm.1.weight” in state_dict’

The loading part of the code:
CKPT_PATH = ‘model.pth.tar’

cudnn.benchmark = True

model = DenseNet121(N_CLASSES).cuda()
model = torch.nn.DataParallel(model).cuda()

if os.path.isfile(CKPT_PATH):
    print("Loading checkpoint...")
    checkpoint = torch.load(CKPT_PATH)
    print("Checkpoint Loaded Successfully.")
    print("Checkpoint Not found.")

NOTE: I’ve tried to remove the ‘module.’ suggestion from another thread but did not help.

Please suggest.

I guess you should try to rename the variable model as densenet121. As far as I can see if the state dict contains module.childrens it should say module.model.childrens instead of module.densenet121

This indicates they called the variable densenet121