【问题标题】:Keras - Generator for large dataset of Images and MasksKeras - 大型图像和蒙版数据集的生成器
【发布时间】:2017-02-22 18:59:25
【问题描述】:

我正在尝试构建一个模型,它的输入和输出(掩码)都有图像。 由于数据集的大小和我有限的内存,我尝试使用the Generator Approach introduced in the Keras Documentation

# Provide the same seed and keyword arguments to the fit and flow methods
seed = 1

image_generator = image_datagen.flow_from_directory(
    'data/images',
    class_mode=None,
    seed=seed)

mask_generator = mask_datagen.flow_from_directory(
    'data/masks',
    class_mode=None,
    seed=seed)

# combine generators into one which yields image and masks
train_generator = zip(image_generator, mask_generator)

model.fit_generator(
    train_generator,
    samples_per_epoch=2000,
    nb_epoch=50)

似乎一切正常,除非代码到达这一行:

train_generator = zip(image_generator, mask_generator)

似乎压缩这两个列表的过程显式地使它们生成了它们的内容,并且系统开始消耗大量的 RAM,直到它耗尽内存。

使用生成器的目的是避免在这段代码完全相反的情况下耗尽内存。

有没有办法解决这个问题?

【问题讨论】:

  • 您能否分享您的完整解决方案,包括itertools.izip()

标签: python machine-learning computer-vision theano keras


【解决方案1】:

您可以使用 itertools.izip() 返回迭代器而不是列表。

itertools.izip(*iterables)

Make an iterator that aggregates elements from each of the iterables. Like zip() except that it returns an iterator instead of a list. Used for lock-step iteration over several iterables at a time.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2021-06-29
    • 2018-04-22
    • 2020-03-09
    • 2018-02-09
    • 2017-06-01
    • 2011-05-10
    相关资源
    最近更新 更多