forked from Linfeng-Tang/SuperFusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fullevaluate.py
183 lines (171 loc) · 6.18 KB
/
fullevaluate.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
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
181
182
183
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import os.path as osp
import logging
import numpy as np
from tqdm import tqdm
import math
from PIL import Image
class MscEval(object):
def __init__(
self,
model,
dataloader,
scales=[0.5, 0.75, 1, 1.25, 1.5, 1.75],
n_classes=9,
lb_ignore=255,
cropsize=1024,
flip=False,
*args,
**kwargs
):
self.scales = scales
self.n_classes = n_classes
self.lb_ignore = lb_ignore
self.flip = flip
self.cropsize = cropsize
## dataloader
self.dl = dataloader
self.net = model
def pad_tensor(self, inten, size):
N, C, H, W = inten.size()
outten = torch.zeros(N, C, size[0], size[1]).cuda()
outten.requires_grad = False
margin_h, margin_w = size[0] - H, size[1] - W
hst, hed = margin_h // 2, margin_h // 2 + H
wst, wed = margin_w // 2, margin_w // 2 + W
outten[:, :, hst:hed, wst:wed] = inten
return outten, [hst, hed, wst, wed]
def eval_chip(self, crop):
with torch.no_grad():
out = self.net(crop)[0]
prob = F.softmax(out, 1)
if self.flip:
crop = torch.flip(crop, dims=(3,))
out = self.net(crop)[0]
out = torch.flip(out, dims=(3,))
prob += F.softmax(out, 1)
prob = torch.exp(prob)
return prob
def crop_eval(self, im):
cropsize = self.cropsize
stride_rate = 5 / 6.0
N, C, H, W = im.size()
long_size, short_size = (H, W) if H > W else (W, H)
if long_size < cropsize:
im, indices = self.pad_tensor(im, (cropsize, cropsize))
prob = self.eval_chip(im)
prob = prob[:, :, indices[0] : indices[1], indices[2] : indices[3]]
else:
stride = math.ceil(cropsize * stride_rate)
if short_size < cropsize:
if H < W:
im, indices = self.pad_tensor(im, (cropsize, W))
else:
im, indices = self.pad_tensor(im, (H, cropsize))
N, C, H, W = im.size()
n_x = math.ceil((W - cropsize) / stride) + 1
n_y = math.ceil((H - cropsize) / stride) + 1
prob = torch.zeros(N, self.n_classes, H, W).cuda()
prob.requires_grad = False
for iy in range(n_y):
for ix in range(n_x):
hed, wed = (
min(H, stride * iy + cropsize),
min(W, stride * ix + cropsize),
)
hst, wst = hed - cropsize, wed - cropsize
chip = im[:, :, hst:hed, wst:wed]
prob_chip = self.eval_chip(chip)
prob[:, :, hst:hed, wst:wed] += prob_chip
if short_size < cropsize:
prob = prob[:, :, indices[0] : indices[1], indices[2] : indices[3]]
return prob
def scale_crop_eval(self, im, scale):
N, C, H, W = im.size()
new_hw = [int(H * scale), int(W * scale)]
im = F.interpolate(im, new_hw, mode='bilinear', align_corners=True)
prob = self.crop_eval(im)
prob = F.interpolate(prob, (H, W), mode='bilinear', align_corners=True)
return prob
def get_palette(self):
unlabelled = [0, 0, 0]
car = [64, 0, 128]
person = [64, 64, 0]
bike = [0, 128, 192]
curve = [0, 0, 192]
car_stop = [128, 128, 0]
guardrail = [64, 64, 128]
color_cone = [192, 128, 128]
bump = [192, 64, 0]
palette = np.array(
[
unlabelled,
car,
person,
bike,
curve,
car_stop,
guardrail,
color_cone,
bump,
]
)
return palette
def visualize(self, save_name, predictions):
palette = self.get_palette()
# print(predictions.shape)
# 遍历predictions
# for (i, pred) in enumerate(predictions):
pred = predictions
img = np.zeros((pred.shape[0], pred.shape[1], 3), dtype=np.uint8)
for cid in range(1, int(predictions.max())):
img[pred == cid] = palette[cid]
img = Image.fromarray(np.uint8(img))
img.save(save_name)
def compute_hist(self, pred, lb):
n_classes = self.n_classes
ignore_idx = self.lb_ignore
keep = np.logical_not(lb == ignore_idx)
merge = pred[keep] * n_classes + lb[keep]
hist = np.bincount(merge, minlength=n_classes ** 2)
hist = hist.reshape((n_classes, n_classes))
return hist
def evaluate(self, Method='IFCNN'):
## evaluate
n_classes = self.n_classes
hist = np.zeros((n_classes, n_classes), dtype=np.float32)
dloader = tqdm(self.dl)
# dloader = self.dl
for i, (imgs, label, fn) in enumerate(dloader):
N, _, H, W = label.shape
probs = torch.zeros((N, self.n_classes, H, W))
probs.requires_grad = False
imgs = imgs.cuda()
prob = self.net(imgs)
probs = prob[0].data.cpu().numpy()
preds = np.argmax(probs, axis=1)
for i in range(1):
outpreds = preds[i]
name = fn[i]
folder_path = os.path.join('results', Method)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
file_path = os.path.join(folder_path, '%s.png' % name)
# self.visualize(file_path, outpreds)
# 可视化label
self.visualize(file_path,label[0,0,:,:])
hist_once = self.compute_hist(preds, label.data.numpy().squeeze(1))
hist = hist + hist_once
IOUs = np.diag(hist) / (
np.sum(hist, axis=0) + np.sum(hist, axis=1) - np.diag(hist)
)
mIOU = np.mean(IOUs)
mIOU = mIOU
IoU_list = IOUs.tolist()
IoU_list.append(mIOU)
IoU_list = [round(i, 4) for i in IoU_list]
print(Method, ':\tIoU:', IoU_list, '\n')
return mIOU