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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With