def f1_score(y_true, y_pred): """calcultes f1 score """ true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) possible_positives = K.sum(K.round(K.clip(y_true, 0, 1))) predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1))) precision = true_positives / (predicted_positives + K.epsilon()) recall = true_positives / (possible_positives + K.epsilon()) return 2 * ((precision * recall) / (precision + recall + K.epsilon()))
def f1_loss(y_true, y_pred): tp = K.sum(K.cast(y_true * y_pred, 'float'), axis=0) tn = K.sum(K.cast((1 - y_true) * (1 - y_pred), 'float'), axis=0) fp = K.sum(K.cast((1 - y_true) * y_pred, 'float'), axis=0) fn = K.sum(K.cast(y_true * (1 - y_pred), 'float'), axis=0) p = tp / (tp + fp + K.epsilon()) r = tp / (tp + fn + K.epsilon()) f1 = 2 * p * r / (p + r + K.epsilon()) f1 = tf.where(tf.math.is_nan(f1), tf.zeros_like(f1), f1) return 1 - K.mean(f1)
def R(y_true, y_pred): true_positives = K.sum( K.cast(K.greater(K.clip(y_true * y_pred, 0, 1), 0.20), 'float32')) poss_positives = K.sum( K.cast(K.greater(K.clip(y_true, 0, 1), 0.20), 'float32')) recall = true_positives / (poss_positives + K.epsilon()) return recall
def P(y_true, y_pred): true_positives = K.sum( K.cast(K.greater(K.clip(y_true * y_pred, 0, 1), 0.20), 'float32')) pred_positives = K.sum( K.cast(K.greater(K.clip(y_pred, 0, 1), 0.20), 'float32')) precision = true_positives / (pred_positives + K.epsilon()) return precision
def bbox_ciou (boxes1, boxes2): boxes1_x0y0x1y1 = tf.concat([boxes1[..., :2] - boxes1[..., 2:] * 0.5, boxes1[..., :2] + boxes1[..., 2:] * 0.5], axis=-1) boxes2_x0y0x1y1 = tf.concat([boxes2[..., :2] - boxes2[..., 2:] * 0.5, boxes2[..., :2] + boxes2[..., 2:] * 0.5], axis=-1) boxes1_x0y0x1y1 = tf.concat([tf.minimum(boxes1_x0y0x1y1[..., :2], boxes1_x0y0x1y1[..., 2:]), tf.maximum(boxes1_x0y0x1y1[..., :2], boxes1_x0y0x1y1[..., 2:])], axis=-1) boxes2_x0y0x1y1 = tf.concat([tf.minimum(boxes2_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., 2:]), tf.maximum(boxes2_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., 2:])], axis=-1) boxes1_area = (boxes1_x0y0x1y1[..., 2] - boxes1_x0y0x1y1[..., 0]) * ( boxes1_x0y0x1y1[..., 3] - boxes1_x0y0x1y1[..., 1]) boxes2_area = (boxes2_x0y0x1y1[..., 2] - boxes2_x0y0x1y1[..., 0]) * ( boxes2_x0y0x1y1[..., 3] - boxes2_x0y0x1y1[..., 1]) left_up = tf.maximum(boxes1_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., :2]) right_down = tf.minimum(boxes1_x0y0x1y1[..., 2:], boxes2_x0y0x1y1[..., 2:]) inter_section = tf.maximum(right_down - left_up, 0.0) inter_area = inter_section[..., 0] * inter_section[..., 1] union_area = boxes1_area + boxes2_area - inter_area iou = inter_area / (union_area + K.epsilon()) enclose_left_up = tf.minimum(boxes1_x0y0x1y1[..., :2], boxes2_x0y0x1y1[..., :2]) enclose_right_down = tf.maximum(boxes1_x0y0x1y1[..., 2:], boxes2_x0y0x1y1[..., 2:]) enclose_wh = enclose_right_down - enclose_left_up enclose_c2 = K.pow(enclose_wh[..., 0], 2) + K.pow(enclose_wh[..., 1], 2) p2 = K.pow(boxes1[..., 0] - boxes2[..., 0], 2) + K.pow(boxes1[..., 1] - boxes2[..., 1], 2) atan1 = tf.atan(boxes1[..., 2] / (boxes1[..., 3] + K.epsilon())) atan2 = tf.atan(boxes2[..., 2] / (boxes2[..., 3] + K.epsilon())) v = 4.0 * K.pow(atan1 - atan2, 2) / (math.pi ** 2) a = v / (1 - iou + v) ciou = iou - 1.0 * p2 / enclose_c2 - 1.0 * a * v return ciou
def FocalLoss(y_true, y_pred): """ :param y_true: A tensor of the same shape as `y_pred` :param y_pred: A tensor resulting from a sigmoid :return: Output tensor. """ gamma = 2.0 alpha = 0.25 pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred)) pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred)) epsilon = K.epsilon() # clip to prevent NaN's and Inf's pt_1 = K.clip(pt_1, epsilon, 1. - epsilon) pt_0 = K.clip(pt_0, epsilon, 1. - epsilon) return -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) \ - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))
def euclidean_distance(x, y): sum_sqrt = K.sum(K.square(x - y), axis=1, keepdims=True) return K.sqrt(K.maximum(sum_sqrt, K.epsilon()))