-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
430 lines (348 loc) · 16.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from re import L
from tqdm import tqdm
import network
import utils
import os
import random
import argparse
import numpy as np
from torch.utils import data
from datasets import Cityscapes, gta5
from utils import ext_transforms as et
from metrics import StreamSegMetrics
import torch
import torch.nn as nn
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
import pickle
from utils.utils import denormalize
from torchvision.utils import save_image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
def get_argparser():
parser = argparse.ArgumentParser()
# Dataset Options
parser.add_argument("--data_root", type=str, default='./datasets/data',
help="path to Dataset")
parser.add_argument("--dataset", type=str, default='cityscapes',
choices=['cityscapes','ACDC','gta5'], help='Name of dataset')
parser.add_argument("--ACDC_sub", type=str, default="night",
help = "specify which subset of ACDC to use")
# Deeplab Options
available_models = sorted(name for name in network.modeling.__dict__ if name.islower() and \
not (name.startswith("__") or name.startswith('_')) and callable(
network.modeling.__dict__[name])
)
parser.add_argument("--model", type=str, default='deeplabv3plus_resnet_clip',
choices=available_models, help='model name')
parser.add_argument("--BB", type = str, default = "RN50",
help = "backbone of the segmentation network")
# Train Options
parser.add_argument("--test_only", action='store_true', default=False)
parser.add_argument("--total_itrs", type=int, default=200e3,
help="epoch number (default: 200k)")
parser.add_argument("--lr", type=float, default=0.1,
help="learning rate (default: 0.1)")
parser.add_argument("--lr_policy", type=str, default='poly', choices=['poly', 'step'],
help="learning rate scheduler policy")
parser.add_argument("--step_size", type=int, default=10)
parser.add_argument("--batch_size", type=int, default=2,
help='batch size (default: 16)')
parser.add_argument("--val_batch_size", type=int, default=4,
help='batch size for validation (default: 4)')
parser.add_argument("--crop_size", type=int, default=768)
parser.add_argument("--ckpt", default=None, type=str,
help="restore from checkpoint")
parser.add_argument("--continue_training", action='store_true', default=False)
parser.add_argument("--loss_type", type=str, default='cross_entropy',
choices=['cross_entropy', 'focal_loss'], help="loss type (default: False)")
parser.add_argument("--gpu_id", type=str, default='0',
help="GPU ID")
parser.add_argument("--weight_decay", type=float, default=1e-4,
help='weight decay (default: 1e-4)')
parser.add_argument("--random_seed", type=int, default=1,
help="random seed (default: 1)")
parser.add_argument("--val_interval", type=int, default=100,
help="epoch interval for eval (default: 100)")
parser.add_argument("--forward_pass",action='store_true',default=False,
help="forward pass to update BN statistics")
parser.add_argument("--save_val_results", action='store_true', default=False,
help="save segmentation results to \"./results\"")
parser.add_argument("--freeze_BB", action='store_true',default=False,
help="Freeze the backbone when training")
parser.add_argument("--ckpts_path", type = str ,
help="path for checkpoints saving")
parser.add_argument("--data_aug", action='store_true', default=False)
#validation
parser.add_argument("--val_results_dir", type=str,help="Folder name for validation results saving")
#Augmented features
parser.add_argument("--train_aug",action='store_true',default=False,
help="train on augmented features using CLIP")
parser.add_argument("--path_mu_sig", type=str)
parser.add_argument("--mix", action='store_true',default=False,
help="mix statistics")
return parser
def get_dataset(dataset,data_root,crop_size,ACDC_sub="night",data_aug=True):
""" Dataset And Augmentation
"""
if dataset == 'cityscapes':
if data_aug:
train_transform = et.ExtCompose([
et.ExtRandomCrop(size=(crop_size, crop_size)),
et.ExtColorJitter(brightness=0.5, contrast=0.5, saturation=0.5),
et.ExtRandomHorizontalFlip(),
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
else:
train_transform = et.ExtCompose([
et.ExtRandomCrop(size=(crop_size, crop_size)),
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
val_transform = et.ExtCompose([
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
train_dst = Cityscapes(root=data_root,dataset=dataset,
split='train', transform=train_transform)
val_dst = Cityscapes(root=data_root,dataset=dataset,
split='val', transform=val_transform)
if dataset == 'ACDC':
train_transform = et.ExtCompose([
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
val_transform = et.ExtCompose([
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
train_dst = Cityscapes(root=data_root,dataset=dataset,
split='train', transform=train_transform, ACDC_sub = ACDC_sub)
val_dst = Cityscapes(root=data_root,dataset=dataset,
split='val', transform=val_transform, ACDC_sub = ACDC_sub)
if dataset == "gta5":
if data_aug:
train_transform = et.ExtCompose([
et.ExtRandomCrop(size=(768, 768)),
et.ExtColorJitter(brightness=0.5, contrast=0.5, saturation=0.5),
et.ExtRandomHorizontalFlip(),
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
else:
train_transform = et.ExtCompose([
et.ExtRandomCrop(size=(768, 768)),
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
val_transform = et.ExtCompose([
et.ExtCenterCrop(size=(1046, 1914)),
et.ExtToTensor(),
et.ExtNormalize(mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711]),
])
train_dst = gta5.GTA5DataSet(data_root, 'datasets/gta5_list/gtav_split_train.txt',transform=train_transform)
val_dst = gta5.GTA5DataSet(data_root, 'datasets/gta5_list/gtav_split_val.txt',transform=val_transform)
return train_dst, val_dst
def validate(opts, model, loader, device, metrics):
"""Do validation and return specified samples"""
metrics.reset()
if opts.save_val_results:
if not os.path.exists(opts.val_results_dir):
os.mkdir(opts.val_results_dir)
img_id = 0
with torch.no_grad():
for i, (im_id, tg_id, images, labels) in tqdm(enumerate(loader), total=len(loader)):
images = images.to(device, dtype=torch.float32)
labels = labels.to(device, dtype=torch.long)
outputs,features = model(images)
preds = outputs.detach().max(dim=1)[1].cpu().numpy()
targets = labels.cpu().numpy()
metrics.update(targets, preds)
if opts.save_val_results:
for j in range(len(images)):
target = targets[j]
pred = preds[j]
target = loader.dataset.decode_target(target).astype(np.uint8)
pred = loader.dataset.decode_target(pred).astype(np.uint8)
Image.fromarray(target).save(opts.val_results_dir+'/%d_target.png' % img_id)
Image.fromarray(pred).save(opts.val_results_dir+'/%d_pred.png' % img_id)
images[j] = denormalize(images[j],mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711])
save_image(images[j],opts.val_results_dir+'/%d_image.png' % img_id)
fig = plt.figure()
plt.axis('off')
plt.imshow(pred, alpha=0.7)
ax = plt.gca()
ax.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
ax.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
#plt.savefig(opts.val_results_dir+'/%d_overlay.png' % img_id, bbox_inches='tight', pad_inches=0)
plt.close()
img_id += 1
score = metrics.get_results()
return score
def main():
opts = get_argparser().parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = opts.gpu_id
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Device: %s" % device)
# Setup random seed
# INIT
torch.manual_seed(opts.random_seed)
torch.cuda.manual_seed(opts.random_seed)
np.random.seed(opts.random_seed)
random.seed(opts.random_seed)
# Setup dataloader
train_dst,val_dst = get_dataset(opts.dataset,opts.data_root,opts.crop_size,opts.ACDC_sub,
data_aug=opts.data_aug)
train_loader = data.DataLoader(
train_dst, batch_size=opts.batch_size, shuffle=True, num_workers=4,
drop_last=True) # drop_last=True to ignore single-image batches.
val_loader = data.DataLoader(
val_dst, batch_size=opts.val_batch_size, shuffle=True, num_workers=4)
print("Dataset: %s, Train set: %d, Val set: %d" %
(opts.dataset, len(train_dst), len(val_dst)))
# Set up model
model = network.modeling.__dict__[opts.model](num_classes=19, BB= opts.BB,replace_stride_with_dilation=[False,False,True])
model.backbone.attnpool = nn.Identity()
#fix the backbone
if opts.freeze_BB:
for param in model.backbone.parameters():
param.requires_grad = False
model.backbone.eval()
# Set up metrics
metrics = StreamSegMetrics(19)
# Set up optimizer
if opts.freeze_BB:
optimizer = torch.optim.SGD(params=[
{'params': model.classifier.parameters(), 'lr': opts.lr},
], lr=opts.lr, momentum=0.9, weight_decay=opts.weight_decay)
else:
optimizer = torch.optim.SGD(params=[
{'params': model.backbone.parameters(), 'lr': 0.001 * opts.lr},
{'params': model.classifier.parameters(), 'lr': opts.lr},
], lr=opts.lr, momentum=0.9, weight_decay=opts.weight_decay)
if opts.lr_policy == 'poly':
scheduler = utils.PolyLR(optimizer, opts.total_itrs, power=0.9)
elif opts.lr_policy == 'step':
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=opts.step_size, gamma=0.9)
# Set up criterion
if opts.loss_type == 'focal_loss':
criterion = utils.FocalLoss(ignore_index=255, size_average=True)
elif opts.loss_type == 'cross_entropy':
criterion = nn.CrossEntropyLoss(ignore_index=255, reduction='mean')
def save_ckpt(path):
""" save current model
"""
torch.save({
"cur_itrs": cur_itrs,
"model_state": model.state_dict(),
"optimizer_state": optimizer.state_dict(),
"scheduler_state": scheduler.state_dict(),
"best_score": best_score,
}, path)
print("Model saved as %s" % path)
if not opts.test_only:
utils.mkdir(opts.ckpts_path)
# Restore
best_score = 0.0
cur_itrs = 0
cur_epochs = 0
if opts.ckpt is not None and os.path.isfile(opts.ckpt):
checkpoint = torch.load(opts.ckpt, map_location=torch.device('cpu'))
model.load_state_dict(checkpoint["model_state"])
model.to(device)
if opts.continue_training:
optimizer.load_state_dict(checkpoint["optimizer_state"])
scheduler.load_state_dict(checkpoint["scheduler_state"])
cur_itrs = checkpoint["cur_itrs"]
best_score = checkpoint['best_score']
print("Training state restored from %s" % opts.ckpt)
print("Model restored from %s" % opts.ckpt)
del checkpoint # free memory
else:
print("[!] Retrain")
model.to(device)
# ========== Train Loop ==========#
if opts.test_only:
model.eval()
val_score = validate(
opts=opts, model=model, loader=val_loader, device=device, metrics=metrics)
print(metrics.to_str(val_score))
print(val_score["Mean IoU"])
print(val_score["Class IoU"])
return
interval_loss = 0
if opts.train_aug:
files = [f for f in os.listdir(opts.path_mu_sig+'/')]
relu = nn.ReLU(inplace=True)
while True: # cur_itrs < opts.total_itrs:
# ===== Train =====
if opts.freeze_BB:
model.classifier.train()
else:
model.train()
cur_epochs += 1
for (im_id, tg_id, images, labels) in train_loader:
cur_itrs += 1
images = images.to(device, dtype=torch.float32)
labels = labels.to(device, dtype=torch.long)
optimizer.zero_grad()
if opts.train_aug:
mu_t_f1 = torch.zeros([opts.batch_size,256,1,1])
std_t_f1 = torch.zeros([opts.batch_size,256,1,1])
for k in range(opts.batch_size):
with open(opts.path_mu_sig+'/'+random.choice(files), 'rb') as f:
loaded_dict = pickle.load(f)
mu_t_f1[k] = loaded_dict['mu_f1']
std_t_f1[k] = loaded_dict['std_f1']
outputs,features = model(images,mu_t_f1.to(device),std_t_f1.to(device),
transfer=True,mix=opts.mix,activation=relu)
else:
outputs,features = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
writer.add_scalar("loss",loss,cur_itrs)
np_loss = loss.detach().cpu().numpy()
interval_loss += np_loss
if (cur_itrs) % 10 == 0:
interval_loss = interval_loss / 10
print("Epoch %d, Itrs %d/%d, Loss=%f" %
(cur_epochs, cur_itrs, opts.total_itrs, interval_loss))
interval_loss = 0.0
if (cur_itrs) % opts.val_interval == 0 and not opts.train_aug:
save_ckpt(opts.ckpts_path+'/latest_%s_%s.pth' %
(opts.model, opts.dataset))
print("validation...")
model.eval()
val_score = validate(
opts=opts, model=model, loader=val_loader,device=device, metrics=metrics
)
print(metrics.to_str(val_score))
if val_score['Mean IoU'] > best_score: # save best model
best_score = val_score['Mean IoU']
save_ckpt(opts.ckpts_path+'/best_%s_%s.pth' %
(opts.model, opts.dataset))
writer.add_scalar("mIoU", val_score['Mean IoU'] ,cur_itrs)
if opts.freeze_BB:
model.classifier.train()
else:
model.train()
if opts.train_aug and cur_itrs == opts.total_itrs:
save_ckpt(opts.ckpts_path+'/adapted_%s_%s.pth' %
(opts.model, opts.dataset))
scheduler.step()
if cur_itrs >= opts.total_itrs:
return
if __name__ == '__main__':
main()