Remove standalone FLAIR panel, keep 3-region breakdown only
Browse files- app.py +29 -33
- inference.py +17 -15
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
from model import MedSegNet
|
| 6 |
from inference import preprocess_volume, run_inference, make_slice_figure, make_combined_figure
|
| 7 |
from report import build_pdf_report
|
|
@@ -29,15 +28,14 @@ model.eval()
|
|
| 29 |
|
| 30 |
|
| 31 |
@spaces.GPU
|
| 32 |
-
def run(t1_file, t1ce_file, t2_file
|
| 33 |
-
if not all([t1_file, t1ce_file, t2_file
|
| 34 |
-
raise gr.Error("Please upload all 4 modalities: T1, T1ce,
|
| 35 |
|
| 36 |
model.to("cuda")
|
| 37 |
volume, affine = preprocess_volume(t1_file, t1ce_file, t2_file, flair_file)
|
| 38 |
pred = run_inference(model, volume, "cuda")
|
| 39 |
|
| 40 |
-
# simple volumetric stats (voxel counts -> approx cm^3 assuming 1mm^3 voxels post-crop)
|
| 41 |
tc_voxels = int(pred[0].sum())
|
| 42 |
wt_voxels = int(pred[1].sum())
|
| 43 |
et_voxels = int(pred[2].sum())
|
|
@@ -58,10 +56,10 @@ def run(t1_file, t1ce_file, t2_file, flair_file):
|
|
| 58 |
|
| 59 |
def load_example():
|
| 60 |
return (
|
|
|
|
| 61 |
"examples/BRATS_001_0001.nii.gz", # T1
|
| 62 |
"examples/BRATS_001_0002.nii.gz", # T1ce
|
| 63 |
"examples/BRATS_001_0003.nii.gz", # T2
|
| 64 |
-
"examples/BRATS_001_0000.nii.gz", # FLAIR
|
| 65 |
)
|
| 66 |
|
| 67 |
|
|
@@ -73,58 +71,56 @@ theme = gr.themes.Soft(
|
|
| 73 |
)
|
| 74 |
|
| 75 |
with gr.Blocks(title="MedSegNet — 3D Brain Tumor Segmentation", theme=theme) as demo:
|
| 76 |
-
gr.Markdown(
|
| 77 |
-
"""
|
| 78 |
-
# 🧠 MedSegNet
|
| 79 |
-
### Transformer-based 3D Brain Tumor Segmentation from Multi-Modal MRI
|
| 80 |
-
|
| 81 |
-
Final Year Project Demonstration — SegFormer3D architecture trained on the BraTS2017 dataset.
|
| 82 |
-
Upload four co-registered MRI modalities (T1, T1ce, T2, FLAIR) to generate an automated
|
| 83 |
-
tumor segmentation across three clinically relevant regions.
|
| 84 |
-
"""
|
| 85 |
-
)
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=1):
|
| 89 |
gr.Markdown("#### 1. Upload MRI Volumes")
|
| 90 |
gr.Markdown("*(.nii or .nii.gz format, all 4 modalities required)*")
|
|
|
|
| 91 |
t1 = gr.File(label="T1")
|
| 92 |
t1ce = gr.File(label="T1ce")
|
| 93 |
t2 = gr.File(label="T2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
flair = gr.File(label="FLAIR")
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
example_btn = gr.Button("📁 Load Example Case", variant="secondary")
|
| 98 |
submit_btn = gr.Button("▶ Run Segmentation", variant="primary")
|
| 99 |
|
| 100 |
-
gr.
|
| 101 |
-
"""
|
| 102 |
-
---
|
| 103 |
-
#### About
|
| 104 |
-
**Architecture:** SegFormer3D (hierarchical transformer encoder + lightweight MLP decoder)
|
| 105 |
-
**Training data:** BraTS2017
|
| 106 |
-
**Output regions:**
|
| 107 |
-
- 🟡 Whole Tumor (WT) — total abnormal tissue
|
| 108 |
-
- 🟢 Tumor Core (TC) — solid tumor mass
|
| 109 |
-
- 🔴 Enhancing Tumor (ET) — actively enhancing region
|
| 110 |
-
|
| 111 |
-
*Research/demonstration output only — not for clinical diagnosis.*
|
| 112 |
-
"""
|
| 113 |
-
)
|
| 114 |
|
| 115 |
with gr.Column(scale=2):
|
| 116 |
gr.Markdown("#### 2. Segmentation Result")
|
| 117 |
combined_image = gr.Image(label="Combined Segmentation Overlay")
|
| 118 |
stats_output = gr.Markdown()
|
| 119 |
-
pdf_output = gr.File(label="📄 Download Full PDF Report")
|
| 120 |
gr.Markdown("#### Region Breakdown")
|
| 121 |
output_image = gr.Image(label="FLAIR / WT / TC / ET")
|
| 122 |
|
| 123 |
-
example_btn.click(fn=load_example, outputs=[t1, t1ce, t2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
submit_btn.click(
|
| 126 |
fn=run,
|
| 127 |
-
inputs=[t1, t1ce, t2
|
| 128 |
outputs=[combined_image, output_image, stats_output, pdf_output],
|
| 129 |
)
|
| 130 |
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
|
|
|
| 4 |
from model import MedSegNet
|
| 5 |
from inference import preprocess_volume, run_inference, make_slice_figure, make_combined_figure
|
| 6 |
from report import build_pdf_report
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
@spaces.GPU
|
| 31 |
+
def run(flair_file, t1_file, t1ce_file, t2_file):
|
| 32 |
+
if not all([flair_file, t1_file, t1ce_file, t2_file]):
|
| 33 |
+
raise gr.Error("Please upload all 4 modalities: FLAIR, T1, T1ce, and T2.")
|
| 34 |
|
| 35 |
model.to("cuda")
|
| 36 |
volume, affine = preprocess_volume(t1_file, t1ce_file, t2_file, flair_file)
|
| 37 |
pred = run_inference(model, volume, "cuda")
|
| 38 |
|
|
|
|
| 39 |
tc_voxels = int(pred[0].sum())
|
| 40 |
wt_voxels = int(pred[1].sum())
|
| 41 |
et_voxels = int(pred[2].sum())
|
|
|
|
| 56 |
|
| 57 |
def load_example():
|
| 58 |
return (
|
| 59 |
+
"examples/BRATS_001_0000.nii.gz", # FLAIR
|
| 60 |
"examples/BRATS_001_0001.nii.gz", # T1
|
| 61 |
"examples/BRATS_001_0002.nii.gz", # T1ce
|
| 62 |
"examples/BRATS_001_0003.nii.gz", # T2
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
with gr.Blocks(title="MedSegNet — 3D Brain Tumor Segmentation", theme=theme) as demo:
|
| 74 |
+
gr.Markdown("# MedSegNet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column(scale=1):
|
| 78 |
gr.Markdown("#### 1. Upload MRI Volumes")
|
| 79 |
gr.Markdown("*(.nii or .nii.gz format, all 4 modalities required)*")
|
| 80 |
+
flair = gr.File(label="FLAIR")
|
| 81 |
t1 = gr.File(label="T1")
|
| 82 |
t1ce = gr.File(label="T1ce")
|
| 83 |
t2 = gr.File(label="T2")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
example_btn = gr.Button("📁 Load Example Case", variant="secondary")
|
| 87 |
+
submit_btn = gr.Button("▶ Run Segmentation", variant="primary")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
with gr.Column(scale=1):
|
| 91 |
+
gr.Markdown("#### 1. Upload MRI Volumes")
|
| 92 |
+
gr.Markdown("*(.nii or .nii.gz format, all 4 modalities required)*")
|
| 93 |
flair = gr.File(label="FLAIR")
|
| 94 |
+
t1 = gr.File(label="T1")
|
| 95 |
+
t1ce = gr.File(label="T1ce")
|
| 96 |
+
t2 = gr.File(label="T2")
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
example_btn = gr.Button("📁 Load Example Case", variant="secondary")
|
| 100 |
submit_btn = gr.Button("▶ Run Segmentation", variant="primary")
|
| 101 |
|
| 102 |
+
pdf_output = gr.DownloadButton("📄 Download PDF Report", variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
with gr.Column(scale=2):
|
| 105 |
gr.Markdown("#### 2. Segmentation Result")
|
| 106 |
combined_image = gr.Image(label="Combined Segmentation Overlay")
|
| 107 |
stats_output = gr.Markdown()
|
|
|
|
| 108 |
gr.Markdown("#### Region Breakdown")
|
| 109 |
output_image = gr.Image(label="FLAIR / WT / TC / ET")
|
| 110 |
|
| 111 |
+
example_btn.click(fn=load_example, outputs=[flair, t1, t1ce, t2])
|
| 112 |
+
|
| 113 |
+
submit_btn.click(
|
| 114 |
+
fn=run,
|
| 115 |
+
inputs=[flair, t1, t1ce, t2],
|
| 116 |
+
outputs=[combined_image, output_image, stats_output, pdf_output],
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
example_btn.click(fn=load_example, outputs=[flair, t1, t1ce, t2])
|
| 120 |
|
| 121 |
submit_btn.click(
|
| 122 |
fn=run,
|
| 123 |
+
inputs=[flair, t1, t1ce, t2],
|
| 124 |
outputs=[combined_image, output_image, stats_output, pdf_output],
|
| 125 |
)
|
| 126 |
|
inference.py
CHANGED
|
@@ -135,23 +135,25 @@ def make_slice_figure(volume, pred, save_path="preview.png"):
|
|
| 135 |
return save_path
|
| 136 |
|
| 137 |
|
| 138 |
-
def
|
| 139 |
mid = find_best_slice(pred)
|
| 140 |
-
flair_slice = volume[0][mid]
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
legend_elements = [
|
| 150 |
-
Patch(facecolor="yellow", label="Whole Tumor (WT)"),
|
| 151 |
-
Patch(facecolor="green", label="Tumor Core (TC)"),
|
| 152 |
-
Patch(facecolor="red", label="Enhancing Tumor (ET)"),
|
| 153 |
]
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
plt.tight_layout()
|
| 157 |
plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
|
|
|
|
| 135 |
return save_path
|
| 136 |
|
| 137 |
|
| 138 |
+
def make_slice_figure(volume, pred, save_path="preview.png"):
|
| 139 |
mid = find_best_slice(pred)
|
| 140 |
+
flair_slice = volume[0][mid] # channel 0 = FLAIR
|
| 141 |
+
|
| 142 |
+
pred_tc, pred_wt, pred_et = pred[0][mid], pred[1][mid], pred[2][mid]
|
| 143 |
+
|
| 144 |
+
rows = [
|
| 145 |
+
("Whole Tumor (WT)", pred_wt),
|
| 146 |
+
("Tumor Core (TC)", pred_tc),
|
| 147 |
+
("Enhancing Tumor (ET)", pred_et),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
]
|
| 149 |
+
|
| 150 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
| 151 |
+
|
| 152 |
+
for i, (region_name, region_slice) in enumerate(rows):
|
| 153 |
+
axes[i].imshow(flair_slice, cmap="gray")
|
| 154 |
+
axes[i].imshow(region_slice, cmap="Reds", alpha=0.5)
|
| 155 |
+
axes[i].set_title(region_name, fontsize=13)
|
| 156 |
+
axes[i].axis("off")
|
| 157 |
|
| 158 |
plt.tight_layout()
|
| 159 |
plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
|