Pick slice with most tumor instead of fixed middle slice
Browse files- inference.py +40 -19
inference.py
CHANGED
|
@@ -3,35 +3,40 @@ import numpy as np
|
|
| 3 |
import torch
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from matplotlib.colors import ListedColormap
|
| 6 |
-
from
|
| 7 |
from monai.data import MetaTensor
|
| 8 |
from monai.transforms import Orientation, EnsureType
|
|
|
|
| 9 |
from sklearn.preprocessing import MinMaxScaler
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
SW_ROI = [128, 128, 128]
|
| 14 |
SW_BATCH_SIZE = 2
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return img.get_fdata().astype(np.float32), img.affine
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def normalize(x: np.ndarray) -> np.ndarray:
|
| 22 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 23 |
normalized_1D_array = scaler.fit_transform(x.reshape(-1, x.shape[-1]))
|
| 24 |
return normalized_1D_array.reshape(x.shape)
|
| 25 |
|
|
|
|
| 26 |
def reorient_to_ras(x: MetaTensor) -> MetaTensor:
|
| 27 |
return Orientation(axcodes="RAS")(x)
|
| 28 |
|
|
|
|
| 29 |
def convert_metatensor_to_numpy(x: MetaTensor) -> np.ndarray:
|
| 30 |
return EnsureType(data_type="numpy", track_meta=False)(x)
|
| 31 |
|
|
|
|
| 32 |
def crop_brats2021_volume(x: np.ndarray) -> np.ndarray:
|
| 33 |
return x[:, 56:184, 56:184, 13:141]
|
| 34 |
|
|
|
|
| 35 |
def preprocess_single_modality(fp):
|
| 36 |
nifti_data = nib.load(fp)
|
| 37 |
data = nifti_data.get_fdata()
|
|
@@ -46,8 +51,9 @@ def preprocess_single_modality(fp):
|
|
| 46 |
data = crop_brats2021_volume(data) # -> (1, 128, 128, 128)
|
| 47 |
return data
|
| 48 |
|
|
|
|
| 49 |
def preprocess_volume(t1_path, t1ce_path, t2_path, flair_path):
|
| 50 |
-
#
|
| 51 |
flair = preprocess_single_modality(flair_path).swapaxes(1, 3)
|
| 52 |
t1w = preprocess_single_modality(t1_path).swapaxes(1, 3)
|
| 53 |
t1gd = preprocess_single_modality(t1ce_path).swapaxes(1, 3)
|
|
@@ -56,8 +62,13 @@ def preprocess_volume(t1_path, t1ce_path, t2_path, flair_path):
|
|
| 56 |
volume = np.concatenate((flair, t1w, t1gd, t2w), axis=0).astype(np.float32)
|
| 57 |
return volume, None
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def run_inference(model, volume, device):
|
| 60 |
-
tensor = torch.from_numpy(volume).unsqueeze(0).to(device)
|
| 61 |
|
| 62 |
with torch.no_grad():
|
| 63 |
output = sliding_window_inference(
|
|
@@ -69,23 +80,35 @@ def run_inference(model, volume, device):
|
|
| 69 |
)
|
| 70 |
pred = (torch.sigmoid(output) > 0.5).float()
|
| 71 |
|
| 72 |
-
return pred[0].cpu().numpy()
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def combine_regions(vol_3ch):
|
| 77 |
tc, wt, et = vol_3ch[0], vol_3ch[1], vol_3ch[2]
|
| 78 |
combined = np.zeros_like(wt)
|
| 79 |
-
combined[wt > 0] = 1
|
| 80 |
-
combined[tc > 0] = 2
|
| 81 |
-
combined[et > 0] = 3
|
| 82 |
return combined
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def make_slice_figure(volume, pred, save_path="preview.png"):
|
| 85 |
-
mid =
|
| 86 |
-
flair_slice = volume[0][mid] # channel 0 = FLAIR
|
| 87 |
|
| 88 |
-
pred_combined = combine_regions(pred)[mid]
|
| 89 |
pred_tc, pred_wt, pred_et = pred[0][mid], pred[1][mid], pred[2][mid]
|
| 90 |
|
| 91 |
rows = [
|
|
@@ -112,9 +135,8 @@ def make_slice_figure(volume, pred, save_path="preview.png"):
|
|
| 112 |
return save_path
|
| 113 |
|
| 114 |
|
| 115 |
-
|
| 116 |
def make_combined_figure(volume, pred, save_path="combined.png"):
|
| 117 |
-
mid =
|
| 118 |
flair_slice = volume[0][mid]
|
| 119 |
pred_combined = combine_regions(pred)[mid]
|
| 120 |
|
|
@@ -124,7 +146,6 @@ def make_combined_figure(volume, pred, save_path="combined.png"):
|
|
| 124 |
ax.set_title("Combined Prediction", fontsize=14)
|
| 125 |
ax.axis("off")
|
| 126 |
|
| 127 |
-
from matplotlib.patches import Patch
|
| 128 |
legend_elements = [
|
| 129 |
Patch(facecolor="yellow", label="Whole Tumor (WT)"),
|
| 130 |
Patch(facecolor="green", label="Tumor Core (TC)"),
|
|
|
|
| 3 |
import torch
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
from matplotlib.colors import ListedColormap
|
| 6 |
+
from matplotlib.patches import Patch
|
| 7 |
from monai.data import MetaTensor
|
| 8 |
from monai.transforms import Orientation, EnsureType
|
| 9 |
+
from monai.inferers import sliding_window_inference
|
| 10 |
from sklearn.preprocessing import MinMaxScaler
|
| 11 |
|
|
|
|
|
|
|
| 12 |
SW_ROI = [128, 128, 128]
|
| 13 |
SW_BATCH_SIZE = 2
|
| 14 |
|
| 15 |
+
region_cmap = ListedColormap(["none", "yellow", "green", "red"])
|
| 16 |
+
|
|
|
|
| 17 |
|
| 18 |
+
# ---------------------------------------------------------------------------
|
| 19 |
+
# Preprocessing (matches training pipeline exactly)
|
| 20 |
+
# ---------------------------------------------------------------------------
|
| 21 |
|
| 22 |
def normalize(x: np.ndarray) -> np.ndarray:
|
| 23 |
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 24 |
normalized_1D_array = scaler.fit_transform(x.reshape(-1, x.shape[-1]))
|
| 25 |
return normalized_1D_array.reshape(x.shape)
|
| 26 |
|
| 27 |
+
|
| 28 |
def reorient_to_ras(x: MetaTensor) -> MetaTensor:
|
| 29 |
return Orientation(axcodes="RAS")(x)
|
| 30 |
|
| 31 |
+
|
| 32 |
def convert_metatensor_to_numpy(x: MetaTensor) -> np.ndarray:
|
| 33 |
return EnsureType(data_type="numpy", track_meta=False)(x)
|
| 34 |
|
| 35 |
+
|
| 36 |
def crop_brats2021_volume(x: np.ndarray) -> np.ndarray:
|
| 37 |
return x[:, 56:184, 56:184, 13:141]
|
| 38 |
|
| 39 |
+
|
| 40 |
def preprocess_single_modality(fp):
|
| 41 |
nifti_data = nib.load(fp)
|
| 42 |
data = nifti_data.get_fdata()
|
|
|
|
| 51 |
data = crop_brats2021_volume(data) # -> (1, 128, 128, 128)
|
| 52 |
return data
|
| 53 |
|
| 54 |
+
|
| 55 |
def preprocess_volume(t1_path, t1ce_path, t2_path, flair_path):
|
| 56 |
+
# channel order matches training: Flair, T1w, T1gd, T2w
|
| 57 |
flair = preprocess_single_modality(flair_path).swapaxes(1, 3)
|
| 58 |
t1w = preprocess_single_modality(t1_path).swapaxes(1, 3)
|
| 59 |
t1gd = preprocess_single_modality(t1ce_path).swapaxes(1, 3)
|
|
|
|
| 62 |
volume = np.concatenate((flair, t1w, t1gd, t2w), axis=0).astype(np.float32)
|
| 63 |
return volume, None
|
| 64 |
|
| 65 |
+
|
| 66 |
+
# ---------------------------------------------------------------------------
|
| 67 |
+
# Inference
|
| 68 |
+
# ---------------------------------------------------------------------------
|
| 69 |
+
|
| 70 |
def run_inference(model, volume, device):
|
| 71 |
+
tensor = torch.from_numpy(volume).unsqueeze(0).to(device) # (1, 4, D, H, W)
|
| 72 |
|
| 73 |
with torch.no_grad():
|
| 74 |
output = sliding_window_inference(
|
|
|
|
| 80 |
)
|
| 81 |
pred = (torch.sigmoid(output) > 0.5).float()
|
| 82 |
|
| 83 |
+
return pred[0].cpu().numpy() # (3, D, H, W) -- channel order: TC, WT, ET
|
| 84 |
|
| 85 |
+
|
| 86 |
+
# ---------------------------------------------------------------------------
|
| 87 |
+
# Visualization helpers
|
| 88 |
+
# ---------------------------------------------------------------------------
|
| 89 |
|
| 90 |
def combine_regions(vol_3ch):
|
| 91 |
tc, wt, et = vol_3ch[0], vol_3ch[1], vol_3ch[2]
|
| 92 |
combined = np.zeros_like(wt)
|
| 93 |
+
combined[wt > 0] = 1 # whole tumor / edema region -> yellow
|
| 94 |
+
combined[tc > 0] = 2 # tumor core -> green
|
| 95 |
+
combined[et > 0] = 3 # enhancing tumor -> red
|
| 96 |
return combined
|
| 97 |
|
| 98 |
+
|
| 99 |
+
def find_best_slice(pred):
|
| 100 |
+
"""Pick the D-axis slice with the most total tumor voxels (TC+WT+ET),
|
| 101 |
+
so small regions like TC/ET are actually visible instead of relying
|
| 102 |
+
on the raw geometric middle slice."""
|
| 103 |
+
combined_mask = (pred[0] > 0) | (pred[1] > 0) | (pred[2] > 0)
|
| 104 |
+
voxels_per_slice = combined_mask.sum(axis=(1, 2))
|
| 105 |
+
return int(voxels_per_slice.argmax())
|
| 106 |
+
|
| 107 |
+
|
| 108 |
def make_slice_figure(volume, pred, save_path="preview.png"):
|
| 109 |
+
mid = find_best_slice(pred)
|
| 110 |
+
flair_slice = volume[0][mid] # channel 0 = FLAIR
|
| 111 |
|
|
|
|
| 112 |
pred_tc, pred_wt, pred_et = pred[0][mid], pred[1][mid], pred[2][mid]
|
| 113 |
|
| 114 |
rows = [
|
|
|
|
| 135 |
return save_path
|
| 136 |
|
| 137 |
|
|
|
|
| 138 |
def make_combined_figure(volume, pred, save_path="combined.png"):
|
| 139 |
+
mid = find_best_slice(pred)
|
| 140 |
flair_slice = volume[0][mid]
|
| 141 |
pred_combined = combine_regions(pred)[mid]
|
| 142 |
|
|
|
|
| 146 |
ax.set_title("Combined Prediction", fontsize=14)
|
| 147 |
ax.axis("off")
|
| 148 |
|
|
|
|
| 149 |
legend_elements = [
|
| 150 |
Patch(facecolor="yellow", label="Whole Tumor (WT)"),
|
| 151 |
Patch(facecolor="green", label="Tumor Core (TC)"),
|