-
Notifications
You must be signed in to change notification settings - Fork 4
/
exe_breast.py
65 lines (54 loc) · 2.11 KB
/
exe_breast.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
import argparse
import torch
import datetime
import json
import yaml
import os
from src.main_model_table import TabCSDI
from src.utils_table import train, evaluate
from dataset_breast import get_dataloader
parser = argparse.ArgumentParser(description="TabCSDI")
parser.add_argument("--config", type=str, default="breast.yaml")
parser.add_argument("--device", default="cpu", help="Device")
parser.add_argument("--seed", type=int, default=1)
parser.add_argument("--testmissingratio", type=float, default=0.2)
parser.add_argument("--nfold", type=int, default=5, help="for 5-fold test")
parser.add_argument("--unconditional", action="store_true", default=0)
parser.add_argument("--modelfolder", type=str, default="")
parser.add_argument("--nsample", type=int, default=100)
args = parser.parse_args()
print(args)
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
path = "config/" + args.config
with open(path, "r") as f:
config = yaml.safe_load(f)
config["model"]["is_unconditional"] = args.unconditional
config["model"]["test_missing_ratio"] = args.testmissingratio
print(json.dumps(config, indent=4))
# Create folder
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
foldername = "./save/breast_fold" + str(args.nfold) + "_" + current_time + "/"
print("model folder:", foldername)
os.makedirs(foldername, exist_ok=True)
with open(foldername + "config.json", "w") as f:
json.dump(config, f, indent=4)
# Every loader contains "observed_data", "observed_mask", "gt_mask", "timepoints"
train_loader, valid_loader, test_loader = get_dataloader(
seed=args.seed,
nfold=args.nfold,
batch_size=config["train"]["batch_size"],
missing_ratio=config["model"]["test_missing_ratio"],
)
model = TabCSDI(config, args.device).to(args.device)
if args.modelfolder == "":
train(
model,
config["train"],
train_loader,
valid_loader=valid_loader,
foldername=foldername,
)
else:
model.load_state_dict(torch.load("./save/" + args.modelfolder + "/model.pth"))
print("---------------Start testing---------------")
evaluate(model, test_loader, nsample=args.nsample, scaler=1, foldername=foldername)