import os import numpy as np import gradio as gr import cv2 import tensorflow as tf import keras from keras.models import Model from keras.preprocessing import image #from tensorflow.keras.preprocessing import image from huggingface_hub import hf_hub_download import pandas as pd from PIL import Image import plotly.express as px import time import os os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' theme = gr.themes.Soft( primary_hue="purple", secondary_hue="yellow", text_size="sm", ) css = """ /* Police lisible et compatible */ body { font-family: Arial, sans-serif !important; } /* Conteneur du diagnostic global */ .diagnostic-global { margin-bottom: 15px; } /* Style pour les badges Malin/Bénin */ .highlight.malin { background-color: #F54927; color: white; padding: 4px 8px; border-radius: 4px; display: inline-block; font-weight: bold; font-size:24px; margin: 5px 0; } .highlight.benin { background-color: #34EA3A; color: black; padding: 4px 8px; border-radius: 4px; display: inline-block; font-weight: bold; font-size:24px; margin: 5px 0; } /* Conteneur pour le warning */ .warning-container { background-color: #f9f9f9; border-radius: 5px; padding: 0px; margin-bottom: 0px; border: 1px solid #e0e0e0; } /* Message d'avertissement */ .warning-message { background-color: #e9d5ff; border-radius: 3px; padding: 10px; border: 1px solid #d4b5ff; font-size: 14px; color: #333; font-family: Arial, sans-serif !important; } /* Pour les images dans le diagnostic */ .diagnostic-global img { max-width: 100%; height: auto; float: left; margin-right: 10px; margin-bottom: 10px; } .feedback-container { background-color: #f9f9f9; border-radius: 5px; padding: 0px; margin-bottom: 0px; border: 1px solid #e0e0e0; font-size:16px; font-family: Arial, sans-serif !important; } /* Clearfix pour les flottants */ .clearfix::after { content: ""; display: table; clear: both; } """ # Désactiver GPU et logs TensorFlow os.environ['CUDA_VISIBLE_DEVICES'] = '-1' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' tf.config.set_visible_devices([], 'GPU') # ---- Configuration ---- CLASS_NAMES = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel'] label_to_index = {name: i for i, name in enumerate(CLASS_NAMES)} diagnosis_map = { 'akiec': 'Bénin', 'bcc': 'Malin', 'bkl': 'Bénin', 'df': 'Bénin', 'nv': 'Bénin', 'vasc': 'Bénin', 'mel': 'Malin' } description = { "akiec": "Bénin : AKIEC ou - kératoses solaires - sont des excroissances précancéreuses provoquées par l'exposition solaire prolongée. Le risque de progression d'une Kératose Actinique vers un carcinome épidermoïde (cancer de la peau non mélanome) existe mais reste modéré", "bcc": "Malin : BCC ou - carcinome basocellulaire - est un type de cancer cutané. C’est le cancer de la peau le plus fréquent. Il se manifeste par la formation d'une masse, d'un bouton ou d'une lésion sur la couche externe de la peau.", "bkl": "Bénin : BKL ou - kératose séborrhéique - se présente sous la forme d’une lésion pigmentée, en relief, verruqueuse (qui ressemble à une verrue), souvent croûteuse, de plus ou moins grande taille.", "df": "Bénin : DF ou - dermatofibrome - est une lésion papuleuse ou nodulaire ferme, le plus souvent de petite taille, de couleur rouge marron, de nature fibrohistiocytaire.", "nv": "Bénin : NV (Nevus) ou le - grain de beauté -, couramment appelés nevus mélanocytaires représentent une accumulation localisée de mélanocytes dans la peau", "vasc": "Bénin : VASC ou - lésion vasculaire - se traduit par la survenue d’anomalies visibles en surface de la peau et d’aspect variable : rougeurs, taches planes ou en relief, capillaires sanguins apparents", "mel": "Malin : MEL ou - Mélanome - est un nodule noir ou couleur « peau » présent sur n'importe quelle partie de la peau. Sa consistance est ferme et le nodule peut s'ulcérer, se couvrir d'une croûte, suinter ou saigner." } # ---- Chargement des modèles ---- def load_models_safely(): models = {} try: print("📥 Téléchargement ResNet50...") resnet_path = hf_hub_download(repo_id="ericjedha/resnet50", filename="Resnet50.keras") models['resnet50'] = keras.saving.load_model(resnet_path, compile=False) print("✅ ResNet50 chargé") except Exception as e: models['resnet50'] = None try: print("📥 Téléchargement DenseNet201...") densenet_path = hf_hub_download(repo_id="ericjedha/densenet201", filename="Densenet201.keras") models['densenet201'] = keras.saving.load_model(densenet_path, compile=False) print("✅ DenseNet201 chargé") except Exception as e: models['densenet201'] = None try: print("📥 Chargement Xception local...") if os.path.exists("Xception.keras"): models['xception'] = keras.saving.load_model("Xception.keras", compile=False) print("✅ Xception chargé") else: models['xception'] = None except Exception as e: models['xception'] = None loaded = {k: v for k, v in models.items() if v is not None} if not loaded: raise Exception("❌ Aucun modèle n'a pu être chargé!") print(f"🎯 Modèles chargés: {list(loaded.keys())}") return models try: models_dict = load_models_safely() model_resnet50 = models_dict.get('resnet50') model_densenet = models_dict.get('densenet201') model_xcept = models_dict.get('xception') except Exception as e: print(f"🚨 ERREUR CRITIQUE: {e}") model_resnet50 = model_densenet = model_xcept = None # ---- Préprocesseurs ---- from tensorflow.keras.applications.xception import preprocess_input as preprocess_xception from tensorflow.keras.applications.resnet50 import preprocess_input as preprocess_resnet from tensorflow.keras.applications.densenet import preprocess_input as preprocess_densenet # ---- Utils ---- def _renorm_safe(p: np.ndarray) -> np.ndarray: p = np.clip(p, 0.0, None) # Évite les valeurs négatives s = np.sum(p) if s <= 0: return np.ones_like(p, dtype=np.float32) / len(p) normalized = p / s return normalized / np.sum(normalized) if np.sum(normalized) > 1.0001 else normalized def get_primary_input_name(model): if isinstance(model.inputs, list) and len(model.inputs) > 0: return model.inputs[0].name.split(':')[0] return "input_1" def _update_progress(progress, value, desc=""): """ Met à jour la barre de progression. """ if progress is not None: progress(value / 100.0, desc=desc) # ---- PREDICT SINGLE ---- def predict_single(img_input, weights=(0.45, 0.25, 0.3), normalize=True): print("🔍 DEBUG GRADIO HARMONISÉ - Début de la prédiction") # Chargement uniforme des images if isinstance(img_input, str): # Cas fichier local img_path = img_input print(f"📁 Chargement depuis fichier: {img_path}") img_raw_x = image.load_img(img_path, target_size=(299, 299)) img_raw_r = image.load_img(img_path, target_size=(224, 224)) img_raw_d = image.load_img(img_path, target_size=(224, 224)) else: # Cas upload Gradio - AMÉLIORATION ICI print("📁 Chargement depuis upload Gradio") # Option 1: Éviter la sauvegarde temporaire en utilisant BytesIO from io import BytesIO import PIL.Image # Convertir en RGB si nécessaire (évite les problèmes de format) if img_input.mode != 'RGB': img_input = img_input.convert('RGB') print(f"🔄 Conversion en RGB effectuée") # Redimensionner directement depuis PIL img_raw_x = img_input.resize((299, 299), PIL.Image.Resampling.LANCZOS) img_raw_r = img_input.resize((224, 224), PIL.Image.Resampling.LANCZOS) img_raw_d = img_input.resize((224, 224), PIL.Image.Resampling.LANCZOS) # Alternative si vous voulez garder la sauvegarde temporaire # temp_path = "temp_debug_image.png" # PNG pour éviter compression JPEG # img_input.save(temp_path, format='PNG') # img_raw_x = image.load_img(temp_path, target_size=(299, 299)) # img_raw_r = image.load_img(temp_path, target_size=(224, 224)) # img_raw_d = image.load_img(temp_path, target_size=(224, 224)) # import os # os.remove(temp_path) # Conversion en arrays avec vérification array_x = image.img_to_array(img_raw_x) array_r = image.img_to_array(img_raw_r) array_d = image.img_to_array(img_raw_d) print(f"📸 Images loaded:") print(f" Xception (299x299): {array_x.shape}") print(f" ResNet (224x224): {array_r.shape}") print(f" DenseNet (224x224): {array_d.shape}") print(f"🔧 Arrays avant preprocessing:") print(f" X shape: {array_x.shape}, dtype: {array_x.dtype}, range: [{array_x.min()}, {array_x.max()}]") print(f" R shape: {array_r.shape}, dtype: {array_r.dtype}, range: [{array_r.min()}, {array_r.max()}]") print(f" D shape: {array_d.shape}, dtype: {array_d.dtype}, range: [{array_d.min()}, {array_d.max()}]") # Vérification de cohérence avec le local expected_range = (0, 255) actual_ranges = [ (array_x.min(), array_x.max()), (array_r.min(), array_r.max()), (array_d.min(), array_d.max()) ] print(f"🔍 Vérification des ranges:") for i, (min_val, max_val) in enumerate(actual_ranges): model_name = ['Xception', 'ResNet', 'DenseNet'][i] if min_val < expected_range[0] or max_val > expected_range[1]: print(f" ⚠️ {model_name}: range inhabituel [{min_val}, {max_val}]") else: print(f" ✅ {model_name}: range normal [{min_val}, {max_val}]") # Pré-traitement identique au local img_x = np.expand_dims(preprocess_xception(array_x), axis=0) img_r = np.expand_dims(preprocess_resnet(array_r), axis=0) img_d = np.expand_dims(preprocess_densenet(array_d), axis=0) print(f"🔧 Après preprocessing:") print(f" Xception range: [{img_x.min():.6f}, {img_x.max():.6f}]") print(f" ResNet range: [{img_r.min():.6f}, {img_r.max():.6f}]") print(f" DenseNet range: [{img_d.min():.6f}, {img_d.max():.6f}]") # Suite du code identique... preds = {} if model_xcept is not None: preds['xception'] = model_xcept.predict(img_x, verbose=0)[0] print("\n--- Xception ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['xception'])): print(f"{class_name}: {prob*100:.2f}%") if model_resnet50 is not None: preds['resnet50'] = model_resnet50.predict(img_r, verbose=0)[0] print("\n--- ResNet ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['resnet50'])): print(f"{class_name}: {prob*100:.2f}%") if model_densenet is not None: preds['densenet201'] = model_densenet.predict(img_d, verbose=0)[0] print("\n--- DenseNet ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, preds['densenet201'])): print(f"{class_name}: {prob*100:.2f}%") # Combinaison pondérée ensemble = np.zeros(len(CLASS_NAMES), dtype=np.float32) if 'xception' in preds: ensemble += weights[0] * preds['xception'] if 'resnet50' in preds: ensemble += weights[1] * preds['resnet50'] if 'densenet201' in preds: ensemble += weights[2] * preds['densenet201'] print("\n--- Ensemble avant mel boost ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, ensemble)): print(f"{class_name}: {prob*100:.2f}%") print("Ensemble sum avant mel boost:", np.sum(ensemble)) # Ajustement pour "mel" mel_idx = label_to_index['mel'] if 'densenet201' in preds: old_mel_prob = ensemble[mel_idx] ensemble[mel_idx] = 0.5 * ensemble[mel_idx] + 0.5 * preds['densenet201'][mel_idx] print(f"\nMel boost: {old_mel_prob*100:.2f}% -> {ensemble[mel_idx]*100:.2f}%") print("\n--- Ensemble après mel boost ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, ensemble)): print(f"{class_name}: {prob*100:.2f}%") if normalize: ensemble_before_norm = ensemble.copy() ensemble = _renorm_safe(ensemble) print("\n--- Ensemble final après normalisation ---") for i, (class_name, prob) in enumerate(zip(CLASS_NAMES, ensemble)): print(f"{class_name}: {prob*100:.2f}%") print("Ensemble sum final:", np.sum(ensemble)) preds['ensemble'] = ensemble return preds # ---- Helpers Grad-CAM ---- LAST_CONV_LAYERS = { "xception": "block14_sepconv2_act", "resnet50": "conv5_block3_out", "densenet201": "conv5_block32_concat" } def find_last_dense_layer(model): for layer in reversed(model.layers): if isinstance(layer, keras.layers.Dense): return layer raise ValueError("Aucune couche Dense trouvée dans le modèle.") # ---- GRAD-CAM AVEC PROGRESSION OPTIMISÉE ---- def make_gradcam(image_pil, model, last_conv_layer_name, class_index, progress=None): """ Grad-CAM avec progression fluide grâce aux micro-pauses """ if model is None: return np.array(image_pil) try: steps = [ (5, "🔄 Initialisation..."), (10, "🖼️ Analyse de l'image..."), (15, "⚙️ Configuration du preprocesseur..."), (20, "📐 Redimensionnement image..."), (25, "🧠 Configuration du modèle..."), (30, "🔗 Création du gradient model..."), (35, "⚡ Préparation du calcul..."), (40, "🔥 Forward pass..."), (45, "📊 Calcul des activations..."), (50, "🎯 Extraction classe cible..."), (55, "⚡ Calcul du gradient..."), (60, "📈 Traitement des gradients..."), (70, "📊 Pooling des gradients..."), (75, "🎨 Construction heatmap..."), (80, "🌡️ Normalisation heatmap..."), (85, "🎯 Application colormap..."), (90, "🖼️ Redimensionnement final..."), (95, "✨ Superposition images..."), (100, "✅ Terminé !") ] step = 0 def next_step(): nonlocal step if step < len(steps): val, desc = steps[step] _update_progress(progress, val, desc) time.sleep(0.02) # Micro-pause pour permettre la mise à jour step += 1 next_step() # 5% - Initialisation # Détermination de la taille d'entrée et du preprocesseur input_size = model.input_shape[1:3] if 'xception' in model.name.lower(): preprocessor = preprocess_xception elif 'resnet50' in model.name.lower(): preprocessor = preprocess_resnet elif 'densenet' in model.name.lower(): preprocessor = preprocess_densenet else: preprocessor = preprocess_densenet next_step() # 10% - Analyse image next_step() # 15% - Config preprocesseur # Préparation de l'image img_np = np.array(image_pil.convert("RGB")) img_resized = cv2.resize(img_np, input_size) img_array_preprocessed = preprocessor(np.expand_dims(img_resized, axis=0)) next_step() # 20% - Redimensionnement next_step() # 25% - Config modèle # Configuration du modèle pour Grad-CAM try: conv_layer = model.get_layer(last_conv_layer_name) except ValueError: return img_resized grad_model = Model(model.inputs, [conv_layer.output, model.output]) input_name = get_primary_input_name(model) input_for_model = {input_name: img_array_preprocessed} next_step() # 30% - Gradient model next_step() # 35% - Préparation calcul next_step() # 40% - Forward pass # Le calcul critique avec étapes intermédiaires with tf.GradientTape() as tape: next_step() # 45% - Calcul activations last_conv_layer_output, preds = grad_model(input_for_model, training=False) next_step() # 50% - Extraction classe if isinstance(preds, list): preds = preds[0] class_channel = preds[:, int(class_index)] next_step() # 55% - Calcul gradient grads = tape.gradient(class_channel, last_conv_layer_output) if grads is None: return img_resized next_step() # 60% - Traitement gradients next_step() # 70% - Pooling # Pooling des gradients pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2)) last_conv_layer_output = last_conv_layer_output[0] next_step() # 75% - Construction heatmap # Construction de la heatmap heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis] heatmap = tf.squeeze(heatmap) heatmap = tf.maximum(heatmap, 0) max_val = tf.math.reduce_max(heatmap) if max_val == 0: heatmap = tf.ones_like(heatmap) * 0.5 else: heatmap = heatmap / max_val next_step() # 80% - Normalisation next_step() # 85% - Colormap # Conversion et application du colormap heatmap_np = heatmap.numpy() heatmap_np = np.clip(heatmap_np.astype(np.float32), 0, 1) heatmap_resized = cv2.resize(heatmap_np, (img_resized.shape[1], img_resized.shape[0])) heatmap_uint8 = np.uint8(255 * heatmap_resized) heatmap_colored = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET) next_step() # 90% - Redimensionnement next_step() # 95% - Superposition # Superposition des images img_bgr = cv2.cvtColor(img_resized, cv2.COLOR_RGB2BGR) superimposed_img = cv2.addWeighted(img_bgr, 0.6, heatmap_colored, 0.4, 0) next_step() # 100% - Terminé return cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB) except Exception as e: import traceback traceback.print_exc() _update_progress(progress, 100, "❌ Erreur") return np.array(image_pil) # ---- GESTION ASYNCHRONE / ÉTAT ---- current_image = None current_predictions = None # ---- Fonctions pour l'UI Gradio ---- import plotly.graph_objects as go import numpy as np import gradio as gr def quick_predict_ui(image_pil): global current_image, current_predictions if image_pil is None: # Figure vide pour le cas où il n'y a pas d'image empty_fig = go.Figure() empty_fig.update_layout( title="Veuillez uploader une image pour voir les probabilités", height=450, template="plotly_white", showlegend=False ) return ( '