-
Notifications
You must be signed in to change notification settings - Fork 22
/
train.py
81 lines (68 loc) · 3.12 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
import os
import random
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from models.HiFormer import HiFormer
import configs.HiFormer_configs as configs
from trainer import trainer
parser = argparse.ArgumentParser()
parser.add_argument('--root_path', type=str,
default='./data/Synapse/train_npz', help='root dir for data')
parser.add_argument('--test_path', type=str,
default='./data/Synapse/test_vol_h5', help='root dir for data')
parser.add_argument('--dataset', type=str,
default='Synapse', help='experiment_name')
parser.add_argument('--list_dir', type=str,
default='./lists/lists_Synapse', help='list dir')
parser.add_argument('--num_classes', type=int,
default=9, help='output channel of network')
parser.add_argument('--max_iterations', type=int,
default=30000, help='maximum epoch number to train')
parser.add_argument('--max_epochs', type=int,
default=401, help='maximum epoch number to train')
parser.add_argument('--batch_size', type=int,
default=10, help='batch_size per gpu')
parser.add_argument('--n_gpu', type=int, default=1, help='total gpu')
parser.add_argument('--deterministic', type=int, default=1,
help='whether use deterministic training')
parser.add_argument('--base_lr', type=float, default=0.01,
help='segmentation network learning rate')
parser.add_argument('--num_workers', type=int, default=2,
help='number of workers')
parser.add_argument('--img_size', type=int,
default=224, help='input patch size of network input')
parser.add_argument('--seed', type=int,
default=1234, help='random seed')
parser.add_argument('--output_dir', type=str,
default='./results', help='root dir for output log')
parser.add_argument('--model_name', type=str,
default='hiformer-b', help='[hiformer-s, hiformer-b, hiformer-l]')
parser.add_argument('--eval_interval', type=int,
default=20, help='evaluation epoch')
parser.add_argument('--z_spacing', type=int,
default=1, help='z_spacing')
args = parser.parse_args()
args.output_dir = args.output_dir + f'/{args.model_name}'
os.makedirs(args.output_dir, exist_ok=True)
if __name__ == "__main__":
if not args.deterministic:
cudnn.benchmark = True
cudnn.deterministic = False
else:
cudnn.benchmark = False
cudnn.deterministic = True
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
CONFIGS = {
'hiformer-s': configs.get_hiformer_s_configs(),
'hiformer-b': configs.get_hiformer_b_configs(),
'hiformer-l': configs.get_hiformer_l_configs(),
}
if args.batch_size != 24 and args.batch_size % 6 == 0:
args.base_lr *= args.batch_size / 24
model = HiFormer(config=CONFIGS[args.model_name], img_size=args.img_size, n_classes=args.num_classes).cuda()
trainer(args, model, args.output_dir)