-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
284 lines (236 loc) · 10 KB
/
utils.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import pathlib
import pprint
import SimpleITK as sitk
import numpy as np
import pandas as pd
import torch
import yaml
from matplotlib import pyplot as plt
from numpy import logical_and as l_and, logical_not as l_not
from scipy.spatial.distance import directed_hausdorff
from torch import distributed as dist
from torch.cuda.amp import autocast
from dataset.batch_utils import pad_batch1_to_compatible_size
def save_args(args):
"""Save parsed arguments to config file.
"""
config = vars(args).copy()
del config['save_folder']
del config['seg_folder']
config_file = args.save_folder / (args.exp_name + ".yaml")
with open(config_file, "w") as file:
yaml.dump(config, file)
def master_do(func, *args, **kwargs):
"""Help calling function only on the rank0 process id ddp"""
try:
rank = dist.get_rank()
if rank == 0:
return func(*args, **kwargs)
except AssertionError:
# not in DDP setting, just do as usual
func(*args, **kwargs)
def save_checkpoint(state: dict, save_folder: pathlib.Path):
"""Save Training state."""
best_filename = f'{str(save_folder)}/model_best.pth.tar'
torch.save(state, best_filename)
class AverageMeter(object):
"""Computes and stores the average and current value."""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
@staticmethod
def _get_batch_fmtstr(num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
# TODO remove dependency to args
def reload_ckpt(args, model, optimizer, scheduler):
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
raise ValueError("=> no checkpoint found at '{}'".format(args.resume))
def reload_ckpt_bis(ckpt, model, optimizer=None):
if os.path.isfile(ckpt):
print(f"=> loading checkpoint {ckpt}")
try:
checkpoint = torch.load(ckpt)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
if optimizer:
optimizer.load_state_dict(checkpoint['optimizer'])
print(f"=> loaded checkpoint '{ckpt}' (epoch {start_epoch})")
return start_epoch
except RuntimeError:
# TO account for checkpoint from Alex nets
print("Loading model Alex style")
model.load_state_dict(torch.load(ckpt, map_location='cpu'))
else:
raise ValueError(f"=> no checkpoint found at '{ckpt}'")
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def calculate_metrics(preds, targets, patient, tta=False):
"""
Parameters
----------
preds:
torch tensor of size 1*C*Z*Y*X
targets:
torch tensor of same shape
patient :
The patient ID
tta:
is tta performed for this run
"""
pp = pprint.PrettyPrinter(indent=4)
assert preds.shape == targets.shape, "Preds and targets do not have the same size"
labels = ["ET", "TC", "WT"]
metrics_list = []
for i, label in enumerate(labels):
metrics = dict(
patient_id=patient,
label=label,
tta=tta,
)
if np.sum(targets[i]) == 0:
print(f"{label} not present for {patient}")
sens = np.nan
dice = 1 if np.sum(preds[i]) == 0 else 0
tn = np.sum(l_and(l_not(preds[i]), l_not(targets[i])))
fp = np.sum(l_and(preds[i], l_not(targets[i])))
spec = tn / (tn + fp)
haussdorf_dist = np.nan
else:
preds_coords = np.argwhere(preds[i])
targets_coords = np.argwhere(targets[i])
haussdorf_dist = directed_hausdorff(preds_coords, targets_coords)[0]
tp = np.sum(l_and(preds[i], targets[i]))
tn = np.sum(l_and(l_not(preds[i]), l_not(targets[i])))
fp = np.sum(l_and(preds[i], l_not(targets[i])))
fn = np.sum(l_and(l_not(preds[i]), targets[i]))
sens = tp / (tp + fn)
spec = tn / (tn + fp)
dice = 2 * tp / (2 * tp + fp + fn)
metrics[HAUSSDORF] = haussdorf_dist
metrics[DICE] = dice
metrics[SENS] = sens
metrics[SPEC] = spec
pp.pprint(metrics)
metrics_list.append(metrics)
return metrics_list
def save_metrics(epoch, metrics, writer, current_epoch, teacher=False, save_folder=None):
metrics = list(zip(*metrics))
# print(metrics)
# TODO check if doing it directly to numpy work
metrics = [torch.tensor(dice, device="cpu").numpy() for dice in metrics]
# print(metrics)
labels = ("ET", "TC", "WT")
metrics = {key: value for key, value in zip(labels, metrics)}
# print(metrics)
fig, ax = plt.subplots()
ax.set_title("Dice metrics")
ax.boxplot(metrics.values(), labels=metrics.keys())
ax.set_ylim(0, 1)
writer.add_figure(f"val/plot", fig, global_step=epoch)
print(f"Epoch {current_epoch} :{'val' + '_teacher :' if teacher else 'Val :'}",
[f"{key} : {np.nanmean(value)}" for key, value in metrics.items()])
with open(f"{save_folder}/val{'_teacher' if teacher else ''}.txt", mode="a") as f:
print(f"Epoch {current_epoch} :{'val' + '_teacher :' if teacher else 'Val :'}",
[f"{key} : {np.nanmean(value)}" for key, value in metrics.items()], file=f)
for key, value in metrics.items():
tag = f"val{'_teacher' if teacher else ''}{''}/{key}_Dice"
writer.add_scalar(tag, np.nanmean(value), global_step=epoch)
def generate_segmentations(data_loader, model, writer, args):
metrics_list = []
for i, batch in enumerate(data_loader):
# measure data loading time
inputs = batch["image"]
patient_id = batch["patient_id"][0]
ref_path = batch["seg_path"][0]
crops_idx = batch["crop_indexes"]
inputs, pads = pad_batch1_to_compatible_size(inputs)
inputs = inputs.cuda()
ref_seg_img = sitk.ReadImage(ref_path)
ref_seg = sitk.GetArrayFromImage(ref_seg_img)
with autocast():
with torch.no_grad():
pre_segs = model(inputs)
pre_segs = torch.sigmoid(pre_segs)
# remove pads
maxz, maxy, maxx = pre_segs.size(2) - pads[0], pre_segs.size(3) - pads[1], pre_segs.size(4) - pads[2]
pre_segs = pre_segs[:, :, 0:maxz, 0:maxy, 0:maxx].cpu()
segs = torch.zeros((1, 3, ref_seg.shape[0], ref_seg.shape[1], ref_seg.shape[2]))
segs[0, :, slice(*crops_idx[0]), slice(*crops_idx[1]), slice(*crops_idx[2])] = pre_segs[0]
segs = segs[0].numpy() > 0.5
et = segs[0]
net = np.logical_and(segs[1], np.logical_not(et))
ed = np.logical_and(segs[2], np.logical_not(segs[1]))
labelmap = np.zeros(segs[0].shape)
labelmap[et] = 4
labelmap[net] = 1
labelmap[ed] = 2
labelmap = sitk.GetImageFromArray(labelmap)
ref_seg_img = sitk.ReadImage(ref_path)
ref_seg = sitk.GetArrayFromImage(ref_seg_img)
refmap_et, refmap_tc, refmap_wt = [np.zeros_like(ref_seg) for i in range(3)]
refmap_et = ref_seg == 4
refmap_tc = np.logical_or(refmap_et, ref_seg == 1)
refmap_wt = np.logical_or(refmap_tc, ref_seg == 2)
refmap = np.stack([refmap_et, refmap_tc, refmap_wt])
patient_metric_list = calculate_metrics(segs, refmap, patient_id)
metrics_list.append(patient_metric_list)
labelmap.CopyInformation(ref_seg_img)
print(f"Writing {args.seg_folder}/{patient_id}.nii.gz")
sitk.WriteImage(labelmap, f"{args.seg_folder}/{patient_id}.nii.gz")
val_metrics = [item for sublist in metrics_list for item in sublist]
df = pd.DataFrame(val_metrics)
overlap = df.boxplot(METRICS[1:], by="label", return_type="axes")
overlap_figure = overlap[0].get_figure()
writer.add_figure("benchmark/overlap_measures", overlap_figure)
haussdorf_figure = df.boxplot(METRICS[0], by="label").get_figure()
writer.add_figure("benchmark/distance_measure", haussdorf_figure)
grouped_df = df.groupby("label")[METRICS]
summary = grouped_df.mean().to_dict()
for metric, label_values in summary.items():
for label, score in label_values.items():
writer.add_scalar(f"benchmark_{metric}/{label}", score)
df.to_csv((args.save_folder / 'results.csv'), index=False)
HAUSSDORF = "haussdorf"
DICE = "dice"
SENS = "sens"
SPEC = "spec"
METRICS = [HAUSSDORF, DICE, SENS, SPEC]