Spaces:
Build error
Build error
added application, model, and examples
Browse files- app.py +93 -3
- examples/cat_1.jpg +0 -0
- examples/cat_2.jpg +0 -0
- examples/dog_1.jpg +0 -0
- examples/dog_2.jpg +0 -0
- requirements.txt +2 -1
- tf_model.h5 +3 -0
app.py
CHANGED
@@ -1,12 +1,102 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import from_pretrained_keras
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
model = from_pretrained_keras("Narsil/pet-segmentation")
|
5 |
|
6 |
-
print(model.summary())
|
7 |
|
8 |
def greet(name):
|
9 |
return "Hello " + name + "!!"
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
12 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import from_pretrained_keras
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import base64
|
8 |
+
|
9 |
+
|
10 |
+
model = tf.keras.load_model("./tf_model.h5")
|
11 |
|
|
|
12 |
|
|
|
13 |
|
14 |
def greet(name):
|
15 |
return "Hello " + name + "!!"
|
16 |
|
17 |
+
|
18 |
+
def predict(image):
|
19 |
+
img = np.array(image)
|
20 |
+
|
21 |
+
im = tf.image.resize(img, (128, 128))
|
22 |
+
im = tf.cast(im, tf.float32) / 255.0
|
23 |
+
pred_mask = model.predict(im[tf.newaxis, ...])
|
24 |
+
|
25 |
+
# take the best performing class for each pixel
|
26 |
+
# the output of argmax looks like this [[1, 2, 0], ...]
|
27 |
+
pred_mask_arg = tf.argmax(pred_mask, axis=-1)
|
28 |
+
|
29 |
+
labels = []
|
30 |
+
|
31 |
+
# convert the prediction mask into binary masks for each class
|
32 |
+
binary_masks = {}
|
33 |
+
mask_codes = {}
|
34 |
+
|
35 |
+
# when we take tf.argmax() over pred_mask, it becomes a tensor object
|
36 |
+
# the shape becomes TensorShape object, looking like this TensorShape([128])
|
37 |
+
# we need to take get shape, convert to list and take the best one
|
38 |
+
|
39 |
+
rows = pred_mask_arg[0][1].get_shape().as_list()[0]
|
40 |
+
cols = pred_mask_arg[0][2].get_shape().as_list()[0]
|
41 |
+
|
42 |
+
for cls in range(pred_mask.shape[-1]):
|
43 |
+
|
44 |
+
binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class
|
45 |
+
|
46 |
+
for row in range(rows):
|
47 |
+
|
48 |
+
for col in range(cols):
|
49 |
+
|
50 |
+
if pred_mask_arg[0][row][col] == cls:
|
51 |
+
|
52 |
+
binary_masks[f"mask_{cls}"][row][col] = 1
|
53 |
+
else:
|
54 |
+
binary_masks[f"mask_{cls}"][row][col] = 0
|
55 |
+
|
56 |
+
mask = binary_masks[f"mask_{cls}"]
|
57 |
+
mask *= 255
|
58 |
+
img = Image.fromarray(mask.astype(np.int8), mode="L")
|
59 |
+
|
60 |
+
# we need to make it readable for the widget
|
61 |
+
with io.BytesIO() as out:
|
62 |
+
img.save(out, format="PNG")
|
63 |
+
png_string = out.getvalue()
|
64 |
+
mask = base64.b64encode(png_string).decode("utf-8")
|
65 |
+
|
66 |
+
mask_codes[f"mask_{cls}"] = mask
|
67 |
+
|
68 |
+
# widget needs the below format, for each class we return label and mask string
|
69 |
+
labels.append({
|
70 |
+
"label": f"LABEL_{cls}",
|
71 |
+
"mask": mask_codes[f"mask_{cls}"],
|
72 |
+
"score": 1.0,
|
73 |
+
})
|
74 |
+
|
75 |
+
return labels["mask"], labels["label"]
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
inputs = gr.inputs.Image(label="Upload a fetal standard plane image", type = 'pil', optional=False)
|
80 |
+
outputs = [
|
81 |
+
gr.outputs.Image(label="Segmentation"),
|
82 |
+
gr.outputs.Textbox(type="auto",label="Fetal Plane Prediction")
|
83 |
+
]
|
84 |
+
|
85 |
+
examples = [
|
86 |
+
"./examples/cat_1.jpg",
|
87 |
+
"./examples/cat_2.jpg",
|
88 |
+
"./examples/dog_1.jpg",
|
89 |
+
"./examples/dog_2.jpg",
|
90 |
+
]
|
91 |
+
|
92 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
93 |
+
iface.launch()
|
94 |
+
|
95 |
+
interface = gr.Interface(fn=model.predict,
|
96 |
+
inputs=inputs,
|
97 |
+
outputs=outputs,
|
98 |
+
# title = title,
|
99 |
+
# description=description,
|
100 |
+
examples=examples
|
101 |
+
)
|
102 |
+
interface.launch()
|
examples/cat_1.jpg
ADDED
examples/cat_2.jpg
ADDED
examples/dog_1.jpg
ADDED
examples/dog_2.jpg
ADDED
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
huggingface_hub
|
2 |
-
tensorflow
|
|
|
|
1 |
huggingface_hub
|
2 |
+
tensorflow
|
3 |
+
pillow
|
tf_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0258ea75c11d977fae78f747902e48541c5e6996d3d5c700175454ffeb42aa0f
|
3 |
+
size 63661584
|