-
Notifications
You must be signed in to change notification settings - Fork 14
/
loss.py
33 lines (23 loc) · 1.03 KB
/
loss.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
import tensorflow as tf
def IOU_(y_pred, y_true):
"""Returns a (approx) IOU score
intesection = y_pred.flatten() * y_true.flatten()
Then, IOU = 2 * intersection / (y_pred.sum() + y_true.sum() + 1e-7) + 1e-7
Args:
y_pred (4-D array): (N, H, W, 1)
y_true (4-D array): (N, H, W, 1)
Returns:
float: IOU score
"""
H, W, _ = y_pred.get_shape().as_list()[1:]
pred_flat = tf.reshape(y_pred, [-1, H * W])
true_flat = tf.reshape(y_true, [-1, H * W])
intersection = 2 * tf.reduce_sum(pred_flat * true_flat, axis=1) + 1e-7
denominator = tf.reduce_sum(pred_flat, axis=1) + tf.reduce_sum(true_flat, axis=1) + 1e-7
return -tf.reduce_mean(intersection / denominator)
def cross_entropy(y_pred, y_true):
H, W, _ = y_pred.get_shape().as_list()[1:]
pred_flat = tf.reshape(y_pred, [-1, H * W])
true_flat = tf.reshape(y_true, [-1, H * W])
cross_entropy_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred_flat, labels=true_flat))
return cross_entropy_loss