Nina-HK commited on
Commit
249c24f
·
1 Parent(s): 8b7cac2

demo_launching

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -27,43 +27,53 @@ from tensorflow.keras.preprocessing import image as image_utils
27
  from tensorflow.keras.applications import densenet, efficientnet
28
 
29
 
30
- # load the CNN_binary model
 
 
 
31
  model_cnn = tf.keras.models.load_model("CNN_binary")
32
 
33
- # define the labels for the CNN_binary model
34
  labels_cnn = {0: 'healthy', 1: 'Patients'}
35
 
36
- # load the efficientNet_binary model
37
- model_enet = tf.keras.models.load_model("efficientNet_binary")
38
 
39
- # define the labels for the efficientNet_binary model
40
- labels_enet = {0: 'healthy', 1: 'Patients'}
41
 
42
- def classify_image(model, labels, inp):
43
  inp = inp.reshape((-1, 224, 224, 3))
44
  inp = tf.keras.applications.densenet.preprocess_input(inp)
45
- prediction = model.predict(inp)
46
- confidence = float(prediction[0])
47
- return {labels[prediction.argmax()]: confidence}
48
-
49
- cnn_interface = gr.Interface(fn=lambda inp: classify_image(model_cnn, labels_cnn, inp),
50
- inputs=gr.inputs.Image(shape=(224, 224)),
51
- outputs=gr.outputs.Label(num_top_classes=2),
52
- title="CNN Binary Image Classification",
53
- description="Classify an image as healthy or patient using CNN binary model.",
54
- #examples=[["patient.jpg"], ["healthy.jpg"]]
55
- )
56
-
57
- enet_interface = gr.Interface(fn=lambda inp: classify_image(model_enet, labels_enet, inp),
58
- inputs=gr.inputs.Image(shape=(224, 224)),
59
- outputs=gr.outputs.Label(num_top_classes=2),
60
- title="EfficientNet Binary Image Classification",
61
- description="Classify an image as healthy or patient using EfficientNet binary model.",
62
- #examples=[["patient.jpg"], ["healthy.jpg"]]
63
- )
64
-
65
- iface = gr.Interface([cnn_interface, enet_interface],
66
- "tabs",
 
 
 
 
 
 
 
67
  title="Binary Image Classification",
68
- description="Choose a binary classification model to classify an image as healthy or patient.")
69
- iface.launch()
 
27
  from tensorflow.keras.applications import densenet, efficientnet
28
 
29
 
30
+ import tensorflow as tf
31
+ import gradio as gr
32
+
33
+ # load the CNN binary classification model
34
  model_cnn = tf.keras.models.load_model("CNN_binary")
35
 
36
+ # define the labels for the binary classification model
37
  labels_cnn = {0: 'healthy', 1: 'Patients'}
38
 
39
+ # load the EfficientNet binary classification model
40
+ model_efn = tf.keras.models.load_model("efficientNet_binary")
41
 
42
+ # define the labels for the binary classification model
43
+ labels_efn = {0: 'healthy', 1: 'Patients'}
44
 
45
+ def classify_cnn(inp):
46
  inp = inp.reshape((-1, 224, 224, 3))
47
  inp = tf.keras.applications.densenet.preprocess_input(inp)
48
+ prediction = model_cnn.predict(inp)
49
+ confidences = {labels_cnn[i]: float(prediction[0][i]) for i in range(2)}
50
+ return confidences
51
+
52
+ def classify_efn(inp):
53
+ inp = inp.reshape((-1, 224, 224, 3))
54
+ inp = tf.keras.applications.efficientnet.preprocess_input(inp)
55
+ prediction = model_efn.predict(inp)
56
+ confidences = {labels_efn[i]: float(prediction[0][i]) for i in range(2)}
57
+ return confidences
58
+
59
+ binary_interface_cnn = gr.Interface(fn=classify_cnn,
60
+ inputs=gr.inputs.Image(shape=(224, 224)),
61
+ outputs=gr.outputs.Label(num_top_classes=2),
62
+ title="CNN Binary Image Classification",
63
+ description="Classify an image as healthy or patient using CNN.",
64
+ examples=[['300104.png']]
65
+ )
66
+
67
+ binary_interface_efn = gr.Interface(fn=classify_efn,
68
+ inputs=gr.inputs.Image(shape=(224, 224)),
69
+ outputs=gr.outputs.Label(num_top_classes=2),
70
+ title="EfficientNet Binary Image Classification",
71
+ description="Classify an image as healthy or patient using EfficientNet.",
72
+ examples=[['300104.png']]
73
+ )
74
+
75
+ demo = gr.Interface([binary_interface_cnn, binary_interface_efn],
76
+ "tab",
77
  title="Binary Image Classification",
78
+ description="Classify an image as healthy or patient using either CNN or EfficientNet.")
79
+ demo.launch()