-1

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'
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Muratali016
  • 13
  • 2
  • 8
  • Did you read the pytorch [documentation](https://pytorch.org/tutorials/beginner/basics/saveloadrun_tutorial.html)? – Michael Szczesny Dec 19 '21 at 19:31
  • i did it sir , i wrote ,,original_model = models.resnet50(), original_model.load_state_dict(torch.load('model_best.pth',map_location='cpu')),original_model.eval(), than gives me RuntimeError: Error(s) in loading state_dict for ResNet: size mismatch for fc.weight: copying a param with shape torch.Size([2, 2048]) from checkpoint, the shape in current model is torch.Size([1000, 2048]). size mismatch for fc.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([1000]). – Muratali016 Dec 19 '21 at 19:50
  • @MichaelSzczesny but I already used the same model with onnx and predicted with OpenCV and it works with opencv without problem – Muratali016 Dec 19 '21 at 19:51
  • Does this answer your question? https://stackoverflow.com/questions/49941426/attributeerror-collections-ordereddict-object-has-no-attribute-eval – kkgarg Dec 19 '21 at 21:12
  • 1
    Please do *not* post code in the comments (it is literally undreadable), post the full error trace, and remove any code that comes *after* the error - see how to create a [mre]. – desertnaut Dec 19 '21 at 21:18
  • @kkgarg i tried almost every solutions from stack overflow .I got : RuntimeError: Error(s) in loading state_dict for original_model: Unexpected key(s) in state_dict: "conv1.weight", "bn1.weight", "bn1.bias", "bn1.running_mean", "bn1.running_var"........ – Muratali016 Dec 20 '21 at 10:06
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 27 '21 at 10:19

1 Answers1

0

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()
Muratali016
  • 13
  • 2
  • 8