如何在keras中的层之间共享卷积内核?

假设我想用深度卷积NN比较两个图像.如何在keras中使用相同的内核实现两个不同的路径?

像这样:

enter image description here

我需要卷积层1,2和3使用并训练相同的内核.

可能吗?

我也在考虑连接下面的图像

enter image description here

但问题是关于如何在第一张图片上实现托盘学.

解决方法:

您可以在模型中使用相同的图层两次,创建nodes

from keras.models import Model    
from keras.layers import *

#create the shared layers
layer1 = Conv2D(filters, kernel_size.....)
layer2 = Conv2D(...)    
layer3 = ....

#create one input tensor for each side
input1 = Input((imageX, imageY, channels))
input2 = Input((imageX, imageY, channels))   

#use the layers in side 1
out1 = layer1(input1)   
out1 = layer2(out1)   
out1 = layer3(out1)

#use the layers in side 2
out2 = layer1(input2)   
out2 = layer2(out2)   
out2 = layer3(out2)

#concatenate and add the fully connected layers
out = Concatenate()([out1,out2])
out = Flatten()(out)
out = Dense(...)(out)   
out = Dense(...)(out)   

#create the model taking 2 inputs with one output
model = Model([input1,input2],out)

您也可以使用相同的模型两次,使其成为更大模型的子模型:

#have a previously prepared model 
convModel = some model previously prepared

#define two different inputs
input1 = Input((imageX, imageY, channels))
input2 = Input((imageX, imageY, channels))   

#use the model to get two different outputs:
out1 = convModel(input1)
out2 = convModel(input2)

#concatenate the outputs and add the final part of your model: 
out = Concatenate()([out1,out2])
out = Flatten()(out)
out = Dense(...)(out)   
out = Dense(...)(out)   

#create the model taking 2 inputs with one output
model = Model([input1,input2],out)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐