keras: Smooth L1 loss

yuan zhou picture yuan zhou · May 23, 2017 · Viewed 10.2k times · Source

Try to customize loss function(smooth L1 loss) in keras like below

ValueError: Shape must be rank 0 but is rank 5 for 'cond/Switch' (op: 'Switch') with input shapes: [?,24,24,24,?], [?,24,24,24,?].

from keras import backend as K
import numpy as np


def smooth_L1_loss(y_true, y_pred):
    THRESHOLD = K.variable(1.0)
    mae = K.abs(y_true-y_pred)
    flag = K.greater(mae, THRESHOLD)
    loss = K.mean(K.switch(flag, (mae - 0.5), K.pow(mae, 2)), axis=-1)
    return loss

Answer

felixwege picture felixwege · May 22, 2019

I know I'm two years late to the party, but if you are using tensorflow as keras backend you can use tensorflow's Huber loss (which is essentially the same) like so:

import tensorflow as tf


def smooth_L1_loss(y_true, y_pred):
    return tf.losses.huber_loss(y_true, y_pred)