forked from szemenyeim/RoboCupVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidLabelProp.py
More file actions
180 lines (150 loc) · 6.33 KB
/
validLabelProp.py
File metadata and controls
180 lines (150 loc) · 6.33 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import torch
from torch.autograd import Variable
from torch.utils import data
from model import PB_FCN, LabelProp
from dataset import LPDataSet
from transform import Scale, ToLabel, Colorize, ToYUV, labelToPred, optFlow, updateLabels
from torchvision.transforms import Compose, Normalize, ToTensor
from PIL import Image
import progressbar
from paramSave import saveParams
import argparse
import time
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--finetuned", help="Use finetuned net and dataset",
action="store_true")
parser.add_argument("--pruned", help="Use pruned net",
action="store_true")
parser.add_argument("--optFlow", help="Use optical flow",
action="store_true")
args = parser.parse_args()
fineTune = args.finetuned
pruned = args.pruned
optflow = args.optFlow
fineTuneStr = "Finetuned" if fineTune else ""
pruneStr = "Pruned" if pruned else ""
scale = 4
input_transform = Compose([
Scale(scale, Image.BILINEAR),
ToYUV(),
ToTensor(),
Normalize([.5, .0, .0], [.5, .5, .5]),
])
target_transform = Compose([
Scale(scale, Image.NEAREST),
ToTensor(),
ToLabel(),
])
labSize = (480.0/scale, 640.0/scale)
outSize = 1.0/(labSize[0] * labSize[1])
batchSize = 1
root = "./data/LabelProp/Synthetic/"
outDir = "./output/LabelProp/Synthetic/"
if fineTune:
outDir = "./output/LabelProp/Real/"
root = "./data/LabelProp/Real/"
valloader = data.DataLoader(LPDataSet(root, split="val", img_transform=input_transform,
label_transform=target_transform),
batch_size=batchSize, shuffle=False)
numClass = 5
kernelSize = 5
numPlanes = 32
modelSeg = PB_FCN(numPlanes, numClass, kernelSize, False, 0)
model = LabelProp(numClass,numPlanes, 0)
mapLoc = {'cuda:0': 'cpu'}
if torch.cuda.is_available():
modelSeg = modelSeg.cuda()
model = model.cuda()
mapLoc = None
if not optflow:
stateDict = torch.load("./pth/bestModelSeg" + fineTuneStr + pruneStr + ".pth", map_location=mapLoc)
modelSeg.load_state_dict(stateDict)
stateDict = torch.load("./pth/bestModelLP" + fineTuneStr + pruneStr + ".pth", map_location=mapLoc)
model.load_state_dict(stateDict)
saveParams("./weightsLP", model.cpu())
running_acc = 0.0
imgCnt = 0
conf = torch.zeros(numClass, numClass)
IoU = torch.zeros(numClass)
labCnts = torch.zeros(numClass)
model.eval()
t=0
bar = progressbar.ProgressBar(0, len(valloader), redirect_stdout=False)
for i, (images, labels) in enumerate(valloader):
currBSize = images.size()[0] * 2
chCnt = images.size()[2] + numClass
H = images.size()[3]
W = images.size()[4]
inputs = torch.FloatTensor(currBSize, chCnt, H, W)
outputs = torch.LongTensor(currBSize, H, W)
predClass = torch.LongTensor(currBSize, H, W)
if torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
inputs = inputs.cuda()
outputs = outputs.cuda()
predClass = predClass.cuda()
if optflow:
cnt = 0
for img, lab in zip(images, labels):
outputs[cnt] = lab[0]
outputs[cnt + 1] = lab[1]
predClass[cnt] = updateLabels( lab[1], optFlow(img[1][0], img[0][0]))
predClass[cnt + 1] = updateLabels( lab[0], optFlow(img[0][0], img[1][0]))
else:
cnt = 0
for img, lab in zip(images, labels):
# preds = F.softmax(modelSeg(img))*2 - 1.0
preds = labelToPred(lab, numClass)
inputs[cnt] = torch.cat(
[torch.unsqueeze(img[0][0], 0), torch.unsqueeze(img[1][0], 0), torch.unsqueeze(img[0][0] - img[1][0], 0),
preds[1]])
inputs[cnt + 1] = torch.cat(
[torch.unsqueeze(img[1][0], 0), torch.unsqueeze(img[0][0], 0), torch.unsqueeze(img[1][0] - img[0][0], 0),
preds[0]])
# inputs[cnt] = torch.cat([img[0], preds[0]])
# inputs[cnt + 1] = torch.cat([img[1], preds[1]])
outputs[cnt] = lab[0]
outputs[cnt + 1] = lab[1]
cnt += 2
beg = time.clock()
pred = model(inputs)
t += time.clock() - beg
_, predClass = torch.max(pred, 1)
bSize = inputs.data.size()[0]
running_acc += torch.sum(predClass == outputs).item() * outSize * 100
for j in range(bSize):
img = Image.fromarray(Colorize(predClass.data[j]).permute(1, 2, 0).numpy().astype('uint8'))
img.save(outDir + "%d.png" % (imgCnt + j))
imgCnt += bSize
maskPred = torch.zeros(numClass, bSize, int(labSize[0]), int(labSize[1])).long()
maskLabel = torch.zeros(numClass, bSize, int(labSize[0]), int(labSize[1])).long()
for currClass in range(numClass):
maskPred[currClass] = predClass == currClass
maskLabel[currClass] = outputs == currClass
for labIdx in range(numClass):
labCnts[labIdx] += torch.sum(maskLabel[labIdx]).item()
for predIdx in range(numClass):
inter = torch.sum(maskPred[predIdx] & maskLabel[labIdx]).item()
conf[(predIdx, labIdx)] += inter
if labIdx == predIdx:
if labIdx == predIdx:
union = torch.sum(maskPred[predIdx] | maskLabel[labIdx]).item()
if union == 0:
IoU[labIdx] += 1
else:
IoU[labIdx] += inter/union
bar.update(i)
bar.finish()
t = t/imgCnt*1000
for labIdx in range(numClass):
for predIdx in range(numClass):
conf[(predIdx, labIdx)] /= (labCnts[labIdx] / 100.0)
meanClassAcc = 0.0
for j in range(numClass):
meanClassAcc += conf[(j, j)] / numClass
meanIoU = torch.sum(IoU/imgCnt).item()/numClass*100*2
print("Validation Pixel Acc: %.2f Mean Class Acc: %.2f Mean IoU: %.2f" % (running_acc / (imgCnt), meanClassAcc, meanIoU))
print(conf)
print(t)