Spaces:
Sleeping
Sleeping
two_interface_3
Browse files
app.py
CHANGED
@@ -2,6 +2,17 @@
|
|
2 |
|
3 |
# %%capture
|
4 |
# #Use capture to not show the output of installing the libraries!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import gradio as gr
|
6 |
import requests
|
7 |
import torch
|
@@ -13,57 +24,47 @@ import numpy as np
|
|
13 |
import tensorflow as tf
|
14 |
from transformers import pipeline
|
15 |
|
16 |
-
# load the model from the Hugging Face Model Hub
|
17 |
-
#model = pipeline('image-classification', model='image_classification/densenet')
|
18 |
-
#model = tf.keras.models.load_model("densenet")
|
19 |
-
#binary_model = tf.keras.models.load_model("densenet")
|
20 |
-
#binary_labels = {0: 'healthy', 1: 'patient'}
|
21 |
-
|
22 |
# load the binary classification model
|
23 |
-
|
24 |
|
25 |
# load the multi-label classification model
|
26 |
-
|
27 |
|
28 |
# define the labels for the multi-label classification model
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Define the labels
|
40 |
-
labels = {0: 'healthy', 1: 'patient'}
|
41 |
|
42 |
-
def
|
43 |
inp = inp.reshape((-1, 224, 224, 3))
|
44 |
inp = tf.keras.applications.densenet.preprocess_input(inp)
|
45 |
-
prediction =
|
46 |
-
confidences = {
|
47 |
return confidences
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
outputs=gr.Label(num_top_classes=2),
|
53 |
title="Binary Image Classification",
|
54 |
description="Classify an image as healthy or patient.",
|
55 |
examples=[['300104.png']]
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
outputs=gr.Label(num_top_classes=3),
|
62 |
title="Multi-class Image Classification",
|
63 |
description="Classify an image as healthy, mild or moderate.",
|
64 |
examples=[['300104.png']]
|
65 |
)
|
66 |
|
67 |
-
|
68 |
-
|
|
|
69 |
|
|
|
2 |
|
3 |
# %%capture
|
4 |
# #Use capture to not show the output of installing the libraries!
|
5 |
+
|
6 |
+
#model_multi = tf.keras.models.load_model("densenet")
|
7 |
+
|
8 |
+
# define the labels for the multi-label classification model
|
9 |
+
#labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
|
10 |
+
|
11 |
+
|
12 |
+
#model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
|
13 |
+
#labels = ['Healthy', 'Patient']
|
14 |
+
#labels = {0: 'healthy', 1: 'patient'}
|
15 |
+
|
16 |
import gradio as gr
|
17 |
import requests
|
18 |
import torch
|
|
|
24 |
import tensorflow as tf
|
25 |
from transformers import pipeline
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# load the binary classification model
|
28 |
+
model_binary = tf.keras.models.load_model("densenet")
|
29 |
|
30 |
# load the multi-label classification model
|
31 |
+
model_multi = tf.keras.models.load_model("densenet")
|
32 |
|
33 |
# define the labels for the multi-label classification model
|
34 |
+
labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
|
|
|
35 |
|
36 |
+
def classify_binary(inp):
|
37 |
+
inp = inp.reshape((-1, 224, 224, 3))
|
38 |
+
inp = tf.keras.applications.densenet.preprocess_input(inp)
|
39 |
+
prediction = model_binary.predict(inp)
|
40 |
+
confidence = float(prediction[0])
|
41 |
+
label = {0: 'healthy', 1: 'patient'}
|
42 |
+
return {label: confidence}
|
|
|
|
|
43 |
|
44 |
+
def classify_multi(inp):
|
45 |
inp = inp.reshape((-1, 224, 224, 3))
|
46 |
inp = tf.keras.applications.densenet.preprocess_input(inp)
|
47 |
+
prediction = model_multi.predict(inp)
|
48 |
+
confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(len(labels_multi))}
|
49 |
return confidences
|
50 |
|
51 |
+
binary_interface = gr.Interface(fn=classify_binary,
|
52 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
53 |
+
outputs=gr.outputs.Label(num_top_classes=2),
|
|
|
54 |
title="Binary Image Classification",
|
55 |
description="Classify an image as healthy or patient.",
|
56 |
examples=[['300104.png']]
|
57 |
)
|
58 |
|
59 |
+
multi_interface = gr.Interface(fn=classify_multi,
|
60 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
61 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
|
|
62 |
title="Multi-class Image Classification",
|
63 |
description="Classify an image as healthy, mild or moderate.",
|
64 |
examples=[['300104.png']]
|
65 |
)
|
66 |
|
67 |
+
demo = gr.TabbedInterface([binary_interface, multi_interface], ["Binary", "Multi-class"])
|
68 |
+
|
69 |
+
demo.launch()
|
70 |
|