forked from prachigarg23/MDIL-SS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iouEval.py
105 lines (88 loc) · 3.63 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
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
# Code for evaluating IoU
# Nov 2017
# Eduardo Romera
#######################
import torch
class iouEval:
def __init__(self, nClasses, ignoreIndex=19):
self.nClasses = nClasses
self.ignoreIndex = ignoreIndex if nClasses>ignoreIndex else -1 #if ignoreIndex is larger than nClasses, consider no ignoreIndex
self.reset()
def reset (self):
classes = self.nClasses if self.ignoreIndex==-1 else self.nClasses-1
self.tp = torch.zeros(classes).double()
self.fp = torch.zeros(classes).double()
self.fn = torch.zeros(classes).double()
def addBatch(self, x, y): #x=preds, y=targets
#sizes should be "batch_size x nClasses x H x W"
#print ("X is cuda: ", x.is_cuda)
#print ("Y is cuda: ", y.is_cuda)
# print('testing nClasses inside iouEval.addBatch::::::: ', self.nClasses)
if (x.is_cuda or y.is_cuda):
x = x.cuda()
y = y.cuda()
#if size is "batch_size x 1 x H x W" scatter to onehot
if (x.size(1) == 1):
x_onehot = torch.zeros(x.size(0), self.nClasses, x.size(2), x.size(3))
if x.is_cuda:
x_onehot = x_onehot.cuda()
x_onehot.scatter_(1, x, 1).float()
else:
x_onehot = x.float()
if (y.size(1) == 1):
y_onehot = torch.zeros(y.size(0), self.nClasses, y.size(2), y.size(3))
if y.is_cuda:
y_onehot = y_onehot.cuda()
y_onehot.scatter_(1, y, 1).float()
else:
y_onehot = y.float()
if (self.ignoreIndex != -1):
ignores = y_onehot[:,self.ignoreIndex].unsqueeze(1)
x_onehot = x_onehot[:, :self.ignoreIndex]
y_onehot = y_onehot[:, :self.ignoreIndex]
else:
ignores=0
#print(type(x_onehot))
#print(type(y_onehot))
#print(x_onehot.size())
#print(y_onehot.size())
tpmult = x_onehot * y_onehot #times prediction and gt coincide is 1
tp = torch.sum(torch.sum(torch.sum(tpmult, dim=0, keepdim=True), dim=2, keepdim=True), dim=3, keepdim=True).squeeze()
fpmult = x_onehot * (1-y_onehot-ignores) #times prediction says its that class and gt says its not (subtracting cases when its ignore label!)
fp = torch.sum(torch.sum(torch.sum(fpmult, dim=0, keepdim=True), dim=2, keepdim=True), dim=3, keepdim=True).squeeze()
fnmult = (1-x_onehot) * (y_onehot) #times prediction says its not that class and gt says it is
fn = torch.sum(torch.sum(torch.sum(fnmult, dim=0, keepdim=True), dim=2, keepdim=True), dim=3, keepdim=True).squeeze()
self.tp += tp.double().cpu()
self.fp += fp.double().cpu()
self.fn += fn.double().cpu()
def getIoU(self):
num = self.tp
den = self.tp + self.fp + self.fn + 1e-15
iou = num / den
print('check:::::::::size of io tensor: ', iou.size())
return torch.mean(iou), iou #returns "iou mean", "iou per class"
# Class for colors
class colors:
RED = '\033[31;1m'
GREEN = '\033[32;1m'
YELLOW = '\033[33;1m'
BLUE = '\033[34;1m'
MAGENTA = '\033[35;1m'
CYAN = '\033[36;1m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
# Colored value output if colorized flag is activated.
def getColorEntry(val):
if not isinstance(val, float):
return colors.ENDC
if (val < .20):
return colors.RED
elif (val < .40):
return colors.YELLOW
elif (val < .60):
return colors.BLUE
elif (val < .80):
return colors.CYAN
else:
return colors.GREEN