Nina-HK commited on
Commit
b3bd50f
1 Parent(s): 271a29f
Files changed (1) hide show
  1. app.py +43 -34
app.py CHANGED
@@ -40,42 +40,51 @@ model_binary = tf.keras.models.load_model("densenet")
40
  # load the multi-label classification model
41
  model_multi = tf.keras.models.load_model("densenet")
42
 
 
 
 
43
  # define the labels for the multi-label classification model
44
  labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
45
 
46
 
47
- def classify_image_binary(inp):
48
- inp = inp.reshape((-1, 224, 224, 3))
49
- inp = tf.keras.applications.densenet.preprocess_input(inp)
50
- prediction = model_binary.predict(inp)
51
- predicted_class = np.argmax(prediction)
52
- confidence = float(prediction[0][predicted_class])
53
- label = {0: 'healthy', 1: 'patient'}[predicted_class]
54
- return {label: confidence}
55
-
56
- def classify_image_multi(inp):
57
- inp = inp.reshape((-1, 224, 224, 3))
58
- inp = tf.keras.applications.densenet.preprocess_input(inp)
59
- prediction = model_multi.predict(inp)
60
- confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(len(prediction[0]))}
61
- return confidences
62
-
63
- binary_interface = gr.Interface(fn=classify_image_binary,
64
- inputs=gr.inputs.Image(shape=(224, 224)),
65
- outputs=gr.outputs.Label(num_top_classes=2),
66
- title="Binary Image Classification",
67
- description="Classify an image as healthy or patient.",
68
- examples=[['300104.png']]
69
- )
70
-
71
- multi_interface = gr.Interface(fn=classify_image_multi,
72
- inputs=gr.inputs.Image(shape=(224, 224)),
73
- outputs=gr.outputs.Label(num_top_classes=3),
74
- title="Multi-class Image Classification",
75
- description="Classify an image as healthy, mild or moderate.",
76
- examples=[['300104.png']]
77
- )
78
-
79
- binary_interface.launch()
80
- multi_interface.launch()
 
 
 
 
 
 
81
 
 
40
  # load the multi-label classification model
41
  model_multi = tf.keras.models.load_model("densenet")
42
 
43
+ # define the labels for the binary classification model
44
+ labels_binary = {0: 'healthy', 1: 'patient'}
45
+
46
  # define the labels for the multi-label classification model
47
  labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
48
 
49
 
50
+ # define the function for the binary classification model
51
+ def classify_binary(inp):
52
+ inp = inp.reshape((-1, 224, 224, 3))
53
+ inp = tf.keras.applications.densenet.preprocess_input(inp)
54
+ prediction = model_binary.predict(inp)
55
+ confidence = float(prediction[0])
56
+ label = labels_binary[int(np.round(confidence))]
57
+ return {label: confidence}
58
+
59
+
60
+ # define the function for the multi-label classification model
61
+ def classify_multi(inp):
62
+ inp = inp.reshape((-1, 224, 224, 3))
63
+ inp = tf.keras.applications.densenet.preprocess_input(inp)
64
+ prediction = model_multi.predict(inp)
65
+ confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(3)}
66
+ return confidences
67
+
68
+
69
+ # create the Gradio interface for the binary classification model
70
+ binary_interface = gr.Interface(fn=classify_binary,
71
+ inputs=gr.inputs.Image(shape=(224, 224)),
72
+ outputs=gr.outputs.Label(num_top_classes=2),
73
+ title="Binary Image Classification",
74
+ description="Classify an image as healthy or patient.",
75
+ examples=[['300104.png']]
76
+ )
77
+
78
+ # create the Gradio interface for the multi-label classification model
79
+ multi_interface = gr.Interface(fn=classify_multi,
80
+ inputs=gr.inputs.Image(shape=(224, 224)),
81
+ outputs=gr.outputs.Label(num_top_classes=3),
82
+ title="Multi-class Image Classification",
83
+ description="Classify an image as healthy, mild or moderate.",
84
+ examples=[['300104.png']]
85
+ )
86
+
87
+ # create the Gradio app with both interfaces
88
+ app = gr.Interface([binary_interface, multi_interface], title="Image Classification App")
89
+ app.launch()
90