-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
64 lines (54 loc) · 2.29 KB
/
main.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
import yaml
from trainer import *
from test import evaluate, test_rec, evaluate_r
from argparse import ArgumentParser
torch.backends.cudnn.benchmark = True
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('--config', dest='config', type=str, default="config/RSNA_AE.yaml") # config file
parser.add_argument('--mode', dest='mode', type=str, default=None, help="e.g., a, b, r, eval, eval_r, test")
parser.add_argument('--refine', dest='refine_in', type=str, default="dual", help="dual, intra")
"""
Description of modes.
a: train one network in the Unknown Distribution Module (UDM) on normal+unlabeled data
b: train one network in the Normative Distribution Module (NDM) on normal data
r: train the ASR-Net (after finishing the training of UDM and NDM).
eval: evaluate the reconstruction ensemble and DDAD (without ASR-Net).
eval_r: evaluate the DDAD with ASR-Net
test: test the reconstruction baselines
"""
opt = parser.parse_args()
with open(opt.config, "r") as f:
cfgs = yaml.safe_load(f)
torch.cuda.set_device(cfgs["Exp"]["gpu"])
out_dir = cfgs["Exp"]["out_dir"]
if not os.path.exists(out_dir):
os.makedirs(out_dir)
if opt.refine_in == "dual":
refine_in = ["inter_dis", "intra_dis"] # For R_{dual}
elif opt.refine_in == "intra":
refine_in = ["intra_dis"] # For R_{intra}
else:
raise Exception("Invalid refine in: {}".format(opt.refine_in))
if opt.mode == "a" or opt.mode == "b":
module = "UDM" if opt.mode == "a" else "NDM"
print("=> Training the {}".format(module))
train_module_ab(cfgs, opt)
elif opt.mode == "r":
print("=> Training the ASR-Net ...")
if len(refine_in) == 2:
print("=> Refine dual discrepancies")
else:
print("=> Refine intra-discrepancy")
train_refine(cfgs, refine_in)
elif opt.mode == "eval":
print("=> Evaluating DDAD without ASR ...")
evaluate(cfgs)
elif opt.mode == "eval_r":
print("=> Evaluating DDAD with ASR ...")
evaluate_r(cfgs, refine_in)
elif opt.mode == "test":
print("=> Testing the reconstruction models ...")
test_rec(cfgs)
else:
raise Exception("Invalid mode: {}".format(opt.mode))