Commit ad371929 authored by Patrick Wieschollek's avatar Patrick Wieschollek Committed by Yuxin Wu

Add Livedata (#91)

* add live feed to progressbar

* fix cosine algorithm
parent e1bb7c2f
...@@ -77,7 +77,7 @@ class SiameseModel(EmbeddingModel): ...@@ -77,7 +77,7 @@ class SiameseModel(EmbeddingModel):
tf.identity(self.embed(inputs[0]), name="emb") tf.identity(self.embed(inputs[0]), name="emb")
# compute the actual loss # compute the actual loss
cost, pos_dist, neg_dist = symbf.contrastive_loss(x, y, label, 5., extra=True) cost, pos_dist, neg_dist = symbf.contrastive_loss(x, y, label, 5., extra=True, scope="loss")
self.cost = tf.identity(cost, name="cost") self.cost = tf.identity(cost, name="cost")
# track these values during training # track these values during training
...@@ -92,7 +92,7 @@ class CosineModel(SiameseModel): ...@@ -92,7 +92,7 @@ class CosineModel(SiameseModel):
with tf.variable_scope(tf.get_variable_scope(), reuse=True): with tf.variable_scope(tf.get_variable_scope(), reuse=True):
tf.identity(self.embed(inputs[0]), name="emb") tf.identity(self.embed(inputs[0]), name="emb")
cost = symbf.cosine_loss(x, y, label) cost = symbf.cosine_loss(x, y, label, scope="loss")
self.cost = tf.identity(cost, name="cost") self.cost = tf.identity(cost, name="cost")
add_moving_summary(self.cost) add_moving_summary(self.cost)
...@@ -110,7 +110,7 @@ class TripletModel(EmbeddingModel): ...@@ -110,7 +110,7 @@ class TripletModel(EmbeddingModel):
InputVar(tf.float32, (None, 28, 28), 'input_n')] InputVar(tf.float32, (None, 28, 28), 'input_n')]
def loss(self, a, p, n): def loss(self, a, p, n):
return symbf.triplet_loss(a, p, n, 5., extra=True) return symbf.triplet_loss(a, p, n, 5., extra=True, scope="loss")
def _build_graph(self, inputs): def _build_graph(self, inputs):
a, p, n = inputs a, p, n = inputs
...@@ -120,16 +120,17 @@ class TripletModel(EmbeddingModel): ...@@ -120,16 +120,17 @@ class TripletModel(EmbeddingModel):
tf.identity(self.embed(inputs[0]), name="emb") tf.identity(self.embed(inputs[0]), name="emb")
cost, pos_dist, neg_dist = self.loss(a, p, n) cost, pos_dist, neg_dist = self.loss(a, p, n)
self.cost = tf.identity(cost, name="cost") self.cost = tf.identity(cost, name="cost")
add_moving_summary(pos_dist, neg_dist, self.cost) add_moving_summary(pos_dist, neg_dist, self.cost)
class SoftTripletModel(TripletModel): class SoftTripletModel(TripletModel):
def loss(self, a, p, n): def loss(self, a, p, n):
return symbf.soft_triplet_loss(a, p, n) return symbf.soft_triplet_loss(a, p, n, scope="loss")
def get_config(model): def get_config(model, algorithm_name):
logger.auto_set_dir() logger.auto_set_dir()
dataset = model.get_data() dataset = model.get_data()
...@@ -137,6 +138,10 @@ def get_config(model): ...@@ -137,6 +138,10 @@ def get_config(model):
lr = symbf.get_scalar_var('learning_rate', 1e-4, summary=True) lr = symbf.get_scalar_var('learning_rate', 1e-4, summary=True)
extra_display = ["cost"]
if not algorithm_name == "cosine":
extra_display = extra_display + ["loss/pos-dist", "loss/neg-dist"]
return TrainConfig( return TrainConfig(
dataflow=dataset, dataflow=dataset,
model=model(), model=model(),
...@@ -145,6 +150,10 @@ def get_config(model): ...@@ -145,6 +150,10 @@ def get_config(model):
ModelSaver(), ModelSaver(),
ScheduledHyperParamSetter('learning_rate', [(10, 1e-5), (20, 1e-6)]) ScheduledHyperParamSetter('learning_rate', [(10, 1e-5), (20, 1e-6)])
], ],
extra_callbacks=[
MovingAverageSummary(),
ProgressBar(extra_display),
StatPrinter()],
steps_per_epoch=steps_per_epoch, steps_per_epoch=steps_per_epoch,
max_epoch=20, max_epoch=20,
) )
...@@ -213,7 +222,7 @@ if __name__ == '__main__': ...@@ -213,7 +222,7 @@ if __name__ == '__main__':
if FLAGS.visualize: if FLAGS.visualize:
visualize(FLAGS.load, ALGO_CONFIGS[FLAGS.algorithm]) visualize(FLAGS.load, ALGO_CONFIGS[FLAGS.algorithm])
else: else:
config = get_config(ALGO_CONFIGS[FLAGS.algorithm]) config = get_config(ALGO_CONFIGS[FLAGS.algorithm], FLAGS.algorithm)
if FLAGS.load: if FLAGS.load:
config.session_init = SaverRestore(FLAGS.load) config.session_init = SaverRestore(FLAGS.load)
else: else:
......
...@@ -70,13 +70,30 @@ class MaintainStepCounter(Callback): ...@@ -70,13 +70,30 @@ class MaintainStepCounter(Callback):
class ProgressBar(Callback): class ProgressBar(Callback):
""" A progress bar based on tqdm. Enabled by default. """ """ A progress bar based on tqdm. Enabled by default. """
def __init__(self, names=[]):
"""
Args:
names(list): list of string, the names of the tensors to display.
"""
super(ProgressBar, self).__init__()
self._names = [get_op_tensor_name(n)[1] for n in names]
self._tags = [get_op_tensor_name(n)[0].split("/")[-1] for n in names]
def _extra_fetches(self):
return self._names
def _before_train(self): def _before_train(self):
self._total = self.trainer.config.steps_per_epoch self._total = self.trainer.config.steps_per_epoch
self._tqdm_args = get_tqdm_kwargs(leave=True) self._tqdm_args = get_tqdm_kwargs(leave=True)
if self._names is not []:
self._tqdm_args['bar_format'] = self._tqdm_args['bar_format'] + "{postfix} "
def _trigger_step(self, *args): def _trigger_step(self, *args):
if self.local_step == 1: if self.local_step == 1:
self._bar = tqdm.trange(self._total, **self._tqdm_args) self._bar = tqdm.trange(self._total, **self._tqdm_args)
self._bar.set_postfix(zip(self._tags, args))
self._bar.update() self._bar.update()
if self.local_step == self._total: if self.local_step == self._total:
self._bar.close() self._bar.close()
...@@ -213,7 +213,7 @@ def saliency_map(output, input, name="saliency_map"): ...@@ -213,7 +213,7 @@ def saliency_map(output, input, name="saliency_map"):
return saliency_op return saliency_op
def contrastive_loss(left, right, y, margin, extra=False): def contrastive_loss(left, right, y, margin, extra=False, scope="constrastive_loss"):
r"""Loss for Siamese networks as described in the paper: r"""Loss for Siamese networks as described in the paper:
`Learning a Similarity Metric Discriminatively, with Application to Face `Learning a Similarity Metric Discriminatively, with Application to Face
Verification <http://yann.lecun.com/exdb/publis/pdf/chopra-05.pdf>`_ by Chopra et al. Verification <http://yann.lecun.com/exdb/publis/pdf/chopra-05.pdf>`_ by Chopra et al.
...@@ -231,7 +231,7 @@ def contrastive_loss(left, right, y, margin, extra=False): ...@@ -231,7 +231,7 @@ def contrastive_loss(left, right, y, margin, extra=False):
Returns: Returns:
tf.Tensor: constrastive_loss (averaged over the batch), (and optionally average_pos_dist, average_neg_dist) tf.Tensor: constrastive_loss (averaged over the batch), (and optionally average_pos_dist, average_neg_dist)
""" """
with tf.name_scope("constrastive_loss"): with tf.name_scope(scope):
y = tf.cast(y, tf.float32) y = tf.cast(y, tf.float32)
delta = tf.reduce_sum(tf.square(left - right), 1) delta = tf.reduce_sum(tf.square(left - right), 1)
...@@ -256,7 +256,7 @@ def contrastive_loss(left, right, y, margin, extra=False): ...@@ -256,7 +256,7 @@ def contrastive_loss(left, right, y, margin, extra=False):
return loss return loss
def cosine_loss(left, right, y): def cosine_loss(left, right, y, scope="cosine_loss"):
r"""Loss for Siamese networks (cosine version). r"""Loss for Siamese networks (cosine version).
Same as :func:`contrastive_loss` but with different similarity measurment. Same as :func:`contrastive_loss` but with different similarity measurment.
...@@ -280,14 +280,14 @@ def cosine_loss(left, right, y): ...@@ -280,14 +280,14 @@ def cosine_loss(left, right, y):
with tf.name_scope("l2_norm"): with tf.name_scope("l2_norm"):
return tf.sqrt(tf.reduce_sum(tf.square(t), 1) + eps) return tf.sqrt(tf.reduce_sum(tf.square(t), 1) + eps)
with tf.name_scope("cosine_loss"): with tf.name_scope(scope):
y = 2 * tf.cast(y, tf.float32) - 1 y = 2 * tf.cast(y, tf.float32) - 1
pred = tf.reduce_sum(left * right, 1) / (l2_norm(left) * l2_norm(right) + 1e-10) pred = tf.reduce_sum(left * right, 1) / (l2_norm(left) * l2_norm(right) + 1e-10)
return tf.nn.l2_loss(y - pred) / tf.cast(tf.shape(left)[0], tf.float32) return tf.nn.l2_loss(y - pred) / tf.cast(tf.shape(left)[0], tf.float32)
def triplet_loss(anchor, positive, negative, margin, extra=False): def triplet_loss(anchor, positive, negative, margin, extra=False, scope="triplet_loss"):
r"""Loss for Triplet networks as described in the paper: r"""Loss for Triplet networks as described in the paper:
`FaceNet: A Unified Embedding for Face Recognition and Clustering `FaceNet: A Unified Embedding for Face Recognition and Clustering
<https://arxiv.org/abs/1503.03832>`_ <https://arxiv.org/abs/1503.03832>`_
...@@ -312,7 +312,7 @@ def triplet_loss(anchor, positive, negative, margin, extra=False): ...@@ -312,7 +312,7 @@ def triplet_loss(anchor, positive, negative, margin, extra=False):
tf.Tensor: triplet-loss as scalar (and optionally average_pos_dist, average_neg_dist) tf.Tensor: triplet-loss as scalar (and optionally average_pos_dist, average_neg_dist)
""" """
with tf.name_scope("triplet_loss"): with tf.name_scope(scope):
d_pos = tf.reduce_sum(tf.square(anchor - positive), 1) d_pos = tf.reduce_sum(tf.square(anchor - positive), 1)
d_neg = tf.reduce_sum(tf.square(anchor - negative), 1) d_neg = tf.reduce_sum(tf.square(anchor - negative), 1)
...@@ -326,7 +326,7 @@ def triplet_loss(anchor, positive, negative, margin, extra=False): ...@@ -326,7 +326,7 @@ def triplet_loss(anchor, positive, negative, margin, extra=False):
return loss return loss
def soft_triplet_loss(anchor, positive, negative, extra=True): def soft_triplet_loss(anchor, positive, negative, extra=True, scope="soft_triplet_loss"):
"""Loss for triplet networks as described in the paper: """Loss for triplet networks as described in the paper:
`Deep Metric Learning using Triplet Network `Deep Metric Learning using Triplet Network
<https://arxiv.org/abs/1412.6622>`_ by Hoffer et al. <https://arxiv.org/abs/1412.6622>`_ by Hoffer et al.
...@@ -345,7 +345,7 @@ def soft_triplet_loss(anchor, positive, negative, extra=True): ...@@ -345,7 +345,7 @@ def soft_triplet_loss(anchor, positive, negative, extra=True):
""" """
eps = 1e-10 eps = 1e-10
with tf.name_scope("soft_triplet_loss"): with tf.name_scope(scope):
d_pos = tf.sqrt(tf.reduce_sum(tf.square(anchor - positive), 1) + eps) d_pos = tf.sqrt(tf.reduce_sum(tf.square(anchor - positive), 1) + eps)
d_neg = tf.sqrt(tf.reduce_sum(tf.square(anchor - negative), 1) + eps) d_neg = tf.sqrt(tf.reduce_sum(tf.square(anchor - negative), 1) + eps)
......
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