Error(s) in loading state_dict for DenseNet:


I would like to load the weights of the densenet121 network like that :

net = models.densenet121(pretrained=False)

it return me this error :
RuntimeError: Error(s) in loading state_dict for DenseNet:
Missing key(s) in state_dict: “features.denseblock1.denselayer1.norm1.weight”, “features.denseblock1.denselayer1.norm1.bias”, “features.denseblock1.denselayer1.norm1.running_mean”, “features.denseblock1.denselayer1.norm1.running_var”, “features.denseblock1.denselayer1.conv1.weight”, “features.denseblock1.denselayer1.norm2.weight”, “features.denseblock1.denselayer1.norm2.bias”, “features.denseblock1.denselayer1.norm2.running_mean”, “features.denseblock1.denselayer1.norm2.running_var”, “features.denseblock1.denselayer1.conv2.weight”

I use this method instead of :
net = models.densenet121(pretrained=True)
because the proxy of my company don’t allow to do it…

Could someone help me ?

Thank you

I used to load model’s weights like this:

net = models.densenet121(pretrained=False).cuda()

1 Like

Thank you, you answer guided me to look at why my network don’t know state dict so is decide to use the constructor code from pytorch densenet and it works :
import re

Code modified from torchvision densenet source for loading from pre .4 densenet weights.

checkpoint = torch.load(’./model.pth.tar’)
state_dict = checkpoint[‘state_dict’]
remove_data_parallel = False # Change if you don’t want to use nn.DataParallel(model)

pattern = re.compile(
for key in list(state_dict.keys()):
match = pattern.match(key)
new_key = + if match else key
new_key = new_key[7:] if remove_data_parallel else new_key
state_dict[new_key] = state_dict[key]
# Delete old key only if modified.
if match or remove_data_parallel:
del state_dict[key]

1 Like