This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathsiamese_network.py
executable file
·303 lines (252 loc) · 13.2 KB
/
siamese_network.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import os
import keras.backend as K
from keras.models import Model, Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Input, Subtract, Lambda
from keras.optimizers import Adam, SGD
from keras.regularizers import l2
import keras.backend as K
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
from omniglot_loader import OmniglotLoader
from modified_sgd import Modified_SGD
class SiameseNetwork:
"""Class that constructs the Siamese Net for training
This Class was constructed to create the siamese net and train it.
Attributes:
input_shape: image size
model: current siamese model
learning_rate: SGD learning rate
omniglot_loader: instance of OmniglotLoader
summary_writer: tensorflow writer to store the logs
"""
def __init__(self, dataset_path, learning_rate, batch_size, use_augmentation,
learning_rate_multipliers, l2_regularization_penalization, tensorboard_log_path):
"""Inits SiameseNetwork with the provided values for the attributes.
It also constructs the siamese network architecture, creates a dataset
loader and opens the log file.
Arguments:
dataset_path: path of Omniglot dataset
learning_rate: SGD learning rate
batch_size: size of the batch to be used in training
use_augmentation: boolean that allows us to select if data augmentation
is used or not
learning_rate_multipliers: learning-rate multipliers (relative to the learning_rate
chosen) that will be applied to each fo the conv and dense layers
for example:
# Setting the Learning rate multipliers
LR_mult_dict = {}
LR_mult_dict['conv1']=1
LR_mult_dict['conv2']=1
LR_mult_dict['dense1']=2
LR_mult_dict['dense2']=2
l2_regularization_penalization: l2 penalization for each layer.
for example:
# Setting the Learning rate multipliers
L2_dictionary = {}
L2_dictionary['conv1']=0.1
L2_dictionary['conv2']=0.001
L2_dictionary['dense1']=0.001
L2_dictionary['dense2']=0.01
tensorboard_log_path: path to store the logs
"""
self.input_shape = (105, 105, 1) # Size of images
self.model = []
self.learning_rate = learning_rate
self.omniglot_loader = OmniglotLoader(
dataset_path=dataset_path, use_augmentation=use_augmentation, batch_size=batch_size)
self.summary_writer = tf.summary.FileWriter(tensorboard_log_path)
self._construct_siamese_architecture(learning_rate_multipliers,
l2_regularization_penalization)
def _construct_siamese_architecture(self, learning_rate_multipliers,
l2_regularization_penalization):
""" Constructs the siamese architecture and stores it in the class
Arguments:
learning_rate_multipliers
l2_regularization_penalization
"""
# Let's define the cnn architecture
convolutional_net = Sequential()
convolutional_net.add(Conv2D(filters=64, kernel_size=(10, 10),
activation='relu',
input_shape=self.input_shape,
kernel_regularizer=l2(
l2_regularization_penalization['Conv1']),
name='Conv1'))
convolutional_net.add(MaxPool2D())
convolutional_net.add(Conv2D(filters=128, kernel_size=(7, 7),
activation='relu',
kernel_regularizer=l2(
l2_regularization_penalization['Conv2']),
name='Conv2'))
convolutional_net.add(MaxPool2D())
convolutional_net.add(Conv2D(filters=128, kernel_size=(4, 4),
activation='relu',
kernel_regularizer=l2(
l2_regularization_penalization['Conv3']),
name='Conv3'))
convolutional_net.add(MaxPool2D())
convolutional_net.add(Conv2D(filters=256, kernel_size=(4, 4),
activation='relu',
kernel_regularizer=l2(
l2_regularization_penalization['Conv4']),
name='Conv4'))
convolutional_net.add(Flatten())
convolutional_net.add(
Dense(units=4096, activation='sigmoid',
kernel_regularizer=l2(
l2_regularization_penalization['Dense1']),
name='Dense1'))
# Now the pairs of images
input_image_1 = Input(self.input_shape)
input_image_2 = Input(self.input_shape)
encoded_image_1 = convolutional_net(input_image_1)
encoded_image_2 = convolutional_net(input_image_2)
# L1 distance layer between the two encoded outputs
# One could use Subtract from Keras, but we want the absolute value
l1_distance_layer = Lambda(
lambda tensors: K.abs(tensors[0] - tensors[1]))
l1_distance = l1_distance_layer([encoded_image_1, encoded_image_2])
# Same class or not prediction
prediction = Dense(units=1, activation='sigmoid')(l1_distance)
self.model = Model(
inputs=[input_image_1, input_image_2], outputs=prediction)
# Define the optimizer and compile the model
optimizer = Modified_SGD(
lr=self.learning_rate,
lr_multipliers=learning_rate_multipliers,
momentum=0.5)
self.model.compile(loss='binary_crossentropy', metrics=['binary_accuracy'],
optimizer=optimizer)
def _write_logs_to_tensorboard(self, current_iteration, train_losses,
train_accuracies, validation_accuracy,
evaluate_each):
""" Writes the logs to a tensorflow log file
This allows us to see the loss curves and the metrics in tensorboard.
If we wrote every iteration, the training process would be slow, so
instead we write the logs every evaluate_each iteration.
Arguments:
current_iteration: iteration to be written in the log file
train_losses: contains the train losses from the last evaluate_each
iterations.
train_accuracies: the same as train_losses but with the accuracies
in the training set.
validation_accuracy: accuracy in the current one-shot task in the
validation set
evaluate each: number of iterations defined to evaluate the one-shot
tasks.
"""
summary = tf.Summary()
# Write to log file the values from the last evaluate_every iterations
for index in range(0, evaluate_each):
value = summary.value.add()
value.simple_value = train_losses[index]
value.tag = 'Train Loss'
value = summary.value.add()
value.simple_value = train_accuracies[index]
value.tag = 'Train Accuracy'
if index == (evaluate_each - 1):
value = summary.value.add()
value.simple_value = validation_accuracy
value.tag = 'One-Shot Validation Accuracy'
self.summary_writer.add_summary(
summary, current_iteration - evaluate_each + index + 1)
self.summary_writer.flush()
def train_siamese_network(self, number_of_iterations, support_set_size,
final_momentum, momentum_slope, evaluate_each,
model_name):
""" Train the Siamese net
This is the main function for training the siamese net.
In each every evaluate_each train iterations we evaluate one-shot tasks in
validation and evaluation set. We also write to the log file.
Arguments:
number_of_iterations: maximum number of iterations to train.
support_set_size: number of characters to use in the support set
in one-shot tasks.
final_momentum: mu_j in the paper. Each layer starts at 0.5 momentum
but evolves linearly to mu_j
momentum_slope: slope of the momentum evolution. In the paper we are
only told that this momentum evolves linearly. Because of that I
defined a slope to be passed to the training.
evaluate each: number of iterations defined to evaluate the one-shot
tasks.
model_name: save_name of the model
Returns:
Evaluation Accuracy
"""
# First of all let's divide randomly the 30 train alphabets in train
# and validation with 24 for training and 6 for validation
self.omniglot_loader.split_train_datasets()
# Variables that will store 100 iterations losses and accuracies
# after evaluate_each iterations these will be passed to tensorboard logs
train_losses = np.zeros(shape=(evaluate_each))
train_accuracies = np.zeros(shape=(evaluate_each))
count = 0
earrly_stop = 0
# Stop criteria variables
best_validation_accuracy = 0.0
best_accuracy_iteration = 0
validation_accuracy = 0.0
# Train loop
for iteration in range(number_of_iterations):
# train set
images, labels = self.omniglot_loader.get_train_batch()
train_loss, train_accuracy = self.model.train_on_batch(
images, labels)
# Decay learning rate 1 % per 500 iterations (in the paper the decay is
# 1% per epoch). Also update linearly the momentum (starting from 0.5 to 1)
if (iteration + 1) % 500 == 0:
K.set_value(self.model.optimizer.lr, K.get_value(
self.model.optimizer.lr) * 0.99)
if K.get_value(self.model.optimizer.momentum) < final_momentum:
K.set_value(self.model.optimizer.momentum, K.get_value(
self.model.optimizer.momentum) + momentum_slope)
train_losses[count] = train_loss
train_accuracies[count] = train_accuracy
# validation set
count += 1
print('Iteration %d/%d: Train loss: %f, Train Accuracy: %f, lr = %f' %
(iteration + 1, number_of_iterations, train_loss, train_accuracy, K.get_value(
self.model.optimizer.lr)))
# Each 100 iterations perform a one_shot_task and write to tensorboard the
# stored losses and accuracies
if (iteration + 1) % evaluate_each == 0:
number_of_runs_per_alphabet = 40
# use a support set size equal to the number of character in the alphabet
validation_accuracy = self.omniglot_loader.one_shot_test(
self.model, support_set_size, number_of_runs_per_alphabet, is_validation=True)
self._write_logs_to_tensorboard(
iteration, train_losses, train_accuracies,
validation_accuracy, evaluate_each)
count = 0
# Some hyperparameters lead to 100%, although the output is almost the same in
# all images.
if (validation_accuracy == 1.0 and train_accuracy == 0.5):
print('Early Stopping: Gradient Explosion')
print('Validation Accuracy = ' +
str(best_validation_accuracy))
return 0
elif train_accuracy == 0.0:
return 0
else:
# Save the model
if validation_accuracy > best_validation_accuracy:
best_validation_accuracy = validation_accuracy
best_accuracy_iteration = iteration
model_json = self.model.to_json()
if not os.path.exists('./models'):
os.makedirs('./models')
with open('models/' + model_name + '.json', "w") as json_file:
json_file.write(model_json)
self.model.save_weights('models/' + model_name + '.h5')
# If accuracy does not improve for 10000 batches stop the training
if iteration - best_accuracy_iteration > 10000:
print(
'Early Stopping: validation accuracy did not increase for 10000 iterations')
print('Best Validation Accuracy = ' +
str(best_validation_accuracy))
print('Validation Accuracy = ' + str(best_validation_accuracy))
break
print('Trained Ended!')
return best_validation_accuracy