-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_vox_unet.py
293 lines (252 loc) · 12.3 KB
/
train_vox_unet.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
import numpy as np
import argparse
import matplotlib.pyplot as plt
import torch
import models
import datasets
from util.osutils import mkdir_p, isfile, isdir, join
from util.logger import Logger
from util.training_util import adjust_learning_rate, save_checkpoint
from util.evaluation_util import accuracy, AverageMeter
from util.vox_util import dilate_vox, three_view_with_heatmap
from losses import wMSELoss
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
best_acc = 0
device = None
def main(args):
global best_acc
global device
# create checkpoint dir
if not isdir(args.checkpoint):
print("build new folder")
mkdir_p(args.checkpoint)
# create model
print("==> creating model {}".format(args.arch))
model = models.__dict__[args.arch](num_classes=args.num_classes)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.device_count() > 1:
print('Using', torch.cuda.device_count(), 'GPUs.')
model = torch.nn.DataParallel(model)
model.to(device)
# define loss function (criterion) and optimizer
if args.wmse:
criterion = wMSELoss().to(device)
else:
criterion = torch.nn.MSELoss(size_average=True).to(device)
'''optimizer = torch.optim.RMSprop(model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)'''
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
# optionally resume from a checkpoint
title = args.arch
if args.resume:
if isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
best_acc = checkpoint['best_acc']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
logger = Logger(join(args.checkpoint, 'log.txt'), title=title, resume=True)
else:
print("=> no checkpoint found at '{}'".format(args.resume))
else:
logger = Logger(join(args.checkpoint, 'log.txt'), title=title)
logger.set_names(['Epoch', 'LR', 'Train Loss', 'Val Loss', 'Train Acc', 'Val Acc'])
print(' Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0))
# Data loading code
train_loader = torch.utils.data.DataLoader(
datasets.Fish_Vox('data/fish_vox_annotations_trainval_64.json',
'data',
'data/voxel/trainval_64_thin',
sigma=args.sigma),
batch_size=args.train_batch, shuffle=True,
num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
datasets.Fish_Vox('data/fish_vox_annotations_trainval_64.json',
'data',
'data/voxel/trainval_64_thin',
sigma=args.sigma, train=False),
batch_size=args.test_batch, shuffle=False,
num_workers=args.workers, pin_memory=True)
'''train_loader = torch.utils.data.DataLoader(
datasets.Fish_Vox('/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data/fish_vox_annotations_trainval_64.json',
'/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data',
'/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data/voxel/trainval_64_thin',
sigma=args.sigma),
batch_size=args.train_batch, shuffle=True,
num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
datasets.Fish_Vox('/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data/fish_vox_annotations_trainval_64.json',
'/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data',
'/mnt/nfs/work1/kalo/zhanxu/shark_pose_dataset/3d_data/voxel/trainval_64_thin',
sigma=args.sigma, train=False),
batch_size=args.test_batch, shuffle=False,
num_workers=args.workers, pin_memory=True)'''
if args.evaluate:
print('\nEvaluation only')
loss, acc = validate(val_loader, model, criterion, args.debug)
print('loss = {0}, accuracy = {1}'.format(loss, acc))
return
lr = args.lr
for epoch in range(args.start_epoch, args.epochs):
lr = adjust_learning_rate(optimizer, epoch, lr, args.schedule, args.gamma)
print('\nEpoch: %d | LR: %.8f' % (epoch + 1, lr))
# train for one epoch
train_loss, train_acc = train(train_loader, model, criterion, optimizer, args.debug, args.wmse)
# evaluate on validation set
valid_loss, valid_acc = validate(val_loader, model, criterion, args.debug, args.wmse)
# append logger file
logger.append([epoch + 1, lr, train_loss, valid_loss, train_acc, valid_acc])
# remember best acc and save checkpoint
is_best = valid_acc > best_acc
best_acc = max(valid_acc, best_acc)
save_checkpoint({
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_acc': best_acc,
'optimizer': optimizer.state_dict(),
}, is_best, checkpoint=args.checkpoint)
logger.close()
logger.plot(['Train Acc', 'Train Loss','Val Acc','Val Loss'])
def train(train_loader, model, criterion, optimizer, debug=False, wmse=False):
global device
losses = AverageMeter()
acces = AverageMeter()
# switch to train mode
model.train()
for i, (inputs, target, meta) in enumerate(train_loader):
input_var = inputs.to(device)
target_var = target.to(device)
# compute output
output = model(input_var)
score_map = output.cpu()
if wmse:
# weightes_MSE loss
mse_weight = inputs[:,0,...].clone()
mse_weight = mse_weight.detach()
mse_weight = dilate_vox(mse_weight, 5)
mse_weight = mse_weight.to(device)
loss = criterion(output[0], target_var[0], mse_weight[0])
for j in range(1, len(output)):
loss += criterion(output[j], target_var[j], mse_weight[j])
else:
# MSE loss
loss = criterion(output[0], target_var[0])
for j in range(1, len(output)):
loss += criterion(output[j], target_var[j])
acc,_ = accuracy(score_map, target)
# measure accuracy and record loss
losses.update(loss.data, inputs.size(0))
acces.update(acc[0], inputs.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("({0:d}/{1:d}) Loss: {2:.10f} | Acc: {3: .10f}".format(i + 1, len(train_loader), losses.avg, acces.avg))
score_map_np = score_map.clone().detach()
if debug: # visualize groundtruth and predictions
sample_view = []
for i in range(min(3, inputs.size(0))):
inp = inputs[i].squeeze().numpy()
tar = target[i].numpy()
wmse = mse_weight[i].cpu().squeeze().numpy()
scr = score_map_np[i].numpy()
vi = three_view_with_heatmap(inp[0,...], wmse, tar, scr)
sample_view.append(vi)
sample_view = np.concatenate(sample_view, axis=1)
plt.imshow(sample_view)
#plt.show()
plt.pause(.05)
return losses.avg, acces.avg
def validate(val_loader, model, criterion, debug=False, wmse=False):
losses = AverageMeter()
acces = AverageMeter()
# switch to evaluate mode
model.eval()
for i, (inputs, target, meta) in enumerate(val_loader):
input_var = inputs.to(device)
target_var = target.to(device)
# compute output
output = model(input_var)
score_map = output.cpu()
if wmse:
#weighted_MSE loss
mse_weight = inputs[:, 0, ...].clone()
mse_weight = mse_weight.detach()
mse_weight = dilate_vox(mse_weight, 3)
mse_weight = mse_weight.to(device)
loss = criterion(output[0], target_var[0], mse_weight[0])
for j in range(1, len(output)):
loss += criterion(output[j], target_var[j],mse_weight[j])
else:
# MSE loss
loss = criterion(output[0], target_var[0])
for j in range(1, len(output)):
loss += criterion(output[j], target_var[j])
acc,_ = accuracy(score_map, target.cpu())
# measure accuracy and record loss
losses.update(loss.data, inputs.size(0))
acces.update(acc[0], inputs.size(0))
print("({0:d}/{1:d}) Loss: {2:.10f} | Acc: {3: .10f}".format(i + 1, len(val_loader), losses.avg, acces.avg))
if debug: # visualize groundtruth and predictions
sample_view = []
for i in range(min(3, inputs.size(0))):
inp = inputs[i].squeeze().numpy()
tar = target[i].numpy()
vi = three_view_with_heatmap(inp[0,...], tar)
sample_view.append(vi)
sample_view = np.concatenate(sample_view, axis=1)
plt.imshow(sample_view)
#plt.show()
plt.pause(.05)
return losses.avg, acces.avg
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch 3D Voxel Training')
# Model structure
parser.add_argument('--arch', '-a', metavar='ARCH', default='vox_unet', choices=model_names,
help='model architecture: ' + ' | '.join(model_names) + ' (default: vox_unet)')
parser.add_argument('--num-classes', default=10, type=int, metavar='N',
help='Number of keypoints')
# Training strategy
parser.add_argument('--workers', '-j', default=1, type=int, metavar='N',
help='number of data loading workers (default: 1)')
parser.add_argument('--epochs', default=300, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('--train-batch', default=2, type=int, metavar='N',
help='train batchsize')
parser.add_argument('--test-batch', default=2, type=int, metavar='N',
help='test batchsize')
parser.add_argument('--lr', '--learning-rate', default=1e-4, type=float,
metavar='LR', help='initial learning rate') # 2.5e-4
parser.add_argument('--momentum', default=0, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-5, type=float,
metavar='W', help='weight decay (default: 0)')
parser.add_argument('--schedule', type=int, nargs='+', default=[100, 200],
help='Decrease learning rate at these epochs.')
parser.add_argument('--gamma', type=float, default=0.1,
help='LR is multiplied by gamma on schedule.')
# Data processing
parser.add_argument('--sigma', type=float, default=4.0,
help='Groundtruth Gaussian sigma.')
# Miscs
parser.add_argument('-c', '--checkpoint', default='checkpoint/checkpoint_vox_test', type=str, metavar='PATH',
help='path to save checkpoint (default: checkpoint)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('-d', '--debug', dest='debug', action='store_true',
help='show intermediate results')
#experiment params
parser.add_argument('-wmse', dest='wmse', action='store_true', help='use weighted mes loss')
main(parser.parse_args())