-
Notifications
You must be signed in to change notification settings - Fork 150
/
cityscapes_mIoU.py
218 lines (177 loc) · 6.95 KB
/
cityscapes_mIoU.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import math
import os
import threading
import numpy as np
import torch
import torch.nn as nn
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from . import drn
class FromArray(object):
def __init__(self, size):
self.size = size
def __call__(self, image_numpy, label):
image = Image.fromarray(image_numpy)
return image, label
class Normalize(object):
"""Given mean: (R, G, B) and std: (R, G, B),
will normalize each channel of the torch.*Tensor, i.e.
channel = (channel - mean) / std
"""
def __init__(self, mean, std):
self.mean = torch.FloatTensor(mean)
self.std = torch.FloatTensor(std)
def __call__(self, image, label):
for t, m, s in zip(image, self.mean, self.std):
t.sub_(m).div_(s)
return image, label
class ToTensor(object):
"""Converts a PIL.Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
"""
def __call__(self, pic, label):
img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
# PIL image mode: 1, L, P, I, F, RGB, YCbCr, RGBA, CMYK
if pic.mode == 'YCbCr':
nchannel = 3
else:
nchannel = len(pic.mode)
img = img.view(pic.size[1], pic.size[0], nchannel)
img = img.transpose(0, 1).transpose(0, 2).contiguous()
img = img.float() / 255
return img, torch.from_numpy(np.array(label, dtype=np.int))
class Compose(object):
"""Composes several transforms together.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, *args):
for t in self.transforms:
args = t(*args)
return args
class SegList(Dataset):
def __init__(self, images, names, table_path, data_dir):
self.images = images
self.names = names
self.table_path = table_path
self.data_dir = data_dir
self.transforms = Compose([
FromArray([2048, 1024]),
ToTensor(),
Normalize(mean=[0.29010095242892997, 0.32808144844279574, 0.28696394422942517],
std=[0.1829540508368939, 0.18656561047509476, 0.18447508988480435])
])
self.read_lists()
def __getitem__(self, index):
data = [self.images[index], Image.open(os.path.join(self.data_dir, self.label_list[index]))]
data = list(self.transforms(*data))
return tuple(data)
def __len__(self):
return len(self.names)
def read_lists(self):
self.label_list = []
table = []
with open(self.table_path, 'r') as f:
lines = f.readlines()
for line in lines:
table.append(line.strip().split(' '))
for name in self.names:
for item in table:
if item[0] == name or item[2][:-len('.png')].endswith(name):
self.label_list.append(item[1])
break
assert len(self.label_list) == len(self.names)
def per_class_iu(hist):
return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
def fill_up_weights(up):
w = up.weight.data
f = math.ceil(w.size(2) / 2)
c = (2 * f - 1 - f % 2) / (2. * f)
for i in range(w.size(2)):
for j in range(w.size(3)):
w[0, 0, i, j] = \
(1 - math.fabs(i / f - c)) * (1 - math.fabs(j / f - c))
for c in range(1, w.size(0)):
w[c, 0, :, :] = w[0, 0, :, :]
class DRNSeg(nn.Module):
def __init__(self, model_name, classes, pretrained_model=None,
pretrained=True, use_torch_up=False):
super(DRNSeg, self).__init__()
model = drn.__dict__.get(model_name)(
pretrained=pretrained, num_classes=1000)
pmodel = nn.DataParallel(model)
if pretrained_model is not None:
pmodel.load_state_dict(pretrained_model)
self.base = nn.Sequential(*list(model.children())[:-2])
self.seg = nn.Conv2d(model.out_dim, classes,
kernel_size=1, bias=True)
self.softmax = nn.LogSoftmax()
m = self.seg
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
m.bias.data.zero_()
if use_torch_up:
self.up = nn.UpsamplingBilinear2d(scale_factor=8)
else:
up = nn.ConvTranspose2d(classes, classes, 16, stride=8, padding=4,
output_padding=0, groups=classes,
bias=False)
fill_up_weights(up)
up.weight.requires_grad = False
self.up = up
def forward(self, x):
x = self.base(x)
x = self.seg(x)
y = self.up(x)
return self.softmax(y), x
def optim_parameters(self, memo=None):
raise NotImplementedError('This code is just for evaluation!!!')
def fast_hist(pred, label, n):
k = (label >= 0) & (label < n)
return np.bincount(
n * label[k].astype(int) + pred[k], minlength=n ** 2).reshape(n, n)
def resize_4d_tensor(tensor, width, height):
"""
tensor: the semantic label tensor of shape [B, C, H, W]
width: target width
height: target height
"""
tensor_cpu = tensor.cpu().numpy()
if tensor.size(2) == height and tensor.size(3) == width:
return tensor_cpu
out_size = (tensor.size(0), tensor.size(1), height, width)
out = np.empty(out_size, dtype=np.float32)
def resize_channel(j):
for i in range(tensor.size(0)):
out[i, j] = np.array(
Image.fromarray(tensor_cpu[i, j]).resize(
(width, height), Image.BILINEAR))
workers = [threading.Thread(target=resize_channel, args=(j,))
for j in range(tensor.size(1))]
for w in workers:
w.start()
for w in workers:
w.join()
return out
def test(fakes, names, model, device, table_path='datasets/table.txt', data_dir='database/cityscapes',
batch_size=1, num_workers=8, num_classes=19, tqdm_position=None):
dataset = SegList(fakes, names, table_path, data_dir)
eval_dataloader = DataLoader(dataset, batch_size=batch_size,
shuffle=False, num_workers=num_workers)
model.eval()
hist = np.zeros((num_classes, num_classes))
if tqdm_position is None or tqdm_position >= 0:
from tqdm import tqdm
dataloader_tqdm = tqdm(eval_dataloader, desc='mIoU ', position=tqdm_position, leave=False)
else:
dataloader_tqdm = eval_dataloader
with torch.no_grad():
for image, label in dataloader_tqdm:
image = image.to(device)
final = model(image)[0]
final = resize_4d_tensor(final, 2048, 1024)
pred = final.argmax(axis=1)
label = label.numpy()
hist += fast_hist(pred.flatten(), label.flatten(), num_classes)
ious = per_class_iu(hist) * 100
return round(np.nanmean(ious), 2)