【问题标题】:Merge two different deep learning models in Keras在 Keras 中合并两个不同的深度学习模型
【发布时间】:2017-10-31 12:24:55
【问题描述】:

我有两种不同类型的数据(图像体积和坐标),我想在图像体积数据上使用卷积神经网络,然后在此之后我想附加一些附加信息(即图像的坐标)量)。

独立地,这应该为我的函数创建一个非常可靠的预测器。我如何使用 Keras 来实现这一点。

我在网上找到的唯一答案要么模棱两可,要么使用了我必须使用的已弃用的方法。但是我真的很想使用当前的 API 来实现它,这样我可以更轻松地保存模型以供以后使用。

model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv3D(64, (3, 3, 3), activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
print(model.output_shape)

# The additional data (the coordinates x,y,z)
extra = Sequential()
extra.add(Activation('sigmoid', input_shape=(3,)))
print(extra.output_shape)

merged = Concatenate([model, extra])

# New model should encompass the outputs of the convolutional network and the coordinates that have been merged.
# But how?
new_model = Sequential()
new_model.add(Dense(128, activation='relu'))
new_model.add(Dropout(0.8))
new_model.add(Dense(32, activation='sigmoid'))
new_model.add(Dense(num_classes, activation='softmax'))

new_model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

【问题讨论】:

    标签: python deep-learning keras


    【解决方案1】:

    顺序模型不适合创建带有分支的模型。

    您可以将两个独立的模型作为顺序模型,就像您所做的那样,但是从Concatenate 开始,您应该开始使用功能模型 API。

    这个想法是获取两个模型的输出张量并将它们馈送到其他层以获得新的输出张量。

    所以,考虑到您有 modelextra

    mergedOutput = Concatenate()([model.output, extra.output])
    

    这个mergetOutput 是一个张量。您可以使用此张量创建模型的最后一部分,也可以独立创建最后一部分,并在此张量上调用它。如果您想分别训练每个模型(似乎不是您的情况),第二种方法可能会很好。

    现在,将新模型创建为功能性 API 模型:

    out = Dense(128, activation='relu')(mergetOutput)
    out = Dropout(0.8)(out)
    out = Dense(32, activation='sigmoid')(out)
    out = Dense(num_classes, activation='softmax')(out)
    
    new_model = Model(
        [model.input, extra.input], #model with two input tensors
        out                         #and one output tensor
    ) 
    

    一种更简单的方法是使用您已经创建的所有三个模型并使用它们来创建一个组合模型:

    model = Sequential() #your first model
    extra = Sequential() #your second model    
    new_model = Sequential() #all these three exactly as you did   
    
    #in this case, you just need to add an input shape to new_model, compatible with the concatenated output of the previous models. 
    new_model.add(FirstNewModelLayer(...,input_shape=(someValue,)))
    

    像这样加入他们:

    mergedOutput = Concatenate()([model.output, extra.output])
    finalOutput = new_model(mergedOutput)    
    
    fullModel = Model([model.input,extra.input],finalOutput)
    

    【讨论】:

    • 非常感谢您澄清 API 中的示例。我很感激!
    • 还有一个问题,如何保存和加载这种模型。只保存 fullModel 就足够了吗?
    • 不幸的是,我从来没有能够在 keras 中保存模型。我不知道为什么。我所做的是fullModel.save_weights(filename)fullModel.load_weights(filename)。这足以保存和加载学习的模型,但是当您想再次开始训练时可能会遇到困难(优化器在此过程中丢失,并且必须在训练期间再次自我调整)。
    • @DanielMöller 你能看看this吗?
    【解决方案2】:

    使用 Keras 的功能 API (https://keras.io/models/model/)。您可以将图层应用到 Keras 中的合并图层。函数式 API 的工作方式是这样的。你有一个张量,你对这个张量应用一个函数。然后对其进行递归评估。因为在 Keras 中几乎所有东西都是张量,所以效果非常好。

    一个例子是:

    activation = Dense(128, activation='relu')(merged)
    

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 2018-02-03
      • 2021-04-08
      • 2018-09-03
      • 2021-03-24
      • 2017-06-15
      • 1970-01-01
      • 2021-12-21
      • 2021-04-04
      相关资源
      最近更新 更多