Python Keras ValueError:输入 0 与层 conv2d_1 不兼容:预期 ndim=4,发现 ndim=5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47665391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:18:54  来源:igfitidea点击:

Keras ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

pythontensorflowdeep-learningkerasconv-neural-network

提问by Lucky

I have checked all the solutions, but still, I am facing the same error. My training images shape is (26721, 32, 32, 1), which I believe it is 4 dimension, but I don't know why error shows it is 5 dimension.

我已经检查了所有解决方案,但仍然面临同样的错误。我的训练图像形状是(26721, 32, 32, 1),我相信它是 4 维,但我不知道为什么错误显示它是 5 维。

 model = Sequential()

 model.add(Convolution2D(16, 5, 5, border_mode='same', input_shape= input_shape ))

So this is how I am defining model.fit_generator

所以这就是我的定义 model.fit_generator

model.fit_generator(train_dataset, train_labels, nb_epoch=epochs, verbose=1,validation_data=(valid_dataset, valid_labels), nb_val_samples=valid_dataset.shape[0],callbacks=model_callbacks)

回答by Daniel M?ller

The problem is input_shape.

问题是input_shape

It should actually contain 3 dimensions only. And internally keras will add the batch dimension making it 4.

它实际上应该只包含 3 个维度。并且在内部 keras 将添加批处理维度使其成为 4。

Since you probably used input_shapewith 4 dimensions (batch included), keras is adding the 5th.

由于您可能使用input_shape了 4 个维度(包括批次),因此 keras 将添加第 5 个维度。

You should use input_shape=(32,32,1).

你应该使用input_shape=(32,32,1).

回答by SANDEEP KUMAR H

The problem is with input_shape. Try adding an extra dimension/channel for letting keras know that you are working on a grayscale image ie -->1

问题在于input_shape. 尝试添加额外的维度/通道,让 keras 知道您正在处理灰度图像,即 -->1

input_shape= (56,56,1). Probably if you are using a normal Deep learning model then it won't raise an issue but for Convnet it does.

input_shape= (56,56,1). 可能如果您使用的是普通的深度学习模型,那么它不会引发问题,但对于 Convnet 来说却是。

回答by Akash Desai

For reshape the data we need to add fourth dimensions i.e changing from (6000,28,28)to (6000,28,28,1)

为了重塑数据,我们需要添加第四维,即从(6000,28,28)(6000,28,28,1)

My code is:

我的代码是:

img_rows=x_train[0].shape[0]
img_cols=x_test[0].shape[1]

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,**).  *->  I forgot to put 1 here.

I have face the same problem

我遇到了同样的问题

Input 0 is incompatible with layer conv2d_4 : except ndim=4 ,found ndim=3

Input 0 is incompatible with layer conv2d_4 : except ndim=4 ,found ndim=3

I solved this problem by simply putting value in the input shape

我通过简单地在输入形状中放置值来解决这个问题

Input_shape=(img_rows,img_cols,1)#store the shape of single image.

With this problem is solved

有了这个问题就解决了

回答by dgamer

Here you need to check the "channels_first"whenever CNN is used as 2d,Also reshape your train_data and test data as:

在这里,无论何时将 CNN 用作 2d,您都需要检查“channels_first”,还将您的 train_data 和测试数据重塑为:

if K.image_data_format() == 'channels_first':   #check for channels_first
 train_img.reshape(train_img.shape[0],1,x,x)
 Input_shape=(1,x,x)                            #In your case x is 32
else:
 train_img.reshape(train_img.shape[0],x,x,1)
 Input_shape=(x,x,1)

回答by Akash Desai

I have faced the same problem

我遇到了同样的问题

Input 0 is incompatible with layer conv2d_4 : except ndim=4 ,found ndim=3

输入 0 与层 conv2d_4 不兼容:ndim=4 除外,发现 ndim=3

I solved this problem by simply putting value in the input shape

我通过简单地在输入形状中放置值来解决这个问题

Input_shape=(img_rows,img_cols,1)#store the shape of single image. .. & the problem is solved