Committed_For_Exemple1
Browse files- .gitattributes +2 -0
- app.py +127 -0
- examples/flair.nii +3 -0
- examples/t1.nii +3 -0
- examples/t1ce.nii +3 -0
- examples/t2.nii +3 -0
- last_state_dict .pth +3 -0
- requirements.txt +12 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.nii filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
last_state_dict[[:space:]].pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import numpy as np
|
| 3 |
+
import nibabel as nib
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from monai.transforms import (
|
| 9 |
+
Compose, NormalizeIntensityd,
|
| 10 |
+
ResizeWithPadOrCropd, ToTensord
|
| 11 |
+
)
|
| 12 |
+
from monai.networks.nets import SwinUNETR
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
model = SwinUNETR(img_size=(128, 128, 128),
|
| 16 |
+
in_channels=4,
|
| 17 |
+
out_channels=3,
|
| 18 |
+
feature_size=48,
|
| 19 |
+
use_checkpoint=True)
|
| 20 |
+
|
| 21 |
+
model.load_dtate_dict(torch.load("last_state_dict .pth",map_location=torch.device('cpu')))
|
| 22 |
+
|
| 23 |
+
model.eval()
|
| 24 |
+
val_transform = Compose([
|
| 25 |
+
NormalizeIntensityd(keys="image", nonzero=True, channel_wise=True),
|
| 26 |
+
ResizeWithPadOrCropd(keys="image", spatial_size=(128, 128, 128)),
|
| 27 |
+
ToTensord(keys="image")
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
# This will be set in predict_and_store
|
| 31 |
+
cached_images = []
|
| 32 |
+
cached_masks = []
|
| 33 |
+
|
| 34 |
+
# Helper to generate an overlay image for one slice
|
| 35 |
+
def get_overlay_figure(image, mask, slice_index):
|
| 36 |
+
img_slice = image[:, :, slice_index]
|
| 37 |
+
img_slice = (img_slice - img_slice.min()) / (img_slice.max() - img_slice.min())
|
| 38 |
+
img_rgb = np.stack([img_slice]*3, axis=-1)
|
| 39 |
+
|
| 40 |
+
mask_rgb = np.zeros_like(img_rgb)
|
| 41 |
+
mask_rgb[mask[:, :, slice_index] == 2] = [0, 0, 1] # Blue: WT
|
| 42 |
+
mask_rgb[mask[:, :, slice_index] == 1] = [0, 1, 0] # Green: TC
|
| 43 |
+
mask_rgb[mask[:, :, slice_index] == 4] = [1, 0, 0] # Red: ET
|
| 44 |
+
|
| 45 |
+
overlay = np.clip((1 - 0.4) * img_rgb + 0.4 * mask_rgb, 0, 1)
|
| 46 |
+
|
| 47 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 48 |
+
ax.imshow(overlay)
|
| 49 |
+
ax.axis("off")
|
| 50 |
+
ax.set_title(f"Segmentation Overlay - Slice {slice_index}")
|
| 51 |
+
buf = io.BytesIO()
|
| 52 |
+
plt.savefig(buf, format='png')
|
| 53 |
+
buf.seek(0)
|
| 54 |
+
plt.close(fig)
|
| 55 |
+
return Image.open(buf)
|
| 56 |
+
|
| 57 |
+
# Prediction function that stores the processed image and mask for slider use
|
| 58 |
+
def predict_and_store(flair_file, t1_file, t1ce_file, t2_file):
|
| 59 |
+
global cached_images, cached_masks
|
| 60 |
+
|
| 61 |
+
flair = nib.load(flair_file.name).get_fdata()
|
| 62 |
+
t1 = nib.load(t1_file.name).get_fdata()
|
| 63 |
+
t1ce = nib.load(t1ce_file.name).get_fdata()
|
| 64 |
+
t2 = nib.load(t2_file.name).get_fdata()
|
| 65 |
+
|
| 66 |
+
image = np.stack([flair, t1, t1ce, t2], axis=0)
|
| 67 |
+
data = {"image": image}
|
| 68 |
+
data = val_transform(data)
|
| 69 |
+
image_tensor = data["image"].unsqueeze(0).to(device)
|
| 70 |
+
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
output = model(image_tensor)
|
| 73 |
+
prob = torch.sigmoid(output)
|
| 74 |
+
seg = prob[0].detach().cpu().numpy()
|
| 75 |
+
seg = (seg > 0.5).astype(np.int8)
|
| 76 |
+
seg_out = np.zeros((seg.shape[1], seg.shape[2], seg.shape[3]))
|
| 77 |
+
seg_out[seg[1] == 1] = 2
|
| 78 |
+
seg_out[seg[0] == 1] = 1
|
| 79 |
+
seg_out[seg[2] == 1] = 4
|
| 80 |
+
|
| 81 |
+
cached_images = image_tensor.cpu().numpy()[0, 0]
|
| 82 |
+
cached_masks = seg_out
|
| 83 |
+
return f"Segmentation done. Use the slider to browse slices."
|
| 84 |
+
|
| 85 |
+
# Function to get the overlay for a specific slice
|
| 86 |
+
def get_slice_overlay(slice_index):
|
| 87 |
+
global cached_images, cached_masks
|
| 88 |
+
if len(cached_images) == 0:
|
| 89 |
+
return None
|
| 90 |
+
return get_overlay_figure(cached_images, cached_masks, slice_index)
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as iface:
|
| 93 |
+
gr.Markdown("### 🧠 SwinUNETR Brain Tumor Segmentation Viewer")
|
| 94 |
+
|
| 95 |
+
gr.Markdown("#### 📂 Upload MRI Modalities")
|
| 96 |
+
with gr.Row():
|
| 97 |
+
flair = gr.File(file_types=[".nii", ".nii.gz"], label="FLAIR (.nii)")
|
| 98 |
+
t1 = gr.File(file_types=[".nii", ".nii.gz"], label="T1 (.nii)")
|
| 99 |
+
t1ce = gr.File(file_types=[".nii", ".nii.gz"], label="T1ce (.nii)")
|
| 100 |
+
t2 = gr.File(file_types=[".nii", ".nii.gz"], label="T2 (.nii)")
|
| 101 |
+
|
| 102 |
+
# Ajout de la section exemples
|
| 103 |
+
gr.Markdown("#### 🧪 Or use example data")
|
| 104 |
+
examples = gr.Examples(
|
| 105 |
+
examples=[
|
| 106 |
+
["examples/flair.nii", "examples/t1.nii", "examples/t1ce.nii", "examples/t2.nii"]
|
| 107 |
+
],
|
| 108 |
+
inputs=[flair, t1, t1ce, t2],
|
| 109 |
+
label="Example MRI files"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
output_msg = gr.Textbox(label="📣 Status")
|
| 113 |
+
run_button = gr.Button("▶️ Run Segmentation")
|
| 114 |
+
|
| 115 |
+
run_button.click(fn=predict_and_store,
|
| 116 |
+
inputs=[flair, t1, t1ce, t2],
|
| 117 |
+
outputs=output_msg)
|
| 118 |
+
|
| 119 |
+
gr.Markdown("### 🖼️ View Slices with Overlay")
|
| 120 |
+
slice_slider = gr.Slider(minimum=0, maximum=127, value=64, step=1, label="Slice Index")
|
| 121 |
+
slice_image = gr.Image(type="pil", label="Segmentation Overlay")
|
| 122 |
+
|
| 123 |
+
slice_slider.change(fn=get_slice_overlay,
|
| 124 |
+
inputs=slice_slider,
|
| 125 |
+
outputs=slice_image)
|
| 126 |
+
|
| 127 |
+
iface.launch()
|
examples/flair.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:69f04d7db20f5fd64f8616bcda2079573253208850102b86c7bc86dbdab1e08d
|
| 3 |
+
size 17858880
|
examples/t1.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0f30a8d82039ae19ca79611ff6809b7b6e839ef2a5580f48c6c976bbbb3ed976
|
| 3 |
+
size 17858880
|
examples/t1ce.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d172fcebe1a37782706f2e11df73c6cbb46f79b8261a2b3188d7ad377cd24533
|
| 3 |
+
size 17858880
|
examples/t2.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:13747faa70e519e3a27fd643f526377132a65c8da12465048535301feaa33db6
|
| 3 |
+
size 17858880
|
last_state_dict .pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f1e23bd9e8f68a7d358f4aa0269692b838227e073e698132f7642d99572c6d0
|
| 3 |
+
size 256366028
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Interface
|
| 2 |
+
gradio
|
| 3 |
+
matplotlib
|
| 4 |
+
Pillow
|
| 5 |
+
|
| 6 |
+
# Traitement d'image médicale
|
| 7 |
+
nibabel
|
| 8 |
+
monai
|
| 9 |
+
|
| 10 |
+
# Deep Learning
|
| 11 |
+
torch
|
| 12 |
+
numpy
|