So I'm trying to implement this paper about a Siamese neural network: Learning a similarity metric discriminatively, with application to face verification, by Sumit Chopra, Raia Hadsell and Yann LeCun (2005). I'm using the CIFAR10 dataset instead, though, with 10 classes.
The specifications of one of the legs is reproduced for convenience. Notation: C_x is a convolution layer, S_x is a subsampling layer and F_x is a fully connected layer; with a shared index x:
- C1: feature maps: 15, kernel size = (7, 7)
- S2: feature maps: 15, field-of-view = (2, 2)
- C3: feature maps: 45, kernel size = (6, 6)
- S4: feature maps: 45, field-of-view = (4, 3)
- C5: feature maps: 250, kernel size = (5, 5)
- F6 (fully connected layer): no. of units = 50
What I've Tried
model = Sequential()
#C1
model.add(Convolution2D(15, 7, 7,
activation='relu',
border_mode='same',
input_shape=input_img_shape))
print("C1 shape: ", model.output_shape)
#S2
model.add(MaxPooling2D((2,2), border_mode='same'))
print("S2 shape: ", model.output_shape)
#...
#C5
model.add(Convolution2D(250, 5, 5,
activation='relu',
border_mode='same'))
print("C5 shape: ", model.output_shape)
#F6
model.add(Dense(50))
This throws a long error message, which I believe is a reshape error. A snippet of the error:
Exception: Input 0 is incompatible with layer dense_13: expected
ndim=2, found ndim=4
I know that the problem is isolated in that final Dense layer, because the code proceeds smoothly if I comment it out. But I'm not sure exactly how I should then shape/specify my final fully connected layer so that it's compatible with the prior convolution layer?
Some Places I've Looked
This is a related problem, though the implementation is slightly different (it seems that there isn't a 'Siamese' core layer in keras at the time of this writing). I'm aware that there are also implementations in Theano, which I'll bear in mind if I'm just not able to do it in keras.
Thanks!