model.add(Convolution2D(64, 3, 3, border_mode='same', input_shape=(32, 32, 3)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
这是错误
ValueError Traceback (most recent call last)
<ipython-input-21-a60216c72b54> in <module>()
----> 1 model.add(Convolution2D(64, 3, 3, border_mode='same', input_shape=(3, 32, 32)))
2 model.add(Activation('relu'))
3 model.add(Convolution2D(32, 3, 3))
4 model.add(Activation('relu'))
5 model.add(MaxPooling2D(pool_size=(2, 2)))
/home/pranshu_44/anaconda3/lib/python3.5/site-packages/keras/models.py in add(self, layer)
330 output_shapes=[self.outputs[0]._keras_shape])
331 else:
--> 332 output_tensor = layer(self.outputs[0])
333 if isinstance(output_tensor, list):
334 raise TypeError('All layers in a Sequential model '
/home/pranshu_44/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, x, mask)
527 # Raise exceptions in case the input is not compatible
528 # with the input_spec specified in the layer constructor.
--> 529 self.assert_input_compatibility(x)
530
531 # Collect input shapes to build layer.
/home/pranshu_44/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, input)
467 self.name + ': expected ndim=' +
468 str(spec.ndim) + ', found ndim=' +
--> 469 str(K.ndim(x)))
470 if spec.dtype is not None:
471 if K.dtype(x) != spec.dtype:
ValueError: Input 0 is incompatible with layer convolution2d_11: expected ndim=4, found ndim=2
我正在尝试对cifar 10进行图像分类,但我收到了这个错误 根据做文档[https://keras.io/layers/convolutional/][1]我的回答是正确的,但我不知道为什么我得到这个错误
答案 0 :(得分:0)
Conv2D图层需要输入4个维度,但显然,您仅输入2。但是我敢肯定,您已经注意到了这一点。
根据adventuresinmachinelearning:
要提供的数据格式为[i,j,k,l],其中i是训练样本的数量,j是图像的高度,k是权重,l是频道号。 / p>
我不熟悉您使用的数据,但是l(通道号)的值应为:
[对于a]灰度图像,l将始终等于1(如果我们有RGB图像,则它将等于3)
所以基本上您只需要:
import tensorflow as tf
tf.reshape(your_image_tensor, [-1, 28, 28, 1]) #For a grayscale image
tf.reshape(your_image_tensor, [-1, 28, 28, 3]) #For a RGB image
对您自己的代码进行适当的更改。如果您不想使用tensorflow,建议您阅读this
更新: 您还可以使用numpy.reshape()重塑数组 有关更多信息:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html