-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetric.py
33 lines (29 loc) · 1.38 KB
/
Metric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from keras import backend as K
import tensorflow as tf
## Evaluate Method
def k_recall(y_target, y_pred):
y_target_yn = K.round(K.clip(y_target, 0, 1))
y_pred_yn = K.round(K.clip(y_pred, 0, 1))
count_true_positive = K.sum(y_target_yn * y_pred_yn)
count_true_positive_false_negative = K.sum(y_target_yn)
recall = count_true_positive / (count_true_positive_false_negative + K.epsilon())
return recall
def k_precision(y_target, y_pred):
y_pred_yn = K.round(K.clip(y_pred, 0, 1))
y_target_yn = K.round(K.clip(y_target, 0, 1))
count_true_positive = K.sum(y_target_yn * y_pred_yn)
count_true_positive_false_positive = K.sum(y_pred_yn)
precision = count_true_positive / (count_true_positive_false_positive + K.epsilon())
return precision
def k_f1score(y_target, y_pred):
_recall = k_recall(y_target, y_pred)
_precision = k_precision(y_target, y_pred)
_f1score = ( 2 * _recall * _precision) / (_recall + _precision+ K.epsilon())
return _f1score
#### LOSS #####
def focal_loss(gamma=2., alpha=.25):
def focal_loss_fixed(y_true, y_pred):
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))
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))
return focal_loss_fixed