-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt_online.py
309 lines (266 loc) · 12.2 KB
/
adapt_online.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
import os
import time
import argparse
import numpy as np
import random
import torch
import models
from models import MinkUNet18_HEADS, MinkUNet18_MCMC
from utils.config import get_config
from utils.collation import CollateSeparated, CollateFN
from utils.dataset_online import get_online_dataset
from utils.online_logger import OnlineWandbLogger, OnlineCSVLogger
from pipelines import OnlineTrainer
parser = argparse.ArgumentParser()
parser.add_argument("--config_file",
default="configs/deva/nuscenes_sequence.yaml",
type=str,
help="Path to config file")
parser.add_argument("--split_size",
default=4071,
type=int,
help="Num frames per sub sequence (SemanticKITTI only)")
parser.add_argument("--drop_prob",
default=None,
type=float,
help="Dropout prob MCMC")
parser.add_argument("--save_predictions",
default=False,
action='store_true')
parser.add_argument("--note",
default=None,
type=str)
parser.add_argument("--use_pseudo_new",
default=False,
action='store_true')
parser.add_argument("--use_prototype",
default=False,
action='store_true')
parser.add_argument("--use_all_pseudo",
default=False,
action='store_true')
parser.add_argument("--score_weight",
default=False,
action='store_true')
parser.add_argument("--loss_use_score_weight",
default=False,
action='store_true')
parser.add_argument("--without_pre_eval_synlidar2kitti",
default=False,
action='store_true')
parser.add_argument("--without_pre_eval_synth4d2kitti",
default=False,
action='store_true')
parser.add_argument("--without_pre_eval_synth4dnusc",
default=False,
action='store_true')
parser.add_argument("--kitti_sim",
default=False,
action='store_true')
parser.add_argument("--only_certainty",
default=False,
action='store_true')
parser.add_argument("--only_purity",
default=False,
action='store_true')
parser.add_argument("--without_reload",
default=False,
action='store_true')
parser.add_argument("--save_gem_predictions",
default=False,
action='store_true')
parser.add_argument("--sample_pos",
default=False,
action='store_true')
parser.add_argument("--coord_weight",
default=False,
action='store_true')
parser.add_argument("--use_hard_label",
default=False,
action='store_true')
parser.add_argument("--BMD_prototype",
default=False,
action='store_true')
parser.add_argument("--only_use_BMD_prototype",
default=False,
action='store_true')
parser.add_argument("--score_weight_new",
default=False,
action='store_true')
parser.add_argument("--use_ema",
default=False,
action='store_true')
parser.add_argument("--use_pre_label",
default=False,
action='store_true')
parser.add_argument("--without_ssl_loss",
default=False,
action='store_true')
parser.add_argument("--only_use_prototype",
default=False,
action='store_true')
parser.add_argument("--lr",
default=0.0,
type=float)
parser.add_argument("--ssl_beta",
default=1.0,
type=float)
parser.add_argument("--pseudo_th",
default=0.5,
type=float)
parser.add_argument("--loss_eps",
default=0.25,
type=float)
parser.add_argument("--segmentation_beta",
default=1.0,
type=float)
parser.add_argument("--max_time_window",
default=0,
type=int)
parser.add_argument("--loss_method_num",
default=0,
type=int)
parser.add_argument("--pre_label_num",
default=2,
type=int)
parser.add_argument("--pre_label_knn",
default=1,
type=int)
parser.add_argument("--pseudo_knn",
default=5,
type=int)
parser.add_argument("--seed",
default=1234,
type=int)
AUG_DICT = None
def get_mini_config(main_c):
return dict(time_window=main_c.dataset.max_time_window,
mcmc_it=main_c.pipeline.num_mc_iterations,
metric=main_c.pipeline.metric,
cbst_p=main_c.pipeline.top_p,
th_pseudo=main_c.pipeline.th_pseudo,
top_class=main_c.pipeline.top_class,
propagation_size=main_c.pipeline.propagation_size,
drop_prob=main_c.model.drop_prob)
def train(config, split_size=4071, save_preds=False, args=None):
mapping_path = config.dataset.mapping_path
if args.max_time_window != 0:
config.dataset.max_time_window = args.max_time_window
eval_dataset = get_online_dataset(dataset_name=config.dataset.name,
dataset_path=config.dataset.dataset_path,
voxel_size=config.dataset.voxel_size,
augment_data=config.dataset.augment_data,
max_time_wdw=config.dataset.max_time_window,
version=config.dataset.version,
sub_num=config.dataset.num_pts,
ignore_label=config.dataset.ignore_label,
split_size=split_size,
mapping_path=mapping_path,
num_classes=config.model.out_classes,
args=args)
adapt_dataset = get_online_dataset(dataset_name=config.dataset.name,
dataset_path=config.dataset.dataset_path,
voxel_size=config.dataset.voxel_size,
augment_data=config.dataset.augment_data,
max_time_wdw=config.dataset.max_time_window,
version=config.dataset.version,
sub_num=config.dataset.num_pts,
ignore_label=config.dataset.ignore_label,
split_size=split_size,
mapping_path=mapping_path,
num_classes=config.model.out_classes,
args=args)
Model = getattr(models, config.model.name)
model = Model(config.model.in_feat_size, config.model.out_classes)
if config.model.name == 'MinkUNet18':
model = MinkUNet18_HEADS(model)
if config.pipeline.is_double:
source_model = Model(config.model.in_feat_size, config.model.out_classes)
if config.pipeline.use_mcmc:
if args.drop_prob is not None:
config.model.drop_prob = args.drop_prob
source_model = MinkUNet18_MCMC(source_model, p_drop=config.model.drop_prob)
else:
source_model = None
if config.pipeline.delayed_freeze_list is not None:
delayed_list = dict(zip(config.pipeline.delayed_freeze_list, config.pipeline.delayed_freeze_frames))
else:
delayed_list = None
run_time = time.strftime("%Y_%m_%d_%H:%M", time.gmtime())
if config.pipeline.wandb.run_name is not None:
run_name = run_time + '_' + config.pipeline.wandb.run_name
else:
run_name = run_time
mini_configs = get_mini_config(config)
if args.note is not None:
run_name += f'_{args.note}'
else:
for k, v in mini_configs.items():
run_name += f'_{str(k)}:{str(v)}'
save_dir = os.path.join(config.pipeline.save_dir, run_name)
args.save_dir = save_dir
# save_dir += "_normal_test"
os.makedirs(save_dir, exist_ok=True)
wandb_logger = OnlineWandbLogger(project=config.pipeline.wandb.project_name,
entity=config.pipeline.wandb.entity_name,
name=run_name,
offline=config.pipeline.wandb.offline,
config=mini_configs)
csv_logger = OnlineCSVLogger(save_dir=save_dir,
version='logs')
loggers = [wandb_logger, csv_logger]
if args.lr != 0.0:
config.pipeline.optimizer.lr = args.lr
trainer = OnlineTrainer(
eval_dataset=eval_dataset,
adapt_dataset=adapt_dataset,
model=model,
num_classes=config.model.out_classes,
source_model=source_model,
criterion=config.pipeline.loss,
epsilon=config.pipeline.eps,
ssl_criterion=config.pipeline.ssl_loss,
ssl_beta=config.pipeline.ssl_beta,
seg_beta=config.pipeline.segmentation_beta,
optimizer_name=config.pipeline.optimizer.name,
adaptation_batch_size=config.pipeline.dataloader.adaptation_batch_size,
stream_batch_size=config.pipeline.dataloader.stream_batch_size,
lr=config.pipeline.optimizer.lr,
clear_cache_int=config.pipeline.trainer.clear_cache_int,
scheduler_name=config.pipeline.scheduler.scheduler_name,
train_num_workers=config.pipeline.dataloader.num_workers,
val_num_workers=config.pipeline.dataloader.num_workers,
use_random_wdw=config.pipeline.random_time_window,
freeze_list=config.pipeline.freeze_list,
delayed_freeze_list=delayed_list,
num_mc_iterations=config.pipeline.num_mc_iterations,
collate_fn_eval=CollateFN(),
collate_fn_adapt=CollateSeparated(),
device=config.pipeline.gpu,
default_root_dir=config.pipeline.save_dir,
weights_save_path=os.path.join(save_dir, 'checkpoints'),
loggers=loggers,
save_checkpoint_every=config.pipeline.trainer.save_checkpoint_every,
source_checkpoint=config.pipeline.source_model,
student_checkpoint=config.pipeline.student_model,
is_double=config.pipeline.is_double,
is_pseudo=config.pipeline.is_pseudo,
use_mcmc=config.pipeline.use_mcmc,
sub_epochs=config.pipeline.sub_epoch,
save_predictions=save_preds,
args=args,)
trainer.adapt_double()
def set_random_seed(seed=0):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
args = parser.parse_args()
config = get_config(args.config_file)
set_random_seed(args.seed)
train(config, split_size=args.split_size, save_preds=args.save_predictions, args=args)