File size: 11,391 Bytes
8c4cb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29ffeab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c4cb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9fc500
8c4cb40
 
 
a9fc500
8c4cb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
084ab60
 
 
 
 
8c4cb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import aiohttp
import numpy as np
import logging
from PIL import Image
import io
from io import BytesIO
import base64
import numpy as np
import aiohttp
shannon_threashold=0.15
from app.model import  predict_with_model,compute_entropy_safe
logging.basicConfig(
    level=logging.INFO,  # ou logging.DEBUG
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
logger = logging.getLogger(__name__)

from scipy.spatial.distance import jensenshannon

import numpy as np
from scipy.spatial.distance import jensenshannon


def compute_js_divergence(all_probs):
    """
    Calcule la divergence de Jensen-Shannon sur une liste de distributions de probabilités.
    
    Args:
        all_probs (list of np.array): Liste des prédictions de chaque modèle (softmax).
    
    Returns:
        float: La divergence de Jensen-Shannon entre les modèles.
    """
    if len(all_probs) < 2:
        return 0.0  # Pas de désaccord possible avec un seul modèle

    # Convertir la liste en tableau numpy (shape: [nb_modèles, nb_classes])
    probs_array = np.array(all_probs)

    # Calculer la moyenne des distributions (distribution "moyenne")
    mean_probs = np.mean(probs_array, axis=0)

    # Calculer la JSD entre chaque modèle et la moyenne
    jsd_values = []
    for probs in probs_array:
        jsd = jensenshannon(probs, mean_probs, base=2)  # base=2 : divergence bornée entre 0 et 1
        jsd_values.append(jsd)

    # Retourner la moyenne des divergences
    return np.mean(jsd_values)

# Si js_divergence > 0.1 → Désaccord modéré

async def soft_voting(model_configs, image_bytes: bytes, mode, show_heatmap, default_model):
    logger.info("🔁 Début de la prédiction multi-modèles")

    all_probs = []
    models = []
    models_predictions = []
    models_confidences = []
    models_entropies = []
    models_uncertainties = []
    models_heatmaps = []

    # On commence toujours par le modèle par défaut
    default_config = next((config for config in model_configs if config["model_name"].lower() == default_model.lower()), None)

    if default_config is None:
        logger.error(f"❌ Modèle par défaut '{default_model}' introuvable dans les configurations.")
        return None

    async with aiohttp.ClientSession() as session:
        # Prédiction avec le modèle par défaut
        logger.info(f"🚀 Prédiction avec le modèle par défaut : {default_model}")
        prediction = predict_with_model(default_config, image_bytes, show_heatmap)

        all_probs.append(prediction["preds"])
        models_predictions.append(prediction["predicted_class"])
        models_confidences.append(prediction["confidence"])
        models_entropies.append(prediction["entropy"])
        models_uncertainties.append(prediction["is_uncertain_model"])
        models.append(default_config["model_name"])

        if show_heatmap:
            heatmap = prediction.get("heatmap")
            if heatmap and len(heatmap) > 0:
                models_heatmaps.append(heatmap)
            else:
                logger.warning(f"⚠️ Heatmap vide ou invalide pour le modèle {default_config['model_name']}")

        if not all_probs:
            logger.warning("⚠️ Aucune prédiction reçue, vérifie les APIs appelées.")
            raise Exception("No predictions received.")

        mean_probs = np.mean(all_probs, axis=0)
        final_class = int(np.argmax(mean_probs))
        final_confidence = float(mean_probs[final_class])
        entropy=float(compute_entropy_safe(mean_probs))
        jsd_score = float(compute_js_divergence(all_probs))

    
        logger.debug(f"🧠 Moyenne des probabilités : {mean_probs.tolist()}")


        # Mode 'single' : on s'arrête ici
        if mode == "single":
            is_global_uncertain=models_uncertainties[0]
            logger.info("🛑 Mode 'single' activé, utilisation uniquement du modèle par défaut.")

            logger.info(f"✅ Prediction terminé : classe={final_class}"
                    f"confiance={final_confidence:.4f}\n"
                    f"entropy={entropy:.4f}\n"
                    f"jsd_score={jsd_score:.4f}\n"
                    f"is_global_uncertain={is_global_uncertain}\n"
                    )
            return {
            "predicted_class": final_class,
            "confidence": final_confidence,
            "entropy":entropy,
            "jsd_score":jsd_score,
            "models": models,
            "is_global_uncertain":is_global_uncertain,
            "models_predictions": models_predictions,
            "models_confidences": models_confidences,
            "models_entropies":models_entropies,
            "models_uncertainties":models_uncertainties,
            "models_heatmaps": models_heatmaps
            
            }

        # Si mode == 'automatic' et confiance suffisante, on s'arrête
        if mode == "automatic" and prediction["confidence"] >= 0.90:
            is_global_uncertain=models_uncertainties[0]
            logger.info(f"✅ Confiance élevée ({prediction['confidence']:.2f}), pas besoin de voter.")
            logger.info(f"✅ Prediction terminé : classe={final_class}"
                    f"confiance={final_confidence:.4f}\n"
                    f"entropy={entropy:.4f}\n"
                    f"jsd_score={jsd_score:.4f}\n"
                    f"is_global_uncertain={is_global_uncertain}\n"
                    )
            return {
                "predicted_class": final_class,
                "confidence": final_confidence,
                "entropy":entropy,
                "jsd_score":jsd_score,
                "models": models,
                "is_global_uncertain":is_global_uncertain,
                "models_predictions": models_predictions,
                "models_confidences": models_confidences,
                "models_entropies":models_entropies,
                "models_uncertainties":models_uncertainties,
                "models_heatmaps": models_heatmaps
            
            }

        # Sinon, on continue avec tous les autres modèles (voting ou automatic avec faible confiance)
        logger.info(f"🔍 Mode '{mode}' : Prédictions complémentaires en cours.")

        for config in model_configs:
            if config["model_name"].lower() == default_model.lower():
                continue  # On a déjà traité le modèle par défaut

            prediction = predict_with_model(config, image_bytes, show_heatmap)

            all_probs.append(prediction["preds"])
            models_predictions.append(prediction["predicted_class"])
            models_confidences.append(prediction["confidence"])
            models_entropies.append(prediction["entropy"])
            models_uncertainties.append(prediction["is_uncertain_model"])
            models.append(config["model_name"])

            

            if show_heatmap:
                heatmap = prediction.get("heatmap")
                if heatmap and len(heatmap) > 0:
                    models_heatmaps.append(heatmap)
                else:
                    logger.warning(f"⚠️ Heatmap vide ou invalide pour le modèle {config['model_name']}")
        
        mean_probs = np.mean(all_probs, axis=0)
        final_class = int(np.argmax(mean_probs))
        final_confidence = float(mean_probs[final_class])
        entropy=float(compute_entropy_safe(mean_probs))
        jsd_score = float(compute_js_divergence(all_probs))

        is_global_uncertain = any(models_uncertainties) and jsd_score > shannon_threashold
        logger.info(f"✅ Prediction terminé : classe={final_class}"
                    f"confiance={final_confidence:.4f}\n"
                    f"entropy={entropy:.4f}\n"
                    f"jsd_score={jsd_score:.4f}\n"
                    f"is_global_uncertain={is_global_uncertain}\n"
                    )
        return {
                "predicted_class": final_class,
                "confidence": final_confidence,
                "entropy":entropy,
                "jsd_score":jsd_score,
                "models": models,
                "is_global_uncertain":is_global_uncertain,
                "models_predictions": models_predictions,
                "models_confidences": models_confidences,
                "models_entropies":models_entropies,
                "models_uncertainties":models_uncertainties,
                "models_heatmaps": models_heatmaps
            
            }


async def soft_voting_v1(model_configs,image_bytes: bytes,mode,show_heatmap,default_model):
    logger.info("🔁 Début du vote multi-modèles")
    all_probs = []
    models = []
    models_predictions = []
    models_confidences = []
    models_entropies = []
    models_uncertainties = []
    models_heatmaps=[]
    async with aiohttp.ClientSession() as session:
        for config in model_configs:
            prediction=predict_with_model(config,image_bytes,show_heatmap)
            all_probs.append(prediction["preds"])
           
            models_predictions.append(prediction["predicted_class"])
          
            models_confidences.append(prediction["confidence"])

            models_entropies.append(prediction["entropy"])

            models_uncertainties.append(prediction["is_uncertain_model"])

            if show_heatmap:
                heatmap = prediction.get("heatmap")
                if heatmap and len(heatmap) > 0:
                    models_heatmaps.append(heatmap)
                else:
                    logger.warning(f"⚠️ Heatmap vide ou invalide, non ajoutée pour le modèle {config['model_name']}")
                logger.info(f"Taille heatmaps :{len(models_heatmaps)}")    
            
            models.append(config["model_name"])
            logger.info(f"📊 Prédictions ajoutées pour {config['model_name']}")
            if mode == "single":
                logger.info("🛑 Mode 'single' activé, arrêt après le premier modèle.")
                break
                
    

    if not all_probs:
        logger.warning("⚠️ Aucune prédiction reçue, vérifie les APIs appelées.")
        raise Exception("No predictions received.")

    mean_probs = np.mean(all_probs, axis=0)
    final_class = int(np.argmax(mean_probs))
    final_confidence = float(mean_probs[final_class])
    entropy=float(compute_entropy_safe(mean_probs))
    jsd_score = float(compute_js_divergence(all_probs))
    if mode=='single':
        is_global_uncertain=models_uncertainties[0]
    else:
        is_global_uncertain = any(models_uncertainties) and jsd_score > shannon_threashold

    logger.info(f"✅ Vote terminé : classe={final_class}"
                f"confiance={final_confidence:.4f}\n"
                f"entropy={entropy:.4f}\n"
                f"jsd_score={jsd_score:.4f}\n"
                f"is_global_uncertain={is_global_uncertain}\n"
                )
    logger.debug(f"🧠 Moyenne des probabilités : {mean_probs.tolist()}")

    return {
        "predicted_class": final_class,
        "confidence": final_confidence,
        "entropy":entropy,
        "jsd_score":jsd_score,
        "models": models,
        "is_global_uncertain":is_global_uncertain,
        "models_predictions": models_predictions,
        "models_confidences": models_confidences,
        "models_entropies":models_entropies,
        "models_uncertainties":models_uncertainties,
        "models_heatmaps": models_heatmaps
        
    }