-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss_calculator.py
46 lines (36 loc) · 1.61 KB
/
loss_calculator.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
import tensorflow as tf
def triplet_loss(margins,oa,op,on):
margin_0 = margins[0]
margin_1 = margins[1]
margin_2 = margins[2]
eucd_p = tf.pow(tf.subtract(oa, op), 2)
eucd_p = tf.reduce_sum(eucd_p, 1)
eucd_p = tf.sqrt(eucd_p + 1e-6)
eucd_n1 = tf.pow(tf.subtract(oa, on), 2)
eucd_n1 = tf.reduce_sum(eucd_n1, 1)
eucd_n1 = tf.sqrt(eucd_n1 + 1e-6)
eucd_n2 = tf.pow(tf.subtract(op, on), 2)
eucd_n2 = tf.reduce_sum(eucd_n2, 1)
eucd_n2 = tf.sqrt(eucd_n2 + 1e-6)
random_negative_margin = tf.constant(margin_0)
rand_neg = tf.pow(tf.maximum(tf.subtract(random_negative_margin,
tf.minimum(eucd_n1, eucd_n2)), 0), 2)
positive_margin = tf.constant(margin_1)
with tf.name_scope('all_loss'):
# invertable loss for standard patches
with tf.name_scope('rand_neg'):
rand_neg = tf.pow(tf.maximum(tf.subtract(random_negative_margin,
tf.minimum(eucd_n1, eucd_n2)), 0), 2)
# covariance loss for transformed patches
with tf.name_scope('pos'):
pos = tf.pow(tf.maximum(tf.subtract(positive_margin,
tf.subtract(tf.minimum(eucd_n1, eucd_n2), eucd_p)), 0), 2)
# total loss
with tf.name_scope('loss'):
losses = rand_neg + pos
loss = tf.reduce_mean(losses)
# write summary
tf.summary.scalar('random_negative_loss', rand_neg)
tf.summary.scalar('positive_loss', pos)
tf.summary.scalar('total_loss', loss)
return loss, eucd_p, eucd_n1, eucd_n2