Python Keras module 'keras.backend' has no attribute 'image_data_format'


問題:

當使用Keras運行示例程序mnist_cnn時,出現如下錯誤: 'keras.backend' has no attribute 'image_data_format'

程序路徑https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py

使用的python conda環境是udacity自動駕駛課程的carnd-term1

 

故障程序段:

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

完整代碼見 https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py

 

故障分析:

conda環境中的Keras版本比例子程序中的版本舊,因此沒有'image_data_format'這個變量

 

解決方法:

以下兩種方法,任選其一

1)如果不升級Keras版本

將 K.image_data_format() == 'channels_first' 替換為 K.image_dim_ordering() == 'th'

 

2)升級Keras版本到最新

 > activate carnd-term1 //激活你的conda環境,我的這個環境叫做carnd-term1

 (carnd-term1)> conda list //顯示當前環境中安裝的包

 (carnd-term1)> pip uninstall Keras //卸載舊版本的Keras, Keras是使用pip方式安裝的,因此卸載和重裝都要使用pip相關命令

 (carnd-term1)> pip install Keras //重新安裝新版本的Keras

 (carnd-term1)> conda list //檢查版本是否已經更新

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM