Keras: How to use fit_generator with multiple outputs of different type

后端 未结 3 593
春和景丽
春和景丽 2020-12-05 05:21

In a Keras model with the Functional API I need to call fit_generator to train on augmented images data using an ImageDataGenerator. The problem is my model has two outputs:

3条回答
  •  有刺的猬
    2020-12-05 06:03

    If you have separated both mask and binary value you can try something like this:

    generator = ImageDataGenerator(rotation_range=5.,
                                    width_shift_range=0.1, 
                                    height_shift_range=0.1, 
                                    horizontal_flip=True,  
                                    vertical_flip=True)
    
    def generate_data_generator(generator, X, Y1, Y2):
        genX = generator.flow(X, seed=7)
        genY1 = generator.flow(Y1, seed=7)
        while True:
                Xi = genX.next()
                Yi1 = genY1.next()
                Yi2 = function(Y2)
                yield Xi, [Yi1, Yi2]
    

    So, you use the same generator for both input and mask with the same seed to define the same operation. You may change the binary value or not depending on your needs (Y2). Then, you call the fit_generator():

    model.fit_generator(generate_data_generator(generator, X, Y1, Y2),
                    epochs=epochs)
    

提交回复
热议问题