forked from zhangyongshun/BagofTricks-LT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
107 lines (87 loc) · 3.18 KB
/
evaluate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import torch
import numpy as np
from matplotlib import pyplot as plt
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count if self.count != 0 else 0
class FusionMatrix(object):
def __init__(self, num_classes):
self.num_classes = num_classes
self.reset()
def reset(self):
self.matrix = np.zeros((self.num_classes, self.num_classes), dtype=int)
def update(self, output, label):
length = output.shape[0]
for i in range(length):
self.matrix[output[i], label[i]] += 1
def get_rec_per_class(self):
rec = np.array(
[
self.matrix[i, i] / self.matrix[:, i].sum()
for i in range(self.num_classes)
]
)
rec[np.isnan(rec)] = 0
return rec
def get_pre_per_class(self):
pre = np.array(
[
self.matrix[i, i] / self.matrix[i, :].sum()
for i in range(self.num_classes)
]
)
pre[np.isnan(pre)] = 0
return pre
def get_accuracy(self):
acc = (
np.sum([self.matrix[i, i] for i in range(self.num_classes)])
/ self.matrix.sum()
)
return acc
def plot_confusion_matrix(self, normalize = False, cmap=plt.cm.Blues):
if normalize:
title = 'Normalized confusion matrix'
else:
title = 'Confusion matrix, without normalization'
# Compute confusion matrix
cm = self.matrix.T
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
# ... and label them with the respective list entries
xticklabels=np.arange(self.num_classes), yticklabels=np.arange(self.num_classes),
title=title,
ylabel='True label',
xlabel='Predicted label')
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
#Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
return fig
def accuracy(output, label):
cnt = label.shape[0]
true_count = (output == label).sum()
now_accuracy = true_count / cnt
return now_accuracy, cnt