Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import tensorflow as tf
|
|
8 |
import keras
|
9 |
from keras.models import Model
|
10 |
from keras.preprocessing import image
|
|
|
11 |
|
12 |
# ---- Configuration ----
|
13 |
CLASS_NAMES = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
|
@@ -22,23 +23,15 @@ diagnosis_map = {
|
|
22 |
'mel': 'Malin'
|
23 |
}
|
24 |
|
25 |
-
|
26 |
-
import keras
|
27 |
-
|
28 |
-
# Télécharge le fichier .keras localement
|
29 |
resnet_path = hf_hub_download(repo_id="ericjedha/resnet50", filename="Resnet50.keras")
|
30 |
densenet_path = hf_hub_download(repo_id="ericjedha/densenet201", filename="Densenet201.keras")
|
31 |
|
32 |
-
#
|
33 |
model_resnet50 = keras.saving.load_model(resnet_path, compile=False)
|
34 |
model_densenet = keras.saving.load_model(densenet_path, compile=False)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
# ---- Chargement modèles ----
|
39 |
model_xcept = keras.saving.load_model("Xception.keras", compile=False)
|
40 |
|
41 |
-
|
42 |
# ---- Préprocesseurs ----
|
43 |
from tensorflow.keras.applications.xception import preprocess_input as preprocess_xception
|
44 |
from tensorflow.keras.applications.resnet50 import preprocess_input as preprocess_resnet
|
@@ -54,9 +47,9 @@ def predict_single(img_path, weights=(0.45, 0.25, 0.30)):
|
|
54 |
br = preprocess_resnet(np.expand_dims(load_image(img_path, (224, 224)), axis=0))
|
55 |
bd = preprocess_densenet(np.expand_dims(load_image(img_path, (224, 224)), axis=0))
|
56 |
|
57 |
-
pred_x = model_xcept.predict(bx, verbose=0)
|
58 |
-
pred_r = model_resnet50.predict(br, verbose=0)
|
59 |
-
pred_d = model_densenet.predict(bd, verbose=0)
|
60 |
|
61 |
preds = (weights[0] * pred_x + weights[1] * pred_r + weights[2] * pred_d)
|
62 |
|
@@ -75,6 +68,7 @@ def make_gradcam(img_path, model, last_conv_layer_name="conv5_block32_concat", c
|
|
75 |
|
76 |
if class_index is None:
|
77 |
preds = model.predict(input_array)
|
|
|
78 |
class_index = np.argmax(preds[0])
|
79 |
|
80 |
grad_model = Model(inputs=model.inputs, outputs=[
|
@@ -84,6 +78,12 @@ def make_gradcam(img_path, model, last_conv_layer_name="conv5_block32_concat", c
|
|
84 |
|
85 |
with tf.GradientTape() as tape:
|
86 |
conv_outputs, predictions = grad_model(input_array)
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
loss = predictions[:, class_index]
|
88 |
|
89 |
grads = tape.gradient(loss, conv_outputs)[0]
|
|
|
8 |
import keras
|
9 |
from keras.models import Model
|
10 |
from keras.preprocessing import image
|
11 |
+
from huggingface_hub import hf_hub_download
|
12 |
|
13 |
# ---- Configuration ----
|
14 |
CLASS_NAMES = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
|
|
|
23 |
'mel': 'Malin'
|
24 |
}
|
25 |
|
26 |
+
# ---- Téléchargement des modèles depuis Hugging Face ----
|
|
|
|
|
|
|
27 |
resnet_path = hf_hub_download(repo_id="ericjedha/resnet50", filename="Resnet50.keras")
|
28 |
densenet_path = hf_hub_download(repo_id="ericjedha/densenet201", filename="Densenet201.keras")
|
29 |
|
30 |
+
# ---- Chargement modèles ----
|
31 |
model_resnet50 = keras.saving.load_model(resnet_path, compile=False)
|
32 |
model_densenet = keras.saving.load_model(densenet_path, compile=False)
|
|
|
|
|
|
|
|
|
33 |
model_xcept = keras.saving.load_model("Xception.keras", compile=False)
|
34 |
|
|
|
35 |
# ---- Préprocesseurs ----
|
36 |
from tensorflow.keras.applications.xception import preprocess_input as preprocess_xception
|
37 |
from tensorflow.keras.applications.resnet50 import preprocess_input as preprocess_resnet
|
|
|
47 |
br = preprocess_resnet(np.expand_dims(load_image(img_path, (224, 224)), axis=0))
|
48 |
bd = preprocess_densenet(np.expand_dims(load_image(img_path, (224, 224)), axis=0))
|
49 |
|
50 |
+
pred_x = np.array(model_xcept.predict(bx, verbose=0))
|
51 |
+
pred_r = np.array(model_resnet50.predict(br, verbose=0))
|
52 |
+
pred_d = np.array(model_densenet.predict(bd, verbose=0))
|
53 |
|
54 |
preds = (weights[0] * pred_x + weights[1] * pred_r + weights[2] * pred_d)
|
55 |
|
|
|
68 |
|
69 |
if class_index is None:
|
70 |
preds = model.predict(input_array)
|
71 |
+
preds = np.array(preds)
|
72 |
class_index = np.argmax(preds[0])
|
73 |
|
74 |
grad_model = Model(inputs=model.inputs, outputs=[
|
|
|
78 |
|
79 |
with tf.GradientTape() as tape:
|
80 |
conv_outputs, predictions = grad_model(input_array)
|
81 |
+
|
82 |
+
# ✅ Corrige : forcer predictions en Tensor
|
83 |
+
if isinstance(predictions, list):
|
84 |
+
predictions = predictions[0]
|
85 |
+
predictions = tf.convert_to_tensor(predictions)
|
86 |
+
|
87 |
loss = predictions[:, class_index]
|
88 |
|
89 |
grads = tape.gradient(loss, conv_outputs)[0]
|