简体   繁体   English

Keras - 用于大型图像和掩码数据集的生成器

[英]Keras - Generator for large dataset of Images and Masks

I'm trying to build a Model that has Images for both its Inputs and Outputs (masks).我正在尝试构建一个模型,它的输入和输出(掩码)都有图像。 Because of the size of the Dataset and my limited Memory, I tried using the Generator Approach introduced in the Keras Documentation :由于数据集的大小和我有限的内存,我尝试使用Keras 文档中介绍的生成器方法

# 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)

Everything seems to work except when the code gets to this line:除了代码到达这一行时,一切似乎都有效:

train_generator = zip(image_generator, mask_generator)

it seems the process of zipping the two lists explicitly makes them generate their content and the system starts consuming lots of RAM until it runs out of Memory.似乎压缩两个列表的过程明确地使它们生成它们的内容,并且系统开始消耗大量 RAM,直到它耗尽内存。

The point of using Generators is to avoid running out of RAM while this piece of code is exactly doing the opposite.使用 Generators 的目的是避免耗尽 RAM,而这段代码恰恰相反。

Is there any way to fix this problem?有没有办法解决这个问题?

You can user itertools.izip() to return an iterator instead of a list.您可以使用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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM