在Resnet等网络加载权重时出现权重位置与网络位置不匹配导致报错,与系统、pytorch版本有关,报错如图
可以尝试更改运行环境也可以如下将加载权重位置直接改到cpu,再将加载完的模型放进gpu
# 原始加载权重代码
model_weight_path = "./resnet34-pre.pth"
assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
net = resnet34(num_classes=5)
pre_weights = torch.load(model_weight_path, map_location=device)
# 删除map_location即可
model_weight_path = "./resnet34-pre.pth"
assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
net = resnet34(num_classes=5)
pre_weights = torch.load(model_weight_path)
net.to(device)
修改完可以正常运行



