import tensorflow as tf from tensorflow.python.ops import array_ops from keras import backend as K def multi_category_focal_loss2(gamma=2., alpha=1): epsilon = 1.e-7 gamma = float(gamma) alpha = tf.constant(alpha, dtype=tf.float32) def multi_category_focal_loss2_fixed(y_true, y_pred): y_true = tf.cast(y_true, tf.float32) y_pred = tf.clip_by_value(y_pred, epsilon, 1. - epsilon) alpha_t = y_true * alpha + (tf.ones_like(y_true) - y_true) * (1 - alpha) y_t = tf.multiply(y_true, y_pred) + tf.multiply(1 - y_true, 1 - y_pred) ce = -tf.log(y_t) weight = tf.pow(tf.subtract(1., y_t), gamma) fl = tf.multiply(tf.multiply(weight, ce), alpha_t) loss = tf.reduce_mean(fl) return loss return multi_category_focal_loss2_fixed def multi_category_focal_loss1(alpha, gamma=2.0): epsilon = 1.e-7 alpha = tf.constant(alpha, dtype=tf.float32) gamma = float(gamma) def multi_category_focal_loss1_fixed(y_true, y_pred): y_true = tf.cast(y_true, tf.float32) y_pred = tf.clip_by_value(y_pred, epsilon, 1. - epsilon) y_t = tf.multiply(y_true, y_pred) + tf.multiply(1-y_true, 1-y_pred) ce = -tf.log(y_t) weight = tf.pow(tf.subtract(1., y_t), gamma) fl = tf.matmul(tf.multiply(weight, ce), alpha) loss = tf.reduce_mean(fl) return loss return multi_category_focal_loss1_fixed def Cross_entropy_loss(y_true, y_pred): y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon()) crossEntropyLoss = -y_true * tf.log(y_pred) return tf.reduce_sum(crossEntropyLoss, -1) def focal_loss(classes_num, gamma=2., alpha=.25, e=0.1): def focal_loss_fixed(target_tensor, prediction_tensor): zeros = array_ops.zeros_like(prediction_tensor, dtype=prediction_tensor.dtype) one_minus_p = array_ops.where(tf.greater(target_tensor,zeros), target_tensor - prediction_tensor, zeros) FT = -1 * (one_minus_p ** gamma) * tf.log(tf.clip_by_value(prediction_tensor, 1e-8, 1.0)) classes_weight = array_ops.zeros_like(prediction_tensor, dtype=prediction_tensor.dtype) total_num = float(sum(classes_num)) classes_w_t1 = [ total_num / ff for ff in classes_num ] sum_ = sum(classes_w_t1) classes_w_t2 = [ ff/sum_ for ff in classes_w_t1 ] #scale classes_w_tensor = tf.convert_to_tensor(classes_w_t2, dtype=prediction_tensor.dtype) classes_weight += classes_w_tensor alpha = array_ops.where(tf.greater(target_tensor, zeros), classes_weight, zeros) balanced_fl = alpha * FT balanced_fl = tf.reduce_mean(balanced_fl) nb_classes = len(classes_num) fianal_loss = (1-e) * balanced_fl + e * K.categorical_crossentropy(K.ones_like(prediction_tensor)/nb_classes, prediction_tensor) return fianal_loss return focal_loss_fixed