Spaces:
Paused
Paused
introduction gradcam++ pour efficientnetv2m
Browse files- app/__pycache__/model.cpython-312.pyc +0 -0
- app/model.py +15 -7
app/__pycache__/model.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/model.cpython-312.pyc and b/app/__pycache__/model.cpython-312.pyc differ
|
|
|
app/model.py
CHANGED
|
@@ -8,7 +8,7 @@ from PIL import Image
|
|
| 8 |
from keras.applications.efficientnet_v2 import preprocess_input as effnet_preprocess
|
| 9 |
from keras.applications.resnet_v2 import preprocess_input as resnet_preprocess
|
| 10 |
import io
|
| 11 |
-
from tf_keras_vis.gradcam import Gradcam
|
| 12 |
from tf_keras_vis.utils import normalize
|
| 13 |
|
| 14 |
import numpy as np
|
|
@@ -38,6 +38,7 @@ class ModelStruct(TypedDict):
|
|
| 38 |
preprocess_input: Callable[[np.ndarray], Any]
|
| 39 |
target_size: tuple[int, int]
|
| 40 |
last_conv_layer:str
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def load_models() -> list[ModelStruct]:
|
|
@@ -55,7 +56,8 @@ def load_models() -> list[ModelStruct]:
|
|
| 55 |
"gradcam_model":model1_for_gradcam,
|
| 56 |
"preprocess_input": effnet_preprocess,
|
| 57 |
"target_size": (480, 480),
|
| 58 |
-
"last_conv_layer":"top_activation"
|
|
|
|
| 59 |
|
| 60 |
},
|
| 61 |
{
|
|
@@ -64,7 +66,10 @@ def load_models() -> list[ModelStruct]:
|
|
| 64 |
"model":model2,
|
| 65 |
"preprocess_input":resnet_preprocess,
|
| 66 |
"target_size":(224, 224),
|
| 67 |
-
"last_conv_layer":"conv5_block3_out"
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
}
|
| 70 |
|
|
@@ -127,7 +132,7 @@ def compute_saliency_map(model, image_array, class_index=None):
|
|
| 127 |
return saliency_map
|
| 128 |
|
| 129 |
|
| 130 |
-
def compute_gradcam(model, image_array, class_index=None, layer_name=None):
|
| 131 |
"""
|
| 132 |
Calcule la carte Grad-CAM pour une image et un modèle Keras.
|
| 133 |
|
|
@@ -140,13 +145,16 @@ def compute_gradcam(model, image_array, class_index=None, layer_name=None):
|
|
| 140 |
Returns:
|
| 141 |
gradcam_map: np.array (H, W), normalisée entre 0 et 1.
|
| 142 |
"""
|
|
|
|
| 143 |
|
| 144 |
if image_array.ndim == 3:
|
| 145 |
input_tensor = np.expand_dims(image_array, axis=0)
|
| 146 |
else:
|
| 147 |
input_tensor = image_array
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
|
| 151 |
def loss(output):
|
| 152 |
if class_index is None:
|
|
@@ -363,7 +371,7 @@ def predict_with_model(config, image_bytes: bytes,show_heatmap=False):
|
|
| 363 |
logger.info(f"🛠️ Dernière couche utilisée: {config['last_conv_layer']}")
|
| 364 |
|
| 365 |
# Calcul de la heatmap
|
| 366 |
-
heatmap = compute_gradcam(config["gradcam_model"], raw_input, class_index=predicted_class_index, layer_name=config["last_conv_layer"])
|
| 367 |
|
| 368 |
elapsed_time = time.time() - start_time
|
| 369 |
logger.info(f"✅ Heatmap générée en {elapsed_time:.2f} secondes")
|
|
|
|
| 8 |
from keras.applications.efficientnet_v2 import preprocess_input as effnet_preprocess
|
| 9 |
from keras.applications.resnet_v2 import preprocess_input as resnet_preprocess
|
| 10 |
import io
|
| 11 |
+
from tf_keras_vis.gradcam import Gradcam,GradcamPlusPlus
|
| 12 |
from tf_keras_vis.utils import normalize
|
| 13 |
|
| 14 |
import numpy as np
|
|
|
|
| 38 |
preprocess_input: Callable[[np.ndarray], Any]
|
| 39 |
target_size: tuple[int, int]
|
| 40 |
last_conv_layer:str
|
| 41 |
+
gradcam_type:str
|
| 42 |
|
| 43 |
|
| 44 |
def load_models() -> list[ModelStruct]:
|
|
|
|
| 56 |
"gradcam_model":model1_for_gradcam,
|
| 57 |
"preprocess_input": effnet_preprocess,
|
| 58 |
"target_size": (480, 480),
|
| 59 |
+
"last_conv_layer":"top_activation",
|
| 60 |
+
"gradcam_type":"gradcam++"
|
| 61 |
|
| 62 |
},
|
| 63 |
{
|
|
|
|
| 66 |
"model":model2,
|
| 67 |
"preprocess_input":resnet_preprocess,
|
| 68 |
"target_size":(224, 224),
|
| 69 |
+
"last_conv_layer":"conv5_block3_out",
|
| 70 |
+
"gradcam_type":"gradcam"
|
| 71 |
+
#"gradcam_type":"gradcam++"
|
| 72 |
+
|
| 73 |
|
| 74 |
}
|
| 75 |
|
|
|
|
| 132 |
return saliency_map
|
| 133 |
|
| 134 |
|
| 135 |
+
def compute_gradcam(model, image_array, class_index=None, layer_name=None,gradcam_type="gradcam"):
|
| 136 |
"""
|
| 137 |
Calcule la carte Grad-CAM pour une image et un modèle Keras.
|
| 138 |
|
|
|
|
| 145 |
Returns:
|
| 146 |
gradcam_map: np.array (H, W), normalisée entre 0 et 1.
|
| 147 |
"""
|
| 148 |
+
logging.info(f"Lancement calcul de la gradcam avec le type {gradcam_type}")
|
| 149 |
|
| 150 |
if image_array.ndim == 3:
|
| 151 |
input_tensor = np.expand_dims(image_array, axis=0)
|
| 152 |
else:
|
| 153 |
input_tensor = image_array
|
| 154 |
+
if gradcam_type=="gradcam++":
|
| 155 |
+
gradcam = GradcamPlusPlus(model, clone=False)
|
| 156 |
+
else:
|
| 157 |
+
gradcam = Gradcam(model, clone=False)
|
| 158 |
|
| 159 |
def loss(output):
|
| 160 |
if class_index is None:
|
|
|
|
| 371 |
logger.info(f"🛠️ Dernière couche utilisée: {config['last_conv_layer']}")
|
| 372 |
|
| 373 |
# Calcul de la heatmap
|
| 374 |
+
heatmap = compute_gradcam(config["gradcam_model"], raw_input, class_index=predicted_class_index, layer_name=config["last_conv_layer"],gradcam_type=config["gradcam_type"])
|
| 375 |
|
| 376 |
elapsed_time = time.time() - start_time
|
| 377 |
logger.info(f"✅ Heatmap générée en {elapsed_time:.2f} secondes")
|