-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
145 lines (114 loc) · 4.87 KB
/
Copy pathutils.py
File metadata and controls
145 lines (114 loc) · 4.87 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import numpy as np
from matplotlib import pyplot as plt
class AverageMeter:
def __init__(self):
self.val = 0
self.sum = 0
self.count = 0
self.avg = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Evaluator:
def __init__(self, num_classes):
self.num_classes = num_classes
self.confusion_matrix = np.zeros((num_classes, num_classes), dtype=np.int)
def accuracy(self):
"""calculate accuracy"""
return np.diag(self.confusion_matrix).sum() / self.confusion_matrix.sum()
def error(self):
"""calculate error rate"""
return 1 - self.accuracy()
def class_accuracy(self):
"""calculate arruracy for each class"""
return np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=1)
def mean_class_accuracy(self):
"""calculate mean of accuracy for each class"""
return np.nanmean(self.class_accuracy())
def class_precision(self):
"""calculate precision for each class"""
return np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=0)
def mean_class_precision(self):
"""calculate mean of precision for each class"""
return np.nanmean(self.class_precision())
def show_matrix(self, cls_to_idx, save_matrix=False):
"""
visualize confusion matrix
Params:
class_to_idx (dict): original class names and corresponding index for label
save_matrix (bool): whether to save the confusion matrix
"""
cls_to_idx = sorted(cls_to_idx.items(), key=lambda x: x[1])
classes = [pair[0] for pair in cls_to_idx]
# scale value ranging from 0 to 1
norm_conf_mat = np.zeros(self.confusion_matrix.shape)
for i in range(self.num_classes):
norm_conf_mat[i, :] = self.confusion_matrix[i, :] / self.confusion_matrix[i, :].sum()
# set color
cmap = plt.cm.get_cmap('pink') # reference: http://matplotlib.org/examples/color/colormaps_reference.html
plt.imshow(norm_conf_mat, cmap=cmap)
plt.colorbar()
# set text
xlocations = np.array(range(self.num_classes))
plt.xticks(xlocations, classes, rotation=60)
plt.yticks(xlocations, classes, rotation=60)
plt.xlabel('Predict Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix')
# show numbers
for i in range(norm_conf_mat.shape[0]):
for j in range(norm_conf_mat.shape[1]):
plt.text(x=j, y=i, s=int(self.confusion_matrix[i, j]),
va='center', ha='center', color='red', fontsize=10)
# save confusion matrix
if save_matrix:
save_path = os.path.join(os.curdir, 'confusion_matrix.pdf')
plt.savefig(save_path)
plt.show()
plt.close()
def update_matrix(self, trues, preds):
"""
update confusion matrix according to the given batch of true labels and predicted labels.
Params:
trues (numpy.array): shape (batch_size, num_classes), true labels
preds (numpy.array): shape (batch_size, num_classes), predicted labels
"""
assert trues.shape == preds.shape
self.confusion_matrix += self._generate_matrix(trues, preds)
def _generate_matrix(self, trues, preds):
"""
generate confusion matrix according to the given batch of true labels and predicted labels.
Params:
trues (numpy.array): shape (batch_size, num_classes), true labels
preds (numpy.array): shape (batch_size, num_classes), predicted labels
Returns:
conf_mat (numpy.array): shape , confusion matrix.
Its shape is (num_classes, num_classes). True Labels on the row; Pred Labels on the column.
"""
mask = (trues >= 0) & (trues < self.num_classes)
conf_mat = np.bincount(self.num_classes * trues[mask].astype(int) + preds[mask],
minlength=self.num_classes ** 2).reshape(self.num_classes, self.num_classes)
return conf_mat
if __name__ == '__main__':
import torch
evaluator = Evaluator(num_classes=4)
outputs = torch.tensor([
[0.3, 0.1, 0.1, 0.2, 0.1, 0.2],
[0.1, 0.2, 0.4, 0.1, 0.1, 0.1],
[0.2, 0.29, 0.09, 0.31, 0.11, 0.2]
])
targets = torch.tensor([0, 1, 2])
trues = targets.numpy()
preds = outputs.max(dim=1)[1].numpy()
evaluator.update_matrix(trues, preds)
print(evaluator.accuracy())
print(evaluator.error())
print(evaluator.class_accuracy())
print(evaluator.mean_class_accuracy())
print(evaluator.class_precision())
print(evaluator.mean_class_precision())
cls_to_idx = {'cat': 0, 'dog': 1, 'horse': 2, 'frog': 3}
evaluator.show_matrix(cls_to_idx)