简体   繁体   中英

AttributeError: 'Sequential' object has no attribute 'predict_classes'/

Whenever I try run this code, it displays:

 AttributeError: 'Sequential' object has no attribute 'predict_classes'

The first line returns the error:

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)    

I believe the model.predict_classes() has been deprecated. If you use Jupyter Notebook and Tensorflow 2.5.0, you would get a warning like the following:

C:\Anaconda3\envs\tf-gpu-2.5\lib\site-packages\tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01->01. Please use instead:* np.argmax(model.predict(x), axis=-1) , if your >model does multi-class classification (eg if it uses a softmax last->layer activation).* (model.predict(x) > 0.5).astype("int32") , if your >model does binary classification (eg if it uses a sigmoid last-layer >activation). warnings.warn(' model.predict_classes() is deprecated and '

As the warning suggest, please use instead:

  • np.argmax(model.predict(x), axis=-1) , if your model does multi-class classification (eg if it uses a softmax last-layer activation).
  • (model.predict(x) > 0.5).astype("int32") , if your model does binary classification (eg if it uses a sigmoid last-layer activation).

I just upgraded to Tensorflow 2.6.0 with Python 3.9.6, in TF 2.6.0 using model.predict_classes() will straight up showing error.

predict = NN.predict_classes(X_test_NL)
---------------------------------------------------------------------------
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'

If you must use predict_classes() , you would have to roll back to previous version of tensorflow.

Or convert the probabilities you get from using .predict() to class labels.

References: Get Class Labels from predict method in Keras

This function was removed in TensorFlow version 2.6. According to the keras in rstudio reference

update to

predict_x=model.predict(X_test) 
classes_x=np.argmax(predict_x,axis=1)

The answer is from https://stackoverflow.com/users/13094270/xueke

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