-
Notifications
You must be signed in to change notification settings - Fork 9
/
loss_utils.py
51 lines (34 loc) · 1.38 KB
/
loss_utils.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
import tensorflow as tf
epsilon = 1e-7
def conf_criterion_lp(im1, im2, conf_sigma): # factorized laplacian distribution
loss = tf.abs(im1 - im2)
if conf_sigma is not None:
loss = loss * 2 / (conf_sigma + epsilon) + tf.log(conf_sigma * 2 + epsilon)
loss = tf.reduce_mean(loss)
else:
loss = tf.reduce_mean(loss)
return loss
def conf_criterion(im1, im2, conf_sigma): # gaussian distribution
loss = tf.abs(im1 - im2)
if conf_sigma is not None:
loss = tf.math.exp(-conf_sigma) * 5 * loss + conf_sigma / 2
loss = tf.reduce_mean(loss)
else:
loss = tf.reduce_mean(loss)
return loss
def abs_criterion(in_, target):
return tf.reduce_mean(tf.abs(in_ - target))
def mae_criterion(in_, target):
return tf.reduce_mean((in_ - target) ** 2)
def sce_criterion(logits, labels):
return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
def mae_criterion_list(in_, target):
loss = 0.0
for i in range(len(target)):
loss += tf.reduce_mean((in_[i] - target[i]) ** 2)
return loss / len(target)
def sce_criterion_list(logits, labels):
loss = 0.0
for i in range(len(labels)):
loss += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits[i], labels=labels[i]))
return loss / len(labels)