本文介绍了CNN:检查输入时出错:预期密度为2维,但数组的形状为(391,605,700,3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取要训练CNN的图像列表.

I'm getting a list of images to train my CNN.

model = Sequential()
model.add(Dense(32, activation='tanh', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

data, labels = ReadImages(TRAIN_DIR) 

# Train the model, iterating on the data in batches of 32 samples
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=32)

但是我遇到了这个错误:

But I faced this error:

'具有形状'+ str(data_shape))

'with shape ' + str(data_shape))

ValueError:检查输入时出错:预期density_1_input具有2维,但数组的形状为(391,605,700,3)

ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (391, 605, 700, 3)

推荐答案

您正在将图像馈送到密集层.使用.flatten()展平图像或使用带有CNN图层的模型.形状(391,605,700,3)表示您有391张尺寸为605x700的图像,具有3个尺寸(rgb).

You are feeding images to the Dense Layer. Either flatten the images using .flatten() or use a model with CNN Layers. The shape (391,605,700,3) means you have 391 images of size 605x700 having 3 dimensions(rgb).

    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(605, 700, 3)))
    model.add(MaxPooling2D((2, 2)))
    model.add(Flatten())
    model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])

链接对基本的CNN有很好的解释.

This link has good explanations for basic CNN.

这篇关于CNN:检查输入时出错:预期密度为2维,但数组的形状为(391,605,700,3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 02:40