-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtrain_net.py
executable file
·96 lines (82 loc) · 3.18 KB
/
train_net.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
#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates.
"""
Copied directly from detectron2/tools/train_net.py except where noted.
"""
from datetime import timedelta
import detectron2.utils.comm as comm
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2.data import MetadataCatalog
from detectron2.engine import default_argument_parser, default_setup, launch
from detectron2.evaluation import verify_results
from aldi.checkpoint import DetectionCheckpointerWithEMA
from aldi.config import add_aldi_config
from aldi.ema import EMA
from aldi.trainer import ALDITrainer
import aldi.align # register align mixins with Detectron2
import aldi.datasets # register datasets with Detectron2
import aldi.distill # register distillers and distill mixins with Detectron2
import aldi.model # register ALDI R-CNN model with Detectron2
import aldi.backbone # register ViT FPN backbone with Detectron2
def setup(args):
"""
Copied directly from detectron2/tools/train_net.py
"""
cfg = get_cfg()
## Change here
add_aldi_config(cfg)
# enclose YOLO in a try/except because we want the extra pip dependencies to be optional
try:
from aldi.yolo.helpers import add_yolo_config
import aldi.yolo.align # register align mixins with Detectron2
import aldi.yolo.distill # register distillers and distill mixins with Detectron2
add_yolo_config(cfg)
except:
print("Could not load YOLO library.")
from aldi.detr.helpers import add_deformable_detr_config
import aldi.detr.align # register align mixins with Detectron2
import aldi.detr.distill # register distillers and distill mixins with Detectron2
add_deformable_detr_config(cfg)
## End change
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
default_setup(cfg, args)
return cfg
def main(args):
"""
Copied directly from detectron2/tools/train_net.py
But replace Trainer with DATrainer and disable TTA.
"""
cfg = setup(args)
if args.eval_only:
model = ALDITrainer.build_model(cfg)
## Change here
ckpt = DetectionCheckpointerWithEMA(model, save_dir=cfg.OUTPUT_DIR)
if cfg.EMA.ENABLED and cfg.EMA.LOAD_FROM_EMA_ON_START:
ema = EMA(ALDITrainer.build_model(cfg), cfg.EMA.ALPHA, cfg.EMA.START_ITER)
ckpt.add_checkpointable("ema", ema)
ckpt.resume_or_load(cfg.MODEL.WEIGHTS, resume=args.resume)
## End change
res = ALDITrainer.test(cfg, model)
if cfg.TEST.AUG.ENABLED:
raise NotImplementedError("TTA not supported")
if comm.is_main_process():
verify_results(cfg, res)
return res
trainer = ALDITrainer(cfg)
trainer.resume_or_load(resume=args.resume)
return trainer.train()
if __name__ == "__main__":
args = default_argument_parser().parse_args()
print("Command Line Args:", args)
launch(
main,
args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
timeout=timedelta(minutes=1), # added for debugging
args=(args,),
)