Add loss and metrics
This commit is contained in:
parent
bc67061d69
commit
c9271c233a
17
src/loss.py
Normal file
17
src/loss.py
Normal 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
16
src/metrics.py
Normal 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
|
Loading…
Reference in New Issue
Block a user