import cv2
import torch
# Load the model.
best_model = torch.load("average_model.pth", map_location=torch.device('cpu'))
# Capture video from webcam.
cap = cv2.VideoCapture(0)
while True:
# Capture a frame from the camera.
ret, frame = cap.read()
# Convert the frame to RGB.
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Predict handsign on frame.
output = best_model(torch.from_numpy(frame).float().unsqueeze(0))
# Get the predicted class.
predicted_class = output.argmax()
# Display the frame on the screen.
cv2.imshow("Camera", frame)
# Press Q to quit.
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera.
cap.release()
# Close the window.
cv2.destroyAllWindows()
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[14], line 18
15 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
17 # Predict handsign on frame.
---> 18 output = best_model(torch.from_numpy(frame).float().unsqueeze(0))
20 # Get the predicted class.
21 predicted_class = output.argmax()
TypeError: 'dict' object is not callable
我试图建立一个yolo-nas模型,但当我在colab工作时,这个工作正常,但在我的文件jupyter笔记本
1条答案
按热度按时间pxiryf3j1#
保存的模型实际上是一个
state_dict
对象,其中包含模型层中的权重状态。因此,加载的对象自然会充当dict并给出错误。要解决这个问题,请更改加载代码(第4、5行)。创建/导入模型结构,然后将状态字典加载到其中。寻找
"average_model.pth"
来自哪里的线索。请参阅torch docs了解有关加载模型的更多详细信息。