-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
60 lines (45 loc) · 1.91 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
import os
from datetime import datetime
import detectron2.utils.comm as comm
from detectron2.config import get_cfg
from detectron2.utils.logger import setup_logger
from detectron2.engine import default_argument_parser, DefaultTrainer, launch, default_setup
from detectron2.data import build_detection_test_loader, build_detection_train_loader, DatasetCatalog, MetadataCatalog
from yeastmatedetector.data import MaskDetectionLoader, DictGetter
from yeastmatedetector.utils import copy_code, initialize_new_config_values
from yeastmatedetector.models import *
from yeastmatedetector.multimaskrcnn import *
class Trainer(DefaultTrainer):
@classmethod
def build_train_loader(cls, cfg):
return build_detection_train_loader(cfg, mapper=MaskDetectionLoader(cfg, True))
def setup(args):
cfg = get_cfg()
cfg = initialize_new_config_values(cfg)
cfg.merge_from_file(args.config_file)
date_time = datetime.now().strftime("%m%d%y_%H%M%S")
cfg.OUTPUT_DIR = os.path.join(cfg.OUTPUT_DIR, cfg.DATASETS.TRAIN[0], date_time)
if comm.get_rank() == 0:
copy_code(cfg.OUTPUT_DIR)
dict_getter = DictGetter("yeastmate", train_path=cfg.INPUT_DIR)
DatasetCatalog.register("yeastmate", dict_getter.get_train_dicts)
MetadataCatalog.get("yeastmate").thing_classes = ["single_cell", "mating", "budding"]
cfg.freeze()
default_setup(cfg, args)
setup_logger(output=cfg.OUTPUT_DIR, distributed_rank=comm.get_rank(), name="detectron")
return cfg
def main(args):
cfg = setup(args)
trainer = Trainer(cfg)
trainer.resume_or_load()
return trainer.train()
if __name__ == "__main__":
args = default_argument_parser().parse_args()
print("Command Line Args:", args)
launch(main,
num_gpus_per_machine=args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args, ),
)