Spaces:
Runtime error
Runtime error
Duplicate from fhatje/glomseg
Browse filesCo-authored-by: Favian Hatje <fhatje@users.noreply.huggingface.co>
- .gitattributes +34 -0
- .gitignore +1 -0
- README.md +14 -0
- app.py +123 -0
- example_images/ENSG00000010219_kidney_0.png +0 -0
- example_images/ENSG00000010219_kidney_5.png +0 -0
- example_images/ENSG00000049449_kidney_0.png +0 -0
- example_images/ENSG00000049449_kidney_3.png +0 -0
- example_images/ENSG00000122257_kidney_0.png +0 -0
- example_images/ENSG00000122257_kidney_3.png +0 -0
- example_images/ENSG00000122257_kidney_5.png +0 -0
- example_images/ENSG00000122591_kidney_0.png +0 -0
- example_images/ENSG00000124102_kidney_4.png +0 -0
- example_images/ENSG00000124102_kidney_5.png +0 -0
- example_images/ENSG00000126500_kidney_0.png +0 -0
- example_images/ENSG00000137285_kidney_5.png +0 -0
- example_images/ENSG00000155368_kidney_2.png +0 -0
- example_images/ENSG00000155622_kidney_1.png +0 -0
- example_images/ENSG00000155622_kidney_2.png +0 -0
- example_images/ENSG00000163283_kidney_1.png +0 -0
- example_images/ENSG00000165490_kidney_3.png +0 -0
- example_images/ENSG00000165490_kidney_4.png +0 -0
- example_images/ENSG00000172086_kidney_0.png +0 -0
- example_images/ENSG00000172086_kidney_1.png +0 -0
- example_images/ENSG00000172086_kidney_2.png +0 -0
- example_images/ENSG00000180616_kidney_0.png +0 -0
- example_images/ENSG00000180616_kidney_2.png +0 -0
- example_images/ENSG00000183018_kidney_1.png +0 -0
- example_images/ENSG00000251537_kidney_2.png +0 -0
- requirements.txt +3 -0
- unetpp_b4_th60_d9414.pkl +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.vscode
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Glomseg
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.9
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
duplicated_from: fhatje/glomseg
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../main.ipynb.
|
| 2 |
+
|
| 3 |
+
# %% auto 0
|
| 4 |
+
__all__ = ['ORGAN', 'IMAGE_SIZE', 'MODEL_NAME', 'THRESHOLD', 'CODES', 'learn', 'title', 'description', 'examples',
|
| 5 |
+
'interpretation', 'demo', 'x_getter', 'y_getter', 'splitter', 'make3D', 'predict', 'infer',
|
| 6 |
+
'remove_small_segs', 'to_oberlay_image']
|
| 7 |
+
|
| 8 |
+
# %% ../main.ipynb 1
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import skimage
|
| 12 |
+
from fastai.vision.all import *
|
| 13 |
+
import segmentation_models_pytorch as smp
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
|
| 17 |
+
# %% ../main.ipynb 2
|
| 18 |
+
ORGAN = "kidney"
|
| 19 |
+
IMAGE_SIZE = 512
|
| 20 |
+
MODEL_NAME = "unetpp_b4_th60_d9414.pkl"
|
| 21 |
+
THRESHOLD = float(MODEL_NAME.split("_")[2][2:]) / 100.
|
| 22 |
+
CODES = ["Background", "FTU"] # FTU = functional tissue unit
|
| 23 |
+
|
| 24 |
+
# %% ../main.ipynb 3
|
| 25 |
+
def x_getter(r): return r["fnames"]
|
| 26 |
+
def y_getter(r):
|
| 27 |
+
rle = r["rle"]
|
| 28 |
+
shape = (int(r["img_height"]), int(r["img_width"]))
|
| 29 |
+
return rle_decode(rle, shape).T
|
| 30 |
+
def splitter(model):
|
| 31 |
+
enc_params = L(model.encoder.parameters())
|
| 32 |
+
dec_params = L(model.decoder.parameters())
|
| 33 |
+
sg_params = L(model.segmentation_head.parameters())
|
| 34 |
+
untrained_params = L([*dec_params, *sg_params])
|
| 35 |
+
return L([enc_params, untrained_params])
|
| 36 |
+
|
| 37 |
+
# %% ../main.ipynb 4
|
| 38 |
+
learn = load_learner(MODEL_NAME)
|
| 39 |
+
|
| 40 |
+
# %% ../main.ipynb 5
|
| 41 |
+
def make3D(t: np.array) -> np.array:
|
| 42 |
+
t = np.expand_dims(t, axis=2)
|
| 43 |
+
t = np.concatenate((t,t,t), axis=2)
|
| 44 |
+
return t
|
| 45 |
+
|
| 46 |
+
def predict(fn, cutoff_area=200):
|
| 47 |
+
data = infer(fn)
|
| 48 |
+
data = remove_small_segs(data, cutoff_area=cutoff_area)
|
| 49 |
+
return to_oberlay_image(data), data["df"]
|
| 50 |
+
|
| 51 |
+
def infer(fn):
|
| 52 |
+
img = PILImage.create(fn)
|
| 53 |
+
tf_img,_,_,preds = learn.predict(img, with_input=True)
|
| 54 |
+
mask = (F.softmax(preds.float(), dim=0)>THRESHOLD).int()[1]
|
| 55 |
+
mask = np.array(mask, dtype=np.uint8)
|
| 56 |
+
resized_image = Image.fromarray(tf_img.numpy().transpose(1, 2, 0).astype(np.uint8)).resize(img.shape)
|
| 57 |
+
resized_image = np.array(resized_image)
|
| 58 |
+
return {
|
| 59 |
+
"tf_image": tf_img.numpy().transpose(1, 2, 0).astype(np.uint8),
|
| 60 |
+
"tf_mask": mask
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
def remove_small_segs(data, cutoff_area=250):
|
| 64 |
+
labeled_mask = skimage.measure.label(data["tf_mask"])
|
| 65 |
+
props = skimage.measure.regionprops(labeled_mask)
|
| 66 |
+
df = {"Glomerulus":[], "Area (in px)":[]}
|
| 67 |
+
for i, prop in enumerate(props):
|
| 68 |
+
if prop.area < cutoff_area:
|
| 69 |
+
labeled_mask[labeled_mask==i+1] = 0
|
| 70 |
+
continue
|
| 71 |
+
df["Glomerulus"].append(len(df["Glomerulus"]) + 1)
|
| 72 |
+
df["Area (in px)"].append(prop.area)
|
| 73 |
+
labeled_mask[labeled_mask>0] = 1
|
| 74 |
+
data["tf_mask"] = labeled_mask.astype(np.uint8)
|
| 75 |
+
data["df"] = pd.DataFrame(df)
|
| 76 |
+
return data
|
| 77 |
+
|
| 78 |
+
def to_oberlay_image(data):
|
| 79 |
+
img, msk = data["tf_image"], data["tf_mask"]
|
| 80 |
+
msk_im = np.zeros_like(img)
|
| 81 |
+
# rgb code: 255, 80, 80
|
| 82 |
+
msk_im[:,:,0] = 255
|
| 83 |
+
msk_im[:,:,1] = 80
|
| 84 |
+
msk_im[:,:,2] = 80
|
| 85 |
+
img = Image.fromarray(img).convert("RGBA")
|
| 86 |
+
msk_im = Image.fromarray(msk_im).convert("RGBA")
|
| 87 |
+
msk = Image.fromarray((msk*255*0.5).astype(np.uint8))
|
| 88 |
+
|
| 89 |
+
img.paste(msk_im, (0, 0), msk, )
|
| 90 |
+
return img
|
| 91 |
+
|
| 92 |
+
# %% ../main.ipynb 6
|
| 93 |
+
title = "Glomerulus Segmentation"
|
| 94 |
+
description = """
|
| 95 |
+
A web app that segments glomeruli in histological kidney slices!
|
| 96 |
+
|
| 97 |
+
The model deployed here is a [UNet++](https://arxiv.org/abs/1807.10165) with an [efficientnet-b4](https://arxiv.org/abs/1905.11946) encoder from the [segmentation_models_pytorch](https://github.com/qubvel/segmentation_models.pytorch) library.
|
| 98 |
+
|
| 99 |
+
The provided example images are random subset of kidney slices from the [Human Protein Atlas](https://www.proteinatlas.org/). These have been collected separately from model training and have neither been part of the training, validation nor test set.
|
| 100 |
+
|
| 101 |
+
See corresponding [blog post](https://fhatje.github.io/posts/glomseg/train_model.html).
|
| 102 |
+
"""
|
| 103 |
+
#article="<p style='text-align: center'><a href='Blog post URL' target='_blank'>Blog post</a></p>"
|
| 104 |
+
examples = [str(p) for p in get_image_files("example_images")]
|
| 105 |
+
interpretation='default'
|
| 106 |
+
|
| 107 |
+
# %% ../main.ipynb 7
|
| 108 |
+
demo = gr.Interface(
|
| 109 |
+
fn=predict,
|
| 110 |
+
inputs=gr.components.Image(shape=(IMAGE_SIZE, IMAGE_SIZE)),
|
| 111 |
+
outputs=[gr.components.Image(), gr.components.DataFrame()],
|
| 112 |
+
title=title,
|
| 113 |
+
description=description,
|
| 114 |
+
examples=examples,
|
| 115 |
+
interpretation=interpretation,
|
| 116 |
+
# Fixes error when set to True:
|
| 117 |
+
# https://github.com/gradio-app/gradio/pull/1949
|
| 118 |
+
# but generated file names are too long
|
| 119 |
+
_api_mode=False
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# %% ../main.ipynb 9
|
| 123 |
+
demo.launch()
|
example_images/ENSG00000010219_kidney_0.png
ADDED
|
example_images/ENSG00000010219_kidney_5.png
ADDED
|
example_images/ENSG00000049449_kidney_0.png
ADDED
|
example_images/ENSG00000049449_kidney_3.png
ADDED
|
example_images/ENSG00000122257_kidney_0.png
ADDED
|
example_images/ENSG00000122257_kidney_3.png
ADDED
|
example_images/ENSG00000122257_kidney_5.png
ADDED
|
example_images/ENSG00000122591_kidney_0.png
ADDED
|
example_images/ENSG00000124102_kidney_4.png
ADDED
|
example_images/ENSG00000124102_kidney_5.png
ADDED
|
example_images/ENSG00000126500_kidney_0.png
ADDED
|
example_images/ENSG00000137285_kidney_5.png
ADDED
|
example_images/ENSG00000155368_kidney_2.png
ADDED
|
example_images/ENSG00000155622_kidney_1.png
ADDED
|
example_images/ENSG00000155622_kidney_2.png
ADDED
|
example_images/ENSG00000163283_kidney_1.png
ADDED
|
example_images/ENSG00000165490_kidney_3.png
ADDED
|
example_images/ENSG00000165490_kidney_4.png
ADDED
|
example_images/ENSG00000172086_kidney_0.png
ADDED
|
example_images/ENSG00000172086_kidney_1.png
ADDED
|
example_images/ENSG00000172086_kidney_2.png
ADDED
|
example_images/ENSG00000180616_kidney_0.png
ADDED
|
example_images/ENSG00000180616_kidney_2.png
ADDED
|
example_images/ENSG00000183018_kidney_1.png
ADDED
|
example_images/ENSG00000251537_kidney_2.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastai
|
| 2 |
+
scikit-image
|
| 3 |
+
segmentation-models-pytorch
|
unetpp_b4_th60_d9414.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a2f767a32b632552df1b2bdf099229e06335d543640753cee7f449845507e4e9
|
| 3 |
+
size 128286811
|