本文介绍了numpy数组的keras列表,而不是预期的大小模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到将多个输入传递给模型的正确方法.该模型有2个输入

I am having trouble finding the correct way of passing multiple inputs to a model. The model has 2 inputs

  • 形状为(256, 256, 3)的噪声图像
  • 形状为(256, 256, 3)
  • 的输入图像
  • noise image of shape (256, 256, 3)
  • input image of shape (256, 256, 3)

和1个输出

  • 输出形状为(256, 256, 3)
  • 的图像
  • output image of shape (256, 256, 3)

我正在通过ImageDataGenerator生成图像:

x_data_gen = ImageDataGenerator(
    horizontal_flip=True,
    validation_split=0.2)

我正在通过python生成器生成示例:

And I am producing the samples via a python generator:

def image_sampler(datagen, batch_size, subset="training"):

    for imgs in datagen.flow_from_directory('data/r_cropped', batch_size=batch_size, class_mode=None, seed=1, subset=subset):

        g_y = []

        noises = []
        bw_images = []
        for i in imgs:
            # append to expected output the original image
            g_y.append(i/255.0)

            noises.append(generate_noise(1, 256, 3)[0])
            bw_images.append(iu_rgb2gray(i))

        yield(np.array([noises, bw_images]), np.array(g_y))

尝试使用以下方法训练模型时:

When trying to train the model with:

    generator.fit_generator(
       image_sampler(x_data_gen, 32),
       validation_data=image_sampler(x_data_gen,32,"validation"),
       epochs=EPOCHS,
       steps_per_epoch= 540,
       validation_steps=160 )

我收到一条错误消息:

虽然消息很清楚,但我不知道如何解决生成过程来解决它.

while the message is quite clear, I do not understand how to fix the generation process to solve it.

我尝试过:

    yield([noises, bw_images], np.array(g_y))

但是这不起作用,因为它将遇到另一个错误:

but this didn't work as it would reach a different error:

我想念什么?

推荐答案

当您有多个输入/输出时,应将它们作为numpy数组的列表传递.因此,您的第二种方法是正确的,但是您忘记了在第二种方法中将列表转换为numpy数组:

When you have multiple inputs/outputs you should pass them as a list of numpy arrays. So your second approach is correct but you have forgotten to convert the lists to numpy arrays in your second approach:

yield ([np.array(noises), np.array(bw_images)], np.array(g_y))


一种确保所有内容正确的更冗长的方法是为输入和输出层选择名称.示例:


A more verbose approach to make sure everything is correct, is to choose names for the input and output layers. Example:

input_1 = layers.Input(# other args, name='input_1')
input_2 = layers.Input(# other args, name='input_2')

然后,在生成器函数中使用以下名称:

Then, use those names like this in your generator function:

yield ({'input_1': np.array(noises), 'input_2': np.array(bw_images)}, {'output': np.array(g_y)})

这样做,您可以确保映射正确完成.

By doing so, you are making sure that the mapping is done correctly.

这篇关于numpy数组的keras列表,而不是预期的大小模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:06