-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
137 lines (92 loc) · 4.56 KB
/
test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import os
import time
import random
import numpy as np
import setproctitle
import torch
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
import torch.optim
from torch.utils.data import DataLoader
from data.BraTS import BraTS
from predict import validate_softmax
from models.TransBTS.TransBTS_downsample8x_skipconnection import TransBTS
# output segmentation predictions, does not calculate performance
parser = argparse.ArgumentParser()
parser.add_argument('--user', default='name of user', type=str)
parser.add_argument('--root', default='/home/wenhuicu/brats_preprocessed/', type=str)
parser.add_argument('--valid_dir', default='data/', type=str)
parser.add_argument('--valid_file', default='val_list.txt', type=str)
parser.add_argument('--output_dir', default='output', type=str)
parser.add_argument('--submission', default='submission', type=str)
parser.add_argument('--visual', default='visualization', type=str)
parser.add_argument('--experiment', default='TransBTS', type=str)
parser.add_argument('--test_date', default='2022-01-31', type=str)
parser.add_argument('--test_file', default='test_list.txt', type=str)
parser.add_argument('--use_TTA', default=True, type=bool)
parser.add_argument('--post_process', default=True, type=bool)
parser.add_argument('--save_format', default='nii', choices=['npy', 'nii'], type=str)
parser.add_argument('--crop_H', default=128, type=int)
parser.add_argument('--crop_W', default=128, type=int)
parser.add_argument('--crop_D', default=128, type=int)
parser.add_argument('--seed', default=1000, type=int)
parser.add_argument('--model_name', default='TransBTS', type=str)
parser.add_argument('--num_class', default=4, type=int)
parser.add_argument('--no_cuda', default=False, type=bool)
parser.add_argument('--gpu', default='0', type=str)
parser.add_argument('--num_workers', default=4, type=int)
args = parser.parse_args()
def main():
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
_, model = TransBTS(dataset='brats', _conv_repr=True, _pe_type="learned")
model = torch.nn.DataParallel(model).cuda()
load_file = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'checkpoint', args.experiment+args.test_date, args.test_file)
if os.path.exists(load_file):
checkpoint = torch.load(load_file)
model.load_state_dict(checkpoint['state_dict'])
args.start_epoch = checkpoint['epoch']
print('Successfully load checkpoint {}'.format(os.path.join(args.experiment+args.test_date, args.test_file)))
else:
print('There is no resume file to load!')
valid_list = os.path.join(args.valid_dir, args.valid_file)
valid_root = os.path.join(args.root)
valid_set = BraTS(valid_list, valid_root, mode='test')
print('Samples for valid = {}'.format(len(valid_set)))
valid_loader = DataLoader(valid_set, batch_size=1, shuffle=False, num_workers=args.num_workers, pin_memory=True)
submission = os.path.join(os.path.abspath(os.path.dirname(__file__)), args.output_dir,
args.submission, args.experiment+args.test_date)
visual = os.path.join(os.path.abspath(os.path.dirname(__file__)), args.output_dir,
args.visual, args.experiment+args.test_date)
if not os.path.exists(submission):
os.makedirs(submission)
if not os.path.exists(visual):
os.makedirs(visual)
start_time = time.time()
with torch.no_grad():
validate_softmax(valid_loader=valid_loader,
model=model,
load_file=load_file,
multimodel=False,
savepath=submission,
visual=visual,
names=valid_set.names,
use_TTA=args.use_TTA,
save_format=args.save_format,
snapshot=True,
postprocess=True
)
end_time = time.time()
full_test_time = (end_time-start_time)/60
average_time = full_test_time/len(valid_set)
print('{:.2f} minutes!'.format(average_time))
if __name__ == '__main__':
# config = opts()
setproctitle.setproctitle('{}: Testing!'.format(args.user))
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
assert torch.cuda.is_available(), "Currently, we only support CUDA version"
main()