-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
470 lines (416 loc) · 16.5 KB
/
train.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import argparse
import torch
from ezflow.data import DataloaderCreator
from ezflow.engine import DistributedTrainer, Trainer, get_training_cfg
from ezflow.models import build_model
from nnflow import BasicEncoderV2, GMFlowV2, SCCFlow, eval_model
from nnflow.models.flownet_c_v2 import FlowNetC_V2
def count_params(model):
return (
str(sum(p.numel() for p in model.parameters() if p.requires_grad) / 1000000)
+ "M params"
)
def main():
parser = argparse.ArgumentParser(description="Train a model")
parser.add_argument(
"--train_cfg", type=str, required=True, help="Path to the training config file"
)
parser.add_argument(
"--model", type=str, required=True, help="Name of the model to train"
)
parser.add_argument(
"--model_cfg", type=str, required=True, help="Path to the model config file"
)
parser.add_argument(
"--log_dir",
type=str,
required=True,
help="Directory where logs are to be written",
)
parser.add_argument(
"--ckpt_dir",
type=str,
required=True,
help="Directory where ckpts are to be saved",
)
parser.add_argument(
"--ckpt_interval",
type=int,
default=None,
help="Number of intervals to save checkpoint",
)
parser.add_argument(
"--epochs",
type=int,
default=None,
help="Number of epochs to train for",
)
parser.add_argument(
"--num_steps",
type=int,
default=None,
help="Number of steps to train for",
)
parser.add_argument(
"--resume",
action="store_true",
help="Whether to resume training from a previous ckpt",
)
parser.add_argument(
"--resume_ckpt",
type=str,
default=None,
help="Path to ckpt for resuming training",
)
parser.add_argument(
"--start_iteration",
type=int,
default=0,
help="Number of epochs to train after resumption",
)
parser.add_argument("--device", type=str, default="0", help="Device ID")
parser.add_argument(
"--distributed_backend",
type=str,
default="nccl",
help="Backend to use for distributed computing",
)
parser.add_argument(
"--world_size", type=int, help="World size for Distributed Training"
)
parser.add_argument(
"--use_mixed_precision",
action="store_true",
help="Enable mixed precision",
)
parser.add_argument(
"--freeze_batch_norm",
action="store_true",
help="Sets all batch norm layers to eval state.",
)
parser.add_argument(
"--train_ds",
type=str,
help="Name of the dataset \n. Supported datasets: AutoFlow, FlyingChairs, FlyingThings3D, MPISintel, KITTI, SceneFlow",
)
parser.add_argument(
"--train_ds_dir",
type=str,
help="Path of root directory for the training dataset",
)
parser.add_argument(
"--val_ds",
type=str,
help="Name of the dataset \n. Supported datasets: AutoFlow, FlyingChairs, FlyingThings3D, MPISintel, KITTI, SceneFlow",
)
parser.add_argument(
"--val_ds_dir",
type=str,
help="Path of root directory for the validation dataset",
)
parser.add_argument(
"--train_crop_size",
type=int,
nargs="+",
default=None,
help="Crop size for training images",
)
parser.add_argument(
"--val_crop_size",
type=int,
nargs="+",
default=None,
help="Crop size for validation images",
)
parser.add_argument(
"--batch_size", type=int, default=16, help="Training batch size"
)
parser.add_argument("--lr", type=float, required=False, help="Learning rate")
args = parser.parse_args()
training_cfg = get_training_cfg(cfg_path=args.train_cfg)
training_cfg.LOG_DIR = args.log_dir
training_cfg.CKPT_DIR = args.ckpt_dir
training_cfg.DATA.BATCH_SIZE = args.batch_size
training_cfg.DEVICE = args.device
training_cfg.DISTRIBUTED.BACKEND = args.distributed_backend
training_cfg.MIXED_PRECISION = args.use_mixed_precision
training_cfg.FREEZE_BATCH_NORM = args.freeze_batch_norm
if args.resume:
training_cfg.DISTRIBUTED.USE = False
if args.world_size is not None:
training_cfg.DISTRIBUTED.WORLD_SIZE = args.world_size
if args.train_ds is not None and args.train_ds_dir is not None:
training_cfg.DATA.TRAIN_DATASET.NAME = args.train_ds
training_cfg.DATA.TRAIN_DATASET.ROOT_DIR = args.train_ds_dir
if args.val_ds is not None and args.val_ds_dir is not None:
training_cfg.DATA.VAL_DATASET.NAME = args.val_ds
training_cfg.DATA.VAL_DATASET.ROOT_DIR = args.val_ds_dir
if args.train_crop_size is not None:
training_cfg.DATA.TRAIN_CROP_SIZE = args.train_crop_size
if args.val_crop_size is not None:
training_cfg.DATA.VAL_CROP_SIZE = args.val_crop_size
if args.lr is not None:
training_cfg.OPTIMIZER.LR = args.lr
if training_cfg.SCHEDULER.NAME == "OneCycleLR":
training_cfg.SCHEDULER.PARAMS.max_lr = args.lr
# if not resume:
# training_cfg.SCHEDULER.PARAMS.total_steps = args.num_steps
# else:
# training_cfg.SCHEDULER.PARAMS.total_steps = args.num_steps + args.start_iteration
if args.num_steps is not None:
training_cfg.NUM_STEPS = args.num_steps
if training_cfg.SCHEDULER.NAME == "OneCycleLR":
training_cfg.SCHEDULER.PARAMS.total_steps = args.num_steps
if args.epochs is not None:
training_cfg.EPOCHS = args.epochs
if training_cfg.SCHEDULER.NAME == "OneCycleLR":
training_cfg.SCHEDULER.PARAMS.epochs = args.epochs
if args.ckpt_interval is not None:
training_cfg.CKPT_INTERVAL = args.ckpt_interval
if training_cfg.DISTRIBUTED.USE is True:
train_loader_creator = DataloaderCreator(
batch_size=training_cfg.DATA.BATCH_SIZE,
num_workers=training_cfg.DATA.NUM_WORKERS,
pin_memory=training_cfg.DATA.PIN_MEMORY,
distributed=True,
world_size=training_cfg.DISTRIBUTED.WORLD_SIZE,
append_valid_mask=training_cfg.DATA.APPEND_VALID_MASK,
shuffle=training_cfg.DATA.SHUFFLE,
)
val_loader_creator = DataloaderCreator(
batch_size=training_cfg.DATA.BATCH_SIZE,
num_workers=training_cfg.DATA.NUM_WORKERS,
pin_memory=training_cfg.DATA.PIN_MEMORY,
distributed=True,
world_size=training_cfg.DISTRIBUTED.WORLD_SIZE,
append_valid_mask=training_cfg.DATA.APPEND_VALID_MASK,
shuffle=training_cfg.DATA.SHUFFLE,
)
else:
train_loader_creator = DataloaderCreator(
batch_size=training_cfg.DATA.BATCH_SIZE,
num_workers=training_cfg.DATA.NUM_WORKERS,
pin_memory=training_cfg.DATA.PIN_MEMORY,
append_valid_mask=training_cfg.DATA.APPEND_VALID_MASK,
shuffle=training_cfg.DATA.SHUFFLE,
)
val_loader_creator = DataloaderCreator(
batch_size=training_cfg.DATA.BATCH_SIZE,
num_workers=training_cfg.DATA.NUM_WORKERS,
pin_memory=training_cfg.DATA.PIN_MEMORY,
append_valid_mask=training_cfg.DATA.APPEND_VALID_MASK,
shuffle=training_cfg.DATA.SHUFFLE,
)
train_aug_params = {
"color_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.COLOR_AUG_PARAMS,
"eraser_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.ERASER_AUG_PARAMS,
"noise_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.NOISE_AUG_PARAMS,
"flip_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.FLIP_AUG_PARAMS,
"spatial_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.SPATIAL_AUG_PARAMS,
"advanced_spatial_aug_params": training_cfg.DATA.AUGMENTATION.PARAMS.TRAINING.ADVANCED_SPATIAL_AUG_PARAMS,
}
# --------------------- TRAINING DATASETS -----------------------------------#
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "flyingchairs":
train_loader_creator.add_FlyingChairs(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "flyingthings3d":
train_loader_creator.add_FlyingThings3D(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
dstype="frames_cleanpass",
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
train_loader_creator.add_FlyingThings3D(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
dstype="frames_finalpass",
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "sceneflow":
train_loader_creator.add_SceneFlow(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "mpisintel":
train_loader_creator.add_MPISintel(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "kitti":
train_loader_creator.add_Kitti(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "autoflow":
train_loader_creator.add_AutoFlow(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.TRAIN_DATASET.NAME.lower() == "kubric":
train_loader_creator.add_Kubric(
root_dir=training_cfg.DATA.TRAIN_DATASET.ROOT_DIR,
split="training",
crop=True,
crop_type="random",
crop_size=training_cfg.DATA.TRAIN_CROP_SIZE,
augment=training_cfg.DATA.AUGMENTATION.USE,
aug_params=train_aug_params,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
# --------------------- VALIDATION DATASETS -----------------------------------#
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "flyingchairs":
val_loader_creator.add_FlyingChairs(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
split="validation",
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "flyingthings3d":
val_loader_creator.add_FlyingThings3D(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
dstype="frames_cleanpass",
split="validation",
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
val_loader_creator.add_FlyingThings3D(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
dstype="frames_finalpass",
split="validation",
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "sceneflow":
val_loader_creator.add_SceneFlow(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "mpisintel":
val_loader_creator.add_MPISintel(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
split="training",
dstype="clean",
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "kitti":
val_loader_creator.add_Kitti(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "autoflow":
val_loader_creator.add_AutoFlow(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
if training_cfg.DATA.VAL_DATASET.NAME.lower() == "kubric":
val_loader_creator.add_Kubric(
root_dir=training_cfg.DATA.VAL_DATASET.ROOT_DIR,
split="validation",
crop=True,
crop_type="center",
crop_size=training_cfg.DATA.VAL_CROP_SIZE,
augment=False,
norm_params=training_cfg.DATA.NORM_PARAMS,
)
model = build_model(args.model, cfg_path=args.model_cfg, custom_cfg=True)
print(f"{args.model} model params: {count_params(model)}")
model_state_dict = None
optimizer_state_dict = None
scheduler_state_dict = None
if args.resume_ckpt is not None:
training_cfg.DISTRIBUTED.USE = False
state_dict = torch.load(args.resume_ckpt, map_location=torch.device("cpu"))
if "model_state_dict" in state_dict:
print("SAVED STATES: ", state_dict.keys())
model_state_dict = state_dict["model_state_dict"]
else:
model_state_dict = state_dict
model.load_state_dict(model_state_dict)
# if args.resume:
# print("Loading optimizer and scheduler checkpoints to resume training")
# optimizer_state_dict = state_dict["optimizer_state_dict"]
# scheduler_state_dict = state_dict["scheduler_state_dict"]
if training_cfg.DISTRIBUTED.USE is True:
trainer = DistributedTrainer(
training_cfg,
model,
train_loader_creator=train_loader_creator,
val_loader_creator=val_loader_creator,
)
else:
trainer = Trainer(
training_cfg,
model,
train_loader=train_loader_creator.get_dataloader(),
val_loader=val_loader_creator.get_dataloader(),
)
if args.resume:
trainer.resume_training(
consolidated_ckpt=args.resume_ckpt,
start_iteration=args.start_iteration,
total_iterations=args.num_steps,
)
else:
trainer.train(start_iteration=args.start_iteration)
if __name__ == "__main__":
main()