简体   繁体   English

Keras fit_generator 不正确的输入形状

[英]Keras fit_generator incorrect input shapes

I'm using an ImageDataGenerator to input batches of images to a neural network, but can't work out the correct way to feed it.我正在使用 ImageDataGenerator 将成批的图像输入到神经网络中,但无法找出正确的方法来提供它。 Running the following:运行以下内容:

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('/home/Training', target_size=(256,256), batch_size=32, class_mode='binary', color_mode = 'grayscale')

test_set = test_datagen.flow_from_directory('/home/Test', target_size=(256,256), batch_size=32, class_mode='binary',color_mode = 'grayscale' )


input_size = (256, 256, 1)
inputs = Input(input_size)
conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
conv2 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
conv3 = Conv2D(1, 1, activation = 'sigmoid')(conv2)
model1 = Model(inputs = inputs, outputs = conv3)

model1.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy'])

model1.fit_generator(training_set, steps_per_epoch=160, epochs=10, validation_data=test_set, validation_steps=800)   

Results in:结果是:

Error when checking target: expected conv2d_198 to have 4 dimensions, but got array with shape (14, 1)检查目标时出错:预期 conv2d_198 有 4 个维度,但得到形状为 (14, 1) 的数组

It seems to use the batches as the input tensor, since removing all layers but the input layer results in a similar error.似乎使用批次作为输入张量,因为删除所有层但输入层会导致类似的错误。 How can I correctly input them into the network?我怎样才能正确地将它们输入到网络中?

Basically Keras is expecting your to pass your input dimensions and rows.基本上 Keras 期望您传递您的输入维度和行。 Looks like you are passing an array with two dimensions.看起来您正在传递一个二维数组。 Can you make sure you are passing something like (-1, dimension 1, dimension 2, channels) ?你能确定你正在传递类似 (-1, 维度 1, 维度 2, 通道) 的东西吗? you may need to use reshape.您可能需要使用重塑。 The -1 should tell Keras to infer the rows/observations. -1 应该告诉 Keras 推断行/观察。 Im pretty new to Keras so I am sure someone else will have a better answer but you might be able to just do.. myinputarray.reshape()我对 Keras 很陌生,所以我相信其他人会有更好的答案,但您可能只能这样做.. myinputarray.reshape()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM