Update app.py
Browse files
app.py
CHANGED
|
@@ -137,37 +137,57 @@
|
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
-
import gradio as gr
|
| 141 |
import numpy as np
|
| 142 |
-
from PIL import Image
|
| 143 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
| 144 |
from models import create_vgg19_model
|
| 145 |
from gradcam_utils import generate_heatmap_tf_explain
|
| 146 |
|
| 147 |
# Load models
|
| 148 |
ensemble_model = load_model("ensemble_model_best(92.3).h5")
|
| 149 |
-
vgg_model = create_vgg19_model() # Used for Grad-CAM
|
| 150 |
|
| 151 |
def get_class_name(class_id):
|
| 152 |
return "Normal" if class_id == 0 else "Pneumonia"
|
| 153 |
|
| 154 |
def predict_and_heatmap(image):
|
| 155 |
-
#
|
| 156 |
img = image.resize((224, 224))
|
| 157 |
img_array = np.array(img) / 255.0
|
| 158 |
img_array = np.expand_dims(img_array, axis=0)
|
| 159 |
|
| 160 |
-
# Predict
|
| 161 |
prediction = ensemble_model.predict(img_array)
|
| 162 |
class_id = int(np.argmax(prediction[0]))
|
| 163 |
result = get_class_name(class_id)
|
| 164 |
|
| 165 |
-
# Generate heatmap
|
| 166 |
-
heatmap_img = generate_heatmap_tf_explain(
|
| 167 |
|
| 168 |
return result, heatmap_img
|
| 169 |
|
| 170 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
interface = gr.Interface(
|
| 172 |
fn=predict_and_heatmap,
|
| 173 |
inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
|
|
@@ -175,10 +195,10 @@ interface = gr.Interface(
|
|
| 175 |
gr.Label(label="Prediction"),
|
| 176 |
gr.Image(label="Grad-CAM Heatmap")
|
| 177 |
],
|
| 178 |
-
title="Pneumonia Detection Using Deep Learning",
|
| 179 |
-
description="Upload a chest X-ray to detect Pneumonia and see the heatmap visualization (
|
|
|
|
| 180 |
)
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
interface.launch()
|
| 184 |
-
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
|
|
|
|
| 140 |
import numpy as np
|
|
|
|
| 141 |
from tensorflow.keras.models import load_model
|
| 142 |
+
from PIL import Image
|
| 143 |
+
import gradio as gr
|
| 144 |
from models import create_vgg19_model
|
| 145 |
from gradcam_utils import generate_heatmap_tf_explain
|
| 146 |
|
| 147 |
# Load models
|
| 148 |
ensemble_model = load_model("ensemble_model_best(92.3).h5")
|
| 149 |
+
vgg_model = create_vgg19_model() # Used only for Grad-CAM
|
| 150 |
|
| 151 |
def get_class_name(class_id):
|
| 152 |
return "Normal" if class_id == 0 else "Pneumonia"
|
| 153 |
|
| 154 |
def predict_and_heatmap(image):
|
| 155 |
+
# Resize and normalize image for prediction
|
| 156 |
img = image.resize((224, 224))
|
| 157 |
img_array = np.array(img) / 255.0
|
| 158 |
img_array = np.expand_dims(img_array, axis=0)
|
| 159 |
|
| 160 |
+
# Predict with ensemble model
|
| 161 |
prediction = ensemble_model.predict(img_array)
|
| 162 |
class_id = int(np.argmax(prediction[0]))
|
| 163 |
result = get_class_name(class_id)
|
| 164 |
|
| 165 |
+
# Generate heatmap with tf-explain using VGG19
|
| 166 |
+
heatmap_img = generate_heatmap_tf_explain(img, vgg_model, class_index=class_id)
|
| 167 |
|
| 168 |
return result, heatmap_img
|
| 169 |
|
| 170 |
+
# 🎨 Custom CSS styling
|
| 171 |
+
custom_css = """
|
| 172 |
+
body {
|
| 173 |
+
background-color: #1c1c1e;
|
| 174 |
+
font-family: 'Segoe UI', sans-serif;
|
| 175 |
+
}
|
| 176 |
+
h1, h2, .output_class {
|
| 177 |
+
color: #ffffff;
|
| 178 |
+
text-align: center;
|
| 179 |
+
}
|
| 180 |
+
.gr-button {
|
| 181 |
+
background-color: #007aff !important;
|
| 182 |
+
color: white !important;
|
| 183 |
+
}
|
| 184 |
+
.gr-image-preview {
|
| 185 |
+
box-shadow: 0 0 20px rgba(0,0,0,0.5);
|
| 186 |
+
border-radius: 8px;
|
| 187 |
+
}
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
+
# Launch Gradio Interface
|
| 191 |
interface = gr.Interface(
|
| 192 |
fn=predict_and_heatmap,
|
| 193 |
inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
|
|
|
|
| 195 |
gr.Label(label="Prediction"),
|
| 196 |
gr.Image(label="Grad-CAM Heatmap")
|
| 197 |
],
|
| 198 |
+
title="🩺 Pneumonia Detection Using Deep Learning",
|
| 199 |
+
description="Upload a chest X-ray to detect Pneumonia and see the heatmap visualization (powered by tf-explain and VGG19).",
|
| 200 |
+
css=custom_css
|
| 201 |
)
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
interface.launch()
|
|
|