DHEIVER commited on
Commit
f688574
·
1 Parent(s): 4456acb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -1,38 +1,44 @@
1
- import numpy as np
 
2
  import gradio as gr
3
  import tensorflow as tf
4
 
5
- models = [{"name": "my_model_2.h5", "size": 512}, {"name": "my_model.h5", "size": 224}]
 
 
 
 
 
 
 
 
 
6
 
7
- def classify_image(image, model_name):
8
- model_config = next(m for m in models if m["name"] == model_name)
9
- model = tf.keras.models.load_model(model_name)
10
- input_image = np.expand_dims(image, axis=0)
11
- prediction = model.predict(input_image).flatten()
 
12
  if len(prediction) > 1:
13
- probability = 100 * np.exp(prediction[0]) / (np.exp(prediction[0]) + np.exp(prediction[1]))
14
  else:
15
- probability = round(100. / (1 + np.exp(-prediction[0])), 2)
16
  if probability > 45:
17
- label = "Glaucoma"
18
- elif probability > 25:
19
- label = "Unclear"
20
- else:
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(classify_image, inputs, outputs, examples=examples, title="Glaucoma Classification").launch()
 
 
 
 
 
 
 
 
 
 
 
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()