forked from xiaoyufenfei/Efficient-Segmentation-Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
404 lines (325 loc) · 16.3 KB
/
train.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
import os,sys
import time
import torch
from torch import optim
import torch.nn as nn
import timeit
import math
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import torch.backends.cudnn as cudnn
from argparse import ArgumentParser
# user
from builders.model_builder import build_model
from builders.dataset_builder import build_dataset_train
from utils.utils import setup_seed, init_weight, netParams
from utils.metric.metric import get_iou
from utils.losses.loss import LovaszSoftmax, CrossEntropyLoss2d, CrossEntropyLoss2dLabelSmooth,\
ProbOhemCrossEntropy2d, FocalLoss2d
from utils.optim import RAdam, Ranger, AdamW
from utils.scheduler.lr_scheduler import WarmupPolyLR
sys.setrecursionlimit(1000000) # solve problem 'maximum recursion depth exceeded'
torch_ver = torch.__version__[:3]
if torch_ver == '0.3':
from torch.autograd import Variable
print(torch_ver)
GLOBAL_SEED = 1234
def parse_args():
parser = ArgumentParser(description='Efficient semantic segmentation')
# model and dataset
parser.add_argument('--model', type=str, default="ENet", help="model name: (default ENet)")
parser.add_argument('--dataset', type=str, default="camvid", help="dataset: cityscapes or camvid")
parser.add_argument('--input_size', type=str, default="360,480", help="input size of model")
parser.add_argument('--num_workers', type=int, default=4, help=" the number of parallel threads")
parser.add_argument('--classes', type=int, default=11,
help="the number of classes in the dataset. 19 and 11 for cityscapes and camvid, respectively")
parser.add_argument('--train_type', type=str, default="trainval",
help="ontrain for training on train set, ontrainval for training on train+val set")
# training hyper params
parser.add_argument('--max_epochs', type=int, default=1000,
help="the number of epochs: 300 for train set, 350 for train+val set")
parser.add_argument('--random_mirror', type=bool, default=True, help="input image random mirror")
parser.add_argument('--random_scale', type=bool, default=True, help="input image resize 0.5 to 2")
parser.add_argument('--lr', type=float, default=5e-4, help="initial learning rate")
parser.add_argument('--batch_size', type=int, default=8, help="the batch size is set to 16 for 2 GPUs")
parser.add_argument('--optim',type=str.lower,default='adam',choices=['sgd','adam','radam','ranger'],help="select optimizer")
parser.add_argument('--lr_schedule', type=str, default='warmpoly', help='name of lr schedule: poly')
parser.add_argument('--num_cycles', type=int, default=1, help='Cosine Annealing Cyclic LR')
parser.add_argument('--poly_exp', type=float, default=0.9,help='polynomial LR exponent')
parser.add_argument('--warmup_iters', type=int, default=500, help='warmup iterations')
parser.add_argument('--warmup_factor', type=float, default=1.0 / 3, help='warm up start lr=warmup_factor*lr')
parser.add_argument('--use_label_smoothing', action='store_true', default=False, help="CrossEntropy2d Loss with label smoothing or not")
parser.add_argument('--use_ohem', action='store_true', default=False, help='OhemCrossEntropy2d Loss for cityscapes dataset')
parser.add_argument('--use_lovaszsoftmax', action='store_true', default=False, help='LovaszSoftmax Loss for cityscapes dataset')
parser.add_argument('--use_focal', action='store_true', default=False,help=' FocalLoss2d for cityscapes dataset')
# cuda setting
parser.add_argument('--cuda', type=bool, default=True, help="running on CPU or GPU")
parser.add_argument('--gpus', type=str, default="0", help="default GPU devices (0,1)")
# checkpoint and log
parser.add_argument('--resume', type=str, default="",
help="use this file to load last checkpoint for continuing training")
parser.add_argument('--savedir', default="./checkpoint/", help="directory to save the model snapshot")
parser.add_argument('--logFile', default="log.txt", help="storing the training and validation logs")
args = parser.parse_args()
return args
def train_model(args):
"""
args:
args: global arguments
"""
h, w = map(int, args.input_size.split(','))
input_size = (h, w)
print("=====> input size:{}".format(input_size))
print(args)
if args.cuda:
print("=====> use gpu id: '{}'".format(args.gpus))
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
if not torch.cuda.is_available():
raise Exception("No GPU found or Wrong gpu id, please run without --cuda")
# set the seed
setup_seed(GLOBAL_SEED)
print("=====> set Global Seed: ", GLOBAL_SEED)
cudnn.enabled = True
print("=====> building network")
# build the model and initialization
model = build_model(args.model, num_classes=args.classes)
init_weight(model, nn.init.kaiming_normal_,
nn.BatchNorm2d, 1e-3, 0.1,
mode='fan_in')
print("=====> computing network parameters and FLOPs")
total_paramters = netParams(model)
print("the number of parameters: %d ==> %.2f M" % (total_paramters, (total_paramters / 1e6)))
# load data and data augmentation
datas, trainLoader, valLoader = build_dataset_train(args.dataset, input_size, args.batch_size, args.train_type,
args.random_scale, args.random_mirror, args.num_workers)
args.per_iter = len(trainLoader)
args.max_iter = args.max_epochs * args.per_iter
print('=====> Dataset statistics')
print("data['classWeights']: ", datas['classWeights'])
print('mean and std: ', datas['mean'], datas['std'])
# define loss function, respectively
weight = torch.from_numpy(datas['classWeights'])
if args.dataset == 'camvid':
criteria = CrossEntropyLoss2d(weight=weight, ignore_label=ignore_label)
elif args.dataset == 'camvid' and args.use_label_smoothing:
criteria = CrossEntropyLoss2dLabelSmooth(weight=weight, ignore_label=ignore_label)
elif args.dataset == 'cityscapes' and args.use_ohem:
min_kept = int(args.batch_size // len(args.gpus) * h * w // 16)
criteria = ProbOhemCrossEntropy2d(use_weight=True, ignore_label=ignore_label, thresh=0.7, min_kept=min_kept)
elif args.dataset == 'cityscapes' and args.use_label_smoothing:
criteria = CrossEntropyLoss2dLabelSmooth(weight=weight, ignore_label=ignore_label)
elif args.dataset == 'cityscapes' and args.use_lovaszsoftmax:
criteria = LovaszSoftmax(ignore_index=ignore_label)
elif args.dataset == 'cityscapes' and args.use_focal:
criteria = FocalLoss2d(weight=weight, ignore_index=ignore_label)
else:
raise NotImplementedError(
"This repository now supports two datasets: cityscapes and camvid, %s is not included" % args.dataset)
if args.cuda:
criteria = criteria.cuda()
if torch.cuda.device_count() > 1:
print("torch.cuda.device_count()=", torch.cuda.device_count())
args.gpu_nums = torch.cuda.device_count()
model = nn.DataParallel(model).cuda() # multi-card data parallel
else:
args.gpu_nums = 1
print("single GPU for training")
model = model.cuda() # 1-card data parallel
args.savedir = (args.savedir + args.dataset + '/' + args.model + 'bs'
+ str(args.batch_size) + 'gpu' + str(args.gpu_nums) + "_" + str(args.train_type) + '/')
if not os.path.exists(args.savedir):
os.makedirs(args.savedir)
start_epoch = 0
# continue training
if args.resume:
if os.path.isfile(args.resume):
checkpoint = torch.load(args.resume)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['model'])
# model.load_state_dict(convert_state_dict(checkpoint['model']))
print("=====> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']))
else:
print("=====> no checkpoint found at '{}'".format(args.resume))
model.train()
cudnn.benchmark = True
# cudnn.deterministic = True ## my add
logFileLoc = args.savedir + args.logFile
if os.path.isfile(logFileLoc):
logger = open(logFileLoc, 'a')
else:
logger = open(logFileLoc, 'w')
logger.write("Parameters: %s Seed: %s" % (str(total_paramters), GLOBAL_SEED))
logger.write("\n%s\t\t%s\t%s\t%s" % ('Epoch', 'Loss(Tr)', 'mIOU (val)', 'lr'))
logger.flush()
# define optimization strategy
if args.optim == 'sgd':
optimizer = torch.optim.SGD(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, momentum=0.9, weight_decay=1e-4)
elif args.optim == 'adam':
optimizer = torch.optim.Adam(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-4)
elif args.optim == 'radam':
optimizer = RAdam(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, betas=(0.90, 0.999), eps=1e-08, weight_decay=1e-4)
elif args.optim == 'ranger':
optimizer = Ranger(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, betas=(0.95, 0.999), eps=1e-08, weight_decay=1e-4)
elif args.optim == 'adamw':
optimizer = AdamW(
filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-4)
lossTr_list = []
epoches = []
mIOU_val_list = []
print('=====> beginning training')
for epoch in range(start_epoch, args.max_epochs):
# training
lossTr, lr = train(args, trainLoader, model, criteria, optimizer, epoch)
lossTr_list.append(lossTr)
# validation
if epoch % 50 == 0 or epoch == (args.max_epochs - 1):
epoches.append(epoch)
mIOU_val, per_class_iu = val(args, valLoader, model)
mIOU_val_list.append(mIOU_val)
# record train information
logger.write("\n%d\t\t%.4f\t\t%.4f\t\t%.7f" % (epoch, lossTr, mIOU_val, lr))
logger.flush()
print("Epoch : " + str(epoch) + ' Details')
print("Epoch No.: %d\tTrain Loss = %.4f\t mIOU(val) = %.4f\t lr= %.6f\n" % (epoch,
lossTr,
mIOU_val, lr))
else:
# record train information
logger.write("\n%d\t\t%.4f\t\t\t\t%.7f" % (epoch, lossTr, lr))
logger.flush()
print("Epoch : " + str(epoch) + ' Details')
print("Epoch No.: %d\tTrain Loss = %.4f\t lr= %.6f\n" % (epoch, lossTr, lr))
# save the model
model_file_name = args.savedir + '/model_' + str(epoch + 1) + '.pth'
state = {"epoch": epoch + 1, "model": model.state_dict()}
# Individual Setting for save model !!!
if args.dataset == 'camvid':
torch.save(state, model_file_name)
elif args.dataset == 'cityscapes':
if epoch >= args.max_epochs - 10:
torch.save(state, model_file_name)
elif not epoch % 50:
torch.save(state, model_file_name)
# draw plots for visualization
if epoch % 50 == 0 or epoch == (args.max_epochs - 1):
# Plot the figures per 50 epochs
fig1, ax1 = plt.subplots(figsize=(11, 8))
ax1.plot(range(start_epoch, epoch + 1), lossTr_list)
ax1.set_title("Average training loss vs epochs")
ax1.set_xlabel("Epochs")
ax1.set_ylabel("Current loss")
plt.savefig(args.savedir + "loss_vs_epochs.png")
plt.clf()
fig2, ax2 = plt.subplots(figsize=(11, 8))
ax2.plot(epoches, mIOU_val_list, label="Val IoU")
ax2.set_title("Average IoU vs epochs")
ax2.set_xlabel("Epochs")
ax2.set_ylabel("Current IoU")
plt.legend(loc='lower right')
plt.savefig(args.savedir + "iou_vs_epochs.png")
plt.close('all')
logger.close()
def train(args, train_loader, model, criterion, optimizer, epoch):
"""
args:
train_loader: loaded for training dataset
model: model
criterion: loss function
optimizer: optimization algorithm, such as ADAM or SGD
epoch: epoch number
return: average loss, per class IoU, and mean IoU
"""
model.train()
epoch_loss = []
total_batches = len(train_loader)
print("=====> the number of iterations per epoch: ", total_batches)
st = time.time()
for iteration, batch in enumerate(train_loader, 0):
args.per_iter = total_batches
args.max_iter = args.max_epochs * args.per_iter
args.cur_iter = epoch * args.per_iter + iteration
# learming scheduling
if args.lr_schedule == 'poly':
lambda1 = lambda epoch: math.pow((1 - (args.cur_iter / args.max_iter)), args.poly_exp)
scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda1)
elif args.lr_schedule == 'warmpoly':
scheduler = WarmupPolyLR(optimizer, T_max=args.max_iter, cur_iter=args.cur_iter, warmup_factor=1.0 / 3,
warmup_iters=args.warmup_iters, power=0.9)
lr = optimizer.param_groups[0]['lr']
start_time = time.time()
images, labels, _, _ = batch
if torch_ver == '0.3':
images = Variable(images).cuda()
labels = Variable(labels.long()).cuda()
else:
images = images.cuda()
labels = labels.long().cuda()
output = model(images)
loss = criterion(output, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step() # In pytorch 1.1.0 and later, should call 'optimizer.step()' before 'lr_scheduler.step()'
epoch_loss.append(loss.item())
time_taken = time.time() - start_time
print('=====> epoch[%d/%d] iter: (%d/%d) \tcur_lr: %.6f loss: %.3f time:%.2f' % (epoch + 1, args.max_epochs,
iteration + 1, total_batches,
lr, loss.item(), time_taken))
time_taken_epoch = time.time() - st
remain_time = time_taken_epoch * (args.max_epochs - 1 - epoch)
m, s = divmod(remain_time, 60)
h, m = divmod(m, 60)
print("Remaining training time = %d hour %d minutes %d seconds" % (h, m, s))
average_epoch_loss_train = sum(epoch_loss) / len(epoch_loss)
return average_epoch_loss_train, lr
def val(args, val_loader, model):
"""
args:
val_loader: loaded for validation dataset
model: model
return: mean IoU and IoU class
"""
# evaluation mode
model.eval()
total_batches = len(val_loader)
data_list = []
for i, (input, label, size, name) in enumerate(val_loader):
start_time = time.time()
with torch.no_grad():
# input_var = Variable(input).cuda()
input_var = input.cuda()
output = model(input_var)
time_taken = time.time() - start_time
print("[%d/%d] time: %.2f" % (i + 1, total_batches, time_taken))
output = output.cpu().data[0].numpy()
gt = np.asarray(label[0].numpy(), dtype=np.uint8)
output = output.transpose(1, 2, 0)
output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
data_list.append([gt.flatten(), output.flatten()])
meanIoU, per_class_iu = get_iou(data_list, args.classes)
return meanIoU, per_class_iu
if __name__ == '__main__':
start = timeit.default_timer()
args = parse_args()
if args.dataset == 'cityscapes':
args.classes = 19
args.input_size = '512,1024'
ignore_label = 255
elif args.dataset == 'camvid':
args.classes = 11
args.input_size = '360,480'
ignore_label = 11
else:
raise NotImplementedError(
"This repository now supports two datasets: cityscapes and camvid, %s is not included" % args.dataset)
train_model(args)
end = timeit.default_timer()
hour = 1.0 * (end - start) / 3600
minute = (hour - int(hour)) * 60
print("training time: %d hour %d minutes" % (int(hour), int(minute)))