2016-10-13 29 views
3

入力と出力(マスク)の両方の画像を持つモデルを作成しようとしています。 があるためデータセットのサイズと私の限られたメモリを、私は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) 

すべてがコードは、この行を取得するときを除いて動作するようです:

​​

それはプロセスと思われます2つのリストを明示的に圧縮すると、コンテンツが生成され、メモリ不足になるまでシステムは多くのRAMを消費し始めます。

ジェネレータを使用するポイントは、このコードが正確に逆の動作をしている間にRAMが不足しないようにすることです。

この問題を解決する方法はありますか?

答えて

5

ユーザー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. 
関連する問題