-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
238 lines (214 loc) · 9.05 KB
/
main.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
import os, sys
import gc
import torch.nn.parallel
import torch.optim
import torch.utils.data
from tensorboardX import SummaryWriter
import numpy as np
import shutil
import data_loader
import nets
import losses
from common.helper import query_yes_no
from iterater import iterater
from test import test_odom, test_kitti_raw
import yaml
def main():
# ensure numba JIT is on
if 'NUMBA_DISABLE_JIT' in os.environ:
del os.environ['NUMBA_DISABLE_JIT']
# parse arguments
global args
with open(sys.argv[1], 'r') as stream:
args = yaml.safe_load(stream)
cuda = torch.cuda.is_available()
if cuda :
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
device = torch.device("cuda")
else:
device = torch.device("cpu")
print("=> using '{}' for computation.".format(device))
# -------------------- logging args --------------------
print("=> checking ckpt dir...")
if args['test'] is False and os.path.exists(args['ckpt_dir']):
if args['resume_path'] is False:
to_continue = query_yes_no(
'=> Attention!!! ckpt_dir {' + args['ckpt_dir'] + '} already exists!\n'
+ '=> Whether to continue?',
default=None)
if to_continue:
for root, dirs, files in os.walk(args['ckpt_dir'], topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
else:
sys.exit(1)
elif args['pretrained_path'] is not False:
to_continue = query_yes_no(
'=> Attention!!! ckpt_dir {' + args['ckpt_dir'] +
'} already exists! Whether to continue?',
default=None)
if to_continue:
for root, dirs, files in os.walk(args['ckpt_dir'], topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
else:
sys.exit(1)
if args['test'] is False :
os.makedirs(args['ckpt_dir'], mode=0o777, exist_ok=True)
shutil.copyfile(sys.argv[1], os.path.join(args['ckpt_dir'], 'config.yaml'))
summary = SummaryWriter(args['ckpt_dir'])
# -------------------- dataset & loader --------------------
loader = {}
if args['test'] is False :
train_dataset = data_loader.__dict__[args['dataset']](
mode='train',
args=args
)
# print('train_dataset: ' + str(train_dataset))
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=args['batch_size'],
shuffle=True,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
loader['train'] = train_loader
val_dataset = data_loader.__dict__[args['dataset']](
mode='valid',
args=args
)
# print('val_dataset: ' + str(val_dataset))
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args['batch_size'],
shuffle=False,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
loader['validate'] = val_loader
else:
test_dataset = data_loader.__dict__[args['dataset']](
mode=args['test'],
args=args
)
# print('val_dataset: ' + str(val_dataset))
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=args['batch_size'],
shuffle=False,
num_workers=args['workers'],
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
# -------------------- create model --------------------
print("=> creating model and optimizer... ")
model = nets.__dict__[args['arch'] + 'Backbone'](args).to(device)
model = torch.nn.DataParallel(model)
if args['test'] is False :
criterion = losses.__dict__[args['arch'] + 'Criterion'](args)
# -------------------- resume --------------------
if args['test']:
if os.path.isfile(args['ckpt_path']):
print("=> loading checkpoint '{}'".format(args['ckpt_path']))
checkpoint = torch.load(args['ckpt_path'])
model.load_state_dict(checkpoint['state_dict'], strict=True)
print("=> completed.")
print("=> start iter {}, min loss {}"
.format(checkpoint['iter'], checkpoint['min_loss']))
else:
print("=> no checkpoint found at '{}'".format(args['ckpt_path']))
return
if args['dataset'] == 'KITTI_ODOM': test_odom(test_loader, model, args)
elif args['dataset'] == 'KITTI_RAW': test_kitti_raw(test_loader, model, args)
elif args['dataset'] == 'NUSC': test_odom(test_loader, model, args)
elif args['dataset'] == 'RELLIS_3D': test_odom(test_loader, model, args)
return
elif args['resume_path']:
if os.path.isfile(args['resume_path']):
print("=> loading checkpoint '{}'".format(args['resume_path']))
checkpoint = torch.load(args['resume_path'])
model.load_state_dict(checkpoint['state_dict'], strict=True)
if args['test'] is False : model = grad_false_keys_filter(model, args['grad_false_keys'])
print("=> completed.")
print("=> start iter {}, min loss {}"
.format(checkpoint['iter'], checkpoint['min_loss']))
else:
print("=> no checkpoint found at '{}'".format(args['resume_path']))
return
elif args['pretrained_path']:
if os.path.isfile(args['pretrained_path']):
print("=> loading checkpoint '{}'".format(args['pretrained_path']))
checkpoint = torch.load(args['pretrained_path'])
model_dict = model.state_dict()
pretrained_dict = checkpoint['state_dict']
# pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
update_dict = update_dict_filter(pretrained_dict, args['convert_dict'], model_dict)
model_dict.update(update_dict)
model.load_state_dict(update_dict, strict=False)
model = grad_false_keys_filter(model, args['grad_false_keys'])
print("=> completed.")
else:
print("=> no checkpoint found at '{}'".format(args['pretrained_path']))
return
model_named_params = [
p for _, p in model.named_parameters() if p.requires_grad
]
optimizer = torch.optim.Adam(model_named_params,
lr=args['lr'],
weight_decay=args['weight_decay'])
print("=> total model parameters: {:.3f}M".format(
sum(p.numel() for p in model.parameters())/1000000.0))
# -------------------- main loop --------------------
it_dict = {}
if args['resume_path']:
it_dict['iter'] = checkpoint['iter'] + 1
it_dict['min_train_loss'] = None
it_dict['best_train_iter'] = None
it_dict['min_val_loss'] = None
it_dict['best_val_iter'] = None
# it_dict['min_val_loss'] = checkpoint['min_loss']
# it_dict['best_val_iter'] = checkpoint['iter']
optimizer.load_state_dict(checkpoint['optimizer'])
else:
it_dict['iter'] = 0
it_dict['min_train_loss'] = None
it_dict['best_train_iter'] = None
it_dict['min_val_loss'] = None
it_dict['best_val_iter'] = None
while it_dict['iter'] < args['epochs'] * len(loader['train']):
it_dict = \
iterater(loader, model, criterion, optimizer, args, summary, it_dict)
gc.collect()
return
def update_dict_filter(pretrained_dict, convert_dict, model_dict):
update_dict = {}
for pretrainedk in pretrained_dict.keys():
converted = False
for cvtk in convert_dict.keys():
if cvtk in pretrainedk:
newk = pretrainedk.replace(cvtk, convert_dict[cvtk])
update_dict[newk] = pretrained_dict[pretrainedk]
converted = True
print(pretrainedk , '-->', newk)
if converted == False:
update_dict[pretrainedk] = pretrained_dict[pretrainedk]
update_dict = {k: v for k, v in update_dict.items() if k in model_dict}
return update_dict
def grad_false_keys_filter(model, grad_false_keys):
for k, p in model.named_parameters():
k_requires_grad = True
for grad_false_key in grad_false_keys:
if grad_false_key in k:
p.requires_grad = False
k_requires_grad = False
if k_requires_grad: print(k)
return model
if __name__ == '__main__':
main()