Spaces:
Runtime error
Runtime error
Duplicate from MarkTLite/fetal-unet
Browse filesCo-authored-by: Mark T <MarkTLite@users.noreply.huggingface.co>
- .gitattributes +31 -0
- README.md +13 -0
- app.py +77 -0
- image.png +0 -0
- model-best.h5 +3 -0
- requirements.txt +5 -0
.gitattributes
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Fetal Unet
|
| 3 |
+
emoji: 👀
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.1.4
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: MarkTLite/fetal-unet
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def greet(name):
|
| 5 |
+
return "Hello " + name
|
| 6 |
+
|
| 7 |
+
title = "A Machine Learning Strategy for Automatic Phenotyping of High Risk Pregnancies"
|
| 8 |
+
description = """
|
| 9 |
+
The bot was trained to segment, measure and make informed prediction of high risk pregnancy based off of what fetal skull Head circumference (HC) can imply!
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
# <img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px>
|
| 13 |
+
article = "Check out [the github repository](https://github.com/MarkTLite) that this website and model are based off of."
|
| 14 |
+
|
| 15 |
+
import cv2, math
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
import numpy as np
|
| 18 |
+
from tensorflow.keras.utils import normalize
|
| 19 |
+
from tensorflow.keras.models import load_model
|
| 20 |
+
from skimage import measure
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def predict(input_img):
|
| 24 |
+
input_img = input_img.reshape((256,256,1))
|
| 25 |
+
test_normalized_image = normalize(input_img, axis=1)
|
| 26 |
+
# load model
|
| 27 |
+
model = load_model('model-best.h5',compile=False)
|
| 28 |
+
model.compile(optimizer='adam', loss = "binary_crossentropy")
|
| 29 |
+
test_img = test_normalized_image
|
| 30 |
+
orig_img = input_img
|
| 31 |
+
test_img_norm=test_img[:,:,0]
|
| 32 |
+
test_img_input=np.expand_dims(test_img_norm, 0)
|
| 33 |
+
|
| 34 |
+
# Predict and threshold for values above 0.08 probability
|
| 35 |
+
prediction = (model.predict(test_img_input) > 0.08).astype(np.uint8)
|
| 36 |
+
prediction = prediction[0]
|
| 37 |
+
label_image = measure.label(prediction, connectivity=orig_img.ndim)
|
| 38 |
+
|
| 39 |
+
fig, ax = plt.subplots()
|
| 40 |
+
ax.imshow(label_image[:,:,0], cmap=plt.cm.gray)
|
| 41 |
+
regions = measure.regionprops(label_image[:,:,0])
|
| 42 |
+
prev_hc, hc = 0,0
|
| 43 |
+
for props in regions:
|
| 44 |
+
y0, x0 = props.centroid
|
| 45 |
+
orientation = props.orientation
|
| 46 |
+
x1 = x0 + math.cos(orientation) * 0.5 * props.minor_axis_length
|
| 47 |
+
y1 = y0 - math.sin(orientation) * 0.5 * props.minor_axis_length
|
| 48 |
+
x2 = x0 - math.sin(orientation) * 0.5 * props.major_axis_length
|
| 49 |
+
y2 = y0 - math.cos(orientation) * 0.5 * props.major_axis_length
|
| 50 |
+
|
| 51 |
+
minor_distance = ((x0 - x1)**2 + (y0 - y1)**2)**0.5
|
| 52 |
+
print(minor_distance*2)
|
| 53 |
+
major_distance = ((x0 - x2)**2 + (y0 - y2)**2)**0.5
|
| 54 |
+
print(major_distance*2)
|
| 55 |
+
prev_hc = 1.62*(minor_distance+major_distance)
|
| 56 |
+
if(prev_hc>hc):
|
| 57 |
+
hc = prev_hc
|
| 58 |
+
print("HC = ",hc, " mm")
|
| 59 |
+
|
| 60 |
+
ax.plot((x0, x1), (y0, y1), '-r', linewidth=2.5)
|
| 61 |
+
ax.plot((x0, x2), (y0, y2), '-r', linewidth=2.5)
|
| 62 |
+
ax.plot(x0, y0, '.g', markersize=15)
|
| 63 |
+
|
| 64 |
+
plt.show()
|
| 65 |
+
|
| 66 |
+
# Overlap prediction on original image
|
| 67 |
+
drawn_img = cv2.cvtColor(orig_img, cv2.COLOR_GRAY2BGR)
|
| 68 |
+
contours, hierarchy = cv2.findContours(prediction,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
|
| 69 |
+
cv2.drawContours(drawn_img, contours, -1, (255,0,0), 2)
|
| 70 |
+
return drawn_img, "Head Circumference = " + str(hc) + " mm"
|
| 71 |
+
|
| 72 |
+
examples = [
|
| 73 |
+
['image.png']
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
gr.Interface(predict,gr.Image(shape=(256, 256), image_mode='L'), [gr.outputs.Image(type='plot'),'text'],
|
| 77 |
+
description=description, article=article, title=title, examples=examples, analytics_enabled=False).launch()
|
image.png
ADDED
|
model-best.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2b8c26eac12162837a3398994131636564aaf20bda7728fe7ddce060d639b8c7
|
| 3 |
+
size 23623240
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python==4.6.0.66
|
| 2 |
+
matplotlib==3.5.2
|
| 3 |
+
numpy==1.23.1
|
| 4 |
+
tensorflow==2.9.1
|
| 5 |
+
scikit-image==0.19.3
|