-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
86 lines (73 loc) · 2.63 KB
/
utils.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
from __future__ import division
import itertools
import numpy as np
from sklearn.metrics import confusion_matrix
def accuracy(input, target):
return 100 * float(np.count_nonzero(input == target)) / target.size
def grouper(n, iterable):
""" Browse an iterator by chunk of n elements """
it = iter(iterable)
while True:
chunk = tuple(itertools.islice(it, n))
if not chunk:
return
yield chunk
def metrics(predictions, gts, label_values=None):
# compute the overall average loss
# compute the mean loss
# compute mean IU
# fwavacc
cm = confusion_matrix(
gts,
predictions,
range(len(label_values)))
print("Confusion matrix :")
print(cm)
# Compute global accuracy
total = sum(sum(cm))
accuracy = sum([cm[x][x] for x in range(len(cm))])
accuracy *= 100 / float(total)
print("{} pixels processed".format(total))
print("Total accuracy : {}%".format(accuracy))
acc = np.diag(cm).sum() / cm.sum()
acc_cls = np.diag(cm) / cm.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(cm) / (cm.sum(axis=1) + cm.sum(axis=0) - np.diag(cm))
mean_iu = np.nanmean(iu)
freq = cm.sum(axis=1) / cm.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(range(len(label_values)), iu))
print("Overall Acc:", acc)
print("Mean Acc:", acc_cls)
print("FreqW:", fwavacc)
print("Mean IoU:", mean_iu)
print("cls_iu:", cls_iu)
print("---")
# Compute F1 score
F1Score = np.zeros(len(label_values))
for i in range(len(label_values)):
try:
F1Score[i] = 2. * cm[i, i] / (np.sum(cm[i, :]) + np.sum(cm[:, i]))
except:
# Ignore exception if there is no element in class i for test set
pass
print("F1Score :")
for l_id, score in enumerate(F1Score):
print("{}: {}".format(label_values[l_id], score))
print("---")
# Compute kappa coefficient
total = np.sum(cm)
pa = np.trace(cm) / float(total)
pe = np.sum(np.sum(cm, axis=0) * np.sum(cm, axis=1)) / float(total * total)
kappa = (pa - pe) / (1 - pe)
print("Kappa: " + str(kappa))
return accuracy
def sliding_window(top, step=10, window_size=(20, 20)):
""" Slide a window_shape window across the image with a stride of step """
for x in range(0, top.shape[0], step):
if x + window_size[0] > top.shape[0]:
x = top.shape[0] - window_size[0]
for y in range(0, top.shape[1], step):
if y + window_size[1] > top.shape[1]:
y = top.shape[1] - window_size[1]
yield x, y, window_size[0], window_size[1]