[637b40]: / adpkd_segmentation / evaluate_patients.py

Download this file

219 lines (169 with data), 6.9 kB

  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
"""
Model evaluation script for TKV
python -m adpkd_segmentation.evaluate_patients
--config path_to_config_yaml --makelinks --out_path output_csv_path
If using a specific GPU, e.g. device 2, prepend the command with CUDA_VISIBLE_DEVICES=2 # noqa
The makelinks flag is needed only once to create symbolic links to the data.
"""
# %%
from collections import OrderedDict, defaultdict
import argparse
import yaml
import pandas as pd
import torch
from adpkd_segmentation.config.config_utils import get_object_instance
from adpkd_segmentation.data.link_data import makelinks
from adpkd_segmentation.utils.train_utils import load_model_data
from adpkd_segmentation.utils.losses import SigmoidBinarize
# %%
def calculate_dcm_voxel_volumes(
dataloader, model, device, binarize_func,
):
num_examples = 0
dataset = dataloader.dataset
updated_dcm2attribs = {}
output_example_idx = (
hasattr(dataloader.dataset, "output_idx")
and dataloader.dataset.output_idx
)
for batch_idx, output in enumerate(dataloader):
if output_example_idx:
x_batch, y_batch, _ = output
else:
x_batch, y_batch = output
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
batch_size = y_batch.size(0)
num_examples += batch_size
with torch.no_grad():
y_batch_hat = model(x_batch)
y_batch_hat_binary = binarize_func(y_batch_hat)
start_idx = num_examples - batch_size
end_idx = num_examples
for inbatch_idx, dataset_idx in enumerate(
range(start_idx, end_idx)
):
# calculate TKV and TKV inputs for each dcm
# TODO:
# support 3 channel setups where ones could mean background
# needs mask standardization to single channel
_, dcm_path, attribs = dataset.get_verbose(dataset_idx)
attribs["pred_kidney_pixels"] = torch.sum(
y_batch_hat_binary[inbatch_idx] > 0
).item()
attribs["ground_kidney_pixels"] = torch.sum(
y_batch[inbatch_idx] > 0
).item()
# TODO: Clean up method of accessing Resize transform
attribs["transform_resize_dim"] = (
dataloader.dataset.augmentation[0].height,
dataloader.dataset.augmentation[0].width,
)
# scale factor takes into account the difference
# between the original image/mask size and the size
# after mask & prediction resizing
scale_factor = (attribs["dim"][0] ** 2) / (
attribs["transform_resize_dim"][0] ** 2
)
attribs["Vol_GT"] = (
scale_factor
* attribs["vox_vol"]
* attribs["ground_kidney_pixels"]
)
attribs["Vol_Pred"] = (
scale_factor
* attribs["vox_vol"]
* attribs["pred_kidney_pixels"]
)
updated_dcm2attribs[dcm_path] = attribs
return updated_dcm2attribs
# %%
def visualize_performance(
dataloader, model, device, binarize_func,
):
dataset = dataloader.dataset
output_example_idx = (
hasattr(dataloader.dataset, "output_idx")
and dataloader.dataset.output_idx
)
for batch_idx, output in enumerate(dataloader):
if output_example_idx:
x_batch, y_batch, _ = output
else:
x_batch, y_batch = output
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
batch_size = y_batch.size(0)
num_examples += batch_size
with torch.no_grad():
_, dcm_path, attribs = dataset.get_verbose(batch_size * batch_idx)
y_batch_hat = model(x_batch)
y_batch_hat_binary = binarize_func(y_batch_hat)
start_idx = batch_size * batch_idx
end_idx = batch_size * (1 + batch_idx)
# for inbatch_idx, dataset_idx in enumerate(
# range(start_idx, end_idx)
# ):
# _, dcm_path, attribs = dataset.get_verbose(dataset_idx)
# updated_dcm2attribs[dcm_path] = attribs
# %%
def evaluate(config):
model_config = config["_MODEL_CONFIG"]
loader_to_eval = config["_LOADER_TO_EVAL"]
dataloader_config = config[loader_to_eval]
saved_checkpoint = config["_MODEL_CHECKPOINT"]
checkpoint_format = config["_NEW_CKP_FORMAT"]
model = get_object_instance(model_config)()
if saved_checkpoint is not None:
load_model_data(saved_checkpoint, model, new_format=checkpoint_format)
dataloader = get_object_instance(dataloader_config)()
# TODO: support other metrics as needed
binarize_func = SigmoidBinarize(thresholds=[0.5])
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()
updated_dcm2attribs = calculate_dcm_voxel_volumes(
dataloader, model, device, binarize_func
)
return updated_dcm2attribs
# %%
def calculate_TKVs(config_path, run_makelinks=False, output=None):
if run_makelinks:
makelinks()
with open(config_path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
# val or test
split = config["_LOADER_TO_EVAL"].split("_")[1].lower()
dcm2attrib = evaluate(config)
patient_MR_TKV = defaultdict(float)
TKV_data = OrderedDict()
for key, value in dcm2attrib.items():
patient_MR = value["patient"] + value["MR"]
patient_MR_TKV[(patient_MR, "GT")] += value["Vol_GT"]
patient_MR_TKV[(patient_MR, "Pred")] += value["Vol_Pred"]
for key, value in dcm2attrib.items():
patient_MR = value["patient"] + value["MR"]
if patient_MR not in TKV_data:
summary = {
"TKV_GT": patient_MR_TKV[(patient_MR, "GT")],
"TKV_Pred": patient_MR_TKV[(patient_MR, "Pred")],
"sequence": value["seq"],
"split": split,
}
TKV_data[patient_MR] = summary
df = pd.DataFrame(TKV_data).transpose()
if output is not None:
df.to_csv(output)
return TKV_data
# %%
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config", help="YAML config path", type=str, required=True
)
parser.add_argument(
"--makelinks", help="Make data links", action="store_true"
)
parser.add_argument("--out_path", help="Path to output csv", required=True)
args = parser.parse_args()
calculate_TKVs(args.config, args.makelinks, args.out_path)