Keras fit_generator输入形状错误

时间:2019-11-22 23:39:37

标签: machine-learning keras tf.keras keras-2

我正在使用ImageDataGenerator将成批图像输入到神经网络,但是无法找到正确的方式来馈送图像。运行以下命令:

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)   

结果:

  

检查目标时出错:预期conv2d_198具有4个维度,   但是得到了形状为(14,1)的数组

似乎将批次用作输入张量,因为除去除输入层以外的所有层会导致类似的错误。如何正确将它们输入网络?

1 个答案:

答案 0 :(得分:0)

基本上Keras希望您传递输入的维数和行数。看起来您正在传递具有二维的数组。您能确定传递的是(-1,维度1,维度2,通道)吗?您可能需要使用重塑。 -1应该告诉Keras推断行数/观测值。对Keras来说,我还很陌生,所以我敢肯定其他人会提供更好的答案,但您也许可以做到。.myinputarray.reshape()