-
Notifications
You must be signed in to change notification settings - Fork 71
/
validation.py
217 lines (185 loc) · 10.5 KB
/
validation.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
import argparse
import time
import glob
from monai.losses import DiceCELoss
from monai.data import load_decathlon_datalist, decollate_batch
from monai.transforms import AsDiscrete
from monai.metrics import DiceMetric
from monai.inferers import sliding_window_inference
from model.Universal_model import Universal_model
from dataset.dataloader import get_loader
from utils import loss
from utils.utils import dice_score, TEMPLATE, ORGAN_NAME, merge_label, visualize_label, get_key, NUM_CLASS
from utils.utils import extract_topk_largest_candidates, organ_post_process, threshold_organ
from medpy.metric.binary import __surface_distances
torch.multiprocessing.set_sharing_strategy('file_system')
def normalized_surface_dice(a: np.ndarray, b: np.ndarray, threshold: float, spacing: tuple = None, connectivity=1):
"""
This implementation differs from the official surface dice implementation! These two are not comparable!!!!!
The normalized surface dice is symmetric, so it should not matter whether a or b is the reference image
This implementation natively supports 2D and 3D images. Whether other dimensions are supported depends on the
__surface_distances implementation in medpy
:param a: image 1, must have the same shape as b
:param b: image 2, must have the same shape as a
:param threshold: distances below this threshold will be counted as true positives. Threshold is in mm, not voxels!
(if spacing = (1, 1(, 1)) then one voxel=1mm so the threshold is effectively in voxels)
must be a tuple of len dimension(a)
:param spacing: how many mm is one voxel in reality? Can be left at None, we then assume an isotropic spacing of 1mm
:param connectivity: see scipy.ndimage.generate_binary_structure for more information. I suggest you leave that
one alone
:return:
"""
assert all([i == j for i, j in zip(a.shape, b.shape)]), "a and b must have the same shape. a.shape= %s, " \
"b.shape= %s" % (str(a.shape), str(b.shape))
if spacing is None:
spacing = tuple([1 for _ in range(len(a.shape))])
a_to_b = __surface_distances(a, b, spacing, connectivity)
b_to_a = __surface_distances(b, a, spacing, connectivity)
numel_a = len(a_to_b)
numel_b = len(b_to_a)
tp_a = np.sum(a_to_b <= threshold) / numel_a
tp_b = np.sum(b_to_a <= threshold) / numel_b
fp = np.sum(a_to_b > threshold) / numel_a
fn = np.sum(b_to_a > threshold) / numel_b
dc = (tp_a + tp_b) / (tp_a + tp_b + fp + fn + 1e-8) # 1e-8 just so that we don't get div by 0
return dc
def validation(model, ValLoader, args, i):
model.eval()
dice_list = {}
nsd_list = {}
for key in TEMPLATE.keys():
dice_list[key] = np.zeros((2, NUM_CLASS)) # 1st row for dice, 2nd row for count
nsd_list[key] = np.zeros((2, NUM_CLASS)) # 1st row for dice, 2nd row for count
for index, batch in enumerate(tqdm(ValLoader)):
# print('%d processd' % (index))
image, label, name = batch["image"].cuda(), batch["post_label"], batch["name"]
with torch.no_grad():
pred = sliding_window_inference(image, (args.roi_x, args.roi_y, args.roi_z), 1, model, overlap=args.overlap, mode='gaussian')
pred_sigmoid = F.sigmoid(pred)
pred_hard = threshold_organ(pred_sigmoid)
pred_hard = pred_hard.cpu()
torch.cuda.empty_cache()
B = pred_sigmoid.shape[0]
for b in range(B):
content = 'case%s| '%(name[b])
template_key = get_key(name[b])
organ_list = TEMPLATE[template_key]
pred_hard_post = organ_post_process(pred_hard.numpy(), organ_list, args.log_name+'/'+name[0].split('/')[0]+'/'+name[0].split('/')[-1],args)
pred_hard_post = torch.tensor(pred_hard_post)
# pred_hard_post = pred_hard
for organ in organ_list:
if torch.sum(label[b,organ-1,:,:,:]) != 0:
dice_organ, recall, precision = dice_score(pred_hard_post[b,organ-1,:,:,:].cuda(), label[b,organ-1,:,:,:].cuda())
dice_list[template_key][0][organ-1] += dice_organ.item()
dice_list[template_key][1][organ-1] += 1
content += '%s: %.4f, '%(ORGAN_NAME[organ-1], dice_organ.item())
print('%s: dice %.4f, recall %.4f, precision %.4f'%(ORGAN_NAME[organ-1], dice_organ.item(), recall.item(), precision.item()))
print(content)
torch.cuda.empty_cache()
ave_organ_dice = np.zeros((2, NUM_CLASS))
with open('out/'+args.log_name+f'/b_val_{i}.txt', 'w') as f:
for key in TEMPLATE.keys():
organ_list = TEMPLATE[key]
content = 'Task%s| '%(key)
# content1 = 'NSD Task%s| '%(key)
for organ in organ_list:
dice = dice_list[key][0][organ-1] / dice_list[key][1][organ-1]
content += '%s: %.4f, '%(ORGAN_NAME[organ-1], dice)
ave_organ_dice[0][organ-1] += dice_list[key][0][organ-1]
ave_organ_dice[1][organ-1] += dice_list[key][1][organ-1]
print(content)
f.write(content)
f.write('\n')
content = 'Average | '
for i in range(NUM_CLASS):
content += '%s: %.4f, '%(ORGAN_NAME[i], ave_organ_dice[0][i] / ave_organ_dice[1][i])
print(content)
f.write(content)
f.write('\n')
print(np.mean(ave_organ_dice[0] / ave_organ_dice[1]))
f.write('%s: %.4f, '%('average', np.mean(ave_organ_dice[0] / ave_organ_dice[1])))
f.write('\n')
def main():
parser = argparse.ArgumentParser()
## for distributed training
parser.add_argument('--dist', dest='dist', type=bool, default=False,
help='distributed training or not')
parser.add_argument("--local_rank", type=int)
parser.add_argument("--device")
parser.add_argument("--epoch", default=0)
## logging
parser.add_argument('--log_name', default='Nvidia/new_clip/clip1_exo_extendedTrain', help='The path resume from checkpoint')
## model load
parser.add_argument('--start_epoch', default=490, type=int, help='Number of start epoches')
parser.add_argument('--end_epoch', default=490, type=int, help='Number of end epoches')
parser.add_argument('--epoch_interval', default=100, type=int, help='Number of start epoches')
parser.add_argument('--backbone', default='unet', help='backbone [swinunetr or unet]')
## hyperparameter
parser.add_argument('--max_epoch', default=1000, type=int, help='Number of training epoches')
parser.add_argument('--store_num', default=10, type=int, help='Store model how often')
parser.add_argument('--lr', default=1e-4, type=float, help='Learning rate')
parser.add_argument('--weight_decay', default=1e-5, type=float, help='Weight Decay')
## dataset
parser.add_argument('--dataset_list', nargs='+', default=['PAOT_123457891213','PAOT_10_inner']) # 'PAOT', 'felix' 'PAOT_123457891213', 'PAOT_10_inner', 'PAOT_tumor'
### please check this argment carefully
### PAOT: include PAOT_123457891213 and PAOT_10
### PAOT_123457891213: include 1 2 3 4 5 7 8 9 12 13
### PAOT_10_inner
parser.add_argument('--data_root_path', default='/home/jliu288/data/whole_organ/', help='data root path')
parser.add_argument('--data_txt_path', default='./dataset/dataset_list/', help='data txt path')
parser.add_argument('--batch_size', default=1, type=int, help='batch size')
parser.add_argument('--num_workers', default=8, type=int, help='workers numebr for DataLoader')
parser.add_argument('--a_min', default=-175, type=float, help='a_min in ScaleIntensityRanged')
parser.add_argument('--a_max', default=250, type=float, help='a_max in ScaleIntensityRanged')
parser.add_argument('--b_min', default=0.0, type=float, help='b_min in ScaleIntensityRanged')
parser.add_argument('--b_max', default=1.0, type=float, help='b_max in ScaleIntensityRanged')
parser.add_argument('--space_x', default=1.5, type=float, help='spacing in x direction')
parser.add_argument('--space_y', default=1.5, type=float, help='spacing in y direction')
parser.add_argument('--space_z', default=1.5, type=float, help='spacing in z direction')
parser.add_argument('--roi_x', default=96, type=int, help='roi size in x direction')
parser.add_argument('--roi_y', default=96, type=int, help='roi size in y direction')
parser.add_argument('--roi_z', default=96, type=int, help='roi size in z direction')
parser.add_argument('--num_samples', default=1, type=int, help='sample number in each ct')
parser.add_argument('--phase', default='validation', help='train or validation or test')
parser.add_argument('--cache_dataset', action="store_true", default=False, help='whether use cache dataset')
parser.add_argument('--cache_rate', default=0.6, type=float, help='The percentage of cached data in total')
parser.add_argument('--store_result', action="store_true", default=False, help='whether save prediction result')
parser.add_argument('--overlap', default=0.5, type=float, help='overlap for sliding_window_inference')
args = parser.parse_args()
# prepare the 3D model
model = Universal_model(img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=NUM_CLASS,
backbone=args.backbone,
encoding='word_embedding'
)
#Load pre-trained weights
store_path_root = 'out/Nvidia/new_clip/clip1_partialv2/epoch_***.pth'
for store_path in glob.glob(store_path_root):
# store_path = store_path_root
store_dict = model.state_dict()
load_dict = torch.load(store_path)['net']
for key, value in load_dict.items():
if 'swinViT' in key or 'encoder' in key or 'decoder' in key:
name = '.'.join(key.split('.')[1:])
name = 'backbone.' + name
else:
name = '.'.join(key.split('.')[1:])
store_dict[name] = value
model.load_state_dict(store_dict)
print(f'Load {store_path} weights')
model.cuda()
torch.backends.cudnn.benchmark = True
validation_loader, val_transforms = get_loader(args)
i = int(store_path.split('_')[-1].split('.')[0])+1
validation(model, validation_loader, args, i)
if __name__ == "__main__":
main()
#python validation.py >> out/Nvidia/ablation_clip/clip2.txt