我正在尝试将Keras的Siamese图层与共享Convolution2D图层结合使用.我不需要输入在图层之前穿过任何其他图层,Siamese但Siamese图层要求指定输入图层.我无法弄清楚如何创建输入层以匹配转换层的输入.Siamese我能找到的层的唯一具体例子是在测试中,Dense层(带矢量输入)用作输入.基本上,我想要一个输入层,允许我将图像尺寸指定为输入,以便将它们传递给共享的转换层.
在代码中我有类似以下内容:
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')
Run Code Online (Sandbox Code Playgroud)
应该SomeInputLayer是什么?或者我的appraoch一般是不正确的?
我想实现一个Siamese卷积神经网络,其中两个图像在卷积层中共享权重,然后在通过完全连接的层之前连接.我试过一个实现,但它似乎是一个"黑客"解决方案.特别是,我已经将张量上的操作定义为简单的Python函数,我不确定这是否允许.
这是我尝试过的代码:
images = tf.placeholder(tf.float32, shape=[None, 64 * 64])
# Convolutional layers
# ...
# ...
# Results in pool3_flat, which is the flattened output of the third convolutional layer
pool3_flat = tf.reshape(pool3, [-1, 8 * 8 * 128])
# Now, merge the image pairs, where each pair is composed of adjacent images in the batch, with a stride of 2
def merge_pairs():
# Create a tensor to store the merged image pairs
# The batch size is 128, therefore …Run Code Online (Sandbox Code Playgroud)