Redesign UI for demo presentation with example case and stats
Browse files- app.py +81 -15
- examples/BRATS_001_0000.nii.gz +3 -0
- examples/BRATS_001_0001.nii.gz +3 -0
- examples/BRATS_001_0002.nii.gz +3 -0
- examples/BRATS_001_0003.nii.gz +3 -0
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -29,37 +30,102 @@ model.eval()
|
|
| 29 |
|
| 30 |
@spaces.GPU
|
| 31 |
def run(t1_file, t1ce_file, t2_file, flair_file):
|
|
|
|
|
|
|
|
|
|
| 32 |
model.to("cuda")
|
| 33 |
volume, affine = preprocess_volume(t1_file, t1ce_file, t2_file, flair_file)
|
| 34 |
pred = run_inference(model, volume, "cuda")
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
fig_path = make_slice_figure(volume, pred)
|
| 38 |
combined_path = make_combined_figure(volume, pred)
|
| 39 |
pdf_path = build_pdf_report(volume, pred, fig_path, combined_path)
|
| 40 |
-
return fig_path, combined_path, pdf_path
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
combined_image = gr.Image(label="Combined Segmentation")
|
| 57 |
-
pdf_output = gr.File(label="π Download PDF Report", visible=True)
|
| 58 |
|
| 59 |
submit_btn.click(
|
| 60 |
fn=run,
|
| 61 |
inputs=[t1, t1ce, t2, flair],
|
| 62 |
-
outputs=[output_image,
|
| 63 |
)
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 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
|
|
|
|
| 30 |
|
| 31 |
@spaces.GPU
|
| 32 |
def run(t1_file, t1ce_file, t2_file, flair_file):
|
| 33 |
+
if not all([t1_file, t1ce_file, t2_file, flair_file]):
|
| 34 |
+
raise gr.Error("Please upload all 4 modalities: T1, T1ce, T2, and FLAIR.")
|
| 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())
|
| 44 |
+
|
| 45 |
+
stats_md = (
|
| 46 |
+
f"### Predicted Region Volumes (voxels)\n"
|
| 47 |
+
f"- **Whole Tumor (WT):** {wt_voxels:,}\n"
|
| 48 |
+
f"- **Tumor Core (TC):** {tc_voxels:,}\n"
|
| 49 |
+
f"- **Enhancing Tumor (ET):** {et_voxels:,}\n"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
fig_path = make_slice_figure(volume, pred)
|
| 53 |
combined_path = make_combined_figure(volume, pred)
|
| 54 |
pdf_path = build_pdf_report(volume, pred, fig_path, combined_path)
|
|
|
|
| 55 |
|
| 56 |
+
return combined_path, fig_path, stats_md, pdf_path
|
| 57 |
+
|
| 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 |
+
|
| 68 |
+
theme = gr.themes.Soft(
|
| 69 |
+
primary_hue="blue",
|
| 70 |
+
secondary_hue="cyan",
|
| 71 |
+
).set(
|
| 72 |
+
body_background_fill="*neutral_50",
|
| 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.Markdown(
|
| 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, flair])
|
|
|
|
|
|
|
| 124 |
|
| 125 |
submit_btn.click(
|
| 126 |
fn=run,
|
| 127 |
inputs=[t1, t1ce, t2, flair],
|
| 128 |
+
outputs=[combined_image, output_image, stats_output, pdf_output],
|
| 129 |
)
|
| 130 |
|
| 131 |
demo.launch()
|
examples/BRATS_001_0000.nii.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9f4a765ec3842accf41f7dc49d945bd1541bde85a70d2536d4917adf5358160d
|
| 3 |
+
size 2639636
|
examples/BRATS_001_0001.nii.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:593f742b6580476651f8f65376e7b60b9ecd5743b612361636297321327baed6
|
| 3 |
+
size 2464524
|
examples/BRATS_001_0002.nii.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8298fcc2168f539c2601f87daa43462284bb268d92bfd87188a6d7c54575e8a2
|
| 3 |
+
size 2717769
|
examples/BRATS_001_0003.nii.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:00f61919885beb4ede1e433db9c6300498ff1ea82d3183280219f68b5b527a73
|
| 3 |
+
size 2766217
|