简体   繁体   English

在Keras中合并两种不同的深度学习模型

[英]Merge two different deep learning models in Keras

I have two different types of data (image volumes and coordinates) and I would like to use a convolutional neural network on the image volume data and then after this I would like to append some additional information (ie. the coordinates of the volume). 我有两种不同类型的数据(图像体积和坐标),我想在图像体积数据上使用卷积神经网络,然后在此之后我要附加一些附加信息(即体积的坐标)。

Independently this should create a pretty solid predictor for my function. 独立地,这应该为我的功能创建一个相当可靠的预测器。 How can I implement this using Keras. 我如何使用Keras来实现这一点。

The only answers I have found online are either ambiguous or are using the deprecated methods which I have got to work. 我在网上找到的唯一答案要么模棱两可,要么正在使用我必须使用的不推荐使用的方法。 But I would really like to implement this using the current API that way I can more easily save the model for later use. 但是我真的很想使用当前的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'])

Sequential models are not suited for creating models with branches. 顺序模型不适用于创建带有分支的模型。

You can have the two independent models as Sequential models, as you did, but from the Concatenate on, you should start using the functional Model API. 您可以像以前一样将两个独立的模型作为顺序模型,但是从Concatenate开始,您应该开始使用功能性模型API。

The idea is to get the output tensors of the two models and feed them in other layers to get new output tensors. 这个想法是获取两个模型的输出张量,并将它们馈入其他层以获得新的输出张量。

So, considering you have model and extra : 因此,考虑到你有modelextra

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

This mergetOutput is a tensor. 这个mergetOutput是一个张量。 You can either create the last part of the model using this tensor, or create the last part independently, and call it on this tensor. 您可以使用此张量创建模型的最后一部分,也可以独立创建最后一部分,然后在该张量上调用它。 The second approach may be good if you want to train each model separately (doesn't seem to be your case). 如果您想分别训练每个模型(似乎不是您的情况),第二种方法可能很好。

Now, creating the new model as a functional API model: 现在,将新模型创建为功能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
) 

An easier approach is to take all three models you have already created and use them to create a combined model: 一种更简单的方法是采用已经创建的所有三个模型,并使用它们来创建组合模型:

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,)))

Join them like this: 像这样加入他们:

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

fullModel = Model([model.input,extra.input],finalOutput)

Use the functional API of Keras ( https://keras.io/models/model/ ). 使用Keras的功能性API( https://keras.io/models/model/ )。 You can just apply layers to your merged layer in Keras. 您可以仅将图层应用于Keras中的合并图层。 The functional API works like this. 功能性API的工作原理如下。 You have a tensor and you apply a function to this Tensor. 您有一个张量,并将一个函数应用于此张量。 Then this is recursively evaluated. 然后对此进行递归评估。 Because pretty much everything is a tensor in Keras this works quite nicely. 因为在Keras中几乎所有东西都是张量,所以效果很好。

An example for this is: 一个例子是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM