2017-07-24 56 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中修复了(尽管还没有测试过自己)。