Keras ValueError: fit_generator yielding a list

Keras ValueError: fit_generator yielding a list

我正在学习如何使用带有 TF 后端的 Keras 进行图像识别,所以我仍然不确定我在这里做错了什么。

我正在尝试堆叠 2 个模型,一个是 VGG16,另一个是我随机制作的模型,只是为了学习如何堆叠它。我想将图像分类为 5 类.

问题出在最后一部分,当我运行 fit_generator。它不是产生一个有效的元组,而是产生一个看起来像列表的东西。我见过很多人遇到类似的问题,但在他们的情况下,输出是 None,所以我不确定解决方案是否相同。

参数

nb_train_samples = 576
nb_validation_samples = 144
epochs = 30
batch_size = 12
img_width, img_height = 150, 150

发电机

train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=50,
    width_shift_range=0.3,
    height_shift_range=0.3,
    shear_range=0.4,
    zoom_range=0.4,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode=None,
    shuffle=False)

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode=None,
    shuffle=False)

我的模型

input = Input(batch_shape=model.output_shape)
x = Flatten()(input)
x = Dense(256, activation='relu', name="new_block_1")(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu', name="new_block_2")(x)
x = Dropout(0.5)(x)
x = Dense(5, activation='softmax', name="new_block_3")(x)
top_model = Model(input,x)

input = Input(shape=(img_width, img_height, 3))
x = model(input)
x = top_model(x)
final_model = Model(input, x)

final_model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy', metrics=['accuracy'])

拟合与误差

final_model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

ValueError: output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: [[[[ 0.89411771  0.89019614  0.87450987]
   [ 0.89411771  0.89019614  0.87450987]
   [ 0.89411771  0.89019614  0.87450987]
   ..., 

更新 1: 根据 @petezurich 的提示,将激活函数从 'sigmoid' 更改为 'softmax'

您的模型缺少训练标签。

只需将生成器中的 class_mode 设置为 categorical,然后将图像放入每个 class 的子文件夹中。生成器然后很好地从中导出 class 标签。