【问题标题】:How to incorporate validation data in Keras's datagen.flow?如何在 Keras 的 datagen.flow 中合并验证数据?
【发布时间】:2018-07-26 23:14:42
【问题描述】:

这是我在之前的 post 中遇到的问题的扩展。

我在 Keras 中应用以下代码进行数据增强(我暂时不想使用model.fit_generator,所以我使用datagen.flow 手动循环它)。

datagen = ImageDataGenerator(
    featurewise_center=False,
    featurewise_std_normalization=False,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)

# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(x_train)


# alternative to model.fit_generator
for e in range(epochs):
    print('Epoch', e)
    batches = 0
    for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
        model.fit(x_batch, y_batch)
        batches += 1
        if batches >= len(x_train) / 32:
            # we need to break the loop by hand because
            # the generator loops indefinitely
            break

我想将验证数据合并到我正在运行的model.fit 循环中。因此,例如,我想在 for 循环中将 model.fit(X_batch,y_batch) 替换为类似于 model.fit(X_batch,y_batch, validation_data=(x_val, y_val)) 的内容。

我对如何在 for 循环中使用 datagen.flow 合并此验证组件感到有些困惑。欢迎任何关于我应该如何进行的见解。

【问题讨论】:

  • 你可以在 fit 之后调用 model.evaluate(x_val, y_val) 然后你得到损失和你添加的所有度量值。见evaluate()
  • 如果我不做任何数据标准化,是否需要datagen.fit?
  • 是的,否则你不会训练你的模型
  • @TobiasScheck - datagen.fit(some_data) 预先计算一些标准化和白化所需的指标。如果您稍后不执行任何这些操作,则不需要将数据拟合到数据生成器。您可以阅读the docstring 了解fit 方法。
  • 对不起,我正在阅读 model.fit。

标签: python machine-learning deep-learning keras


【解决方案1】:

我假设您已经将数据拆分为训练集和验证集。如果没有,您将不得不为以下建议这样做。

您可以使用验证数据创建第二个数据生成器,然后只需将iterate over this generator at the same time 用作训练数据生成器。我在下面的代码中作为 cmets 得到了进一步的帮助。

这是您的代码,已更改为执行此操作,但也许您仍想更改一些内容:

# unchanged from your code
tr_datagen = ImageDataGenerator(
    featurewise_center=False,
    featurewise_std_normalization=False,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)

# create new generator for validation
val_datagen = ImageDataGenerator()    # don't perform augmentation on validation data


# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)

tr_datagen.fit(x_train)    # can leave this out if not standardising or whitening 
val_datagen.fit(x_val)     # can leave this out if not standardising or whitening

# alternative to model.fit_generator
for e in range(epochs):
    print('Epoch', e)
    batches = 0

    # combine both generators, in python 3 using zip()
    for (x_batch, y_batch), (val_x, val_y) in zip(
                                 tr_datagen.flow(x_train, y_train, batch_size=32),
                                 val_datagen.flow(x_val, y_val, batch_size=32)):
        model.fit(x_batch, y_batch, validation_Data=(val_x, val_y))
        batches += 1
        if batches >= len(x_train) / 32:
            # we need to break the loop by hand because
            # the generator loops indefinitely
            break

【讨论】:

  • 好....一个问题.....因为我们有featurewise_center=False和feaurewise_std_normalization=False,我可以注释掉tr_datagen..fit和val_datagen.fit吗?
  • 不客气!我对源代码的理解(如我对您问题的评论中所述)是,只要我们不执行任何规范化或白化,我们确实可以将其省略。如果您需要说服自己,请尝试一下!比较做和不做的结果! :-) ...如果运行时间很长,您可能希望使用数据子集,并在某处设置随机种子。
  • 首先非常感谢这个解决方案。您能否添加几行来说明如何保存 model.fit 的历史记录,例如 'acc'、'loss'、'val_acc'、'val_loss',当我们使用批处理进行训练时。
  • @weber85 - 保存指标是通过使用metrics 参数将它们包含在模型定义本身中来完成的。例如。 model.compile(..., metrics=["accuracy"])`。查看the relevant documentation。如果您想将它们保存到磁盘,您可以使用 CSVLogger:查看this answer。
  • 非常感谢,CSVLogger 是我需要的解决方案。
【解决方案2】:

即使帖子有几个月我认为它可能有用:从版本2.1.5 of Keras 可以将validation_split 参数传递给构造函数,然后在使用flow 和flow_from_directory 方法时选择子集。

例如:

datagen = ImageDataGenerator(
    featurewise_center=False,
    featurewise_std_normalization=False,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    validation_split=0.2)
datagen.fit(x_train)
model = ...
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size, subset='training'),
                    steps_per_epoch=steps_per_epoch,
                    epochs=epochs,
                    validation_data=datagen.flow(x_train, y_train, batch_size=batch_size, subset='validation'),
                    validation_steps=validation_steps)

【讨论】:

  • steps_per_epoch 和 validation_steps 有哪些好的值?此外,如果您使用验证拆分,它会增加验证数据吗?
  • 由于您使用的是数据增强,因此理论上训练集的大小是无限的。我通常使用 steps_per_epoch = len(trainining_data)/batch_size 以使样本数量等于训练集的原始大小。这同样适用于validation_steps,其值设置为使用整个验证集。不幸的是,如果您使用验证拆分,数据扩充也会应用于验证数据。
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 2018-12-06
  • 2019-09-11
  • 2018-01-11
  • 2021-11-23
  • 2017-09-09
  • 1970-01-01
相关资源
最近更新 更多