在 keras 中使用数据生成器时出现内存错误

数据挖掘 Python 喀拉斯 张量流 数据集 数据增强
2021-09-24 07:17:02

我在 9 GB 的数据集上使用以下扩充:

datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=True,  # apply ZCA whitening
    rotation_range=30,  # randomly rotate images in the range (degrees, 0 to 180)
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=True,  # randomly flip images
    vertical_flip=True)  # randomly flip images
datagen.fit(a)

model.fit_generator(datagen.flow(a,b, batch_size=32),
                    steps_per_epoch=len(a) / 32, epochs=epochs, class_weight = sclass_weight, validation_data = [c, d],callbacks = [MetricsCheckpoint('logs')])

当代码出现时datagen.fit,我进入内存错误(代码甚至没有进入训练)

我有 50 gb 内存,并且正在使用批量大小为 32 的 K80 对其进行训练,所以不要认为这会是个问题。

当我评论所有增强时,它工作正常。

有人可以告诉我哪里出错了吗?

1个回答

似乎根本原因zca_whitening是设置为True. 根据 keras-collaborator rragundez 在这个相关的 Github 问题中的回答,除了禁用 ZCA 美白之外,没有其他解决方法:

这是 ZCA 方法的已知问题。没有真正的直接解决方法。问题在于作为点积的 sigma 矩阵的计算。

禁用它的另一种方法是使用分辨率较小的图像。但我认为这很少是一个实际的解决方案。