-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
56 lines (47 loc) · 1.38 KB
/
metrics.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
def accuracy_fn(pred_labels, gt_labels):
'''
Accuracy score
Args:
pred_labels: N prediction labels
gt_labels: N corresponding gt labels
Returns:
returns the computed accuracy
'''
return np.mean(pred_labels == gt_labels)*100
def macrof1_fn(pred_labels,gt_labels):
'''
Macro F1 score
Arguments:
pred_labels: N prediction labels
gt_labels: N corresponding gt labels
Returns:
returns the computed macro f1 score
'''
class_ids = np.unique(gt_labels)
macrof1 = 0
for val in class_ids:
predpos = (pred_labels == val)
gtpos = (gt_labels==val)
tp = sum(predpos*gtpos)
fp = sum(predpos*~gtpos)
fn = sum(~predpos*gtpos)
if tp == 0:
continue
else:
precision = tp/(tp+fp)
recall = tp/(tp+fn)
macrof1 += 2*(precision*recall)/(precision+recall)
return macrof1/len(class_ids)
def mse_fn(pred,gt):
'''
Mean Squared Error
Arguments:
pred: NxD prediction matrix
gt: NxD groundtruth values for each predictions
Returns:
returns the computed loss
'''
loss = (pred-gt)**2
loss = np.mean(loss)
return loss