如何在keras中的层之间共享卷积内核?
作者:互联网
假设我想用深度卷积NN比较两个图像.如何在keras中使用相同的内核实现两个不同的路径?
像这样:
我需要卷积层1,2和3使用并训练相同的内核.
可能吗?
我也在考虑连接下面的图像
但问题是关于如何在第一张图片上实现托盘学.
解决方法:
您可以在模型中使用相同的图层两次,创建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)
标签:python,tensorflow,keras,conv-neural-network 来源: https://codeday.me/bug/20190717/1486900.html