Add loss and metrics

This commit is contained in:
nlitkowski 2022-02-16 00:36:06 +01:00
parent bc67061d69
commit c9271c233a
2 changed files with 33 additions and 0 deletions

17
src/loss.py Normal file
View File

@ -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

16
src/metrics.py Normal file
View File

@ -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