ericjedha commited on
Commit
baa141b
·
verified ·
1 Parent(s): 0c06537

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -11
app.py CHANGED
@@ -154,23 +154,51 @@ def _update_progress(progress, value, desc=""):
154
  progress(value / 100.0, desc=desc)
155
 
156
  # ---- PREDICT SINGLE ----
 
 
157
  def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True):
158
- # Chargement et pré-traitement (avec logs)
 
 
159
  if isinstance(img_input, str):
160
- pil_img = Image.open(img_input).convert("RGB")
 
 
 
161
  else:
162
- pil_img = img_input.convert("RGB")
163
-
164
- # Pré-traitement Xception
165
- img_x = np.expand_dims(preprocess_xception(np.array(pil_img.resize((299, 299)))), axis=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  print("Xception range (Gradio):", np.min(img_x), np.max(img_x))
167
 
168
- # Pré-traitement ResNet
169
- img_r = np.expand_dims(preprocess_resnet(np.array(pil_img.resize((224, 224)))), axis=0)
170
  print("ResNet range (Gradio):", np.min(img_r), np.max(img_r))
171
 
172
- # Pré-traitement DenseNet
173
- img_d = np.expand_dims(preprocess_densenet(np.array(pil_img.resize((224, 224)))), axis=0)
174
  print("DenseNet range (Gradio):", np.min(img_d), np.max(img_d))
175
 
176
  # Prédictions
@@ -180,11 +208,13 @@ def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True):
180
  print("\n--- Xception (Gradio) ---")
181
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['xception'])):
182
  print(f"{class_name}: {prob*100:.2f}%")
 
183
  if model_resnet50 is not None:
184
  preds['resnet50'] = model_resnet50.predict(img_r, verbose=0)[0]
185
  print("\n--- ResNet (Gradio) ---")
186
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['resnet50'])):
187
  print(f"{class_name}: {prob*100:.2f}%")
 
188
  if model_densenet is not None:
189
  preds['densenet201'] = model_densenet.predict(img_d, verbose=0)[0]
190
  print("\n--- DenseNet (Gradio) ---")
@@ -220,7 +250,6 @@ def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True):
220
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, ensemble)):
221
  print(f"{class_name}: {prob*100:.2f}%")
222
  print("Ensemble sum final (Gradio):", np.sum(ensemble))
223
-
224
  # Afficher l'impact de la normalisation
225
  print("\n--- Impact de la normalisation ---")
226
  for i, (class_name, prob_before, prob_after) in enumerate(zip(CLASS_NAMES, ensemble_before_norm, ensemble)):
@@ -231,6 +260,7 @@ def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True):
231
 
232
 
233
 
 
234
  # ---- Helpers Grad-CAM ----
235
  LAST_CONV_LAYERS = {
236
  "xception": "block14_sepconv2_act",
 
154
  progress(value / 100.0, desc=desc)
155
 
156
  # ---- PREDICT SINGLE ----
157
+ from tensorflow.keras.preprocessing import image
158
+
159
  def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True):
160
+ print("🔍 DEBUG GRADIO - Début de la prédiction")
161
+
162
+ # Chargement et pré-traitement avec Keras (comme en local)
163
  if isinstance(img_input, str):
164
+ # Chargement depuis un chemin
165
+ img_raw_x = image.load_img(img_input, target_size=(299, 299), color_mode="rgb")
166
+ img_raw_r = image.load_img(img_input, target_size=(224, 224), color_mode="rgb")
167
+ img_raw_d = image.load_img(img_input, target_size=(224, 224), color_mode="rgb")
168
  else:
169
+ # Cas d'upload via interface Gradio (img_input est déjà une image)
170
+ # Conversion en array puis rechargement avec Keras pour uniformiser
171
+ temp_path = "temp_debug_image.jpg"
172
+ img_input.save(temp_path) # Sauvegarde temporaire
173
+ img_raw_x = image.load_img(temp_path, target_size=(299, 299), color_mode="rgb")
174
+ img_raw_r = image.load_img(temp_path, target_size=(224, 224), color_mode="rgb")
175
+ img_raw_d = image.load_img(temp_path, target_size=(224, 224), color_mode="rgb")
176
+ import os
177
+ os.remove(temp_path) # Nettoyage
178
+
179
+ print(f"📸 Images loaded:")
180
+ print(f" Xception (299x299): {np.array(img_raw_x).shape}")
181
+ print(f" ResNet (224x224): {np.array(img_raw_r).shape}")
182
+ print(f" DenseNet (224x224): {np.array(img_raw_d).shape}")
183
+
184
+ # Conversion en arrays
185
+ array_x = image.img_to_array(img_raw_x)
186
+ array_r = image.img_to_array(img_raw_r)
187
+ array_d = image.img_to_array(img_raw_d)
188
+
189
+ print(f"🔧 Arrays avant preprocessing:")
190
+ print(f" X shape: {array_x.shape}, dtype: {array_x.dtype}, range: [{array_x.min()}, {array_x.max()}]")
191
+ print(f" R shape: {array_r.shape}, dtype: {array_r.dtype}, range: [{array_r.min()}, {array_r.max()}]")
192
+ print(f" D shape: {array_d.shape}, dtype: {array_d.dtype}, range: [{array_d.min()}, {array_d.max()}]")
193
+
194
+ # Pré-traitement
195
+ img_x = np.expand_dims(preprocess_xception(array_x), axis=0)
196
  print("Xception range (Gradio):", np.min(img_x), np.max(img_x))
197
 
198
+ img_r = np.expand_dims(preprocess_resnet(array_r), axis=0)
 
199
  print("ResNet range (Gradio):", np.min(img_r), np.max(img_r))
200
 
201
+ img_d = np.expand_dims(preprocess_densenet(array_d), axis=0)
 
202
  print("DenseNet range (Gradio):", np.min(img_d), np.max(img_d))
203
 
204
  # Prédictions
 
208
  print("\n--- Xception (Gradio) ---")
209
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['xception'])):
210
  print(f"{class_name}: {prob*100:.2f}%")
211
+
212
  if model_resnet50 is not None:
213
  preds['resnet50'] = model_resnet50.predict(img_r, verbose=0)[0]
214
  print("\n--- ResNet (Gradio) ---")
215
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['resnet50'])):
216
  print(f"{class_name}: {prob*100:.2f}%")
217
+
218
  if model_densenet is not None:
219
  preds['densenet201'] = model_densenet.predict(img_d, verbose=0)[0]
220
  print("\n--- DenseNet (Gradio) ---")
 
250
  for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, ensemble)):
251
  print(f"{class_name}: {prob*100:.2f}%")
252
  print("Ensemble sum final (Gradio):", np.sum(ensemble))
 
253
  # Afficher l'impact de la normalisation
254
  print("\n--- Impact de la normalisation ---")
255
  for i, (class_name, prob_before, prob_after) in enumerate(zip(CLASS_NAMES, ensemble_before_norm, ensemble)):
 
260
 
261
 
262
 
263
+
264
  # ---- Helpers Grad-CAM ----
265
  LAST_CONV_LAYERS = {
266
  "xception": "block14_sepconv2_act",