File size: 23,335 Bytes
26bc3cb
 
 
 
 
 
 
 
 
 
72ef639
c72538b
26bc3cb
e7db87b
 
26bc3cb
 
cdc317a
c72538b
 
 
cdc317a
 
c72538b
 
26bc3cb
 
c72538b
 
cdc317a
 
c72538b
 
26bc3cb
c72538b
26bc3cb
 
 
 
 
61d4684
 
 
 
72ef639
 
 
 
 
 
 
 
 
 
cdc317a
 
 
 
c72538b
cdc317a
 
 
 
26bc3cb
c72538b
26bc3cb
c72538b
cdc317a
 
 
 
 
 
 
 
 
 
 
26bc3cb
ff5c769
c72538b
 
cdc317a
 
 
 
26bc3cb
 
c72538b
 
26bc3cb
cdc317a
 
 
 
 
 
 
c72538b
5a128ba
cdc317a
 
5a128ba
 
 
 
 
 
 
 
 
c72538b
cdc317a
e7db87b
 
 
 
 
 
 
 
c72538b
e7db87b
cdc317a
 
26bc3cb
 
 
 
 
 
cdc317a
 
 
 
61d4684
26bc3cb
cdc317a
26bc3cb
 
 
 
 
 
 
cdc317a
26bc3cb
 
cdc317a
61d4684
 
 
 
cdc317a
61d4684
 
 
cdc317a
 
61d4684
 
 
26bc3cb
 
cdc317a
26bc3cb
 
61d4684
cdc317a
61d4684
26bc3cb
 
cdc317a
26bc3cb
 
 
 
 
 
 
ff5c769
26bc3cb
 
 
cdc317a
26bc3cb
 
 
 
61d4684
40062d1
 
61d4684
 
 
cdc317a
26bc3cb
cdc317a
26bc3cb
 
 
 
 
cdc317a
26bc3cb
 
cdc317a
 
 
26bc3cb
 
cdc317a
61d4684
cdc317a
 
 
 
 
 
c72538b
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d4684
 
 
cdc317a
61d4684
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72538b
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72538b
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26bc3cb
 
cdc317a
26bc3cb
 
61d4684
e7db87b
61d4684
cdc317a
5a128ba
26bc3cb
61d4684
cdc317a
5a128ba
26bc3cb
 
d1c85fc
 
 
 
26bc3cb
 
 
61d4684
26bc3cb
 
 
 
 
 
 
61d4684
ff5c769
26bc3cb
61d4684
 
 
 
26bc3cb
 
61d4684
 
26bc3cb
 
c72538b
26bc3cb
cdc317a
 
 
 
 
 
 
 
 
 
26bc3cb
61d4684
 
 
 
 
 
 
 
e7db87b
 
 
 
 
 
 
 
 
c72538b
e7db87b
 
 
 
 
 
 
 
 
7a0bb55
e7db87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c72538b
e7db87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d4684
 
 
cdc317a
 
 
 
c72538b
61d4684
 
 
c72538b
cdc317a
 
 
c72538b
cdc317a
c72538b
cdc317a
 
c72538b
7a0bb55
c72538b
61d4684
ff5c769
61d4684
 
 
 
 
 
 
 
 
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
c72538b
cdc317a
 
 
 
 
 
61d4684
cdc317a
c72538b
cdc317a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import os
import json
import time
from datetime import datetime
from typing import List, Tuple

import torch
import torch.nn as nn
import torch.optim as optim

from classical_ml_utils import classifier_path
from config import DATASET_DISPLAY_NAME, CLASSICAL_MODEL_TYPES, IMAGE_SIZE, session_model_dir, session_meta_dir
from data_utils import make_loaders
from metrics_utils import compute_classification_metrics, save_confusion_matrix_figure, save_loss_curve_figure
from model import SimpleCNN, BackboneWithFC, MLP


# ---------------------------------------------------------------------------
# Path helpers — tout est scopé par session_id (= session_hash Gradio, un par
# navigateur/onglet) pour qu'un·e étudiant·e ne voie jamais les modèles d'un·e
# autre alors que tous partagent le même Space.
# ---------------------------------------------------------------------------

def model_weight_path(model_name: str, session_id: str) -> str:
    return os.path.join(session_model_dir(session_id), f"{model_name}.pt")


def model_meta_path(model_name: str, session_id: str) -> str:
    return os.path.join(session_meta_dir(session_id), f"{model_name}.json")


def list_saved_models(session_id: str) -> List[str]:
    meta_dir = session_meta_dir(session_id)
    names = []
    for fn in os.listdir(meta_dir):
        if fn.endswith(".json"):
            names.append(fn[:-5])
    return sorted(names, reverse=True)


def get_runtime_device() -> torch.device:
    return torch.device("cuda" if torch.cuda.is_available() else "cpu")


def saved_model_file_path(model_name: str, session_id: str) -> str:
    """Chemin du fichier de poids téléchargeable — .joblib pour les
    classifieurs ML classiques, .pt pour les modèles neuronaux."""
    with open(model_meta_path(model_name, session_id), "r", encoding="utf-8") as f:
        model_type = json.load(f)["config"]["model_type"]
    if model_type in CLASSICAL_MODEL_TYPES:
        return classifier_path(model_name, session_id)
    return model_weight_path(model_name, session_id)


# ---------------------------------------------------------------------------
# Save / load
# ---------------------------------------------------------------------------

def save_model(model: nn.Module, model_name: str, config: dict, training_summary: dict, session_id: str):
    if config["model_type"] == "fc_head":
        state_dict = {k: v.detach().cpu() for k, v in model.classifier.state_dict().items()}
    else:
        state_dict = {k: v.detach().cpu() for k, v in model.state_dict().items()}

    torch.save(state_dict, model_weight_path(model_name, session_id))

    with open(model_meta_path(model_name, session_id), "w", encoding="utf-8") as f:
        json.dump(
            {
                "model_name": model_name,
                "config": config,
                "training_summary": training_summary,
                "created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            },
            f,
            indent=2,
            ensure_ascii=False,
        )


def _load_meta(model_name: str, session_id: str) -> dict:
    path = model_meta_path(model_name, session_id)
    if not os.path.exists(path):
        raise FileNotFoundError(f"Métadonnées introuvables : {model_name}")
    with open(path, "r", encoding="utf-8") as f:
        return json.load(f)


def load_model(model_name: str, device: torch.device, session_id: str) -> Tuple[nn.Module, dict]:
    meta = _load_meta(model_name, session_id)
    cfg = meta["config"]
    model_type = cfg.get("model_type", "cnn")

    if model_type == "fc_head":
        from backbone_utils import load_backbone
        backbone = load_backbone(device)
        model = BackboneWithFC(backbone, cfg["num_classes"], cfg.get("dropout", 0.4), cfg.get("fc_dim", 256))
        model.classifier.load_state_dict(
            torch.load(model_weight_path(model_name, session_id), map_location="cpu")
        )

    elif model_type == "cnn":
        model = SimpleCNN(
            num_classes=cfg["num_classes"],
            num_conv_blocks=cfg.get("num_conv_blocks", 3),
            base_filters=cfg.get("base_filters", 32),
            kernel_size=cfg.get("kernel_size", 3),
            use_batchnorm=cfg.get("use_batchnorm", True),
            dropout=cfg.get("dropout", 0.4),
            fc_dim=cfg.get("fc_dim", 256),
        )
        model.load_state_dict(torch.load(model_weight_path(model_name, session_id), map_location="cpu"))

    elif model_type == "mlp":
        model = MLP(
            num_classes=cfg["num_classes"],
            input_size=cfg.get("input_size", 3 * IMAGE_SIZE * IMAGE_SIZE),
            num_layers=cfg.get("num_layers", 2),
            hidden_dim=cfg.get("hidden_dim", 256),
            dropout=cfg.get("dropout", 0.4),
        )
        model.load_state_dict(torch.load(model_weight_path(model_name, session_id), map_location="cpu"))

    else:
        raise ValueError(f"load_model n'accepte pas le type '{model_type}'. Utilisez load_classical_pipeline pour les modèles ML classiques.")

    model.to(device)
    model.eval()
    return model, meta


# ---------------------------------------------------------------------------
# Training helpers
# ---------------------------------------------------------------------------

def evaluate_loss_acc(model, loader, criterion, device):
    model.eval()
    total_loss, total, correct = 0.0, 0, 0

    with torch.no_grad():
        for images, labels in loader:
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            loss = criterion(outputs, labels)
            total_loss += loss.item() * images.size(0)
            correct += (outputs.argmax(1) == labels).sum().item()
            total += labels.size(0)

    return (total_loss / total if total else 0.0), (correct / total if total else 0.0)


def collect_predictions(model, loader, device):
    model.eval()
    y_true, y_pred = [], []

    with torch.no_grad():
        for images, labels in loader:
            outputs = model(images.to(device))
            y_pred.extend(outputs.argmax(1).detach().cpu().tolist())
            y_true.extend(labels.tolist())

    return y_true, y_pred


def _training_loop(model, train_loader, val_loader, criterion, optimizer, scheduler, epochs, device):
    history = []
    logs = []
    best_val_loss = float("inf")
    best_state = None

    for epoch in range(1, epochs + 1):
        model.train()
        running_loss, total, correct = 0.0, 0, 0

        for images, labels in train_loader:
            images, labels = images.to(device), labels.to(device)
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            optimizer.step()

            running_loss += loss.item() * images.size(0)
            correct += (outputs.argmax(1) == labels).sum().item()
            total += labels.size(0)

        train_loss = running_loss / total if total else 0.0
        train_acc = correct / total if total else 0.0
        val_loss, val_acc = evaluate_loss_acc(model, val_loader, criterion, device)
        scheduler.step(val_loss)
        current_lr = optimizer.param_groups[0]["lr"]

        if val_loss < best_val_loss:
            best_val_loss = val_loss
            best_state = {k: v.detach().cpu().clone() for k, v in model.state_dict().items()}

        history.append({
            "epoch": epoch,
            "train_loss": round(train_loss, 4),
            "train_acc": round(train_acc, 4),
            "val_loss": round(val_loss, 4),
            "val_acc": round(val_acc, 4),
        })
        logs.append(
            f"Époque {epoch}/{epochs} | "
            f"perte train={train_loss:.4f} acc train={train_acc:.4f} | "
            f"perte val={val_loss:.4f} acc val={val_acc:.4f} | "
            f"lr={current_lr:.2e}"
        )

    return history, logs, best_state, best_val_loss


# ---------------------------------------------------------------------------
# Train FC head on frozen backbone
# ---------------------------------------------------------------------------

def train_fc_head(
    session_id: str,
    dropout: float = 0.4,
    fc_dim: int = 256,
    learning_rate: float = 1e-4,
    weight_decay: float = 1e-4,
    batch_size: int = 16,
    epochs: int = 20,
    model_tag: str = "",
):
    from backbone_utils import load_backbone

    device = get_runtime_device()
    train_loader, val_loader, test_loader, class_names = make_loaders(batch_size)
    num_classes = len(class_names)

    backbone = load_backbone(device)

    model = BackboneWithFC(backbone, num_classes, dropout, fc_dim).to(device)

    trainable_params = sum(p.numel() for p in model.classifier.parameters())
    total_params = sum(p.numel() for p in model.parameters())

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.AdamW(model.classifier.parameters(), lr=learning_rate, weight_decay=weight_decay)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(
        optimizer, mode="min", factor=0.5, patience=5, min_lr=learning_rate * 0.1
    )

    t0 = time.time()
    history, logs, best_state, best_val_loss = _training_loop(
        model, train_loader, val_loader, criterion, optimizer, scheduler, epochs, device
    )

    model.load_state_dict(best_state)
    test_loss, test_acc = evaluate_loss_acc(model, test_loader, criterion, device)
    y_true, y_pred = collect_predictions(model, test_loader, device)
    metrics = compute_classification_metrics(y_true, y_pred, class_names)
    elapsed = time.time() - t0

    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    safe_tag = model_tag.strip().replace(" ", "_") if model_tag.strip() else "fc_head"
    model_name = f"{safe_tag}_{timestamp}"

    cm_path = save_confusion_matrix_figure(metrics["confusion_matrix"], model_name)

    config = {
        "dataset_name": DATASET_DISPLAY_NAME,
        "model_type": "fc_head",
        "architecture": f"ResNet18 backbone (gelé) + FC({fc_dim})",
        "num_classes": num_classes,
        "class_names": class_names,
        "dropout": dropout,
        "fc_dim": fc_dim,
        "learning_rate": learning_rate,
        "weight_decay": weight_decay,
        "batch_size": batch_size,
        "epochs": epochs,
    }

    training_summary = {
        "final_train_loss": history[-1]["train_loss"] if history else None,
        "final_train_acc": history[-1]["train_acc"] if history else None,
        "best_val_loss": round(best_val_loss, 4),
        "final_val_loss": history[-1]["val_loss"] if history else None,
        "final_val_acc": history[-1]["val_acc"] if history else None,
        "test_cross_entropy_loss": round(test_loss, 4),
        "test_accuracy": round(test_acc, 4),
        "test_f1_macro": metrics["f1_macro"],
        "test_f1_weighted": metrics["f1_weighted"],
        "elapsed_seconds": round(elapsed, 2),
        "device": str(device),
        "total_params": total_params,
        "trainable_params": trainable_params,
    }

    save_model(model, model_name, config, training_summary, session_id)

    logs += [
        "",
        "Entraînement terminé.",
        f"Modèle sauvegardé : {model_name}",
        f"Architecture : {config['architecture']}",
        f"Paramètres entraînables : {trainable_params} / {total_params}",
        f"Perte test : {test_loss:.4f}  |  Accuracy test : {test_acc:.4f}",
        f"F1 macro : {metrics['f1_macro']:.4f}  |  F1 pondéré : {metrics['f1_weighted']:.4f}",
        f"Temps : {elapsed:.1f}s  |  Appareil : {device}",
    ]

    return {
        "logs": "\n".join(logs),
        "history": history,
        "summary": training_summary,
        "model_name": model_name,
        "classification_report": metrics["classification_report"],
        "confusion_matrix": metrics["confusion_matrix"],
        "confusion_matrix_path": cm_path,
    }


# ---------------------------------------------------------------------------
# Train SimpleCNN from scratch
# ---------------------------------------------------------------------------

def train_cnn(
    session_id: str,
    num_conv_blocks: int = 3,
    base_filters: int = 32,
    kernel_size: int = 3,
    use_batchnorm: bool = True,
    dropout: float = 0.4,
    fc_dim: int = 256,
    learning_rate: float = 1e-3,
    weight_decay: float = 1e-4,
    batch_size: int = 16,
    epochs: int = 30,
    model_tag: str = "",
):
    device = get_runtime_device()
    train_loader, val_loader, test_loader, class_names = make_loaders(batch_size)
    num_classes = len(class_names)

    model = SimpleCNN(
        num_classes=num_classes,
        num_conv_blocks=num_conv_blocks,
        base_filters=base_filters,
        kernel_size=kernel_size,
        use_batchnorm=use_batchnorm,
        dropout=dropout,
        fc_dim=fc_dim,
    ).to(device)

    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    total_params = sum(p.numel() for p in model.parameters())

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(
        optimizer, mode="min", factor=0.5, patience=8, min_lr=learning_rate * 0.2
    )

    t0 = time.time()
    history, logs, best_state, best_val_loss = _training_loop(
        model, train_loader, val_loader, criterion, optimizer, scheduler, epochs, device
    )

    model.load_state_dict(best_state)
    test_loss, test_acc = evaluate_loss_acc(model, test_loader, criterion, device)
    y_true, y_pred = collect_predictions(model, test_loader, device)
    metrics = compute_classification_metrics(y_true, y_pred, class_names)
    elapsed = time.time() - t0

    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    safe_tag = model_tag.strip().replace(" ", "_") if model_tag.strip() else "cnn"
    model_name = f"{safe_tag}_{timestamp}"

    cm_path = save_confusion_matrix_figure(metrics["confusion_matrix"], model_name)
    loss_curve_path = save_loss_curve_figure(history, model_name)

    architecture = f"CNN simple ({num_conv_blocks} blocs, filtres={base_filters}, noyau={kernel_size}×{kernel_size})"

    config = {
        "dataset_name": DATASET_DISPLAY_NAME,
        "model_type": "cnn",
        "architecture": architecture,
        "num_classes": num_classes,
        "class_names": class_names,
        "num_conv_blocks": num_conv_blocks,
        "base_filters": base_filters,
        "kernel_size": kernel_size,
        "use_batchnorm": use_batchnorm,
        "dropout": dropout,
        "fc_dim": fc_dim,
        "learning_rate": learning_rate,
        "weight_decay": weight_decay,
        "batch_size": batch_size,
        "epochs": epochs,
    }

    training_summary = {
        "final_train_loss": history[-1]["train_loss"] if history else None,
        "final_train_acc": history[-1]["train_acc"] if history else None,
        "best_val_loss": round(best_val_loss, 4),
        "final_val_loss": history[-1]["val_loss"] if history else None,
        "final_val_acc": history[-1]["val_acc"] if history else None,
        "test_cross_entropy_loss": round(test_loss, 4),
        "test_accuracy": round(test_acc, 4),
        "test_f1_macro": metrics["f1_macro"],
        "test_f1_weighted": metrics["f1_weighted"],
        "elapsed_seconds": round(elapsed, 2),
        "device": str(device),
        "total_params": total_params,
        "trainable_params": trainable_params,
    }

    save_model(model, model_name, config, training_summary, session_id)

    logs += [
        "",
        "Entraînement terminé.",
        f"Modèle sauvegardé : {model_name}",
        f"Architecture : {architecture}",
        f"Paramètres : {total_params}",
        f"Perte test : {test_loss:.4f}  |  Accuracy test : {test_acc:.4f}",
        f"F1 macro : {metrics['f1_macro']:.4f}  |  F1 pondéré : {metrics['f1_weighted']:.4f}",
        f"Temps : {elapsed:.1f}s  |  Appareil : {device}",
    ]

    return {
        "logs": "\n".join(logs),
        "history": history,
        "summary": training_summary,
        "model_name": model_name,
        "classification_report": metrics["classification_report"],
        "confusion_matrix": metrics["confusion_matrix"],
        "confusion_matrix_path": cm_path,
        "loss_curve_path": loss_curve_path,
    }


# ---------------------------------------------------------------------------
# Train MLP from scratch (baseline avant le CNN)
# ---------------------------------------------------------------------------

def train_mlp(
    session_id: str,
    num_layers: int = 2,
    hidden_dim: int = 256,
    dropout: float = 0.4,
    learning_rate: float = 1e-3,
    weight_decay: float = 1e-4,
    batch_size: int = 16,
    epochs: int = 30,
    model_tag: str = "",
):
    device = get_runtime_device()
    train_loader, val_loader, test_loader, class_names = make_loaders(batch_size)
    num_classes = len(class_names)
    input_size = 3 * IMAGE_SIZE * IMAGE_SIZE

    model = MLP(
        num_classes=num_classes,
        input_size=input_size,
        num_layers=num_layers,
        hidden_dim=hidden_dim,
        dropout=dropout,
    ).to(device)

    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    total_params = sum(p.numel() for p in model.parameters())

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(
        optimizer, mode="min", factor=0.5, patience=8, min_lr=learning_rate * 0.2
    )

    t0 = time.time()
    history, logs, best_state, best_val_loss = _training_loop(
        model, train_loader, val_loader, criterion, optimizer, scheduler, epochs, device
    )

    model.load_state_dict(best_state)
    test_loss, test_acc = evaluate_loss_acc(model, test_loader, criterion, device)
    y_true, y_pred = collect_predictions(model, test_loader, device)
    metrics = compute_classification_metrics(y_true, y_pred, class_names)
    elapsed = time.time() - t0

    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    safe_tag = model_tag.strip().replace(" ", "_") if model_tag.strip() else "mlp"
    model_name = f"{safe_tag}_{timestamp}"

    cm_path = save_confusion_matrix_figure(metrics["confusion_matrix"], model_name)
    loss_curve_path = save_loss_curve_figure(history, model_name)

    architecture = f"MLP ({num_layers} couches cachées de {hidden_dim} neurones)"

    config = {
        "dataset_name": DATASET_DISPLAY_NAME,
        "model_type": "mlp",
        "architecture": architecture,
        "num_classes": num_classes,
        "class_names": class_names,
        "input_size": input_size,
        "num_layers": num_layers,
        "hidden_dim": hidden_dim,
        "dropout": dropout,
        "learning_rate": learning_rate,
        "weight_decay": weight_decay,
        "batch_size": batch_size,
        "epochs": epochs,
    }

    training_summary = {
        "final_train_loss": history[-1]["train_loss"] if history else None,
        "final_train_acc": history[-1]["train_acc"] if history else None,
        "best_val_loss": round(best_val_loss, 4),
        "final_val_loss": history[-1]["val_loss"] if history else None,
        "final_val_acc": history[-1]["val_acc"] if history else None,
        "test_cross_entropy_loss": round(test_loss, 4),
        "test_accuracy": round(test_acc, 4),
        "test_f1_macro": metrics["f1_macro"],
        "test_f1_weighted": metrics["f1_weighted"],
        "elapsed_seconds": round(elapsed, 2),
        "device": str(device),
        "total_params": total_params,
        "trainable_params": trainable_params,
    }

    save_model(model, model_name, config, training_summary, session_id)

    logs += [
        "",
        "Entraînement terminé.",
        f"Modèle sauvegardé : {model_name}",
        f"Architecture : {architecture}",
        f"Paramètres : {total_params}",
        f"Perte test : {test_loss:.4f}  |  Accuracy test : {test_acc:.4f}",
        f"F1 macro : {metrics['f1_macro']:.4f}  |  F1 pondéré : {metrics['f1_weighted']:.4f}",
        f"Temps : {elapsed:.1f}s  |  Appareil : {device}",
    ]

    return {
        "logs": "\n".join(logs),
        "history": history,
        "summary": training_summary,
        "model_name": model_name,
        "classification_report": metrics["classification_report"],
        "confusion_matrix": metrics["confusion_matrix"],
        "confusion_matrix_path": cm_path,
        "loss_curve_path": loss_curve_path,
    }


# ---------------------------------------------------------------------------
# Evaluate any saved model
# ---------------------------------------------------------------------------

def evaluate_saved_model(model_name: str, session_id: str):
    if not model_name:
        raise ValueError("Aucun modèle sélectionné.")

    meta = _load_meta(model_name, session_id)
    model_type = meta["config"].get("model_type", "cnn")

    if model_type in CLASSICAL_MODEL_TYPES:
        return _evaluate_classical(model_name, meta, session_id)
    else:
        return _evaluate_neural(model_name, meta, session_id)


def _evaluate_neural(model_name: str, meta: dict, session_id: str):
    device = get_runtime_device()
    model, meta = load_model(model_name, device, session_id)

    batch_size = int(meta["config"].get("batch_size", 16))
    _, _, test_loader, class_names = make_loaders(batch_size)

    criterion = nn.CrossEntropyLoss()
    test_loss, test_acc = evaluate_loss_acc(model, test_loader, criterion, device)
    y_true, y_pred = collect_predictions(model, test_loader, device)

    metrics = compute_classification_metrics(y_true, y_pred, class_names)
    cm_path = save_confusion_matrix_figure(metrics["confusion_matrix"], model_name)

    return (
        {
            "test_cross_entropy_loss": round(test_loss, 4),
            "test_accuracy": round(test_acc, 4),
            "test_f1_macro": metrics["f1_macro"],
            "test_f1_weighted": metrics["f1_weighted"],
            "device": str(device),
        },
        metrics["classification_report"],
        metrics["confusion_matrix"],
        cm_path,
    )


def _evaluate_classical(model_name: str, meta: dict, session_id: str):
    from backbone_utils import get_cached_features, extract_all_features
    from classical_ml_utils import load_classical_pipeline

    features_cache = get_cached_features()
    if features_cache is None:
        features_cache, _, _ = extract_all_features()

    class_names = meta["config"]["class_names"]
    pipeline = load_classical_pipeline(model_name, session_id)

    X_test = features_cache["test"]["X"]
    y_test = features_cache["test"]["y"]
    y_pred = pipeline.predict(X_test)

    metrics = compute_classification_metrics(y_test.tolist(), y_pred.tolist(), class_names)
    cm_path = save_confusion_matrix_figure(metrics["confusion_matrix"], model_name)

    return (
        {
            "test_accuracy": metrics["accuracy"],
            "test_f1_macro": metrics["f1_macro"],
            "test_f1_weighted": metrics["f1_weighted"],
        },
        metrics["classification_report"],
        metrics["confusion_matrix"],
        cm_path,
    )