【问题标题】:Keras: How to use fit_generator with multiple input type(concanated network)Keras:如何使用具有多种输入类型的 fit_generator(连接网络)
【发布时间】:2021-05-14 21:16:56
【问题描述】:

我的模型是:

我想为此使用图像数据生成器 我为 3vgg inpu 编写了这段代码,它提供图像作为输入,但我也不知道如何为 mlp 生成...

from keras.preprocessing.image import ImageDataGenerator
batch_size=2
# Define the image transformations here
gen = ImageDataGenerator(rotation_range=10,
        zoom_range=0.10,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.15,
        horizontal_flip=True,
    # brightness_range=[0.2,1.0],
        fill_mode="nearest")

# Here is the function that merges our two generators
# We use the exact same generator with the same random seed for both the y and angle arrays
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()
        # X4i = genX4.next()

        yield [X1i[0], X2i[0], X3i[0]], X1i[1]

# Finally create generator
gen_flow = generator_three_img(im_train_whole,im_train_L,im_train_R,y_train_whole,2)

【问题讨论】:

  • 您不应该将 stack sn-p 用于 JS 以外的语言。 // 还包括一个标签语言——我给你加了一个。
  • 看起来您已经有了一些工作代码,那么损坏的代码及其错误消息是什么?

标签: python keras model data-augmentation


【解决方案1】:

您可以将输入作为数据列表传递
例如:model.fit([trainDataset1, trainImages2], y_train,...)
更好的方法是按照here 的说明以这种形式返回生成的数据。

【讨论】:

    【解决方案2】:

    这适用于我的 TF2.4.1 以及

    tf.data.Dataset.from_generator

    如果您希望您的模型有多个输入,请确保首先正确命名您的输入层,例如

    input_x_1 = Input(shape=(10,), name='input_1')
    input_x_2 = Input(shape=(10,), name='input_2')
    x_1 = Dense(16)(input_x_1)
    x_2 = Dense(16)(input_x_2)
    x_concat = concatenate([x_1,x_2])
    decoded = Dense(32)(x_concat)
    Model(inputs=[input_x_1,input_x_2], outputs=decoded, name='super_model')
    

    然后确保生成器返回正确的名称:

    def _input_fn():
      x1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
      x2 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
      x1 = np.reshape(x1, (10, 1))
      x2 = np.reshape(x2, (10, 1))
    
      labels = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64)
      y = np.reshape(labels, (10, 1))
    
      def generator():
          yield {"input_1": x1, "input_2": x2}, y
    
      dataset = tf.data.Dataset.from_generator(generator, output_types=({"input_1": tf.int64, "input_2": tf.int64}, tf.int64))
      dataset = dataset.batch(2)
      return dataset
    
    ...
    model.fit(_input_fn(), epochs=100, steps_per_epoch=10)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-17
      • 2019-08-28
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多