From c9271c233ab9d8571f1a3d2c222a6c74b3113338 Mon Sep 17 00:00:00 2001 From: nlitkowski Date: Wed, 16 Feb 2022 00:36:06 +0100 Subject: [PATCH] Add loss and metrics --- src/loss.py | 17 +++++++++++++++++ src/metrics.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/loss.py create mode 100644 src/metrics.py 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