new losses

parent 3070af95
Pipeline #7626 failed with stages
in 4 minutes and 51 seconds
......@@ -44,8 +44,7 @@ class OnlineSampling(object):
if self.feature_placeholder is None:
shape = tuple([None] + list(data.shape[1:]))
self.feature_placeholder = tf.placeholder(tf.float32, shape=shape, name="feature")
embbeding = self.feature_extractor.compute_graph(self.feature_placeholder, self.feature_extractor.default_feature_layer,
training=False)
embbeding = self.feature_extractor.compute_graph(self.feature_placeholder, self.feature_extractor.default_feature_layer)
self.graph = tf.nn.l2_normalize(embbeding, 1, 1e-10, name='embeddings')
......@@ -54,4 +53,4 @@ class OnlineSampling(object):
#self.feature_placeholder.set_shape(data.shape)
feed_dict = {self.feature_placeholder: data}
return self.session.run([self.graph], feed_dict=feed_dict)[0]
\ No newline at end of file
return self.session.run([self.graph], feed_dict=feed_dict)[0]
......@@ -12,7 +12,7 @@ from .OnlineSampling import OnlineSampling
from scipy.spatial.distance import euclidean, cdist
import logging
logger = logging.getLogger("bob.learn.tensorflow")
logger = logging.getLogger("bob.learn")
from bob.learn.tensorflow.datashuffler.Normalizer import Linear
......@@ -216,11 +216,11 @@ class TripletWithFastSelectionDisk(Triplet, Disk, OnlineSampling):
for i in range(self.shape[0]):
label = anchor_labels[i]
possible_candidates = [d if d > d_anchor_positive[i] else numpy.inf for d in d_anchor_negative[i]]
for j in numpy.argsort(possible_candidates):
# Checking if they don't have the same label
if indexes[j] != label:
if self.labels[indexes[j]] != label:
samples_n[i, ...] = temp_samples_n[j, ...]
if numpy.isinf(possible_candidates[j]):
logger.info("SEMI-HARD negative not found, took the first one")
......
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 10 Aug 2016 16:38 CEST
import logging
logger = logging.getLogger("bob.learn.tensorflow")
import tensorflow as tf
from .BaseLoss import BaseLoss
from bob.learn.tensorflow.utils import compute_euclidean_distance
class TripletAverageLoss(BaseLoss):
"""
Compute the triplet loss as in
Schroff, Florian, Dmitry Kalenichenko, and James Philbin.
"Facenet: A unified embedding for face recognition and clustering."
Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2015.
:math:`L = sum( |f_a - f_p|^2 - |f_a - f_n|^2 + \lambda)`
**Parameters**
left_feature:
First element of the pair
right_feature:
Second element of the pair
label:
Label of the pair (0 or 1)
margin:
Contrastive margin
"""
def __init__(self, margin=0.1):
self.margin = margin
def __call__(self, anchor_embedding, positive_embedding, negative_embedding):
with tf.name_scope("triplet_loss"):
# Normalize
anchor_embedding = tf.nn.l2_normalize(anchor_embedding, 1, 1e-10, name="anchor")
positive_embedding = tf.nn.l2_normalize(positive_embedding, 1, 1e-10, name="positive")
negative_embedding = tf.nn.l2_normalize(negative_embedding, 1, 1e-10, name="negative")
anchor_mean = tf.reduce_mean(anchor_embedding, 0)
d_positive = tf.reduce_sum(tf.square(tf.subtract(anchor_mean, positive_embedding)), 1)
d_negative = tf.reduce_sum(tf.square(tf.subtract(anchor_mean, negative_embedding)), 1)
basic_loss = tf.add(tf.subtract(d_positive, d_negative), self.margin)
loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)
return loss, tf.reduce_mean(d_negative), tf.reduce_mean(d_positive)
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# @author: Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
# @date: Wed 10 Aug 2016 16:38 CEST
import logging
logger = logging.getLogger("bob.learn.tensorflow")
import tensorflow as tf
from .BaseLoss import BaseLoss
from bob.learn.tensorflow.utils import compute_euclidean_distance
class TripletFisherLoss(BaseLoss):
"""
Compute the triplet loss as in
Schroff, Florian, Dmitry Kalenichenko, and James Philbin.
"Facenet: A unified embedding for face recognition and clustering."
Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. 2015.
:math:`L = sum( |f_a - f_p|^2 - |f_a - f_n|^2 + \lambda)`
**Parameters**
left_feature:
First element of the pair
right_feature:
Second element of the pair
label:
Label of the pair (0 or 1)
margin:
Contrastive margin
"""
def __init__(self, margin=0.2):
self.margin = margin
def body(self, mean, x):
buffer = mean - x
return tf.matmul(buffer, tf.transpose(buffer))
"""
def cond(i):
return tf.reduce_sum(i) < 10
def body(i):
return tf.add(i, 1)
i = tf.placeholder(tf.float32)
op = tf.while_loop(cond, body, [i])
print(session.run(op, feed_dict={i: 0}))
"""
def __call__(self, anchor_embedding, positive_embedding, negative_embedding):
with tf.name_scope("triplet_loss"):
# Normalize
anchor_embedding = tf.nn.l2_normalize(anchor_embedding, 1, 1e-10, name="anchor")
positive_embedding = tf.nn.l2_normalize(positive_embedding, 1, 1e-10, name="positive")
negative_embedding = tf.nn.l2_normalize(negative_embedding, 1, 1e-10, name="negative")
#anchor_mean = tf.reduce_mean(anchor_embedding, 0)
#result = tf.while_loop(condition, self.body(anchor_mean), [positive_embedding])
#p_minus_mean = tf.subtract(anchor_mean, positive_embedding)
#s_w = tf.divide(tf.matmul(tf.transpose(p_minus_mean), p_minus_mean), 1)
#s_w = tf.trace(tf.reduce_mean(tf.square(tf.subtract(anchor_mean, positive_embedding)), 1))
#s_b = tf.trace(tf.reduce_mean(tf.square(tf.subtract(anchor_mean, negative_embedding)), 1))
#s_w = tf.reduce_mean(tf.square(tf.subtract(anchor_mean, positive_embedding)), 1)
#loss = s_w/s_b
#return s_w, p_minus_mean
#return tf.multiply(p_minus_mean, tf.transpose(p_minus_mean))
......@@ -44,9 +44,9 @@ class TripletLoss(BaseLoss):
with tf.name_scope("triplet_loss"):
# Normalize
anchor_embedding = tf.nn.l2_normalize(anchor_embedding, 1, 1e-10)
positive_embedding = tf.nn.l2_normalize(positive_embedding, 1, 1e-10)
negative_embedding = tf.nn.l2_normalize(negative_embedding, 1, 1e-10)
anchor_embedding = tf.nn.l2_normalize(anchor_embedding, 1, 1e-10, name="anchor")
positive_embedding = tf.nn.l2_normalize(positive_embedding, 1, 1e-10, name="positive")
negative_embedding = tf.nn.l2_normalize(negative_embedding, 1, 1e-10, name="negative")
d_positive = tf.reduce_sum(tf.square(tf.subtract(anchor_embedding, positive_embedding)), 1)
d_negative = tf.reduce_sum(tf.square(tf.subtract(anchor_embedding, negative_embedding)), 1)
......
from .BaseLoss import BaseLoss
from .ContrastiveLoss import ContrastiveLoss
from .TripletLoss import TripletLoss
from .TripletAverageLoss import TripletAverageLoss
from .TripletFisherLoss import TripletFisherLoss
from .NegLogLoss import NegLogLoss
......@@ -22,6 +24,8 @@ __appropriate__(
BaseLoss,
ContrastiveLoss,
TripletLoss,
TripletFisherLoss,
TripletAverageLoss,
NegLogLoss,
)
__all__ = [_ for _ in dir() if not _.startswith('_')]
......
......@@ -25,7 +25,7 @@ import imp
def main():
args = docopt(__doc__, version='Train Neural Net')
#USE_GPU = args['--use-gpu']
USE_GPU = args['--use-gpu']
OUTPUT_DIR = str(args['--output-dir'])
PREFETCH = args['--prefetch']
ITERATIONS = int(args['--iterations'])
......@@ -43,7 +43,9 @@ def main():
prefetch=PREFETCH,
learning_rate=config.learning_rate,
temp_dir=OUTPUT_DIR,
model_from_file=PRETRAINED_NET
snapshot=10,
model_from_file=PRETRAINED_NET,
use_gpu=USE_GPU
)
trainer.train(config.train_data_shuffler)
......@@ -436,7 +436,6 @@ class Trainer(object):
# TENSOR BOARD SUMMARY
self.train_summary_writter = tf.summary.FileWriter(os.path.join(self.temp_dir, 'train'), self.session.graph)
for step in range(start_step, self.iterations):
start = time.time()
self.fit(step)
end = time.time()
......
......@@ -5,7 +5,7 @@
import tensorflow as tf
from .singleton import Singleton
from tensorflow.python import debug as tf_debug
@Singleton
class Session(object):
......@@ -15,3 +15,4 @@ class Session(object):
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333))
config.gpu_options.allow_growth = True
self.session = tf.Session()
#self.session = tf_debug.LocalCLIDebugWrapperSession(self.session)
......@@ -17,7 +17,6 @@ def compute_euclidean_distance(x, y):
d = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x, y)), 1))
return d
def load_mnist(data_dir="./src/bob.db.mnist/bob/db/mnist/", perc_train=0.9):
import bob.db.mnist
......@@ -189,3 +188,25 @@ def compute_accuracy(data_train, labels_train, data_validation, labels_validatio
tp += 1
return (float(tp) / data_validation.shape[0]) * 100
def debug_embbeding(image, architecture, embbeding_dim=2, feature_layer="fc3"):
"""
"""
import tensorflow as tf
from bob.learn.tensorflow.network import SequenceNetwork
from bob.learn.tensorflow.utils.session import Session
session = Session.instance(new=False).session
inference_graph = architecture.compute_graph(architecture.inference_placeholder, feature_layer=feature_layer, training=False)
embeddings = numpy.zeros(shape=(image.shape[0], embbeding_dim))
for i in range(image.shape[0]):
feed_dict = {architecture.inference_placeholder: image[i:i+1, :,:,:]}
embedding = session.run([tf.nn.l2_normalize(inference_graph, 1, 1e-10)], feed_dict=feed_dict)[0]
embedding = numpy.reshape(embedding, numpy.prod(embedding.shape[1:]))
embeddings[i] = embedding
return embeddings
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment