假设我想用深度卷积NN比较两个图像。如何在keras中使用相同的内核实现两个不同的路径?
像这样:
我需要卷积层1,2和3使用并训练相同的内核。
有可能吗?
我还在考虑连接下面的图像
但问题是关于如何在第一张照片上实施托盘学。
答案 0 :(得分:7)
您可以在模型中使用相同的图层,创建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)
答案 1 :(得分:3)
确实两次使用相同(实例)图层可确保共享权重。
看看siamese example,我只是在这里放一个模型的摘录来展示一个例子:
# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)