简体   繁体   中英

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

I'm using the pretrained resnet50 model and I have to use in this project this model with pretrained weights

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()))

which gives:

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

thank you guys for your comments.I solved. so I used my model and load after my weights than its worked correct

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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