diff --git a/src/loss.py b/src/loss.py new file mode 100644 index 0000000..242037b --- /dev/null +++ b/src/loss.py @@ -0,0 +1,17 @@ +import tensorflow as tf +from metrics import IOU + + +class jaccard_loss(tf.keras.losses.Loss): + + def __init__(self, + smooth=100 + ): + super().__init__() + self.smooth = smooth + + def call(self, + y_true, + y_pred): + iou = IOU(y_true, y_pred, self.smooth) + return (1 - iou) * self.smooth diff --git a/src/metrics.py b/src/metrics.py new file mode 100644 index 0000000..6f4a985 --- /dev/null +++ b/src/metrics.py @@ -0,0 +1,16 @@ +from keras import backend as K + + +def IOU(y_true, + y_pred, + smooth=1e-7): + ''' + Taken from https://github.com/keras-team/keras-contrib/blob/master/keras_contrib/losses/jaccard.py + ''' + intersection = K.sum(K.abs(y_true * y_pred), axis=-1) + sum_ = K.sum(K.abs(y_true) + K.abs(y_pred), axis=-1) + + jacc = (intersection + smooth) / \ + (sum_ - intersection + smooth) + + return jacc