2017-07-24 59 views
0

我碰上以下錯誤,當我運行keras示例腳本:錯誤「:預期輸入是圖像(numpy的陣列)以下的數據格式公約‘channels_first’」在Keras圖像例

C:\Users\johnd\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\preprocessing\image.py:653: UserWarning: Expected input to be images (as Numpy array) following the data format convention "channels_first" (channels on axis 1), i.e. expected either 1, 3 or 4 channels on axis 1. However, it was passed an array with shape (60000, 1, 28, 28) (1 channels). 
    ' (' + str(x.shape[self.channel_axis]) + ' channels).') 

這是我的腳本:

from keras.datasets import mnist 
from keras.preprocessing.image import ImageDataGenerator 
from matplotlib import pyplot 
from keras import backend as K 
K.set_image_dim_ordering('th') 
# load data 
(X_train, y_train), (X_test, y_test) = mnist.load_data() 
# reshape to be [samples][pixels][width][height] 
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28) 
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28) 
# convert from int to float 
X_train = X_train.astype('float32') 
X_test = X_test.astype('float32') 
# define data preparation 
datagen = ImageDataGenerator(zca_whitening=True) 
# fit parameters from data 
datagen.fit(X_train) 
# configure batch size and retrieve one batch of images 
for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9): 
    # create a grid of 3x3 images 
    for i in range(0, 9): 
     pyplot.subplot(330 + 1 + i) 
     pyplot.imshow(X_batch[i].reshape(28, 28), cmap=pyplot.get_cmap('gray')) 
    # show the plot 
    pyplot.show() 
    break 

你能告訴我什麼是錯的嗎?

謝謝。

+0

什麼是您的後端(Tensorflow/Theano)和Keras版本? – desertnaut

+0

確實,這是一個修正了最新版本的問題 - 請參閱我的回答下面的更新 – desertnaut

+0

不滿意答案? – desertnaut

回答

0

這不是一個錯誤,而是一個警告 - 我想你沒有提到其餘代碼運行正常;這裏是我運行腳本後獲得:用(60000, 1, 28, 28)一個X_train.shape和類似的警告

enter image description here

/var/venv/tsats/local/lib/python2.7/site-packages/keras/preprocessing/image.py:648: 
UserWarning: Expected input to be images (as Numpy array) following the data format 
convention "channels_first" (channels on axis 1), i.e. expected either 1, 3 or 4 channels on 
axis 1. However, it was passed an array with shape (60000, 1, 28, 28) (1 channels). ' (' + str(x.shape[self.channel_axis]) + ' channels).') 

關閉該警告的檢查結果顯示,這是荒謬的:它要求的是通道在軸1上(它們是),並且它們在{1, 3, 4}中有一個值(它們的有效值爲1)。顯然這是一個Keras的bug - 我打開了一個issue - 但它不會影響其他代碼和結果。

UPDATE:根據Keras Github中的response問題,這現在已經在master中修復了(儘管還沒有測試過自己)。