Download inference.py from EScO-V10/MedSegNet: direct link, hf CLI and curl.
- Browser
- Download file 5.45 kB
-
https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/inference.py
- Command line
-
hf download hf://spaces/EScO-V10/MedSegNet/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/inference.py
5.45 kB
| import nibabel as nib | |
| import numpy as np | |
| import torch | |
| import matplotlib.pyplot as plt | |
| from matplotlib.colors import ListedColormap | |
| from matplotlib.patches import Patch | |
| from monai.data import MetaTensor | |
| from monai.transforms import Orientation, EnsureType | |
| from monai.inferers import sliding_window_inference | |
| from sklearn.preprocessing import MinMaxScaler | |
| SW_ROI = [128, 128, 128] | |
| SW_BATCH_SIZE = 2 | |
| region_cmap = ListedColormap(["none", "yellow", "green", "red"]) | |
| # --------------------------------------------------------------------------- | |
| # Preprocessing (matches training pipeline exactly) | |
| # --------------------------------------------------------------------------- | |
| def normalize(x: np.ndarray) -> np.ndarray: | |
| scaler = MinMaxScaler(feature_range=(0, 1)) | |
| normalized_1D_array = scaler.fit_transform(x.reshape(-1, x.shape[-1])) | |
| return normalized_1D_array.reshape(x.shape) | |
| def reorient_to_ras(x: MetaTensor) -> MetaTensor: | |
| return Orientation(axcodes="RAS")(x) | |
| def convert_metatensor_to_numpy(x: MetaTensor) -> np.ndarray: | |
| return EnsureType(data_type="numpy", track_meta=False)(x) | |
| def crop_brats2021_volume(x: np.ndarray) -> np.ndarray: | |
| return x[:, 56:184, 56:184, 13:141] | |
| def preprocess_single_modality(fp): | |
| nifti_data = nib.load(fp) | |
| data = nifti_data.get_fdata() | |
| affine = nifti_data.affine | |
| data = normalize(x=data) | |
| data = data[np.newaxis, ...] # (240,240,155) -> (1,240,240,155) | |
| data = MetaTensor(x=data, affine=affine) | |
| data = reorient_to_ras(data) | |
| data = convert_metatensor_to_numpy(data) | |
| data = crop_brats2021_volume(data) # -> (1, 128, 128, 128) | |
| return data | |
| def preprocess_volume(t1_path, t1ce_path, t2_path, flair_path): | |
| # channel order matches training: Flair, T1w, T1gd, T2w | |
| flair = preprocess_single_modality(flair_path).swapaxes(1, 3) | |
| t1w = preprocess_single_modality(t1_path).swapaxes(1, 3) | |
| t1gd = preprocess_single_modality(t1ce_path).swapaxes(1, 3) | |
| t2w = preprocess_single_modality(t2_path).swapaxes(1, 3) | |
| volume = np.concatenate((flair, t1w, t1gd, t2w), axis=0).astype(np.float32) | |
| return volume, None | |
| # --------------------------------------------------------------------------- | |
| # Inference | |
| # --------------------------------------------------------------------------- | |
| def run_inference(model, volume, device): | |
| tensor = torch.from_numpy(volume).unsqueeze(0).to(device) # (1, 4, D, H, W) | |
| with torch.no_grad(): | |
| output = sliding_window_inference( | |
| inputs=tensor, | |
| roi_size=SW_ROI, | |
| sw_batch_size=SW_BATCH_SIZE, | |
| predictor=model, | |
| overlap=0.5, | |
| ) | |
| pred = (torch.sigmoid(output) > 0.5).float() | |
| return pred[0].cpu().numpy() # (3, D, H, W) -- channel order: TC, WT, ET | |
| # --------------------------------------------------------------------------- | |
| # Visualization helpers | |
| # --------------------------------------------------------------------------- | |
| def combine_regions(vol_3ch): | |
| tc, wt, et = vol_3ch[0], vol_3ch[1], vol_3ch[2] | |
| combined = np.zeros_like(wt) | |
| combined[wt > 0] = 1 # whole tumor / edema region -> yellow | |
| combined[tc > 0] = 2 # tumor core -> green | |
| combined[et > 0] = 3 # enhancing tumor -> red | |
| return combined | |
| def find_best_slice(pred): | |
| """Pick the D-axis slice with the most total tumor voxels (TC+WT+ET), | |
| so small regions like TC/ET are actually visible instead of relying | |
| on the raw geometric middle slice.""" | |
| combined_mask = (pred[0] > 0) | (pred[1] > 0) | (pred[2] > 0) | |
| voxels_per_slice = combined_mask.sum(axis=(1, 2)) | |
| return int(voxels_per_slice.argmax()) | |
| def make_slice_figure(volume, pred, save_path="preview.png"): | |
| """3-panel region breakdown: WT, TC, ET overlaid on the FLAIR slice | |
| (no standalone plain-FLAIR panel).""" | |
| mid = find_best_slice(pred) | |
| flair_slice = volume[0][mid] # channel 0 = FLAIR, used only as anatomical background | |
| pred_tc, pred_wt, pred_et = pred[0][mid], pred[1][mid], pred[2][mid] | |
| rows = [ | |
| ("Whole Tumor (WT)", pred_wt), | |
| ("Tumor Core (TC)", pred_tc), | |
| ("Enhancing Tumor (ET)", pred_et), | |
| ] | |
| fig, axes = plt.subplots(1, 3, figsize=(15, 5)) | |
| for i, (region_name, region_slice) in enumerate(rows): | |
| axes[i].imshow(flair_slice, cmap="gray") | |
| axes[i].imshow(region_slice, cmap="Reds", alpha=0.5) | |
| axes[i].set_title(region_name, fontsize=13) | |
| axes[i].axis("off") | |
| plt.tight_layout() | |
| plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white") | |
| plt.close(fig) | |
| return save_path | |
| def make_combined_figure(volume, pred, save_path="combined.png"): | |
| mid = find_best_slice(pred) | |
| flair_slice = volume[0][mid] | |
| pred_combined = combine_regions(pred)[mid] | |
| fig, ax = plt.subplots(figsize=(7, 7)) | |
| ax.imshow(flair_slice, cmap="gray") | |
| ax.imshow(pred_combined, cmap=region_cmap, alpha=0.6, vmin=0, vmax=3) | |
| ax.set_title("Combined Prediction", fontsize=14) | |
| ax.axis("off") | |
| legend_elements = [ | |
| Patch(facecolor="yellow", label="Whole Tumor (WT)"), | |
| Patch(facecolor="green", label="Tumor Core (TC)"), | |
| Patch(facecolor="red", label="Enhancing Tumor (ET)"), | |
| ] | |
| ax.legend(handles=legend_elements, loc="lower right", fontsize=9, framealpha=0.8) | |
| plt.tight_layout() | |
| plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white") | |
| plt.close(fig) | |
| return save_path |