Skip to content
Snippets Groups Projects
Commit ef1c03ce authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Make Siamese network work

parent 9f006a6b
No related branches found
No related tags found
No related merge requests found
...@@ -46,6 +46,7 @@ class Analizer: ...@@ -46,6 +46,7 @@ class Analizer:
def extract_features(self): def extract_features(self):
data, labels = self.data_shuffler.get_batch(train_dataset=False) data, labels = self.data_shuffler.get_batch(train_dataset=False)
feed_dict = {self.feature_placeholder: data} feed_dict = {self.feature_placeholder: data}
return self.session.run([self.graph], feed_dict=feed_dict)[0], labels return self.session.run([self.graph], feed_dict=feed_dict)[0], labels
def __call__(self): def __call__(self):
...@@ -98,7 +99,7 @@ class Analizer: ...@@ -98,7 +99,7 @@ class Analizer:
threshold = bob.measure.eer_threshold(negative_scores, positive_scores) threshold = bob.measure.eer_threshold(negative_scores, positive_scores)
far, frr = bob.measure.farfrr(negative_scores, positive_scores, threshold) far, frr = bob.measure.farfrr(negative_scores, positive_scores, threshold)
eer = (far + frr) / 2. eer = (far + frr) / 2.
self.eer .append(eer) self.eer.append(eer)
# Computing FAR 10 # Computing FAR 10
threshold = bob.measure.far_threshold(negative_scores, positive_scores, far_value=0.1) threshold = bob.measure.far_threshold(negative_scores, positive_scores, far_value=0.1)
...@@ -115,4 +116,4 @@ class Analizer: ...@@ -115,4 +116,4 @@ class Analizer:
far, frr = bob.measure.farfrr(negative_scores, positive_scores, threshold) far, frr = bob.measure.farfrr(negative_scores, positive_scores, threshold)
self.far1000.append(frr) self.far1000.append(frr)
print eer return
...@@ -23,7 +23,7 @@ class DataShuffler(object): ...@@ -23,7 +23,7 @@ class DataShuffler(object):
""" """
self.perc_train = perc_train self.perc_train = perc_train
self.scale = True self.scale = scale
self.scale_value = 0.00390625 self.scale_value = 0.00390625
self.train_batch_size = train_batch_size self.train_batch_size = train_batch_size
self.validation_batch_size = validation_batch_size self.validation_batch_size = validation_batch_size
......
...@@ -27,20 +27,20 @@ class ContrastiveLoss(BaseLoss): ...@@ -27,20 +27,20 @@ class ContrastiveLoss(BaseLoss):
""" """
def __init__(self): def __init__(self, contrastive_margin=1.0):
return self.contrastive_margin = contrastive_margin
def __call__(self, label, left_feature, right_feature, margin): def __call__(self, label, left_feature, right_feature):
with tf.name_scope("contrastive_loss"): with tf.name_scope("contrastive_loss"):
label = tf.to_float(label) label = tf.to_float(label)
one = tf.constant(1.0) one = tf.constant(1.0)
d = compute_euclidean_distance(left_feature, right_feature) d = compute_euclidean_distance(left_feature, right_feature)
between_class = tf.exp(tf.mul(one - label, tf.square(d))) # (1-Y)*(d^2) between_class = tf.exp(tf.mul(one - label, tf.square(d))) # (1-Y)*(d^2)
max_part = tf.square(tf.maximum(margin - d, 0)) max_part = tf.square(tf.maximum(self.contrastive_margin - d, 0))
within_class = tf.mul(label, max_part) # (Y) * max((margin - d)^2, 0) within_class = tf.mul(label, max_part) # (Y) * max((margin - d)^2, 0)
loss = 0.5 * tf.reduce_mean(within_class + between_class) loss = 0.5 * tf.reduce_mean(within_class + between_class)
return tf.reduce_mean(loss), tf.reduce_mean(between_class), tf.reduce_mean(within_class) return loss, tf.reduce_mean(between_class), tf.reduce_mean(within_class)
...@@ -24,6 +24,7 @@ class Lenet(SequenceNetwork): ...@@ -24,6 +24,7 @@ class Lenet(SequenceNetwork):
fc1_output=400, fc1_output=400,
n_classes=10, n_classes=10,
feature_layer="fc2",
seed=10, use_gpu = False): seed=10, use_gpu = False):
""" """
...@@ -41,7 +42,7 @@ class Lenet(SequenceNetwork): ...@@ -41,7 +42,7 @@ class Lenet(SequenceNetwork):
seed = 10 seed = 10
""" """
super(Lenet, self).__init__(feature_layer="fc2") super(Lenet, self).__init__(feature_layer=feature_layer)
self.add(Conv2D(name="conv1", kernel_size=conv1_kernel_size, filters=conv1_output, activation=tf.nn.tanh)) self.add(Conv2D(name="conv1", kernel_size=conv1_kernel_size, filters=conv1_output, activation=tf.nn.tanh))
self.add(MaxPooling(name="pooling1")) self.add(MaxPooling(name="pooling1"))
......
...@@ -44,7 +44,7 @@ def main(): ...@@ -44,7 +44,7 @@ def main():
data_shuffler = PairDataShuffler(data, labels) data_shuffler = PairDataShuffler(data, labels)
# Preparing the architecture # Preparing the architecture
lenet = Lenet() lenet = Lenet(feature_layer="fc1")
loss = ContrastiveLoss() loss = ContrastiveLoss()
trainer = SiameseTrainer(architecture=lenet, loss=loss, iterations=ITERATIONS, base_lr=0.00001) trainer = SiameseTrainer(architecture=lenet, loss=loss, iterations=ITERATIONS, base_lr=0.00001)
......
...@@ -42,6 +42,7 @@ class SiameseTrainer(object): ...@@ -42,6 +42,7 @@ class SiameseTrainer(object):
self.weight_decay = weight_decay self.weight_decay = weight_decay
self.convergence_threshold = convergence_threshold self.convergence_threshold = convergence_threshold
def train(self, data_shuffler): def train(self, data_shuffler):
""" """
Do the loop forward --> backward --| Do the loop forward --> backward --|
...@@ -64,10 +65,9 @@ class SiameseTrainer(object): ...@@ -64,10 +65,9 @@ class SiameseTrainer(object):
train_right_graph = self.architecture.compute_graph(train_placeholder_right_data) train_right_graph = self.architecture.compute_graph(train_placeholder_right_data)
feature_graph = self.architecture.compute_graph(feature_placeholder, cut=True) feature_graph = self.architecture.compute_graph(feature_placeholder, cut=True)
loss_train, between_class, within_class = self.loss(train_placeholder_labels, loss_train, within_class, between_class = self.loss(train_placeholder_labels,
train_left_graph, train_left_graph,
train_right_graph, train_right_graph)
0.2)
batch = tf.Variable(0) batch = tf.Variable(0)
learning_rate = tf.train.exponential_decay( learning_rate = tf.train.exponential_decay(
...@@ -76,8 +76,10 @@ class SiameseTrainer(object): ...@@ -76,8 +76,10 @@ class SiameseTrainer(object):
data_shuffler.train_data.shape[0], data_shuffler.train_data.shape[0],
self.weight_decay # Decay step self.weight_decay # Decay step
) )
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_train, #optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_train,
global_step=batch) # global_step=batch)
optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=0.99, use_locking=False,
name='Momentum').minimize(loss_train, global_step=batch)
#train_prediction = tf.nn.softmax(train_graph) #train_prediction = tf.nn.softmax(train_graph)
#validation_prediction = tf.nn.softmax(validation_graph) #validation_prediction = tf.nn.softmax(validation_graph)
...@@ -112,5 +114,7 @@ class SiameseTrainer(object): ...@@ -112,5 +114,7 @@ class SiameseTrainer(object):
if step % self.snapshot == 0: if step % self.snapshot == 0:
analizer() analizer()
print str(step) + " - " + str(analizer.eer[-1])
train_writer.close() train_writer.close()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment