Update app.py
Browse files
app.py
CHANGED
@@ -60,18 +60,11 @@ def predict_single(image_pil, weights=(0.45, 0.25, 0.30)):
|
|
60 |
return preds[0]
|
61 |
|
62 |
|
63 |
-
# ---- Grad-CAM (Version Finale
|
64 |
-
def find_last_feature_map_layer(model):
|
65 |
-
"""Trouve le nom de la dernière couche avec une sortie 4D (carte de caractéristiques)."""
|
66 |
-
for layer in reversed(model.layers):
|
67 |
-
# Vérifie de manière sûre si l'attribut existe ET si la forme est 4D
|
68 |
-
if hasattr(layer, 'output_shape') and len(layer.output_shape) == 4:
|
69 |
-
return layer.name
|
70 |
-
raise ValueError("Impossible de trouver une couche de features (sortie 4D) dans le modèle.")
|
71 |
-
|
72 |
def make_gradcam(image_pil, model, class_index=None):
|
73 |
-
#
|
74 |
-
last_conv_layer_name =
|
|
|
75 |
|
76 |
img_np = np.array(image_pil)
|
77 |
img_resized = cv2.resize(img_np, (224, 224))
|
@@ -90,12 +83,11 @@ def make_gradcam(image_pil, model, class_index=None):
|
|
90 |
class_channel = preds[:, class_index]
|
91 |
|
92 |
grads = tape.gradient(class_channel, last_conv_layer_output)
|
93 |
-
|
94 |
if grads is None:
|
95 |
-
print("Erreur: Le gradient est None.
|
96 |
-
return img_resized
|
97 |
|
98 |
-
#
|
99 |
pooled_grads = tf.reduce_mean(grads, axis=(0, 1))
|
100 |
|
101 |
last_conv_layer_output = last_conv_layer_output[0]
|
@@ -152,9 +144,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
152 |
output_label = gr.Label(label="Diagnostic global")
|
153 |
output_plot = gr.BarPlot(label="Probabilités par classe", x="Classe", y="Probabilité", y_lim=[0, 1])
|
154 |
output_gradcam = gr.Image(label="Visualisation Grad-CAM")
|
155 |
-
submit_btn.click(fn=gradio_predict, inputs=input_image, outputs=[output_label, output_plot,
|
156 |
-
#output_gradcam
|
157 |
-
])
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
demo.launch()
|
|
|
60 |
return preds[0]
|
61 |
|
62 |
|
63 |
+
# ---- Grad-CAM (Version Finale avec nom de couche fixe) ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def make_gradcam(image_pil, model, class_index=None):
|
65 |
+
# ===== CORRECTION FINALE : On utilise le nom connu et fiable de la dernière couche de features de DenseNet201 =====
|
66 |
+
last_conv_layer_name = "relu"
|
67 |
+
# =================================================================================================================
|
68 |
|
69 |
img_np = np.array(image_pil)
|
70 |
img_resized = cv2.resize(img_np, (224, 224))
|
|
|
83 |
class_channel = preds[:, class_index]
|
84 |
|
85 |
grads = tape.gradient(class_channel, last_conv_layer_output)
|
|
|
86 |
if grads is None:
|
87 |
+
print("Erreur: Le gradient est None.")
|
88 |
+
return img_resized
|
89 |
|
90 |
+
# Correction de la régression : on moyenne UNIQUEMENT sur les axes spatiaux
|
91 |
pooled_grads = tf.reduce_mean(grads, axis=(0, 1))
|
92 |
|
93 |
last_conv_layer_output = last_conv_layer_output[0]
|
|
|
144 |
output_label = gr.Label(label="Diagnostic global")
|
145 |
output_plot = gr.BarPlot(label="Probabilités par classe", x="Classe", y="Probabilité", y_lim=[0, 1])
|
146 |
output_gradcam = gr.Image(label="Visualisation Grad-CAM")
|
147 |
+
submit_btn.click(fn=gradio_predict, inputs=input_image, outputs=[output_label, output_plot, output_gradcam])
|
|
|
|
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
demo.launch()
|