-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.py
334 lines (237 loc) · 12.8 KB
/
main.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from dataset.semi import SemiDataset
from model.semseg.deeplabv2 import DeepLabV2
from model.semseg.deeplabv3plus import DeepLabV3Plus
from model.semseg.pspnet import PSPNet
from utils import count_params, meanIOU, color_map
import argparse
from copy import deepcopy
import numpy as np
import os
from PIL import Image
import torch
from torch.nn import CrossEntropyLoss, DataParallel
from torch.optim import SGD
from torch.utils.data import DataLoader
from tqdm import tqdm
MODE = None
def parse_args():
parser = argparse.ArgumentParser(description='ST and ST++ Framework')
# basic settings
parser.add_argument('--data-root', type=str, required=True)
parser.add_argument('--dataset', type=str, choices=['pascal', 'cityscapes'], default='pascal')
parser.add_argument('--batch-size', type=int, default=16)
parser.add_argument('--lr', type=float, default=None)
parser.add_argument('--epochs', type=int, default=None)
parser.add_argument('--crop-size', type=int, default=None)
parser.add_argument('--backbone', type=str, choices=['resnet50', 'resnet101'], default='resnet50')
parser.add_argument('--model', type=str, choices=['deeplabv3plus', 'pspnet', 'deeplabv2'],
default='deeplabv3plus')
# semi-supervised settings
parser.add_argument('--labeled-id-path', type=str, required=True)
parser.add_argument('--unlabeled-id-path', type=str, required=True)
parser.add_argument('--pseudo-mask-path', type=str, required=True)
parser.add_argument('--save-path', type=str, required=True)
# arguments for ST++
parser.add_argument('--reliable-id-path', type=str)
parser.add_argument('--plus', dest='plus', default=False, action='store_true',
help='whether to use ST++')
args = parser.parse_args()
return args
def main(args):
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
if not os.path.exists(args.pseudo_mask_path):
os.makedirs(args.pseudo_mask_path)
if args.plus and args.reliable_id_path is None:
exit('Please specify reliable-id-path in ST++.')
criterion = CrossEntropyLoss(ignore_index=255)
valset = SemiDataset(args.dataset, args.data_root, 'val', None)
valloader = DataLoader(valset, batch_size=4 if args.dataset == 'cityscapes' else 1,
shuffle=False, pin_memory=True, num_workers=4, drop_last=False)
# <====================== Supervised training with labeled images (SupOnly) ======================>
print('\n================> Total stage 1/%i: '
'Supervised training on labeled images (SupOnly)' % (6 if args.plus else 3))
global MODE
MODE = 'train'
trainset = SemiDataset(args.dataset, args.data_root, MODE, args.crop_size, args.labeled_id_path)
trainset.ids = 2 * trainset.ids if len(trainset.ids) < 200 else trainset.ids
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True,
pin_memory=True, num_workers=16, drop_last=True)
model, optimizer = init_basic_elems(args)
print('\nParams: %.1fM' % count_params(model))
best_model, checkpoints = train(model, trainloader, valloader, criterion, optimizer, args)
"""
ST framework without selective re-training
"""
if not args.plus:
# <============================= Pseudo label all unlabeled images =============================>
print('\n\n\n================> Total stage 2/3: Pseudo labeling all unlabeled images')
dataset = SemiDataset(args.dataset, args.data_root, 'label', None, None, args.unlabeled_id_path)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, pin_memory=True, num_workers=4, drop_last=False)
label(best_model, dataloader, args)
# <======================== Re-training on labeled and unlabeled images ========================>
print('\n\n\n================> Total stage 3/3: Re-training on labeled and unlabeled images')
MODE = 'semi_train'
trainset = SemiDataset(args.dataset, args.data_root, MODE, args.crop_size,
args.labeled_id_path, args.unlabeled_id_path, args.pseudo_mask_path)
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True,
pin_memory=True, num_workers=16, drop_last=True)
model, optimizer = init_basic_elems(args)
train(model, trainloader, valloader, criterion, optimizer, args)
return
"""
ST++ framework with selective re-training
"""
# <===================================== Select Reliable IDs =====================================>
print('\n\n\n================> Total stage 2/6: Select reliable images for the 1st stage re-training')
dataset = SemiDataset(args.dataset, args.data_root, 'label', None, None, args.unlabeled_id_path)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, pin_memory=True, num_workers=4, drop_last=False)
select_reliable(checkpoints, dataloader, args)
# <================================ Pseudo label reliable images =================================>
print('\n\n\n================> Total stage 3/6: Pseudo labeling reliable images')
cur_unlabeled_id_path = os.path.join(args.reliable_id_path, 'reliable_ids.txt')
dataset = SemiDataset(args.dataset, args.data_root, 'label', None, None, cur_unlabeled_id_path)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, pin_memory=True, num_workers=4, drop_last=False)
label(best_model, dataloader, args)
# <================================== The 1st stage re-training ==================================>
print('\n\n\n================> Total stage 4/6: The 1st stage re-training on labeled and reliable unlabeled images')
MODE = 'semi_train'
trainset = SemiDataset(args.dataset, args.data_root, MODE, args.crop_size,
args.labeled_id_path, cur_unlabeled_id_path, args.pseudo_mask_path)
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True,
pin_memory=True, num_workers=16, drop_last=True)
model, optimizer = init_basic_elems(args)
best_model = train(model, trainloader, valloader, criterion, optimizer, args)
# <=============================== Pseudo label unreliable images ================================>
print('\n\n\n================> Total stage 5/6: Pseudo labeling unreliable images')
cur_unlabeled_id_path = os.path.join(args.reliable_id_path, 'unreliable_ids.txt')
dataset = SemiDataset(args.dataset, args.data_root, 'label', None, None, cur_unlabeled_id_path)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, pin_memory=True, num_workers=4, drop_last=False)
label(best_model, dataloader, args)
# <================================== The 2nd stage re-training ==================================>
print('\n\n\n================> Total stage 6/6: The 2nd stage re-training on labeled and all unlabeled images')
trainset = SemiDataset(args.dataset, args.data_root, MODE, args.crop_size,
args.labeled_id_path, args.unlabeled_id_path, args.pseudo_mask_path)
trainloader = DataLoader(trainset, batch_size=args.batch_size, shuffle=True,
pin_memory=True, num_workers=16, drop_last=True)
model, optimizer = init_basic_elems(args)
train(model, trainloader, valloader, criterion, optimizer, args)
def init_basic_elems(args):
model_zoo = {'deeplabv3plus': DeepLabV3Plus, 'pspnet': PSPNet, 'deeplabv2': DeepLabV2}
model = model_zoo[args.model](args.backbone, 21 if args.dataset == 'pascal' else 19)
head_lr_multiple = 10.0
if args.model == 'deeplabv2':
assert args.backbone == 'resnet101'
model.load_state_dict(torch.load('pretrained/deeplabv2_resnet101_coco_pretrained.pth'))
head_lr_multiple = 1.0
optimizer = SGD([{'params': model.backbone.parameters(), 'lr': args.lr},
{'params': [param for name, param in model.named_parameters()
if 'backbone' not in name],
'lr': args.lr * head_lr_multiple}],
lr=args.lr, momentum=0.9, weight_decay=1e-4)
model = DataParallel(model).cuda()
return model, optimizer
def train(model, trainloader, valloader, criterion, optimizer, args):
iters = 0
total_iters = len(trainloader) * args.epochs
previous_best = 0.0
global MODE
if MODE == 'train':
checkpoints = []
for epoch in range(args.epochs):
print("\n==> Epoch %i, learning rate = %.4f\t\t\t\t\t previous best = %.2f" %
(epoch, optimizer.param_groups[0]["lr"], previous_best))
model.train()
total_loss = 0.0
tbar = tqdm(trainloader)
for i, (img, mask) in enumerate(tbar):
img, mask = img.cuda(), mask.cuda()
pred = model(img)
loss = criterion(pred, mask)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
iters += 1
lr = args.lr * (1 - iters / total_iters) ** 0.9
optimizer.param_groups[0]["lr"] = lr
optimizer.param_groups[1]["lr"] = lr * 1.0 if args.model == 'deeplabv2' else lr * 10.0
tbar.set_description('Loss: %.3f' % (total_loss / (i + 1)))
metric = meanIOU(num_classes=21 if args.dataset == 'pascal' else 19)
model.eval()
tbar = tqdm(valloader)
with torch.no_grad():
for img, mask, _ in tbar:
img = img.cuda()
pred = model(img)
pred = torch.argmax(pred, dim=1)
metric.add_batch(pred.cpu().numpy(), mask.numpy())
mIOU = metric.evaluate()[-1]
tbar.set_description('mIOU: %.2f' % (mIOU * 100.0))
mIOU *= 100.0
if mIOU > previous_best:
if previous_best != 0:
os.remove(os.path.join(args.save_path, '%s_%s_%.2f.pth' % (args.model, args.backbone, previous_best)))
previous_best = mIOU
torch.save(model.module.state_dict(),
os.path.join(args.save_path, '%s_%s_%.2f.pth' % (args.model, args.backbone, mIOU)))
best_model = deepcopy(model)
if MODE == 'train' and ((epoch + 1) in [args.epochs // 3, args.epochs * 2 // 3, args.epochs]):
checkpoints.append(deepcopy(model))
if MODE == 'train':
return best_model, checkpoints
return best_model
def select_reliable(models, dataloader, args):
if not os.path.exists(args.reliable_id_path):
os.makedirs(args.reliable_id_path)
for i in range(len(models)):
models[i].eval()
tbar = tqdm(dataloader)
id_to_reliability = []
with torch.no_grad():
for img, mask, id in tbar:
img = img.cuda()
preds = []
for model in models:
preds.append(torch.argmax(model(img), dim=1).cpu().numpy())
mIOU = []
for i in range(len(preds) - 1):
metric = meanIOU(num_classes=21 if args.dataset == 'pascal' else 19)
metric.add_batch(preds[i], preds[-1])
mIOU.append(metric.evaluate()[-1])
reliability = sum(mIOU) / len(mIOU)
id_to_reliability.append((id[0], reliability))
id_to_reliability.sort(key=lambda elem: elem[1], reverse=True)
with open(os.path.join(args.reliable_id_path, 'reliable_ids.txt'), 'w') as f:
for elem in id_to_reliability[:len(id_to_reliability) // 2]:
f.write(elem[0] + '\n')
with open(os.path.join(args.reliable_id_path, 'unreliable_ids.txt'), 'w') as f:
for elem in id_to_reliability[len(id_to_reliability) // 2:]:
f.write(elem[0] + '\n')
def label(model, dataloader, args):
model.eval()
tbar = tqdm(dataloader)
metric = meanIOU(num_classes=21 if args.dataset == 'pascal' else 19)
cmap = color_map(args.dataset)
with torch.no_grad():
for img, mask, id in tbar:
img = img.cuda()
pred = model(img, True)
pred = torch.argmax(pred, dim=1).cpu()
metric.add_batch(pred.numpy(), mask.numpy())
mIOU = metric.evaluate()[-1]
pred = Image.fromarray(pred.squeeze(0).numpy().astype(np.uint8), mode='P')
pred.putpalette(cmap)
pred.save('%s/%s' % (args.pseudo_mask_path, os.path.basename(id[0].split(' ')[1])))
tbar.set_description('mIOU: %.2f' % (mIOU * 100.0))
if __name__ == '__main__':
args = parse_args()
if args.epochs is None:
args.epochs = {'pascal': 80, 'cityscapes': 240}[args.dataset]
if args.lr is None:
args.lr = {'pascal': 0.001, 'cityscapes': 0.004}[args.dataset] / 16 * args.batch_size
if args.crop_size is None:
args.crop_size = {'pascal': 321, 'cityscapes': 721}[args.dataset]
print()
print(args)
main(args)