1 python opencv machine-learning
每当我尝试运行此代码时,它都会显示:
AttributeError: 'Sequential' object has no attribute 'predict_classes'
Run Code Online (Sandbox Code Playgroud)
第一行返回错误:
result = str(model.predict_classes(roi, 1, verbose = 0)[0])
cv2.putText(copy, getLetter(result), (300 , 100), cv2.FONT_HERSHEY_COMPLEX, 2, (0, 255, 0), 2)
cv2.imshow('frame', copy)
Run Code Online (Sandbox Code Playgroud)
我相信model.predict_classes()
已被弃用。如果您使用 Jupyter Notebook 和 Tensorflow 2.5.0,您将收到如下警告:
C:\Anaconda3\envs\tf-gpu-2.5\lib\site-packages\tensorflow\python\keras\engine\sequential.py:455: UserWarning:
model.predict_classes()
已弃用,并将在 2021-01->01 后删除。请改用:*np.argmax(model.predict(x), axis=-1)
,如果您的 >模型进行多类分类(例如,如果它使用softmax
最后一层激活)。*(model.predict(x) > 0.5).astype("int32")
,如果您的 >模型进行二元分类(例如,如果它使用sigmoid
最后一层>激活)。warnings.warn('model.predict_classes()
已弃用并且 '
正如警告所建议的,请改用:
np.argmax(model.predict(x), axis=-1)
,如果您的模型进行多类分类(例如,如果它使用softmax
最后一层激活)。(model.predict(x) > 0.5).astype("int32")
,如果您的模型进行二元分类(例如,如果它使用sigmoid
最后一层激活)。我刚刚使用 Python 3.9.6 升级到 Tensorflow 2.6.0,在 TF 2.6.0 中使用model.predict_classes()
将直接显示错误。
predict = NN.predict_classes(X_test_NL)
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-f1898c8da755> in <module>
----> 1 predict = NN.predict_classes(X_test_NL)
AttributeError: 'Sequential' object has no attribute 'predict_classes'
Run Code Online (Sandbox Code Playgroud)
如果必须使用predict_classes()
,则必须回滚到以前版本的tensorflow。
或者将您从使用中获得的概率转换.predict()
为类标签。
参考文献:从 Keras 中的预测方法获取类标签