NN中具有多个输入的数据生成器

时间:2018-09-09 21:14:23

标签: python tensorflow keras unity3d-unet multiple-input

我正在用两个输入和一个输出训练Keras的神经网络(U-net)。 第一个输入是数组(图像),第二个输入是单个值。

input_img = Input(input_size, name='input_image')
input_depth = Input((1,), name='input_depth')
...
depth1 = RepeatVector(64)(input_depth)
depth1 = Reshape((8,8, 1))(depth1)
pool4 = concatenate([pool4, depth1], -1)
....
Model([input_img, input_depth], conv10)

我已经构建了以下数据生成器来提供模型:

def get_image_depth_generator_on_memory_v2(images, masks, depths, batch_size, data_gen_args):
    seed = 123
    image_datagen = ImageDataGenerator(**data_gen_args)
    mask_datagen = ImageDataGenerator(**data_gen_args)  

    image_f = image_datagen.flow(images, depths, batch_size=batch_size, shuffle=True, seed=seed)
    mask_f = mask_datagen.flow(masks, batch_size=batch_size, shuffle=True, seed=seed)

    while True:
        image_n = image_f.next()
        mask_n = mask_f.next()

        yield [image_n[0], image_n[1]], mask_n

当我在没有生成器的情况下输入模型时,训练会起作用:

model.fit([train_images, train_depths], train_masks)

但是当我使用生成器来填充模型时,它不起作用:

model.fit_generator(generator = get_image_depth_generator_on_memory_v2(
            train_images, train_masks, train_depths,
            batch_size=512, data_gen_args={}), 
            steps_per_epoch=500)

我遇到下一个错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: ...

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

错误是您的model.fit行生成1个输出,而您的model.generate需要2个输出,因此要么提供2个输出,要么尝试使用np.concatenate合并输出以适合