forked from maxpmx/CelloType
-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_xenium.py
83 lines (69 loc) · 2.64 KB
/
train_xenium.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
from cellotype.trainer import *
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
add_deeplab_config(cfg)
add_maskdino_config(cfg)
args.config_file = './configs/maskdino_R50_bs16_50ep_4s_dowsample1_2048.yaml'
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.MODEL.WEIGHTS = './models/maskdino_swinl_50ep_300q_hid2048_3sd1_instance_maskenhanced_mask52.3ap_box59.0ap.pth'
cfg.MODEL.IN_CHANS = 3
cfg.DATASETS.TRAIN = ("cell_train",)
cfg.DATASETS.VAL = ("cell_val",)
cfg.DATASETS.TEST = ('cell_test',)
cfg.OUTPUT_DIR = 'output/xenium'
cfg.SOLVER.AMP.ENABLED = False
cfg.freeze()
default_setup(cfg, args)
setup_logger(output=cfg.OUTPUT_DIR, distributed_rank=comm.get_rank(), name="cellotype")
return cfg
def main(args):
data_dir = 'data/example_xenium'
for d in ["train","val", "test"]:
if d == 'test':
DatasetCatalog.register("cell_" + d, lambda d=d: np.load(os.path.join(data_dir, 'dataset_dicts_{}.npy'.format(d)), allow_pickle=True))
else:
DatasetCatalog.register("cell_" + d, lambda d=d: np.load(os.path.join(data_dir, 'dataset_dicts_{}.npy'.format(d)), allow_pickle=True))
MetadataCatalog.get("cell_" + d).set(thing_classes=["cell"])
args.resume = True
cfg = setup(args)
print("Command cfg:", cfg)
if args.eval_only:
model = Trainer.build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
checkpointer = DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR)
checkpointer.resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
res = Trainer.test(cfg, model)
if cfg.TEST.AUG.ENABLED:
res.update(Trainer.test_with_TTA(cfg, model))
if comm.is_main_process():
verify_results(cfg, res)
return res
trainer = Trainer(cfg)
trainer.resume_or_load(resume=args.resume)
return trainer.train()
if __name__ == "__main__":
parser = default_argument_parser()
parser.add_argument('--eval_only', action='store_true')
parser.add_argument('--EVAL_FLAG', type=int, default=1)
args = parser.parse_args()
# random port
port = random.randint(1000, 20000)
args.dist_url = 'tcp://127.0.0.1:' + str(port)
print("Command Line Args:", args)
print("pwd:", os.getcwd())
launch(
main,
args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)