Created
May 8, 2019 14:24
-
-
Save theikkila/0923d41d377e23c872aec2e1944debb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.layers import Input, Conv2D, Lambda, merge, Dense, Flatten, MaxPooling2D | |
from keras.models import Model, Sequential | |
from keras.regularizers import l2 | |
from keras import backend as K | |
from keras.optimizers import SGD, Adam | |
from keras.losses import binary_crossentropy | |
import numpy.random as rng | |
import numpy as np | |
import os | |
import matplotlib.pyplot as plt | |
from sklearn.utils import shuffle | |
def W_init(shape,name=None): | |
"""Initialize weights randomly""" | |
values = rng.normal(loc=0, scale=1e-2, size=shape) | |
return K.variable(values,name=name) | |
def b_init(shape,name=None): | |
"""Initialize random""" | |
values=rng.normal(loc=0.5,scale=1e-2,size=shape) | |
return K.variable(values,name=name) | |
input_shape = (105, 105, 1) | |
left_input = Input(input_shape) | |
right_input = Input(input_shape) | |
# left and right leg | |
convnet = Sequential() | |
convnet.add(Conv2D(64,(10,10),activation='relu',input_shape=input_shape, | |
kernel_initializer=W_init,kernel_regularizer=l2(2e-4))) | |
convnet.add(MaxPooling2D()) | |
convnet.add(Conv2D(128,(7,7),activation='relu', | |
kernel_regularizer=l2(2e-4),kernel_initializer=W_init,bias_initializer=b_init)) | |
convnet.add(MaxPooling2D()) | |
convnet.add(Conv2D(128,(4,4),activation='relu',kernel_initializer=W_init,kernel_regularizer=l2(2e-4),bias_initializer=b_init)) | |
convnet.add(MaxPooling2D()) | |
convnet.add(Conv2D(256,(4,4),activation='relu',kernel_initializer=W_init,kernel_regularizer=l2(2e-4),bias_initializer=b_init)) | |
convnet.add(Flatten()) | |
convnet.add(Dense(4096,activation="sigmoid",kernel_regularizer=l2(1e-3),kernel_initializer=W_init,bias_initializer=b_init)) | |
#encode each of the two inputs into a vector with the convnet | |
encoded_l = convnet(left_input) | |
encoded_r = convnet(right_input) | |
#merge two encoded inputs with the l1 distance between them | |
L1_distance = lambda x: K.abs(x[0]-x[1]) | |
both = merge([encoded_l, encoded_r], mode = L1_distance, output_shape=lambda x: x[0]) | |
prediction = Dense(1, activation='sigmoid',bias_initializer=b_init)(both) | |
siamese_net = Model(input=[left_input,right_input],output=prediction) | |
optimizer = Adam(0.00006) | |
siamese_net.compile(loss="binary_crossentropy",optimizer=optimizer) | |
siamese_net.count_params() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment