-
Notifications
You must be signed in to change notification settings - Fork 14
/
train_baseline.py
298 lines (252 loc) · 13.3 KB
/
train_baseline.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
import argparse
import os
import random
import sys
import time
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torchvision
from tensorboardX import SummaryWriter
from torch.nn import functional as F
from torch.nn.parallel.scatter_gather import gather
from torch.utils import data
from dataset.data_pascal import DataGenerator
from network.baseline import get_model
from progress.bar import Bar
from utils.gnn_loss import gnn_loss_noatt as ABRLovaszLoss
from utils.metric import *
from utils.parallel import DataParallelModel, DataParallelCriterion
from utils.visualize import inv_preprocess, decode_predictions
def parse_args():
parser = argparse.ArgumentParser(description='PyTorch Segmentation')
parser.add_argument('--method', type=str, default='baseline')
# Datasets
parser.add_argument('--root', default='./data/Person', type=str)
parser.add_argument('--val-root', default='./data/Person', type=str)
parser.add_argument('--lst', default='./dataset/Pascal/train_id.txt', type=str)
parser.add_argument('--val-lst', default='./dataset/Pascal/val_id.txt', type=str)
parser.add_argument('--crop-size', type=int, default=473)
parser.add_argument('--num-classes', type=int, default=7)
parser.add_argument('--hbody-cls', type=int, default=3)
parser.add_argument('--fbody-cls', type=int, default=2)
# Optimization options
parser.add_argument('--epochs', default=150, type=int)
parser.add_argument('--batch-size', default=20, type=int)
parser.add_argument('--learning-rate', default=1e-2, type=float)
parser.add_argument('--lr-mode', type=str, default='poly')
parser.add_argument('--ignore-label', type=int, default=255)
# Checkpoints
parser.add_argument('--restore-from', default='./checkpoints/init/resnet101_stem.pth', type=str)
parser.add_argument('--snapshot_dir', type=str, default='./checkpoints/exp/')
parser.add_argument('--log-dir', type=str, default='./runs/')
parser.add_argument('--init', action="store_true")
parser.add_argument('--save-num', type=int, default=2)
# Misc
parser.add_argument('--seed', type=int, default=123)
args = parser.parse_args()
return args
def adjust_learning_rate(optimizer, epoch, i_iter, iters_per_epoch, method='poly'):
if method == 'poly':
current_step = epoch * iters_per_epoch + i_iter
max_step = args.epochs * iters_per_epoch
lr = args.learning_rate * ((1 - current_step / max_step) ** 0.9)
else:
lr = args.learning_rate
optimizer.param_groups[0]['lr'] = lr
return lr
def main(args):
# initialization
print("Input arguments:")
for key, val in vars(args).items():
print("{:16} {}".format(key, val))
if not os.path.exists(args.snapshot_dir):
os.makedirs(args.snapshot_dir)
writer = SummaryWriter(log_dir=os.path.join(args.log_dir, args.method))
random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.enabled = True
cudnn.benchmark = True
# conduct seg network
seg_model = get_model(num_classes=args.num_classes)
saved_state_dict = torch.load(args.restore_from)
new_params = seg_model.state_dict().copy()
if args.init:
for i in saved_state_dict:
i_parts = i.split('.')
if not i_parts[0] == 'fc':
new_params['encoder.' + '.'.join(i_parts[:])] = saved_state_dict[i]
seg_model.load_state_dict(new_params)
print('loading params w/o fc')
else:
seg_model.load_state_dict(saved_state_dict)
print('loading params all')
model = DataParallelModel(seg_model)
model.float()
model.cuda()
# define dataloader
train_loader = data.DataLoader(DataGenerator(root=args.root, list_path=args.lst,
crop_size=args.crop_size, training=True),
batch_size=args.batch_size, shuffle=True, num_workers=4, pin_memory=True, drop_last=True)
val_loader = data.DataLoader(DataGenerator(root=args.val_root, list_path=args.val_lst,
crop_size=args.crop_size, training=False),
batch_size=args.batch_size, shuffle=False, num_workers=4, pin_memory=True)
# define criterion & optimizer
criterion = ABRLovaszLoss(adj_matrix = torch.tensor(
[[0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0]]), ignore_index=args.ignore_label, only_present=True, upper_part_list=[1, 2, 3, 4], lower_part_list=[5, 6], cls_p= args.num_classes, cls_h= args.hbody_cls, cls_f= args.fbody_cls)
criterion = DataParallelCriterion(criterion).cuda()
optimizer = optim.SGD(
[{'params': filter(lambda p: p.requires_grad, seg_model.parameters()), 'lr': args.learning_rate}],
lr=args.learning_rate, momentum=0.9, weight_decay=5e-4)
# key points
best_val_mIoU = 0
best_val_pixAcc = 0
start = time.time()
for epoch in range(0, args.epochs):
print('\n{} | {}'.format(epoch, args.epochs - 1))
# training
_ = train(model, train_loader, epoch, criterion, optimizer, writer)
# validation
if epoch %10 ==0 or epoch > args.epochs-10:
val_pixacc, val_miou = validation(model, val_loader, epoch, writer)
# save model
if val_pixacc > best_val_pixAcc:
best_val_pixAcc = val_pixacc
if val_miou > best_val_mIoU:
best_val_mIoU = val_miou
model_dir = os.path.join(args.snapshot_dir, args.method + '_miou.pth')
torch.save(seg_model.state_dict(), model_dir)
print('Model saved to %s' % model_dir)
os.rename(model_dir, os.path.join(args.snapshot_dir, args.method + '_miou'+str(best_val_mIoU)+'.pth'))
print('Complete using', time.time() - start, 'seconds')
print('Best pixAcc: {} | Best mIoU: {}'.format(best_val_pixAcc, best_val_mIoU))
def train(model, train_loader, epoch, criterion, optimizer, writer):
# set training mode
model.train()
train_loss = 0.0
iter_num = 0
# Iterate over data.
# bar = Bar('Processing | {}'.format('train'), max=len(train_loader))
# bar.check_tty = False
from tqdm import tqdm
tbar = tqdm(train_loader)
for i_iter, batch in enumerate(tbar):
sys.stdout.flush()
start_time = time.time()
iter_num += 1
# adjust learning rate
iters_per_epoch = len(train_loader)
lr = adjust_learning_rate(optimizer, epoch, i_iter, iters_per_epoch, method=args.lr_mode)
# print("\n=>epoch %d, learning_rate = %f" % (epoch, lr))
image, label, hlabel, flabel, _ = batch
images, labels, hlabel, flabel = image.cuda(), label.long().cuda(), hlabel.cuda(), flabel.cuda()
torch.set_grad_enabled(True)
# zero the parameter gradients
optimizer.zero_grad()
# compute output loss
preds = model(images)
loss = criterion(preds, [labels, hlabel, flabel]) # batch mean
train_loss += loss.item()
# compute gradient and do SGD step
loss.backward()
optimizer.step()
if i_iter % 10 == 0:
writer.add_scalar('learning_rate', lr, iter_num + epoch * len(train_loader))
writer.add_scalar('train_loss', train_loss / iter_num, iter_num + epoch * len(train_loader))
batch_time = time.time() - start_time
# plot progress
tbar.set_description('{} / {} | Time: {batch_time:.4f} | Loss: {loss:.4f}'.format(iter_num, len(train_loader),
batch_time=batch_time,
loss=train_loss / iter_num))
# bar.suffix = '{} / {} | Time: {batch_time:.4f} | Loss: {loss:.4f}'.format(iter_num, len(train_loader),
# batch_time=batch_time,
# loss=train_loss / iter_num)
# bar.next()
epoch_loss = train_loss / iter_num
writer.add_scalar('train_epoch_loss', epoch_loss, epoch)
tbar.close()
# bar.finish()
return epoch_loss
def validation(model, val_loader, epoch, writer):
# set evaluate mode
model.eval()
total_correct, total_label = 0, 0
total_correct_hb, total_label_hb = 0, 0
total_correct_fb, total_label_fb = 0, 0
hist = np.zeros((args.num_classes, args.num_classes))
hist_hb = np.zeros((args.hbody_cls, args.hbody_cls))
hist_fb = np.zeros((args.fbody_cls, args.fbody_cls))
# Iterate over data.
from tqdm import tqdm
tbar = tqdm(val_loader)
for idx, batch in enumerate(tbar):
image, target, hlabel, flabel, _ = batch
image, target, hlabel, flabel = image.cuda(), target.cuda(), hlabel.cuda(), flabel.cuda()
with torch.no_grad():
h, w = target.size(1), target.size(2)
outputs = model(image)
outputs = gather(outputs, 0, dim=0)
preds = F.interpolate(input=outputs[0][-1], size=(h, w), mode='bilinear', align_corners=True)
preds_hb = F.interpolate(input=outputs[1][-1], size=(h, w), mode='bilinear', align_corners=True)
preds_fb = F.interpolate(input=outputs[2][-1], size=(h, w), mode='bilinear', align_corners=True)
# if idx % 50 == 0:
# img_vis = inv_preprocess(image, num_images=args.save_num)
# label_vis = decode_predictions(target.int(), num_images=args.save_num, num_classes=args.num_classes)
# pred_vis = decode_predictions(torch.argmax(preds, dim=1), num_images=args.save_num,
# num_classes=args.num_classes)
# # visual grids
# img_grid = torchvision.utils.make_grid(torch.from_numpy(img_vis.transpose(0, 3, 1, 2)))
# label_grid = torchvision.utils.make_grid(torch.from_numpy(label_vis.transpose(0, 3, 1, 2)))
# pred_grid = torchvision.utils.make_grid(torch.from_numpy(pred_vis.transpose(0, 3, 1, 2)))
# writer.add_image('val_images', img_grid, epoch * len(val_loader) + idx + 1)
# writer.add_image('val_labels', label_grid, epoch * len(val_loader) + idx + 1)
# writer.add_image('val_preds', pred_grid, epoch * len(val_loader) + idx + 1)
# pixelAcc
correct, labeled = batch_pix_accuracy(preds.data, target)
correct_hb, labeled_hb = batch_pix_accuracy(preds_hb.data, hlabel)
correct_fb, labeled_fb = batch_pix_accuracy(preds_fb.data, flabel)
# mIoU
hist += fast_hist(preds, target, args.num_classes)
hist_hb += fast_hist(preds_hb, hlabel, args.hbody_cls)
hist_fb += fast_hist(preds_fb, flabel, args.fbody_cls)
total_correct += correct
total_correct_hb += correct_hb
total_correct_fb += correct_fb
total_label += labeled
total_label_hb += labeled_hb
total_label_fb += labeled_fb
pixAcc = 1.0 * total_correct / (np.spacing(1) + total_label)
IoU = round(np.nanmean(per_class_iu(hist)) * 100, 2)
pixAcc_hb = 1.0 * total_correct_hb / (np.spacing(1) + total_label_hb)
IoU_hb = round(np.nanmean(per_class_iu(hist_hb)) * 100, 2)
pixAcc_fb = 1.0 * total_correct_fb / (np.spacing(1) + total_label_fb)
IoU_fb = round(np.nanmean(per_class_iu(hist_fb)) * 100, 2)
# plot progress
tbar.set_description('{} / {} | {pixAcc:.4f}, {IoU:.4f} |' \
'{pixAcc_hb:.4f}, {IoU_hb:.4f} |' \
'{pixAcc_fb:.4f}, {IoU_fb:.4f}'.format(idx + 1, len(val_loader), pixAcc=pixAcc, IoU=IoU,pixAcc_hb=pixAcc_hb, IoU_hb=IoU_hb,pixAcc_fb=pixAcc_fb, IoU_fb=IoU_fb))
# bar.suffix = '{} / {} | pixAcc: {pixAcc:.4f}, mIoU: {IoU:.4f} |' \
# 'pixAcc_hb: {pixAcc_hb:.4f}, mIoU_hb: {IoU_hb:.4f} |' \
# 'pixAcc_fb: {pixAcc_fb:.4f}, mIoU_fb: {IoU_fb:.4f}'.format(idx + 1, len(val_loader),
# pixAcc=pixAcc, IoU=IoU,
# pixAcc_hb=pixAcc_hb, IoU_hb=IoU_hb,
# pixAcc_fb=pixAcc_fb, IoU_fb=IoU_fb)
# bar.next()
print('\n per class iou part: {}'.format(per_class_iu(hist)*100))
print('per class iou hb: {}'.format(per_class_iu(hist_hb)*100))
print('per class iou fb: {}'.format(per_class_iu(hist_fb)*100))
mIoU = round(np.nanmean(per_class_iu(hist)) * 100, 2)
mIoU_hb = round(np.nanmean(per_class_iu(hist_hb)) * 100, 2)
mIoU_fb = round(np.nanmean(per_class_iu(hist_fb)) * 100, 2)
writer.add_scalar('val_pixAcc', pixAcc, epoch)
writer.add_scalar('val_mIoU', mIoU, epoch)
writer.add_scalar('val_pixAcc_hb', pixAcc_hb, epoch)
writer.add_scalar('val_mIoU_hb', mIoU_hb, epoch)
writer.add_scalar('val_pixAcc_fb', pixAcc_fb, epoch)
writer.add_scalar('val_mIoU_fb', mIoU_fb, epoch)
# bar.finish()
tbar.close()
return pixAcc, mIoU
if __name__ == '__main__':
args = parse_args()
main(args)