Keras中的卷积2D连体网络 [英] Convolutional2D Siamese Network in Keras
问题描述
我正在尝试将Keras的暹罗语图层与共享的Convolution2D
结合使用层.
我不需要输入在Siamese
层之前穿过任何其他层,但是Siamese
层要求指定输入层.我不知道如何创建输入层以匹配转换层的输入.我可以找到的使用的Siamese
层的唯一具体示例是在测试,其中Dense
层(带有矢量输入)用作输入.基本上,我想要一个输入层,允许我指定图像尺寸作为输入,以便可以将其传递到共享的conv层.
I'm trying to use Keras's Siamese layer in conjunction with a shared Convolution2D
layer.
I don't need the input to pass through any other layers before the Siamese
layer but the Siamese
layer requires that input layers be specified. I can't figure out how to create the input layers to match the input of the conv layer. The only concrete example of the Siamese
layer being used I could find is in the tests where Dense
layers (with vector inputs) are used as input. Basically, I want an input layer that allows me to specify the image dimensions as input so they can be passed on to the shared conv layer.
在代码中,我有如下内容:
In code I have something like the following:
img_rows = 28
img_cols = 28
img_input_shape = (1, img_rows, img_cols)
shared = Sequential()
shared.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=img_input_shape))
shared.add(Activation('relu'))
# .... more layers, etc.
right_input_layer = SomeInputLayer(input_shape=img_input_shape) # what should SomeInputLayer be?
left_input_layer = SomeInputLayer(input_shape=img_input_shape)
siamese = Siamese(shared, [left_input_layer, right_input_layer], merge_mode='concat')
model = Sequential()
model.add(siamese)
# ...
model.compile(loss=contrastive_loss, optimizer='rmsprop')
SomeInputLayer
应该是什么?还是我的做法总体上是不正确的?
What should SomeInputLayer
be? Or is my appraoch in general incorrect?
推荐答案
好的,我知道了. 抽象" Layer
类基本上是我所需要的直通层.我还以为我认为Siamese
可以采用整个模型(即多层),但实际上它仅采用单层,这是一个错误.为了减少创建这些暹罗图层的痛苦,提供了一个add_shared_layer
辅助函数.
Okay, I figured it out. The "abstract" Layer
class is basically a pass through layer which is just what I need. I was also making a mistake where I thought Siamese
could take an entire model (i.e. multiple layers) but it in fact only takes a single layer. To make the creation of these Siamese layers less painful there is a add_shared_layer
helper function.
我还应该指出拉请求,该请求将允许共享层模型的第一层,正是我要做的.
I should also point out this pull request that would allow a shared layer to the first layer in a model, exactly what I am trying to do.
这篇关于Keras中的卷积2D连体网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!