-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiamese_net.py
139 lines (98 loc) · 3.79 KB
/
siamese_net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# siamese network modified from: https://github.com/NVIDIA/keras/blob/master/examples/mnist_siamese_graph.py
import numpy as np
seed = 1
np.random.seed(seed) # for reproducibility
import random
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Input, Lambda
from keras.optimizers import RMSprop
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.layers import Input, Conv2D, BatchNormalization, MaxPool2D, Activation, Flatten, Dense, Dropout
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import pandas as pd
import cv2
import math
import glob
import os
# frames are resized to 64x64
input_dim = 64
train_X = np.load('train_X_64.npy')
train_Y = np.load('train_Y_64.npy')
arr = np.arange(train_X.shape[1])
np.random.shuffle(arr)
train_X = train_X[:,arr,:,:,:]
train_Y = train_Y[arr]
val_split = 0.2
val_split = int((1-val_split)*train_X.shape[1])
val_X = train_X[:,val_split:,:,:,:]
val_Y = train_Y[val_split:]
train_X = train_X[:,0:val_split,:,:,:]
train_Y = train_Y[:val_split]
def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))
def eucl_dist_output_shape(shapes):
shape1, shape2 = shapes
return (shape1[0], 1)
def contrastive_loss(y_true, y_pred):
'''Contrastive loss from Hadsell-et-al.'06
http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
'''
margin = 1
return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
def create_base_network():
'''Base network to be shared (eq. to feature extraction).
'''
model = Sequential()
model.add(Conv2D(8, kernel_size = (3,3), border_mode='same',input_shape=(input_dim,input_dim,3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv2D(16, kernel_size = (3,3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv2D(32, kernel_size = (3,3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPool2D((2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.summary()
return model
def compute_accuracy(predictions, labels):
'''Compute classification accuracy with a fixed threshold on distances.
'''
return np.where(labels == np.array([ int(i<0.5) for i in predictions]))[0].shape[0]*1.0/len(labels)
nb_epoch = 5
# network definition
base_network = create_base_network()
input_a = Input(shape=(input_dim,input_dim,3))
input_b = Input(shape=(input_dim,input_dim,3))
# 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)
distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([processed_a, processed_b])
model = Model(input=[input_a, input_b], output=distance)
#auxilary model to extract features
model_fe = Model(input_a, processed_a)
# train
rms = RMSprop(lr=0.01)
model.compile(loss=contrastive_loss, optimizer=rms)
model.fit([train_X[0,],train_X[1,]], train_Y, validation_data=([val_X[0,], val_X[1,]], val_Y), batch_size = 128, shuffle = True, epochs=nb_epoch)
# compute final accuracy on training and test sets
pred = model.predict([train_X[0,],train_X[1,]])
tr_acc = compute_accuracy(pred, train_Y)
pred = model.predict([val_X[0,],val_X[1,]])
val_acc = compute_accuracy(pred, val_Y)
print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
print('* Accuracy on test set: %0.2f%%' % (100 * val_acc))
# save model
model_fe.save('model_test.h5')