-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_prediction.py
150 lines (131 loc) · 8.49 KB
/
export_prediction.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
import os
from copy import deepcopy
from typing import Union, List
import numpy as np
import torch
from acvl_utils.cropping_and_padding.bounding_boxes import bounding_box_to_slice
from batchgenerators.utilities.file_and_folder_operations import load_json, isfile, save_pickle
from nnunetv2.configuration import default_num_processes
from nnunetv2.utilities.label_handling.label_handling import LabelManager
from nnunetv2.utilities.plans_handling.plans_handler import PlansManager, ConfigurationManager
def convert_predicted_logits_to_segmentation_with_correct_shape(predicted_logits: Union[torch.Tensor, np.ndarray],
plans_manager: PlansManager,
configuration_manager: ConfigurationManager,
label_manager: LabelManager,
properties_dict: dict,
return_probabilities: bool = False,
num_threads_torch: int = default_num_processes):
old_threads = torch.get_num_threads()
torch.set_num_threads(num_threads_torch)
# resample to original shape
current_spacing = configuration_manager.spacing if \
len(configuration_manager.spacing) == \
len(properties_dict['shape_after_cropping_and_before_resampling']) else \
[properties_dict['spacing'][0], *configuration_manager.spacing]
predicted_logits = configuration_manager.resampling_fn_probabilities(predicted_logits,
properties_dict['shape_after_cropping_and_before_resampling'],
current_spacing,
properties_dict['spacing'])
# return value of resampling_fn_probabilities can be ndarray or Tensor but that doesnt matter because
# apply_inference_nonlin will covnert to torch
predicted_probabilities = label_manager.apply_inference_nonlin(predicted_logits)
del predicted_logits
segmentation = label_manager.convert_probabilities_to_segmentation(predicted_probabilities)
# segmentation may be torch.Tensor but we continue with numpy
if isinstance(segmentation, torch.Tensor):
segmentation = segmentation.cpu().numpy()
# put segmentation in bbox (revert cropping)
segmentation_reverted_cropping = np.zeros(properties_dict['shape_before_cropping'],
dtype=np.uint8 if len(label_manager.foreground_labels) < 255 else np.uint16)
slicer = bounding_box_to_slice(properties_dict['bbox_used_for_cropping'])
segmentation_reverted_cropping[slicer] = segmentation
del segmentation
# revert transpose
segmentation_reverted_cropping = segmentation_reverted_cropping.transpose(plans_manager.transpose_backward)
if return_probabilities:
# revert cropping
predicted_probabilities = label_manager.revert_cropping_on_probabilities(predicted_probabilities,
properties_dict[
'bbox_used_for_cropping'],
properties_dict[
'shape_before_cropping'])
predicted_probabilities = predicted_probabilities.cpu().numpy()
# revert transpose
predicted_probabilities = predicted_probabilities.transpose([0] + [i + 1 for i in
plans_manager.transpose_backward])
torch.set_num_threads(old_threads)
return segmentation_reverted_cropping, predicted_probabilities
else:
torch.set_num_threads(old_threads)
return segmentation_reverted_cropping
def export_prediction_from_logits(predicted_array_or_file: Union[np.ndarray, torch.Tensor], properties_dict: dict,
configuration_manager: ConfigurationManager,
plans_manager: PlansManager,
dataset_json_dict_or_file: Union[dict, str], output_file_truncated: str,
save_probabilities: bool = False):
# if isinstance(predicted_array_or_file, str):
# tmp = deepcopy(predicted_array_or_file)
# if predicted_array_or_file.endswith('.npy'):
# predicted_array_or_file = np.load(predicted_array_or_file)
# elif predicted_array_or_file.endswith('.npz'):
# predicted_array_or_file = np.load(predicted_array_or_file)['softmax']
# os.remove(tmp)
if isinstance(dataset_json_dict_or_file, str):
dataset_json_dict_or_file = load_json(dataset_json_dict_or_file)
label_manager = plans_manager.get_label_manager(dataset_json_dict_or_file)
ret = convert_predicted_logits_to_segmentation_with_correct_shape(
predicted_array_or_file, plans_manager, configuration_manager, label_manager, properties_dict,
return_probabilities=save_probabilities
)
del predicted_array_or_file
# save
if save_probabilities:
segmentation_final, probabilities_final = ret
np.savez_compressed(output_file_truncated + '.npz', probabilities=probabilities_final)
save_pickle(properties_dict, output_file_truncated + '.pkl')
### Here is a custom modification, which allows to save probability map in the same format
# as segmentation maps. Important: dataset must be in format read by SimpleITK IO ###
rw = plans_manager.image_reader_writer_class()
rw.write_seg(probabilities_final[1], output_file_truncated + '_prob_ch1' + dataset_json_dict_or_file['file_ending'],
properties_dict, probabilities = True)
del probabilities_final, ret, rw
else:
segmentation_final = ret
del ret
rw = plans_manager.image_reader_writer_class()
rw.write_seg(segmentation_final, output_file_truncated + dataset_json_dict_or_file['file_ending'],
properties_dict)
def resample_and_save(predicted: Union[torch.Tensor, np.ndarray], target_shape: List[int], output_file: str,
plans_manager: PlansManager, configuration_manager: ConfigurationManager, properties_dict: dict,
dataset_json_dict_or_file: Union[dict, str], num_threads_torch: int = default_num_processes) \
-> None:
# # needed for cascade
# if isinstance(predicted, str):
# assert isfile(predicted), "If isinstance(segmentation_softmax, str) then " \
# "isfile(segmentation_softmax) must be True"
# del_file = deepcopy(predicted)
# predicted = np.load(predicted)
# os.remove(del_file)
old_threads = torch.get_num_threads()
torch.set_num_threads(num_threads_torch)
if isinstance(dataset_json_dict_or_file, str):
dataset_json_dict_or_file = load_json(dataset_json_dict_or_file)
# resample to original shape
current_spacing = configuration_manager.spacing if \
len(configuration_manager.spacing) == len(properties_dict['shape_after_cropping_and_before_resampling']) else \
[properties_dict['spacing'][0], *configuration_manager.spacing]
target_spacing = configuration_manager.spacing if len(configuration_manager.spacing) == \
len(properties_dict['shape_after_cropping_and_before_resampling']) else \
[properties_dict['spacing'][0], *configuration_manager.spacing]
predicted_array_or_file = configuration_manager.resampling_fn_probabilities(predicted,
target_shape,
current_spacing,
target_spacing)
# create segmentation (argmax, regions, etc)
label_manager = plans_manager.get_label_manager(dataset_json_dict_or_file)
segmentation = label_manager.convert_logits_to_segmentation(predicted_array_or_file)
# segmentation may be torch.Tensor but we continue with numpy
if isinstance(segmentation, torch.Tensor):
segmentation = segmentation.cpu().numpy()
np.savez_compressed(output_file, seg=segmentation.astype(np.uint8))
torch.set_num_threads(old_threads)