-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathIOUEval.py
49 lines (39 loc) · 1.7 KB
/
IOUEval.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
import torch
import numpy as np
#adapted from https://github.com/shelhamer/fcn.berkeleyvision.org/blob/master/score.py
class iouEval:
def __init__(self, nClasses):
self.nClasses = nClasses
self.reset()
def reset(self):
self.overall_acc = 0
self.per_class_acc = np.zeros(self.nClasses, dtype=np.float32)
self.per_class_iu = np.zeros(self.nClasses, dtype=np.float32)
self.mIOU = 0
self.batchCount = 1
def fast_hist(self, a, b):
k = (a >= 0) & (a < self.nClasses)
return np.bincount(self.nClasses * a[k].astype(int) + b[k], minlength=self.nClasses ** 2).reshape(self.nClasses, self.nClasses)
def compute_hist(self, predict, gth):
hist = self.fast_hist(gth, predict)
return hist
def addBatch(self, predict, gth):
predict = predict.cpu().numpy().flatten()
gth = gth.cpu().numpy().flatten()
epsilon = 0.00000001
hist = self.compute_hist(predict, gth)
overall_acc = np.diag(hist).sum() / (hist.sum() + epsilon)
per_class_acc = np.diag(hist) / (hist.sum(1) + epsilon)
per_class_iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist) + epsilon)
mIou = np.nanmean(per_class_iu)
self.overall_acc +=overall_acc
self.per_class_acc += per_class_acc
self.per_class_iu += per_class_iu
self.mIOU += mIou
self.batchCount += 1
def getMetric(self):
overall_acc = self.overall_acc/self.batchCount
per_class_acc = self.per_class_acc / self.batchCount
per_class_iu = self.per_class_iu / self.batchCount
mIOU = self.mIOU / self.batchCount
return overall_acc, per_class_acc, per_class_iu, mIOU