Spaces:
Runtime error
Runtime error
Apple
commited on
Commit
·
438403f
0
Parent(s):
first commit
Browse files- .DS_Store +0 -0
- README.md +13 -0
- app.py +19 -0
- functions.py +106 -0
- images/.DS_Store +0 -0
- images/cell_membrane_segmentation_examples/Example_1.png +0 -0
- images/cell_membrane_segmentation_examples/Example_2.png +0 -0
- requirements.txt +89 -0
- scaler.gz +0 -0
- setup.sh +8 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Membrane Segmentor
|
3 |
+
emoji: 🏃
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: streamlit
|
7 |
+
sdk_version: 1.17.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
# devolearn-web
|
13 |
+
devolearn models deployed on a webapp
|
app.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import functions
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
from PIL import Image
|
6 |
+
from pathlib import Path
|
7 |
+
import joblib
|
8 |
+
|
9 |
+
import numpy as np
|
10 |
+
import cv2
|
11 |
+
import onnxruntime as ort
|
12 |
+
import imutils
|
13 |
+
# import matplotlib.pyplot as plt
|
14 |
+
import pandas as pd
|
15 |
+
import plotly.express as px
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
functions.cell_membrane_segmentation()
|
functions.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from PIL import Image
|
5 |
+
from pathlib import Path
|
6 |
+
import joblib
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import cv2
|
10 |
+
import onnxruntime as ort
|
11 |
+
import imutils
|
12 |
+
# import matplotlib.pyplot as plt
|
13 |
+
import pandas as pd
|
14 |
+
import plotly.express as px
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
def onnx_segment_membrane(input_image, threshold):
|
19 |
+
ort_session = ort.InferenceSession('onnx_models/membrane_segmentor.onnx')
|
20 |
+
img = Image.fromarray(np.uint8(input_image))
|
21 |
+
resized = img.resize((256, 256), Image.NEAREST)
|
22 |
+
img_unsqueeze = expand_dims_twice(resized)
|
23 |
+
onnx_outputs = ort_session.run(None, {'input': img_unsqueeze.astype('float32')})
|
24 |
+
binarized = 1.0 * (onnx_outputs[0][0][0] > threshold)
|
25 |
+
|
26 |
+
resized_ret = Image.fromarray(binarized.astype(np.uint8) ).resize((356, 256), Image.NEAREST)#.convert("L")
|
27 |
+
centroid_img = generate_centroid_image(np.array(onnx_outputs[0][0][0])) *255
|
28 |
+
resized_centroid_img = Image.fromarray(centroid_img.astype(np.uint8)).resize((356, 256), Image.NEAREST)
|
29 |
+
return(resized_ret, resized_centroid_img)
|
30 |
+
|
31 |
+
|
32 |
+
def generate_centroid_image(thresh):
|
33 |
+
thresh = cv2.blur(thresh, (5,5))
|
34 |
+
thresh = thresh.astype(np.uint8)
|
35 |
+
centroid_image = np.zeros(thresh.shape)
|
36 |
+
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
|
37 |
+
cnts = imutils.grab_contours(cnts)
|
38 |
+
centroids = []
|
39 |
+
for c in cnts:
|
40 |
+
try:
|
41 |
+
# compute the center of the contour
|
42 |
+
M = cv2.moments(c)
|
43 |
+
cX = int(M["m10"] / M["m00"])
|
44 |
+
cY = int(M["m01"] / M["m00"])
|
45 |
+
# draw the contour and center of the shape on the image
|
46 |
+
# cv2.drawContours(centroid_image, [c], -1, (255, 255, 255), 2)
|
47 |
+
cv2.circle(centroid_image, (cX, cY), 2, (1, 1, 1), -1)
|
48 |
+
centroids.append((cX, cY))
|
49 |
+
except:
|
50 |
+
pass
|
51 |
+
return(centroid_image)
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
def expand_dims_twice(arr):
|
56 |
+
norm=(arr-np.min(arr))/(np.max(arr)-np.min(arr))
|
57 |
+
ret = np.expand_dims(np.expand_dims(norm, axis=0), axis=0)
|
58 |
+
return(ret)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
def cell_membrane_segmentation():
|
63 |
+
selected_box2 = st.sidebar.selectbox(
|
64 |
+
'Choose Example Input',
|
65 |
+
('Example_1.png','Example_2.png')
|
66 |
+
)
|
67 |
+
|
68 |
+
st.title('Cell Membrane Segmentation')
|
69 |
+
instructions = """
|
70 |
+
Segment Cell Membrane from C. elegans embryo imaging data \n
|
71 |
+
Either upload your own image or select from the sidebar to get a preconfigured image.
|
72 |
+
The image you select or upload will be fed through the Deep Neural Network in real-time
|
73 |
+
and the output will be displayed to the screen.
|
74 |
+
"""
|
75 |
+
st.text(instructions)
|
76 |
+
file = st.file_uploader('Upload an image or choose an example')
|
77 |
+
example_image = Image.open('./images/cell_membrane_segmentation_examples/'+selected_box2)
|
78 |
+
threshold = st.sidebar.slider("Select Threshold (Applied on model output)", 0.0, 1.0, 0.1)
|
79 |
+
col1, col2, col3 = st.beta_columns(3)
|
80 |
+
|
81 |
+
if file:
|
82 |
+
input = Image.open(file)
|
83 |
+
fig1 = px.imshow(input, binary_string=True, labels=dict(x="Input Image"))
|
84 |
+
fig1.update(layout_coloraxis_showscale=False)
|
85 |
+
fig1.update_layout(margin=dict(l=0, r=0, b=0, t=0))
|
86 |
+
col1.plotly_chart(fig1, use_container_width=True)
|
87 |
+
|
88 |
+
else:
|
89 |
+
input = example_image
|
90 |
+
fig1 = px.imshow(input, binary_string=True, labels=dict(x="Input Image"))
|
91 |
+
fig1.update(layout_coloraxis_showscale=False)
|
92 |
+
fig1.update_layout(margin=dict(l=0, r=0, b=0, t=0))
|
93 |
+
col1.plotly_chart(fig1, use_container_width=True)
|
94 |
+
|
95 |
+
pressed = st.button('Run')
|
96 |
+
if pressed:
|
97 |
+
st.empty()
|
98 |
+
model_output = onnx_segment_membrane(np.array(input), threshold)
|
99 |
+
|
100 |
+
fig2 = px.imshow(model_output[0], binary_string=True, labels=dict(x="Segmentation Map"))
|
101 |
+
fig2.update_layout(margin=dict(l=0, r=0, b=0, t=0))
|
102 |
+
col2.plotly_chart(fig2, use_container_width=True)
|
103 |
+
|
104 |
+
fig3 = px.imshow(model_output[1], binary_string=True, labels=dict(x="Centroid Map"))
|
105 |
+
fig3.update_layout(margin=dict(l=0, r=0, b=0, t=0))
|
106 |
+
col3.plotly_chart(fig3, use_container_width=True)
|
images/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
images/cell_membrane_segmentation_examples/Example_1.png
ADDED
![]() |
images/cell_membrane_segmentation_examples/Example_2.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==4.1.0
|
2 |
+
argon2-cffi==20.1.0
|
3 |
+
astor==0.8.1
|
4 |
+
async-generator==1.10
|
5 |
+
attrs==21.2.0
|
6 |
+
backcall==0.2.0
|
7 |
+
base58==2.1.0
|
8 |
+
bleach==3.3.1
|
9 |
+
blinker==1.4
|
10 |
+
cachetools==4.2.2
|
11 |
+
certifi==2021.5.30
|
12 |
+
cffi==1.14.6
|
13 |
+
charset-normalizer==2.0.3
|
14 |
+
click==7.1.2
|
15 |
+
debugpy==1.4.1
|
16 |
+
decorator==5.0.9
|
17 |
+
defusedxml==0.7.1
|
18 |
+
entrypoints==0.3
|
19 |
+
flatbuffers==2.0
|
20 |
+
gitdb==4.0.7
|
21 |
+
GitPython==3.1.18
|
22 |
+
idna==3.2
|
23 |
+
imutils==0.5.4
|
24 |
+
ipykernel==6.0.3
|
25 |
+
ipython==7.25.0
|
26 |
+
ipython-genutils==0.2.0
|
27 |
+
ipywidgets==7.6.3
|
28 |
+
jedi==0.18.0
|
29 |
+
Jinja2==3.0.1
|
30 |
+
joblib==1.0.1
|
31 |
+
jsonschema==3.2.0
|
32 |
+
jupyter-client==6.1.12
|
33 |
+
jupyter-core==4.7.1
|
34 |
+
jupyterlab-pygments==0.1.2
|
35 |
+
jupyterlab-widgets==1.0.0
|
36 |
+
MarkupSafe==2.0.1
|
37 |
+
matplotlib-inline==0.1.2
|
38 |
+
mistune==0.8.4
|
39 |
+
nbclient==0.5.3
|
40 |
+
nbconvert==6.1.0
|
41 |
+
nbformat==5.1.3
|
42 |
+
nest-asyncio==1.5.1
|
43 |
+
notebook==6.4.0
|
44 |
+
numpy==1.21.1
|
45 |
+
onnxruntime==1.8.1
|
46 |
+
opencv-python-headless==4.5.3.56
|
47 |
+
packaging==21.0
|
48 |
+
pandas==1.3.1
|
49 |
+
pandocfilters==1.4.3
|
50 |
+
parso==0.8.2
|
51 |
+
pexpect==4.8.0
|
52 |
+
pickleshare==0.7.5
|
53 |
+
Pillow==8.3.1
|
54 |
+
plotly==5.1.0
|
55 |
+
prometheus-client==0.11.0
|
56 |
+
prompt-toolkit==3.0.19
|
57 |
+
protobuf==3.17.3
|
58 |
+
ptyprocess==0.7.0
|
59 |
+
pyarrow==5.0.0
|
60 |
+
pycparser==2.20
|
61 |
+
pydeck==0.6.2
|
62 |
+
Pygments==2.9.0
|
63 |
+
pyparsing==2.4.7
|
64 |
+
pyrsistent==0.18.0
|
65 |
+
python-dateutil==2.8.2
|
66 |
+
pytz==2021.1
|
67 |
+
pyzmq==22.1.0
|
68 |
+
requests==2.26.0
|
69 |
+
scikit-learn==0.24.1
|
70 |
+
scipy==1.7.0
|
71 |
+
Send2Trash==1.7.1
|
72 |
+
six==1.16.0
|
73 |
+
smmap==4.0.0
|
74 |
+
streamlit==0.85.1
|
75 |
+
tenacity==8.0.1
|
76 |
+
terminado==0.10.1
|
77 |
+
testpath==0.5.0
|
78 |
+
threadpoolctl==2.2.0
|
79 |
+
toml==0.10.2
|
80 |
+
toolz==0.11.1
|
81 |
+
tornado==6.1
|
82 |
+
traitlets==5.0.5
|
83 |
+
tzlocal==2.1
|
84 |
+
urllib3==1.26.6
|
85 |
+
validators==0.18.2
|
86 |
+
watchdog==2.1.3
|
87 |
+
wcwidth==0.2.5
|
88 |
+
webencodings==0.5.1
|
89 |
+
widgetsnbextension==3.5.1
|
scaler.gz
ADDED
Binary file (507 Bytes). View file
|
|
setup.sh
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mkdir -p ~/.streamlit/
|
2 |
+
echo "\
|
3 |
+
[server]\n\
|
4 |
+
headless = true\n\
|
5 |
+
port = $PORT\n\
|
6 |
+
enableCORS = false\n\
|
7 |
+
\n\
|
8 |
+
" > ~/.streamlit/config.toml
|