|
a |
|
b/metrics.py |
|
|
1 |
import numpy as np |
|
|
2 |
import tensorflow as tf |
|
|
3 |
from tensorflow.keras import backend as K |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def iou(y_true, y_pred): |
|
|
7 |
def f(y_true, y_pred): |
|
|
8 |
intersection = (y_true * y_pred).sum() |
|
|
9 |
union = y_true.sum() + y_pred.sum() - intersection |
|
|
10 |
x = (intersection + 1e-15) / (union + 1e-15) |
|
|
11 |
x = x.astype(np.float32) |
|
|
12 |
return x |
|
|
13 |
|
|
|
14 |
return tf.numpy_function(f, [y_true, y_pred], tf.float32) |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
smooth = 1e-15 |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
def dice_coef(y_true, y_pred): |
|
|
21 |
y_true = tf.keras.layers.Flatten()(y_true) |
|
|
22 |
y_pred = tf.keras.layers.Flatten()(y_pred) |
|
|
23 |
intersection = tf.reduce_sum(y_true * y_pred) |
|
|
24 |
return (2.0 * intersection + smooth) / ( |
|
|
25 |
tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth |
|
|
26 |
) |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
def dice_loss(y_true, y_pred): |
|
|
30 |
return 1.0 - dice_coef(y_true, y_pred) |