Spaces:
Runtime error
Runtime error
Formatting and examples
Browse files- README.md +3 -3
- app.py +32 -6
- examples/{spleen_46.nii.gz → spleen_1.nii.gz} +2 -2
- examples/spleen_11.nii.gz +3 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: Spleen Segmentation
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.1.1
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
title: Spleen Segmentation
|
| 3 |
+
emoji: 🩸
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.1.1
|
| 8 |
app_file: app.py
|
app.py
CHANGED
|
@@ -3,11 +3,17 @@ import gradio as gr
|
|
| 3 |
import torch
|
| 4 |
from monai import bundle
|
| 5 |
|
|
|
|
| 6 |
BUNDLE_NAME = 'spleen_ct_segmentation_v0.1.0'
|
| 7 |
BUNDLE_PATH = os.path.join(torch.hub.get_dir(), 'bundle', BUNDLE_NAME)
|
| 8 |
|
| 9 |
-
#examples
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
model, _, _ = bundle.load(
|
| 12 |
name = BUNDLE_NAME,
|
| 13 |
source = 'huggingface_hub',
|
|
@@ -15,12 +21,21 @@ model, _, _ = bundle.load(
|
|
| 15 |
load_ts_module=True,
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 19 |
|
|
|
|
| 20 |
parser = bundle.load_bundle_config(BUNDLE_PATH, 'inference.json')
|
| 21 |
-
preproc_transforms = parser.get_parsed_content(
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def predict(input_file, z_axis, model=model, device=device):
|
| 25 |
data = {'image': [input_file.name]}
|
| 26 |
data = preproc_transforms(data)
|
|
@@ -36,15 +51,26 @@ def predict(input_file, z_axis, model=model, device=device):
|
|
| 36 |
|
| 37 |
return input_image[0, :, :, z_axis], pred_image[0, :, :, z_axis]*255
|
| 38 |
|
|
|
|
| 39 |
iface = gr.Interface(
|
| 40 |
fn=predict,
|
| 41 |
inputs=[
|
| 42 |
-
gr.File(label='
|
| 43 |
gr.Slider(0, 200, label='z-axis', value=50)
|
| 44 |
],
|
| 45 |
outputs=['image', 'image'],
|
| 46 |
-
title='Segment the Spleen
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
iface.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
from monai import bundle
|
| 5 |
|
| 6 |
+
# Set the bundle name and download path
|
| 7 |
BUNDLE_NAME = 'spleen_ct_segmentation_v0.1.0'
|
| 8 |
BUNDLE_PATH = os.path.join(torch.hub.get_dir(), 'bundle', BUNDLE_NAME)
|
| 9 |
|
| 10 |
+
# Set up some examples from the test set for better user experience
|
| 11 |
+
examples = [
|
| 12 |
+
['examples/spleen_1.nii.gz', 50],
|
| 13 |
+
['examples/spleen_11.nii.gz', 50],
|
| 14 |
+
]
|
| 15 |
|
| 16 |
+
# Load the pretrained model from Hugging Face Hub
|
| 17 |
model, _, _ = bundle.load(
|
| 18 |
name = BUNDLE_NAME,
|
| 19 |
source = 'huggingface_hub',
|
|
|
|
| 21 |
load_ts_module=True,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Use GPU if available
|
| 25 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 26 |
|
| 27 |
+
# Load transforms and inferer directly from the bundle
|
| 28 |
parser = bundle.load_bundle_config(BUNDLE_PATH, 'inference.json')
|
| 29 |
+
preproc_transforms = parser.get_parsed_content(
|
| 30 |
+
'preprocessing',
|
| 31 |
+
lazy=True, eval_expr=True,instantiate=True
|
| 32 |
+
)
|
| 33 |
+
inferer = parser.get_parsed_content(
|
| 34 |
+
'inferer',
|
| 35 |
+
lazy=True, eval_expr=True, instantiate=True
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
# Define the prediction function
|
| 39 |
def predict(input_file, z_axis, model=model, device=device):
|
| 40 |
data = {'image': [input_file.name]}
|
| 41 |
data = preproc_transforms(data)
|
|
|
|
| 51 |
|
| 52 |
return input_image[0, :, :, z_axis], pred_image[0, :, :, z_axis]*255
|
| 53 |
|
| 54 |
+
# Set up the demo interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=predict,
|
| 57 |
inputs=[
|
| 58 |
+
gr.File(label='input file'),
|
| 59 |
gr.Slider(0, 200, label='z-axis', value=50)
|
| 60 |
],
|
| 61 |
outputs=['image', 'image'],
|
| 62 |
+
title='Segment the Spleen using MONAI!',
|
| 63 |
+
description="""
|
| 64 |
+
## 🚀 To run
|
| 65 |
+
Upload a abdominal CT scan, or try one of the examples below!
|
| 66 |
+
|
| 67 |
+
More details on the model can be found [here!](https://huggingface.co/katielink/spleen_ct_segmentation_v0.1.0)
|
| 68 |
+
|
| 69 |
+
## ⚠️ Disclaimer
|
| 70 |
+
Not to be used for diagnostic purposes.
|
| 71 |
+
""",
|
| 72 |
+
examples=examples,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# Launch the demo
|
| 76 |
iface.launch()
|
examples/{spleen_46.nii.gz → spleen_1.nii.gz}
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a502f7e81d6b3742f5100e501b4e80180264d00e9aa04dfee43d821b1c6b07e7
|
| 3 |
+
size 9862639
|
examples/spleen_11.nii.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14d3edfadaa761d52a468ec06423fd4a48a2028d60c053629d0b6d378917f87f
|
| 3 |
+
size 47834678
|