Spaces:
Sleeping
Sleeping
Update predict.py
Browse files- predict.py +9 -73
predict.py
CHANGED
|
@@ -68,81 +68,17 @@ def predict_pytorch(image_path: str, model_path: str = "sara_model.pth") -> dict
|
|
| 68 |
|
| 69 |
def predict_tensorflow(image_path: str, model_path: str = "sara_model.h5") -> dict:
|
| 70 |
import tensorflow as tf
|
|
|
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
kwargs.pop("quantization_config", None)
|
| 80 |
-
super().__init__(*args, **kwargs)
|
| 81 |
-
|
| 82 |
-
# ── Couche SeparableConv2D patchée (même raison) ───────────────────────
|
| 83 |
-
class PatchedSeparableConv2D(tf.keras.layers.SeparableConv2D):
|
| 84 |
-
def __init__(self, *args, **kwargs):
|
| 85 |
-
kwargs.pop("quantization_config", None)
|
| 86 |
-
super().__init__(*args, **kwargs)
|
| 87 |
-
|
| 88 |
-
# ── Couche BatchNormalization patchée ──────────────────────────────────
|
| 89 |
-
class PatchedBatchNorm(tf.keras.layers.BatchNormalization):
|
| 90 |
-
def __init__(self, *args, **kwargs):
|
| 91 |
-
kwargs.pop("quantization_config", None)
|
| 92 |
-
super().__init__(*args, **kwargs)
|
| 93 |
-
|
| 94 |
-
# ── Chargement avec les couches patchées ──────────────────────────────
|
| 95 |
-
custom_objects = {
|
| 96 |
-
"Dense": PatchedDense,
|
| 97 |
-
"SeparableConv2D": PatchedSeparableConv2D,
|
| 98 |
-
"BatchNormalization": PatchedBatchNorm,
|
| 99 |
-
}
|
| 100 |
|
| 101 |
-
|
| 102 |
-
# Essai 1 : chargement normal (fonctionne si versions compatibles)
|
| 103 |
-
model = tf.keras.models.load_model(model_path)
|
| 104 |
-
print("[TF] Modèle chargé normalement.")
|
| 105 |
-
|
| 106 |
-
except Exception as e1:
|
| 107 |
-
print(f"[TF] Chargement normal échoué ({e1}), tentative avec custom_objects...")
|
| 108 |
-
|
| 109 |
-
try:
|
| 110 |
-
# Essai 2 : avec les couches patchées
|
| 111 |
-
model = tf.keras.models.load_model(
|
| 112 |
-
model_path,
|
| 113 |
-
custom_objects=custom_objects
|
| 114 |
-
)
|
| 115 |
-
print("[TF] Modèle chargé avec custom_objects.")
|
| 116 |
-
|
| 117 |
-
except Exception as e2:
|
| 118 |
-
print(f"[TF] Echec avec custom_objects ({e2}), tentative safe_mode=False...")
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
# Essai 3 : safe_mode=False (Keras 3.x uniquement)
|
| 122 |
-
model = tf.keras.models.load_model(
|
| 123 |
-
model_path,
|
| 124 |
-
safe_mode=False,
|
| 125 |
-
custom_objects=custom_objects
|
| 126 |
-
)
|
| 127 |
-
print("[TF] Modèle chargé avec safe_mode=False.")
|
| 128 |
-
|
| 129 |
-
except Exception as e3:
|
| 130 |
-
raise RuntimeError(
|
| 131 |
-
f"Impossible de charger le modèle TensorFlow.\n"
|
| 132 |
-
f"Essai 1 : {e1}\n"
|
| 133 |
-
f"Essai 2 : {e2}\n"
|
| 134 |
-
f"Essai 3 : {e3}\n\n"
|
| 135 |
-
f"Solution : Ré-entraîne et sauvegarde le modèle avec "
|
| 136 |
-
f"la même version de TensorFlow que celle du serveur."
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
# ── Prétraitement de l'image ──────────────────────────────────────────
|
| 140 |
-
img = tf.keras.utils.load_img(image_path, target_size=IMAGE_SIZE)
|
| 141 |
-
arr = tf.keras.utils.img_to_array(img) # (H, W, 3), valeurs 0-255
|
| 142 |
-
arr = arr / 255.0 # normalisation → 0-1
|
| 143 |
-
arr = np.expand_dims(arr, axis=0) # ajout dimension batch → (1, H, W, 3)
|
| 144 |
-
|
| 145 |
-
# ── Inférence ─────────────────────────────────────────────────────────
|
| 146 |
probs = model.predict(arr, verbose=0)[0]
|
| 147 |
idx = int(np.argmax(probs))
|
| 148 |
pred_class = CLASS_NAMES[idx]
|
|
|
|
| 68 |
|
| 69 |
def predict_tensorflow(image_path: str, model_path: str = "sara_model.h5") -> dict:
|
| 70 |
import tensorflow as tf
|
| 71 |
+
import numpy as np
|
| 72 |
|
| 73 |
+
# Format .h5 = compatible toutes versions Keras, chargement simple
|
| 74 |
+
model = tf.keras.models.load_model(model_path, compile=False)
|
| 75 |
+
|
| 76 |
+
# Prétraitement
|
| 77 |
+
img = tf.keras.utils.load_img(image_path, target_size=(150, 150))
|
| 78 |
+
arr = tf.keras.utils.img_to_array(img) / 255.0
|
| 79 |
+
arr = np.expand_dims(arr, axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# Prédiction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
probs = model.predict(arr, verbose=0)[0]
|
| 83 |
idx = int(np.argmax(probs))
|
| 84 |
pred_class = CLASS_NAMES[idx]
|