ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)` while using Fit_generator

ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)` while using Fit_generator

我一直在尝试使用一组训练和验证图像来教授模型,但我一直遇到标题错误(我将 post 下面是完整的)。 对如何进行有点迷茫,之前关于这个主题的问题没有产生结果。 我的代码片段是:

train_datagen = ImageDataGenerator(
  preprocessing_function=preprocess_input,
  rotation_range=90,
  horizontal_flip=True,
  vertical_flip=True,
)

val_datagen = ImageDataGenerator(
      preprocessing_function=preprocess_input,
      rotation_range=90,
      horizontal_flip=True,
      vertical_flip=True,
    )

train_generator = train_datagen.flow_from_directory(VAL_DIR,
                                                    target_size=(HEIGHT, WIDTH),
                                                    batch_size=TRAIN_BATCH_SIZE,
                                                    class_mode=None,
                                                    shuffle=True)
val_generator = val_datagen.flow_from_directory(TRAIN_DIR,
                                                target_size=(HEIGHT, WIDTH),
                                                batch_size=VAL_BATCH_SIZE,
                                                class_mode=None,
                                                shuffle=True)

然后我尝试使用以下方法教授模型:

history = finetune_model.fit_generator(train_generator,epochs=NUM_EPOCHS, workers=8,
                                   steps_per_epoch=num_train_images // TRAIN_BATCH_SIZE,
                                   validation_data=val_generator,
                                   validation_steps=num_val_images // VAL_BATCH_SIZE,
                                   shuffle=True, callbacks=callbacks_list)

我得到的错误是:

ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: [[[[-5.30867195e+01 -6.81702271e+01  2.66113968e+01]
   [-5.04675522e+01 -6.62993927e+01  2.90434952e+01]
   [-4.78483849e+01 -6.44285583e+01  3.14755783e+01]
   ...

我想要一些指导,因为我是一名初学者 ML 学生。很乐意提供更多信息。 使用的图像采用 jpeg 格式。 我能做什么 ?似乎找不到问题。

修复错误:

Per the documentation,指定 class_mode=None 生成器 只生成批量图像数据,没有目标 (用于 model.predict_generator()).

fit_generator needs a generator 生成 (inputs, targets) 对。所以你不能让你的模型适合你现在使用的生成器,因为这些生成器没有说明模型应该适合什么目标。您需要弄清楚您的标签是什么,然后选择合适的 class_mode 以便数据生成器包含这些标签。

确保错误已修复:

一旦您选择了正确的 class_mode,您可以 printing/visualizing 批量检查数据生成器1:

在这个例子中,我正在做多类(兔子、猫、狗)分类,所以默认的 class_mode=categorical 可以正常工作。

获取一批图像和标签后,该批次中的第一张图像是狗,该批次的第一个标签是[0, 0, 1]第二 位置,从零开始计数),class_indices 字典表示狗图像具有标签 2(从零开始计数)。

1.通常,始终仔细检查此处的数据是个好主意,即使数据生成器似乎在工作;请参阅 this post,在 "visualize just before the net" 下。