我如何结合两个 keras 生成器功能 [英] How do I combine two keras generator functions
问题描述
我正在尝试在 Keras 中实现一个 Siamese 网络,并且我想使用 Keras 图像数据生成器将图像转换应用于 2 个输入图像.根据文档中的示例 - https://keras.io/preprocessing/image/,我试过像这样实现它 -
I am trying to implement a Siamese network in Keras and I want to apply image transformations to the 2 input images using Keras Image Data Generators. As per the example in the docs- https://keras.io/preprocessing/image/, I've tried to implement it like this-
datagen_args = dict(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)
train_generator = zip(in_gen1, in_gen2)
model.fit(train_generator.flow([pair_df[:, 0,::],pair_df[:, 1,::]],
y_train,batch_size=16), epochs, verbose = 1)
但是这段代码抛出了这个错误:
But this code throws this error:
TypeError:zip 参数 #1 必须支持迭代
TypeError: zip argument #1 must support iteration
我已经尝试使用 itertools.izip
如Keras - 用于大型图像和掩码数据集的生成器 但这会引发相同的错误.
I've tried using itertools.izip
as suggested in Keras - Generator for large dataset of Images and Masks but this throws the same error.
我该如何解决这个问题?
How do I resolve this?
编辑:如果有人感兴趣,这终于奏效了-
EDIT: In case anyone is interested, this worked finally-
datagen_args = dict(
featurewise_center=False,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)
in_gen1 = in_gen1.flow(pair_df[:, 0,::], y_train, batch_size = 16, shuffle = False)
in_gen2 = in_gen2.flow(pair_df[:, 1,::], y_train, batch_size = 16, shuffle = False)
for e in range(epochs):
batches = 0
for x1, x2 in itertools.izip(in_gen1,in_gen2):
# x1, x2 are tuples returned by the generator, check whether targets match
assert sum(x1[1] != x2[1]) == 0
model.fit([x1[0], x2[0]], x1[1], verbose = 1)
batches +=1
if(batches >= len(pair_df)/16):
break
推荐答案
使用 zip()
组合生成器导致生成无限迭代器.改用这个:
Using zip()
to combine generators leads to generation of an infinite iterator.
Use this instead:
def combine_generator(gen1, gen2):
while True:
yield(next(gen1), next(gen2))
修改后的代码如下所示:
Modified code would look something like this:
datagen_args = dict(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
in_gen1 = ImageDataGenerator(**datagen_args)
in_gen2 = ImageDataGenerator(**datagen_args)
def combine_generator(gen1, gen2):
while True:
yield(next(gen1), next(gen2))
train_generator = combine_generator(in_gen1, in_gen2)
model.fit(train_generator.flow([pair_df[:, 0,::],pair_df[:, 1,::]],
y_train,batch_size=16), epochs, verbose = 1)
请参阅此线程以获取进一步参考.
See this thread for further reference.
这篇关于我如何结合两个 keras 生成器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!