Spaces:
Sleeping
Sleeping
model_binary_two
Browse files
app.py
CHANGED
@@ -23,55 +23,56 @@ from torchvision.transforms import functional as F
|
|
23 |
import numpy as np
|
24 |
import tensorflow as tf
|
25 |
from transformers import pipeline
|
|
|
|
|
26 |
|
27 |
|
28 |
# load the binary classification model
|
29 |
-
|
30 |
-
|
31 |
-
# define the labels for the binary classification model
|
32 |
-
labels_binary = {0: 'healthy', 1: 'Patients'}
|
33 |
-
|
34 |
-
# load the multi-label classification model
|
35 |
-
model_multi = tf.keras.models.load_model("CNN_multiclass")
|
36 |
|
37 |
# define the labels for the multi-label classification model
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
confidence = float(prediction[0])
|
45 |
-
return {
|
46 |
-
|
47 |
-
def classify_multi(inp):
|
48 |
-
inp = inp.reshape((-1, 224, 224, 3))
|
49 |
-
inp = tf.keras.applications.densenet.preprocess_input(inp)
|
50 |
-
prediction = model_multi.predict(inp)
|
51 |
-
confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(len(labels_multi))}
|
52 |
-
return confidences
|
53 |
-
|
54 |
-
binary_interface = gr.Interface(fn=classify_binary,
|
55 |
-
inputs=gr.inputs.Image(shape=(224, 224)),
|
56 |
-
outputs=gr.outputs.Label(num_top_classes=2),
|
57 |
-
title="Binary Image Classification",
|
58 |
-
description="Classify an image as healthy or patient.",
|
59 |
-
examples=[['300104.png']]
|
60 |
-
)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
"tab",
|
72 |
-
title="Binary
|
73 |
-
description="Classify an image as healthy
|
74 |
-
|
75 |
|
|
|
76 |
|
77 |
|
|
|
23 |
import numpy as np
|
24 |
import tensorflow as tf
|
25 |
from transformers import pipeline
|
26 |
+
from tensorflow.keras.preprocessing import image as image_utils
|
27 |
+
from tensorflow.keras.applications import densenet, efficientnet
|
28 |
|
29 |
|
30 |
# load the binary classification model
|
31 |
+
model_cnn = tf.keras.models.load_model("CNN_binary")
|
32 |
+
model_efficientnet = tf.keras.models.load_model("EfficientNet_binary")
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# define the labels for the multi-label classification model
|
35 |
+
labels_cnn = {0: 'healthy', 1: 'patient'}
|
36 |
+
labels_efficientnet = {0: 'healthy', 1: 'patient'}
|
37 |
+
|
38 |
+
def classify_cnn(inp):
|
39 |
+
img = np.array(inp)
|
40 |
+
img = img.reshape((1, 224, 224, 3))
|
41 |
+
img = densenet.preprocess_input(img)
|
42 |
+
prediction = model_cnn.predict(img)
|
43 |
confidence = float(prediction[0])
|
44 |
+
return {labels_cnn[prediction.argmax()]: confidence}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
def classify_efficientnet(inp):
|
47 |
+
img = np.array(inp)
|
48 |
+
img = img.reshape((1, 224, 224, 3))
|
49 |
+
img = efficientnet.preprocess_input(img)
|
50 |
+
prediction = model_efficientnet.predict(img)
|
51 |
+
confidence = float(prediction[0])
|
52 |
+
return {labels_efficientnet[prediction.argmax()]: confidence}
|
53 |
+
|
54 |
+
cnn_interface = gr.Interface(fn=classify_cnn,
|
55 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
56 |
+
outputs=gr.outputs.Label(num_top_classes=2),
|
57 |
+
title="CNN Binary Image Classification",
|
58 |
+
description="Classify an image as healthy or patient using a CNN model.",
|
59 |
+
examples=[['300104.png']]
|
60 |
+
)
|
61 |
+
|
62 |
+
efficientnet_interface = gr.Interface(fn=classify_efficientnet,
|
63 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
64 |
+
outputs=gr.outputs.Label(num_top_classes=2),
|
65 |
+
title="EfficientNet Binary Image Classification",
|
66 |
+
description="Classify an image as healthy or patient using an EfficientNet model.",
|
67 |
+
examples=[['300104.png']]
|
68 |
+
)
|
69 |
+
# create a combined interface with tabs for each binary classification model
|
70 |
+
demo = gr.Interface([cnn_interface, efficientnet_interface],
|
71 |
"tab",
|
72 |
+
title="Binary Image Classification",
|
73 |
+
description="Classify an image as healthy or patient using different binary classification models."
|
74 |
+
)
|
75 |
|
76 |
+
demo.launch()
|
77 |
|
78 |
|