本文介绍了Keras:如何将fit_generator与多个输入配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有两个fit_generator?

Is it possible to have two fit_generator?

我正在创建一个具有两个输入的模型,模型配置如下所示.

I'm creating a model with two inputs,The model configuration is shown below.

标签Y对X1和X2数据使用相同的标签.

Label Y uses the same labeling for X1 and X2 data.

以下错误将继续发生.

我的代码如下:

def generator_two_img(X1, X2, Y,batch_size):
    generator = ImageDataGenerator(rotation_range=15,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True,
                                   fill_mode='nearest')

    genX1 = generator.flow(X1, Y, batch_size=batch_size)
    genX2 = generator.flow(X2, Y, batch_size=batch_size)

    while True:
        X1 = genX1.__next__()
        X2 = genX2.__next__()
        yield [X1, X2], Y
  """
      .................................
  """
hist = model.fit_generator(generator_two_img(x_train, x_train_landmark,
                y_train, batch_size),
                steps_per_epoch=len(x_train) // batch_size, epochs=nb_epoch,
                callbacks = callbacks,
                validation_data=(x_validation, y_validation),
                validation_steps=x_validation.shape[0] // batch_size,
                `enter code here`verbose=1)

推荐答案

尝试使用此生成器:

def generator_two_img(X1, X2, y, batch_size):
    genX1 = gen.flow(X1, y,  batch_size=batch_size, seed=1)
    genX2 = gen.flow(X2, y, batch_size=batch_size, seed=1)
    while True:
        X1i = genX1.next()
        X2i = genX2.next()
        yield [X1i[0], X2i[0]], X1i[1]

Thanh Nguyen评论后编辑

EDIT after Thanh Nguyen comment

3个输入的生成器:

def generator_three_img(X1, X2, X3, y, batch_size):
    genX1 = gen.flow(X1, y,  batch_size=batch_size, seed=1)
    genX2 = gen.flow(X2, y, batch_size=batch_size, seed=1)
    genX3 = gen.flow(X3, y, batch_size=batch_size, seed=1)
    while True:
        X1i = genX1.next()
        X2i = genX2.next()
        X3i = genX3.next()
        yield [X1i[0], X2i[0], X3i[0]], X1i[1]

这篇关于Keras:如何将fit_generator与多个输入配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 07:03