Keras:如何使用fit_generator和多个输入

时间:2018-03-21 11:21:18

标签: python machine-learning neural-network keras generator

是否可以有两个fit_generator?

我创建了一个带有两个输入的模型, 型号配置如下所示。

enter image description here

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

将继续发生以下错误。

  

检查模型输入时出错:您传递给模型的Numpy数组列表不是模型预期的大小。预期   看到2个数组,但得到以下1个数组的列表:   [array([[[[[0.75686276,0.75686276,0.75686276],            [0.75686276,0.75686276,0.75686276],            [0.75686276,0.75686276,0.75686276],            ...            [0.65882355,0.65882355,0.65882355 ......

我的代码如下所示:

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)

2 个答案:

答案 0 :(得分:11)

试试这个发电机:

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评论之后编辑

3个输入的生成器:

def generator_two_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]

答案 1 :(得分:0)

我有一个TimeseriesGenerator的多个输入的实现,我已经对其进行了修改(不幸的是,我无法对其进行测试),以实现ImageDataGenerator的示例。我的方法是为keras.utils.Sequence的多个生成器构建一个包装器类,然后实现它的基本方法:__len____getitem__

from keras.preprocessing.image import ImageDataGenerator
from keras.utils import Sequence


class MultipleInputGenerator(Sequence):
    """Wrapper of 2 ImageDataGenerator"""

    def __init__(self, X1, X2, Y, batch_size):
        # Keras generator
        self.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')

        # Real time multiple input data augmentation
        self.genX1 = self.generator.flow(X1, Y, batch_size=batch_size)
        self.genX2 = self.generator.flow(X2, Y, batch_size=batch_size)

    def __len__(self):
        """It is mandatory to implement it on Keras Sequence"""
        return self.genX1.__len__()

    def __getitem__(self, index):
        """Getting items from the 2 generators and packing them"""
        X1_batch, Y_batch = self.genX1.__getitem__(index)
        X2_batch, Y_batch = self.genX2.__getitem__(index)

        X_batch = [X1_batch, X2_batch]

        return X_batch, Y_batch

实例化生成器后,可以将其与model.fit_generator()一起使用。