【问题标题】:keras: ValueError: Input 0 is incompatible with layer convolution2d_11: expected ndim=4, found ndim=2keras: ValueError: Input 0 is in compatible with layer convolution2d_11: expected ndim=4, found ndim=2
【发布时间】:2017-07-28 04:15:58
【问题描述】:
    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 进行图像分类,但出现此错误 根据 do docs [https://keras.io/layers/convolutional/][1] 我的回答是正确的,但我不知道为什么会出现此错误

  • 我应该使用 (None, 32,32,3) 维度

【问题讨论】:

  • 你如何使用你的脚本?

标签: deep-learning convolution keras-layer


【解决方案1】:

Conv2D 层需要 4 个维度的输入,但显然,您只给出 2 个维度。但我相信您已经注意到这一点。

根据adventuresinmachinelearning

要提供的数据格式是[i, j, k, l],其中i是训练样本的数量,j是图像的高度,k是权重,l是通道数。

我不熟悉您使用的数据,但 l(通道号)的值应该是:

[对于一个]灰度图像,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

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 2018-05-19
    • 2019-01-03
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多