![](/img/trans.png)
[英]Keras ImageDataGenerator for segmentation with images and masks in separate directories
[英]Keras - Generator for large dataset of Images and Masks
我正在嘗試構建一個模型,它的輸入和輸出(掩碼)都有圖像。 由於數據集的大小和我有限的內存,我嘗試使用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)
除了代碼到達這一行時,一切似乎都有效:
train_generator = zip(image_generator, mask_generator)
似乎壓縮兩個列表的過程明確地使它們生成它們的內容,並且系統開始消耗大量 RAM,直到它耗盡內存。
使用 Generators 的目的是避免耗盡 RAM,而這段代碼恰恰相反。
有沒有辦法解決這個問題?
您可以使用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.