Update app.py
Browse files
app.py
CHANGED
|
@@ -77,19 +77,76 @@
|
|
| 77 |
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
import gradio as gr
|
| 81 |
import numpy as np
|
| 82 |
-
import cv2
|
| 83 |
from PIL import Image
|
| 84 |
from tensorflow.keras.models import load_model
|
| 85 |
-
from models import create_vgg19_model
|
| 86 |
-
from gradcam_utils import
|
| 87 |
|
| 88 |
# Load models
|
| 89 |
ensemble_model = load_model("ensemble_model_best(92.3).h5")
|
| 90 |
-
vgg_model = create_vgg19_model()
|
| 91 |
-
efficientnet_model = create_efficientnet_model()
|
| 92 |
-
densenet_model = create_densenet_model()
|
| 93 |
|
| 94 |
def get_class_name(class_id):
|
| 95 |
return "Normal" if class_id == 0 else "Pneumonia"
|
|
@@ -102,19 +159,13 @@ def predict_and_heatmap(image):
|
|
| 102 |
|
| 103 |
# Predict using ensemble model
|
| 104 |
prediction = ensemble_model.predict(img_array)
|
| 105 |
-
class_id = np.argmax(prediction[0])
|
| 106 |
result = get_class_name(class_id)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
image.save(temp_img_path)
|
| 111 |
-
|
| 112 |
-
# Generate Grad-CAM heatmap
|
| 113 |
-
heatmap_img = generate_and_merge_heatmaps(
|
| 114 |
-
temp_img_path, vgg_model, efficientnet_model, densenet_model
|
| 115 |
-
)
|
| 116 |
|
| 117 |
-
return result,
|
| 118 |
|
| 119 |
# Gradio Interface
|
| 120 |
interface = gr.Interface(
|
|
@@ -125,8 +176,9 @@ interface = gr.Interface(
|
|
| 125 |
gr.Image(label="Grad-CAM Heatmap")
|
| 126 |
],
|
| 127 |
title="Pneumonia Detection Using Deep Learning",
|
| 128 |
-
description="Upload a chest X-ray to detect Pneumonia and see the heatmap visualization (Grad-CAM)."
|
| 129 |
)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
interface.launch()
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
+
# import gradio as gr
|
| 81 |
+
# import numpy as np
|
| 82 |
+
# import cv2
|
| 83 |
+
# from PIL import Image
|
| 84 |
+
# from tensorflow.keras.models import load_model
|
| 85 |
+
# from models import create_vgg19_model, create_efficientnet_model, create_densenet_model
|
| 86 |
+
# from gradcam_utils import generate_and_merge_heatmaps
|
| 87 |
+
|
| 88 |
+
# # Load models
|
| 89 |
+
# ensemble_model = load_model("ensemble_model_best(92.3).h5")
|
| 90 |
+
# vgg_model = create_vgg19_model()
|
| 91 |
+
# efficientnet_model = create_efficientnet_model()
|
| 92 |
+
# densenet_model = create_densenet_model()
|
| 93 |
+
|
| 94 |
+
# def get_class_name(class_id):
|
| 95 |
+
# return "Normal" if class_id == 0 else "Pneumonia"
|
| 96 |
+
|
| 97 |
+
# def predict_and_heatmap(image):
|
| 98 |
+
# # Preprocess input image
|
| 99 |
+
# img = image.resize((224, 224))
|
| 100 |
+
# img_array = np.array(img) / 255.0
|
| 101 |
+
# img_array = np.expand_dims(img_array, axis=0)
|
| 102 |
+
|
| 103 |
+
# # Predict using ensemble model
|
| 104 |
+
# prediction = ensemble_model.predict(img_array)
|
| 105 |
+
# class_id = np.argmax(prediction[0])
|
| 106 |
+
# result = get_class_name(class_id)
|
| 107 |
+
|
| 108 |
+
# # Save uploaded image temporarily
|
| 109 |
+
# temp_img_path = "temp_input.jpg"
|
| 110 |
+
# image.save(temp_img_path)
|
| 111 |
+
|
| 112 |
+
# # Generate Grad-CAM heatmap
|
| 113 |
+
# heatmap_img = generate_and_merge_heatmaps(
|
| 114 |
+
# temp_img_path, vgg_model, efficientnet_model, densenet_model
|
| 115 |
+
# )
|
| 116 |
+
|
| 117 |
+
# return result, Image.fromarray(heatmap_img)
|
| 118 |
+
|
| 119 |
+
# # Gradio Interface
|
| 120 |
+
# interface = gr.Interface(
|
| 121 |
+
# fn=predict_and_heatmap,
|
| 122 |
+
# inputs=gr.Image(type="pil", label="Upload Chest X-ray"),
|
| 123 |
+
# outputs=[
|
| 124 |
+
# gr.Label(label="Prediction"),
|
| 125 |
+
# gr.Image(label="Grad-CAM Heatmap")
|
| 126 |
+
# ],
|
| 127 |
+
# title="Pneumonia Detection Using Deep Learning",
|
| 128 |
+
# description="Upload a chest X-ray to detect Pneumonia and see the heatmap visualization (Grad-CAM)."
|
| 129 |
+
# )
|
| 130 |
+
|
| 131 |
+
# if __name__ == "__main__":
|
| 132 |
+
# interface.launch()
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 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"
|
|
|
|
| 159 |
|
| 160 |
# Predict using 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 using tf-explain and VGG19
|
| 166 |
+
heatmap_img = generate_heatmap_tf_explain(image, vgg_model, class_index=class_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
return result, heatmap_img
|
| 169 |
|
| 170 |
# Gradio Interface
|
| 171 |
interface = gr.Interface(
|
|
|
|
| 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 (Grad-CAM powered by tf-explain)."
|
| 180 |
)
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
interface.launch()
|
| 184 |
+
|