繁体   English   中英

预训练的 model 错误? 'collections.OrderedDict' object 没有属性 'eval'

[英]pretrained model error ? 'collections.OrderedDict' object has no attribute 'eval'

我正在使用预训练的 resnet50 model 并且我必须在这个项目中使用这个带有预训练权重的 model

original_model=torch.load('model_best.pth',map_location='cpu')
    
original_model.eval()
preproc_img = torch.FloatTensor(preproc_img)
    
out = original_model(preproc_img)
    
print("\nPyTorch model prediction: \n")
print("* shape: ", out.shape)

test_class_id = torch.argmax(out, axis=1).item()
print("* class ID: {}, label: {}".format(test_class_id, test_labels[test_class_id]))
 
confidence = out[0][test_class_id]
print("* confidence: {:.4f}".format(confidence.item()))

这使:

AttributeError: 'collections.OrderedDict' object has no attribute 'eval'

谢谢你们的意见。我解决了。 所以我使用了我的 model 并在我的重量之后加载而不是正常工作

model = models.resnet50(pretrained = True)
model.fc = nn.Linear(in_features=2048, out_features=2, bias=True)
weights = torch.load('model_best.pth',map_location ='cpu')
model.load_state_dict(weights)
model.eval()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM