-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_msra.py
320 lines (260 loc) · 14.3 KB
/
train_msra.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
import numpy as np
import matplotlib.pyplot as plt
import torch, torchvision
from torch.utils.tensorboard import SummaryWriter
import os, argparse
from tqdm import tqdm
from model import PixelwiseRegression
import datasets
from utils import setup_seed, step_loader, save_model, draw_skeleton_torch, select_gpus, draw_features_torch, recover_uvd
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--suffix', type=str, default="default",
help="the suffix of model file and log file"
)
parser.add_argument('--seed', type=int, default=0,
help="the random seed used in the training, 0 means do not use fix seed"
)
parser.add_argument('--subject', type=int, default=0)
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--label_size', type=int, default=64)
parser.add_argument('--kernel_size', type=int, default=7)
parser.add_argument('--sigmoid', type=float, default=1.5)
parser.add_argument('--norm_method', type=str, default='instance', help='choose from batch and instance')
parser.add_argument('--heatmap_method', type=str, default='softmax', help='choose from softmax and sum')
# need more time to train if using any of these augmentation
parser.add_argument('--using_rotation', type=lambda x: [False, True][int(x)], default=True)
parser.add_argument('--using_scale', type=lambda x: [False, True][int(x)], default=True)
parser.add_argument('--using_shift', type=lambda x: [False, True][int(x)], default=True)
parser.add_argument('--using_flip', type=lambda x: [False, True][int(x)], default=False)
parser.add_argument('--gpu_id', type=str, default="0")
parser.add_argument('--epoch', type=int, default=50)
parser.add_argument("--num_workers", type=int, default=9999)
parser.add_argument('--stages', type=int, default=2)
parser.add_argument('--features', type=int, default=128)
parser.add_argument('--level', type=int, default=4)
parser.add_argument('--filter_size', type=int, default=3)
parser.add_argument('--opt', type=str, default='adam', help='choose from adam and sgd')
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--beta1', type=float, default=0.9)
parser.add_argument('--beta2', type=float, default=0.999)
parser.add_argument('--weight_decay', type=float, default=0)
parser.add_argument('--mixed_precision', action='store_true', help='enbale mixed precision training')
parser.add_argument("--lambda_h", type=float, default=1.0)
parser.add_argument('--lambda_d', type=float, default=0.01)
parser.add_argument('--alpha', type=float, default=1.0)
parser.add_argument('--lr_decay', type=float, default=0.2)
parser.add_argument('--decay_epoch', type=float, default=15)
args = parser.parse_args()
if not os.path.exists('Model'):
os.makedirs('Model')
seed = args.seed if args.seed else np.random.randint(0, 100000)
setup_seed(seed)
trainset_parameters = {
"dataset" : "train",
"image_size" : args.label_size * 2,
"label_size" : args.label_size,
"kernel_size" : args.kernel_size,
"sigmoid" : args.sigmoid,
"using_rotation" : args.using_rotation,
"using_scale" : args.using_scale,
"using_shift" : args.using_shift,
"using_flip" : args.using_flip,
"subject" : args.subject,
}
valset_parameters = {
"dataset" : "val",
"image_size" : args.label_size * 2,
"label_size" : args.label_size,
"kernel_size" : args.kernel_size,
"sigmoid" : args.sigmoid,
"using_rotation" : False,
"using_scale" : False,
"using_shift" : False,
"using_flip" : False,
"subject" : args.subject,
}
train_loader_parameters = {
"batch_size" : args.batch_size,
"shuffle" : True,
"pin_memory" : True,
"drop_last" : True,
"num_workers" : min(args.num_workers, os.cpu_count()),
}
val_loader_parameters = {
"batch_size" : args.batch_size,
"shuffle" : False,
"pin_memory" : True,
"drop_last" : False,
"num_workers" : min(args.num_workers, os.cpu_count()),
}
model_parameters = {
"stage" : args.stages,
"label_size" : args.label_size,
"features" : args.features,
"level" : args.level,
"norm_method" : args.norm_method,
"heatmap_method" : args.heatmap_method,
"kernel_size" : args.filter_size,
}
args.suffix += '_subject{}'.format(args.subject)
log_name = "MSRA_{}".format(args.suffix)
model_name = log_name + "_{}.pt"
Dataset = datasets.MSRADataset
trainset = Dataset(**trainset_parameters)
valset = Dataset(**valset_parameters)
joints = trainset.joint_number
config = trainset.config
train_loader = torch.utils.data.DataLoader(trainset, **train_loader_parameters)
val_loader = torch.utils.data.DataLoader(valset, **val_loader_parameters)
select_gpus(args.gpu_id)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = PixelwiseRegression(joints, **model_parameters)
model = model.to(device)
if args.opt == 'adam':
optim = torch.optim.AdamW(model.parameters(), lr=args.lr, betas=(args.beta1, args.beta2), weight_decay=args.weight_decay)
elif args.opt == 'sgd':
optim = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.beta1, weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.StepLR(optim, step_size=args.decay_epoch, gamma=args.lr_decay)
if args.mixed_precision:
scaler = torch.cuda.amp.GradScaler()
writer = SummaryWriter('logs/{}'.format(log_name))
steps_per_epoch = len(trainset) // args.batch_size
print("there are {} steps per epoch!".format(steps_per_epoch))
total_steps = steps_per_epoch * args.epoch
best_epoch = 0
best_error = 9999999
with tqdm(total=total_steps) as pbar:
for epoch in range(args.epoch):
for batch in iter(train_loader):
img, label_img, mask, box_size, cube_size, com, uvd, heatmaps, depthmaps = batch
img = img.to(device, non_blocking=True)
label_img = label_img.to(device, non_blocking=True)
mask = mask.to(device, non_blocking=True)
uvd = uvd.to(device, non_blocking=True)
heatmaps = heatmaps.to(device, non_blocking=True)
depthmaps = depthmaps.to(device, non_blocking=True)
optim.zero_grad()
if args.mixed_precision: # mixed precision training code
with torch.cuda.amp.autocast():
results = model(img, label_img, mask)
every_loss = []
for i, result in enumerate(results):
_heatmaps, _depthmaps, _uvd = result
heatmap_loss = args.lambda_h * torch.mean(torch.sum((_heatmaps - heatmaps) ** 2, dim=(2, 3)))
depthmap_loss = args.lambda_d * torch.mean(torch.sum((_depthmaps - depthmaps) ** 2, dim=(2, 3)))
uvd_loss = torch.mean(torch.sum((_uvd - uvd) ** 2, dim=2))
every_loss.append((heatmap_loss, depthmap_loss, uvd_loss))
loss = 0
for losses in every_loss:
heatmap_loss, depthmap_loss, uvd_loss = losses
loss = loss + args.alpha * uvd_loss + (1 - args.alpha) * (heatmap_loss + depthmap_loss)
scaler.scale(loss).backward()
scaler.step(optim)
scaler.update()
else: # normal training code
results = model(img, label_img, mask)
every_loss = []
for i, result in enumerate(results):
_heatmaps, _depthmaps, _uvd = result
heatmap_loss = args.lambda_h * torch.mean(torch.sum((_heatmaps - heatmaps) ** 2, dim=(2, 3)))
depthmap_loss = args.lambda_d * torch.mean(torch.sum((_depthmaps - depthmaps) ** 2, dim=(2, 3)))
uvd_loss = torch.mean(torch.sum((_uvd - uvd) ** 2, dim=2))
every_loss.append((heatmap_loss, depthmap_loss, uvd_loss))
loss = 0
for losses in every_loss:
heatmap_loss, depthmap_loss, uvd_loss = losses
loss = loss + args.alpha * uvd_loss + (1 - args.alpha) * (heatmap_loss + depthmap_loss)
loss.backward()
optim.step()
pbar.update(1)
scheduler.step()
# log image results in tensorboard
writer.add_images('input_image', img, global_step=epoch)
writer.add_figure('input_heatmap', draw_features_torch(heatmaps[0]), global_step=epoch)
writer.add_figure('input_depthmap', draw_features_torch(depthmaps[0]), global_step=epoch)
skeleton = draw_skeleton_torch(img[0].cpu(), uvd[0].cpu(), config)
writer.add_image('input_skeleton', skeleton, global_step=epoch)
for i, result in enumerate(results):
_heatmaps, _depthmaps, _uvd = result
_heatmaps = _heatmaps.float()
_depthmaps = _depthmaps.float()
_uvd = _uvd.float()
writer.add_figure('stage{}_heatmap'.format(i), draw_features_torch(_heatmaps[0]), global_step=epoch)
writer.add_figure('stage{}_depthmap'.format(i), draw_features_torch(_depthmaps[0]), global_step=epoch)
_skeleton = draw_skeleton_torch(img[0].cpu(), _uvd[0].detach().cpu(), config)
writer.add_image('stage{}_skeleton'.format(i), _skeleton, global_step=epoch)
model.eval()
with torch.no_grad():
# compute val losses
num = 0
val_every_loss = []
dataset_results = []
for i in range(len(results)):
val_every_loss.append((0, 0, 0))
dataset_results.append([])
for val_batch in iter(val_loader):
num += 1
img, label_img, mask, box_size, cube_size, com, uvd, heatmaps, depthmaps = val_batch
img = img.to(device, non_blocking=True)
label_img = label_img.to(device, non_blocking=True)
mask = mask.to(device, non_blocking=True)
uvd = uvd.to(device, non_blocking=True)
heatmaps = heatmaps.to(device, non_blocking=True)
depthmaps = depthmaps.to(device, non_blocking=True)
results = model(img, label_img, mask)
true_uvd = uvd.cpu()
true_uvd = recover_uvd(true_uvd, box_size, com, cube_size)
true_uvd = true_uvd.numpy()
true_xyz = valset.uvd2xyz(true_uvd)
for i, result in enumerate(results):
_heatmaps, _depthmaps, _uvd = result
heatmap_loss = args.lambda_h * torch.mean(torch.sum((_heatmaps - heatmaps) ** 2, dim=(2, 3)))
depthmap_loss = args.lambda_d * torch.mean(torch.sum((_depthmaps - depthmaps) ** 2, dim=(2, 3)))
uvd_loss = torch.mean(torch.sum((_uvd - uvd) ** 2, dim=2))
_heatmaps_loss, _depthmap_loss, _uvd_loss = val_every_loss[i]
val_every_loss[i] = ((
_heatmaps_loss + heatmap_loss,
_depthmap_loss + depthmap_loss,
_uvd_loss + uvd_loss))
_uvd = _uvd.cpu()
_uvd = recover_uvd(_uvd, box_size, com, cube_size)
_uvd = _uvd.numpy()
_xyz = valset.uvd2xyz(_uvd)
dataset_results[i].append(np.mean(np.sqrt(np.sum((_xyz - true_xyz) ** 2, axis=2)), axis=1))
for i in range(len(results)):
_heatmaps_loss, _depthmap_loss, _uvd_loss = val_every_loss[i]
val_every_loss[i] = ((
_heatmaps_loss / num,
_depthmap_loss / num,
_uvd_loss / num))
dataset_results[i] = np.mean(np.concatenate(dataset_results[i], axis=0))
val_loss = 0
for losses in val_every_loss:
heatmap_loss, depthmap_loss, uvd_loss = losses
val_loss = val_loss + args.alpha * uvd_loss + (1 - args.alpha) * (heatmap_loss + depthmap_loss)
model.train()
# log scalas in tensorboard
writer.add_scalars('loss', {'train' : loss.item(), 'val' : val_loss.item()}, global_step=epoch)
for i in range(len(every_loss)):
train_heatmap_loss, train_depthmap_loss, train_uvd_loss = every_loss[i]
val_heatmap_loss, val_depthmap_loss, val_uvd_loss = val_every_loss[i]
writer.add_scalars('stage{}_heatmap_loss'.format(i),
{'train' : train_heatmap_loss, 'val' : val_heatmap_loss},
global_step=epoch
)
writer.add_scalars('stage{}_depthmap_loss'.format(i),
{'train' : train_depthmap_loss, 'val' : val_depthmap_loss},
global_step=epoch
)
writer.add_scalars('stage{}_uvd_loss'.format(i),
{'train' : train_uvd_loss, 'val' : val_uvd_loss},
global_step=epoch
)
writer.add_scalar('stage{}_result'.format(i), dataset_results[i], global_step=epoch)
save_model(model, os.path.join('Model', model_name.format(epoch)), seed=seed, model_param=model_parameters)
if dataset_results[-1] < best_error:
best_epoch = epoch
best_error = dataset_results[-1]
print("best epoch is {}".format(best_epoch))
os.system('cp {} {}'.format(os.path.join('Model', model_name.format(best_epoch)), os.path.join('Model', model_name.format('final'))))