-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_msk.py
338 lines (261 loc) · 10.3 KB
/
main_msk.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
import os
import time
import json
from collections import OrderedDict
import importlib
import logging
import argparse
import numpy as np
import random
import torch
import torch.nn as nn
import torch.optim
import torch.utils.data
import torch.backends.cudnn
import torchvision.utils
import torchvision
try:
from tensorboardX import SummaryWriter
is_tensorboard_available = True
except Exception:
is_tensorboard_available = False
from dataloader import get_loader
torch.backends.cudnn.benchmark = True
logging.basicConfig(
format='[%(asctime)s %(name)s %(levelname)s] - %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
level=logging.DEBUG)
logger = logging.getLogger(__name__)
global_step = 0
def str2bool(s):
if s.lower() == 'true':
return True
elif s.lower() == 'false':
return False
else:
raise RuntimeError('Boolean value expected')
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--arch', type=str, required=True, choices=['lenet', 'levgg', 'msk', 'vgg19', 'resnet50', 'inception', 'ensemble', 'enc_msk'])
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--test_id', type=int, required=True)
parser.add_argument('--outdir', type=str, required=True)
parser.add_argument('--seed', type=int, default=17)
parser.add_argument('--num_workers', type=int, default=7)
# optimizer
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--base_lr', type=float, default=0.03)
parser.add_argument('--weight_decay', type=float, default=1e-4)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--nesterov', type=str2bool, default=True)
parser.add_argument('--milestones', type=str, default='[8, 9]')
parser.add_argument('--lr_decay', type=float, default=0.1)
parser.add_argument('--patience', type=int, default=1)
parser.add_argument('--verbose', type=str2bool, default=True)
parser.add_argument('--factor', type=float, default=0.1)
parser.add_argument('--cooldown', type=int, default=0)
parser.add_argument('--min_lr', type=float, default=1e-8)
# parser.add_argument('--eps', type=float, default=1e-8)
# TensorBoard
parser.add_argument(
'--tensorboard', dest='tensorboard', action='store_true', default=True)
parser.add_argument(
'--no-tensorboard', dest='tensorboard', action='store_false', default=True)
parser.add_argument('--tensorboard_images', action='store_true')
parser.add_argument('--tensorboard_parameters', action='store_true', default=True)
args = parser.parse_args()
if not is_tensorboard_available:
args.tensorboard = False
args.tensorboard_images = False
args.tensorboard_parameters = False
assert os.path.exists(args.dataset)
args.milestones = json.loads(args.milestones)
return args
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, num):
self.val = val
self.sum += val * num
self.count += num
self.avg = self.sum / self.count
def convert_to_unit_vector(angles):
x = -torch.cos(angles[:, 0]) * torch.sin(angles[:, 1])
y = -torch.sin(angles[:, 0])
z = -torch.cos(angles[:, 1]) * torch.cos(angles[:, 1])
norm = torch.sqrt(x**2 + y**2 + z**2)
x /= norm
y /= norm
z /= norm
return x, y, z
def compute_angle_error(preds, labels):
pred_x, pred_y, pred_z = convert_to_unit_vector(preds)
label_x, label_y, label_z = convert_to_unit_vector(labels)
angles = pred_x * label_x + pred_y * label_y + pred_z * label_z
return torch.acos(angles) * 180 / np.pi
args = parse_args()
def train(epoch, model, optimizer, criterion, train_loader, config, writer):
global global_step
logger.info('Train {}'.format(epoch))
model.train()
loss_meter = AverageMeter()
angle_error_meter = AverageMeter()
start = time.time()
for step, (images, poses, gazes) in enumerate(train_loader):
global_step += 1
if config['tensorboard_images'] and step == 0:
image = torchvision.utils.make_grid(
images, normalize=True, scale_each=True)
writer.add_image('Train/Image', image, epoch)
images = images.cuda()
poses = poses.cuda()
gazes = gazes.cuda()
optimizer.zero_grad()
outputs = model(images, poses)
loss = criterion(outputs, gazes)
loss.backward()
optimizer.step()
angle_error = compute_angle_error(outputs, gazes).mean()
num = images.size(0)
loss_meter.update(loss.item(), num)
angle_error_meter.update(angle_error.item(), num)
if config['tensorboard']:
writer.add_scalar('Train/RunningLoss.{}'.format(args.test_id), loss_meter.val, global_step)
if step % 100 == 0:
logger.info('Epoch {} Step {}/{} '
'Loss {:.4f} ({:.4f}) '
'AngleError {:.2f} ({:.2f})'.format(
epoch,
step,
len(train_loader),
loss_meter.val,
loss_meter.avg,
angle_error_meter.val,
angle_error_meter.avg,
))
elapsed = time.time() - start
logger.info('Elapsed {:.2f}'.format(elapsed))
if config['tensorboard']:
writer.add_scalar('Train/Loss', loss_meter.avg, epoch)
writer.add_scalar('Train/AngleError{}'.format(args.test_id), angle_error_meter.avg, epoch)
writer.add_scalar('Train/Time', elapsed, epoch)
def test(epoch, model, criterion, test_loader, config, writer):
logger.info('Test {}'.format(epoch))
model.eval()
loss_meter = AverageMeter()
angle_error_meter = AverageMeter()
start = time.time()
for step, (images, poses, gazes) in enumerate(test_loader):
if config['tensorboard_images'] and epoch == 0 and step == 0:
img = torchvision.utils.make_grid(
images, normalize=True, scale_each=True)
writer.add_image('Test/Image', img, epoch)
#p = nn.ConstantPad2d((164, 164, 188, 188), 0)
#images = p(images).resize_(64,1, 224, 224)
images = images.cuda()
poses = poses.cuda()
gazes = gazes.cuda()
# print('poses shape: ', poses.shape)
#print(poses[0])
# print( 'im shape: ' , images.shape)
#print(images[0])
with torch.no_grad():
outputs = model(images, poses)
loss = criterion(outputs, gazes)
angle_error = compute_angle_error(outputs, gazes).mean()
num = images.size(0)
loss_meter.update(loss.item(), num)
angle_error_meter.update(angle_error.item(), num)
logger.info('Epoch {} Loss {:.4f} AngleError {:.2f}'.format(
epoch, loss_meter.avg, angle_error_meter.avg))
elapsed = time.time() - start
logger.info('Elapsed {:.2f}'.format(elapsed))
if config['tensorboard']:
if epoch > 0:
writer.add_scalar('Test/Loss', loss_meter.avg, epoch)
writer.add_scalar('Test/AngleError{}'.format(args.test_id), angle_error_meter.avg, epoch)
writer.add_scalar('Test/Time', elapsed, epoch)
if config['tensorboard_parameters']:
for name, param in model.named_parameters():
writer.add_histogram(name, param, global_step)
return angle_error_meter.avg
def main():
# args = parse_args()
logger.info(json.dumps(vars(args), indent=2))
# TensorBoard SummaryWriter
writer = SummaryWriter(filename_suffix='Ensemble_att{}'.format(args.test_id)) if args.tensorboard else None
# set random seed
seed = args.seed
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# create output directory
outdir = args.outdir
if not os.path.exists(outdir):
os.makedirs(outdir)
outpath = os.path.join(outdir, 'config.json')
with open(outpath, 'w') as fout:
json.dump(vars(args), fout, indent=2)
# data loaders
train_loader, test_loader = get_loader(
args.dataset, args.test_id, args.batch_size, args.num_workers, True)
module = importlib.import_module('models.{}'.format(args.arch))
posnet = module.PosNet()
imnet = module.ImNet()
posup = module.PosUp()
# model = module.Model()
model = module.Ensemble(imnet, posnet, posup)
model.cuda()
# writer.add_graph(model, images)
criterion = nn.MSELoss(size_average=True)
# optimizer
optimizer = torch.optim.SGD(
model.parameters(),
lr=args.base_lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
nesterov=args.nesterov)
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=args.milestones, gamma=args.lr_decay)
# scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
# optimizer,
# patience=args.patience,
# verbose=args.verbose,
# factor=args.factor,
# cooldown=args.cooldown,
# min_lr=args.min_lr)
#
config = {
'tensorboard': args.tensorboard,
'tensorboard_images': args.tensorboard_images,
'tensorboard_parameters': args.tensorboard_parameters,
}
# run test before start training
test(0, model, criterion, test_loader, config, writer)
for epoch in range(1, args.epochs + 1):
train(epoch, model, optimizer, criterion, train_loader, config, writer)
angle_error = test(epoch, model, criterion, test_loader, config,
writer)
#scheduler.step(angle_error)
scheduler.step()
state = OrderedDict([
('args', vars(args)),
('state_dict', model.state_dict()),
('optimizer', optimizer.state_dict()),
('epoch', epoch),
('angle_error', angle_error),
])
model_path = os.path.join(outdir, 'model_state.pth')
#torch.save(state, model_path)
if args.tensorboard:
outpath = os.path.join(outdir, 'all_scalars.json')
writer.export_scalars_to_json(outpath)
if __name__ == '__main__':
main()