Spaces:
Sleeping
Sleeping
Initial Commit
Browse files
app.py
CHANGED
@@ -83,24 +83,45 @@ custom_symptom_weightage = {
|
|
83 |
"Family Working in Public Exposed Places": 0.5,
|
84 |
}
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
# Create a bar chart to represent symptom weightage
|
105 |
def show_symptom_weightage(symptoms, weights):
|
106 |
selected_symptoms = [symptom for symptom, weight in zip(symptoms, weights) if weight > 0]
|
@@ -163,10 +184,10 @@ def predict_covid(*args):
|
|
163 |
image = args[-1]
|
164 |
|
165 |
if not any(symptoms) and image is None:
|
166 |
-
return "COVID-19 Negative", "100%", "COVID-19 Negative", "100%", "No inputs defined", None, None
|
167 |
|
168 |
if any(symptoms) and image is None:
|
169 |
-
return "NA", "NA", "NA", "NA", "Please select
|
170 |
|
171 |
# Prepare the input data for the text-based model
|
172 |
label_encoder = LabelEncoder()
|
@@ -229,7 +250,8 @@ def predict_covid(*args):
|
|
229 |
image_prediction = None
|
230 |
confidence = None
|
231 |
reason = None
|
232 |
-
|
|
|
233 |
if image is not None:
|
234 |
is_blurry = is_scan_blurry(image)
|
235 |
if is_blurry:
|
@@ -240,6 +262,7 @@ def predict_covid(*args):
|
|
240 |
"NA",
|
241 |
"Your X-Ray scan does not meet the required standards. Please ensure that your scans are not blurry, pixelated, or disfigured",
|
242 |
None,
|
|
|
243 |
None
|
244 |
)
|
245 |
|
@@ -262,6 +285,9 @@ def predict_covid(*args):
|
|
262 |
|
263 |
# Visualize the heatmap
|
264 |
heatmap_img = show_heat_map(image, [prediction for prediction, _ in ensemble_predictions])
|
|
|
|
|
|
|
265 |
|
266 |
# Provide reasoning for the prediction
|
267 |
if ensemble_prediction == 0 and text_prediction == "COVID-19 Positive":
|
@@ -294,8 +320,9 @@ def predict_covid(*args):
|
|
294 |
confidence = 0.0 if confidence is None else confidence
|
295 |
heatmap_img = "" if heatmap_img is None else heatmap_img
|
296 |
symptom_weightage_img = "" if symptom_weightage_img is None else symptom_weightage_img
|
|
|
297 |
|
298 |
-
return text_prediction, f"{text_prob_positive:.2f}%", image_prediction, f"{confidence:.2f}%", reason, heatmap_img,
|
299 |
|
300 |
# Create the input and output components
|
301 |
symptom_components = [gr_components.Checkbox(label=label) for label in column_names[:-1]]
|
@@ -306,8 +333,9 @@ output_components = [
|
|
306 |
gr_components.Label(label="Prediction based on X-Ray Scan"),
|
307 |
gr_components.Label(label="X-Ray Scan Confidence (%)"),
|
308 |
gr_components.Textbox(label="Final Prediction"),
|
|
|
309 |
gr_components.Image(label="X-Ray Prediction Heatmap"),
|
310 |
-
gr_components.Image(label="
|
311 |
]
|
312 |
|
313 |
# Create the interface and launch
|
|
|
83 |
"Family Working in Public Exposed Places": 0.5,
|
84 |
}
|
85 |
|
86 |
+
def grad_cam(model, image, class_index, layer_name):
|
87 |
+
grad_model = tf.keras.models.Model(
|
88 |
+
[model.inputs], [model.get_layer(layer_name).output, model.output]
|
89 |
+
)
|
90 |
+
with tf.GradientTape() as tape:
|
91 |
+
conv_output, predictions = grad_model(image)
|
92 |
+
loss = predictions[:, class_index]
|
93 |
+
|
94 |
+
grads = tape.gradient(loss, conv_output)
|
95 |
+
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
|
96 |
+
|
97 |
+
conv_output = conv_output.numpy()[0]
|
98 |
+
pooled_grads = pooled_grads.numpy()
|
99 |
+
for i in range(conv_output.shape[-1]):
|
100 |
+
conv_output[:, :, i] *= pooled_grads[i]
|
101 |
+
|
102 |
+
heatmap = np.mean(conv_output, axis=-1)
|
103 |
+
heatmap = np.maximum(heatmap, 0)
|
104 |
+
heatmap /= np.max(heatmap)
|
105 |
+
|
106 |
+
return heatmap
|
107 |
+
|
108 |
+
# Create a Grad CAM visualization
|
109 |
+
def show_grad_cam(image, prediction_probabilities):
|
110 |
+
image = image[np.newaxis, ...]
|
111 |
+
predicted_class_index = np.argmax(prediction_probabilities)
|
112 |
+
predicted_class_name = class_index_mapping[predicted_class_index]
|
113 |
+
|
114 |
+
heatmap = grad_cam(ensemble_models['InceptionResnetV2'], image, predicted_class_index, 'conv_7b_ac')
|
115 |
+
heatmap = cv2.resize(heatmap, (image.shape[2], image.shape[1]))
|
116 |
+
heatmap = np.uint8(255 * heatmap)
|
117 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
118 |
+
superimposed_img = cv2.addWeighted(cv2.cvtColor(np.squeeze(image), cv2.COLOR_RGB2BGR), 0.6, heatmap, 0.4, 0)
|
119 |
+
|
120 |
+
# Save the Grad CAM visualization as an image and return the file path
|
121 |
+
file_path = tempfile.NamedTemporaryFile(suffix=".png").name
|
122 |
+
cv2.imwrite(file_path, superimposed_img)
|
123 |
+
return file_path
|
124 |
+
|
125 |
# Create a bar chart to represent symptom weightage
|
126 |
def show_symptom_weightage(symptoms, weights):
|
127 |
selected_symptoms = [symptom for symptom, weight in zip(symptoms, weights) if weight > 0]
|
|
|
184 |
image = args[-1]
|
185 |
|
186 |
if not any(symptoms) and image is None:
|
187 |
+
return "COVID-19 Negative", "100%", "COVID-19 Negative", "100%", "No inputs defined", None, None, None
|
188 |
|
189 |
if any(symptoms) and image is None:
|
190 |
+
return "NA", "NA", "NA", "NA", "Please select the Symptoms and provide the Chest X-Ray Scan", None, None, None
|
191 |
|
192 |
# Prepare the input data for the text-based model
|
193 |
label_encoder = LabelEncoder()
|
|
|
250 |
image_prediction = None
|
251 |
confidence = None
|
252 |
reason = None
|
253 |
+
grad_cam_img = None
|
254 |
+
|
255 |
if image is not None:
|
256 |
is_blurry = is_scan_blurry(image)
|
257 |
if is_blurry:
|
|
|
262 |
"NA",
|
263 |
"Your X-Ray scan does not meet the required standards. Please ensure that your scans are not blurry, pixelated, or disfigured",
|
264 |
None,
|
265 |
+
None,
|
266 |
None
|
267 |
)
|
268 |
|
|
|
285 |
|
286 |
# Visualize the heatmap
|
287 |
heatmap_img = show_heat_map(image, [prediction for prediction, _ in ensemble_predictions])
|
288 |
+
|
289 |
+
# Visualize the Grad CAM for the image-based model
|
290 |
+
grad_cam_img = show_grad_cam(image, [prediction for prediction, _ in ensemble_predictions])
|
291 |
|
292 |
# Provide reasoning for the prediction
|
293 |
if ensemble_prediction == 0 and text_prediction == "COVID-19 Positive":
|
|
|
320 |
confidence = 0.0 if confidence is None else confidence
|
321 |
heatmap_img = "" if heatmap_img is None else heatmap_img
|
322 |
symptom_weightage_img = "" if symptom_weightage_img is None else symptom_weightage_img
|
323 |
+
grad_cam_img = "" if grad_cam_img is None else grad_cam_img
|
324 |
|
325 |
+
return text_prediction, f"{text_prob_positive:.2f}%", image_prediction, f"{confidence:.2f}%", reason, symptom_weightage_img, heatmap_img, grad_cam_img
|
326 |
|
327 |
# Create the input and output components
|
328 |
symptom_components = [gr_components.Checkbox(label=label) for label in column_names[:-1]]
|
|
|
333 |
gr_components.Label(label="Prediction based on X-Ray Scan"),
|
334 |
gr_components.Label(label="X-Ray Scan Confidence (%)"),
|
335 |
gr_components.Textbox(label="Final Prediction"),
|
336 |
+
gr_components.Image(label="Weightage assigned to Symptoms"),
|
337 |
gr_components.Image(label="X-Ray Prediction Heatmap"),
|
338 |
+
gr_components.Image(label="Grad CAM Visualization"),
|
339 |
]
|
340 |
|
341 |
# Create the interface and launch
|