Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,44 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
prediction =
|
|
|
|
| 12 |
if len(prediction) > 1:
|
| 13 |
-
probability = 100 *
|
| 14 |
else:
|
| 15 |
-
probability = round(100. / (1 +
|
| 16 |
if probability > 45:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
label = "Not glaucoma"
|
| 22 |
-
return label, probability
|
| 23 |
|
| 24 |
-
inputs = [
|
| 25 |
-
gr.inputs.Image(shape=(224, 224), label="Eye image"),
|
| 26 |
-
gr.inputs.Dropdown(choices=[m["name"] for m in models], label="Model"),
|
| 27 |
-
]
|
| 28 |
-
outputs = [
|
| 29 |
-
gr.outputs.Textbox(label="Predicted label"),
|
| 30 |
-
gr.outputs.Textbox(label="Probability of glaucoma (0-100)"),
|
| 31 |
-
]
|
| 32 |
-
|
| 33 |
-
examples = [
|
| 34 |
-
[np.zeros((224, 224, 3)), "my_model.h5"],
|
| 35 |
-
[np.ones((224, 224, 3)) * 255, "my_model_2.h5"]
|
| 36 |
-
]
|
| 37 |
|
| 38 |
-
gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
| 5 |
|
| 6 |
+
configs = [
|
| 7 |
+
{
|
| 8 |
+
"model": "my_model_2.h5", "size": 512
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"model": "my_model.h5", "size": 224
|
| 12 |
+
},
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
config = configs[0]
|
| 16 |
|
| 17 |
+
new_model = tf.keras.models.load_model(config["model"])
|
| 18 |
+
|
| 19 |
+
def classify_image(inp):
|
| 20 |
+
inp = inp.reshape((-1, config["size"], config["size"], 3))
|
| 21 |
+
prediction = new_model.predict(inp).flatten()
|
| 22 |
+
print(prediction)
|
| 23 |
if len(prediction) > 1:
|
| 24 |
+
probability = 100 * math.exp(prediction[0]) / (math.exp(prediction[0]) + math.exp(prediction[1]))
|
| 25 |
else:
|
| 26 |
+
probability = round(100. / (1 + math.exp(-prediction[0])), 2)
|
| 27 |
if probability > 45:
|
| 28 |
+
return "Glaucoma", probability
|
| 29 |
+
if probability > 25:
|
| 30 |
+
return "Unclear", probability
|
| 31 |
+
return "Not glaucoma", probability
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
gr.Interface(
|
| 35 |
+
fn=classify_image,
|
| 36 |
+
inputs=gr.inputs.Image(shape=(config["size"], config["size"])),
|
| 37 |
+
outputs=[
|
| 38 |
+
gr.outputs.Textbox(label="Label"),
|
| 39 |
+
gr.outputs.Textbox(label="Glaucoma probability (0 - 100)"),
|
| 40 |
+
],
|
| 41 |
+
examples=["001.jpg", "002.jpg", "225.jpg"],
|
| 42 |
+
flagging_options=["Correct label", "Incorrect label"],
|
| 43 |
+
allow_flagging="manual",
|
| 44 |
+
).launch()
|