File size: 11,729 Bytes
26bc3cb
 
 
 
 
 
 
 
 
 
61d4684
26bc3cb
61d4684
5a128ba
26bc3cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d4684
 
 
 
26bc3cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d4684
ff5c769
26bc3cb
61d4684
26bc3cb
 
 
 
 
 
5a128ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26bc3cb
 
 
 
 
 
 
 
 
61d4684
26bc3cb
61d4684
26bc3cb
 
 
 
 
 
 
 
 
 
 
 
 
61d4684
26bc3cb
 
 
61d4684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff5c769
61d4684
 
 
 
 
 
 
26bc3cb
 
 
5a128ba
d1c85fc
 
 
 
ff5c769
 
d1c85fc
ff5c769
15c5bd9
ff5c769
 
26bc3cb
 
 
 
 
 
5a128ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26bc3cb
61d4684
 
 
26bc3cb
ff5c769
61d4684
 
 
 
 
26bc3cb
40062d1
 
 
 
 
 
 
 
 
 
26bc3cb
 
 
 
61d4684
 
 
26bc3cb
 
61d4684
26bc3cb
 
 
 
 
 
 
 
 
61d4684
26bc3cb
 
ff5c769
 
 
 
26bc3cb
 
 
61d4684
26bc3cb
 
 
 
 
 
61d4684
 
40062d1
 
61d4684
 
 
 
 
 
 
26bc3cb
 
 
 
 
 
 
 
61d4684
26bc3cb
 
 
 
 
40062d1
 
26bc3cb
 
61d4684
 
 
 
 
 
 
 
26bc3cb
 
 
61d4684
26bc3cb
 
61d4684
 
5a128ba
 
 
 
 
26bc3cb
61d4684
5a128ba
 
26bc3cb
 
d1c85fc
 
 
 
26bc3cb
 
 
61d4684
26bc3cb
 
 
 
 
 
 
61d4684
ff5c769
26bc3cb
61d4684
 
 
 
26bc3cb
 
61d4684
 
26bc3cb
 
 
 
 
 
 
 
5a128ba
61d4684
 
 
 
 
 
26bc3cb
 
61d4684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff5c769
61d4684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 config import MODEL_DIR, META_DIR, DATASET_DISPLAY_NAME
from data_utils import make_loaders
from metrics_utils import compute_classification_metrics, save_confusion_matrix_figure
from model import SimpleCNN, ResNet18Classifier


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


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


def list_saved_models() -> List[str]:
    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 save_model(model: nn.Module, model_name: str, config: dict, training_summary: dict):
    cpu_state_dict = {k: v.detach().cpu() for k, v in model.state_dict().items()}
    torch.save(cpu_state_dict, model_weight_path(model_name))

    payload = {
        "model_name": model_name,
        "config": config,
        "training_summary": training_summary,
        "created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    }

    with open(model_meta_path(model_name), "w", encoding="utf-8") as f:
        json.dump(payload, f, indent=2, ensure_ascii=False)


def load_model(model_name: str, device: torch.device) -> Tuple[nn.Module, dict]:
    meta_file = model_meta_path(model_name)
    weight_file = model_weight_path(model_name)

    if not os.path.exists(meta_file):
        raise FileNotFoundError(f"Métadonnées introuvables pour le modèle : {model_name}")

    if not os.path.exists(weight_file):
        raise FileNotFoundError(f"Poids introuvables pour le modèle : {model_name}")

    with open(meta_file, "r", encoding="utf-8") as f:
        meta = json.load(f)

    cfg = meta["config"]

    if cfg.get("model_type", "cnn") == "resnet18":
        model = ResNet18Classifier(
            num_classes=cfg["num_classes"],
            dropout=cfg.get("dropout", 0.4),
            fc_dim=cfg.get("fc_dim", 256),
        )
    else:
        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),
        )

    state_dict = torch.load(weight_file, map_location="cpu")
    model.load_state_dict(state_dict)
    model.to(device)
    model.eval()

    return model, meta


def evaluate_loss_acc(model, loader, criterion, device):
    model.eval()

    total_loss = 0.0
    total = 0
    correct = 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)
            preds = outputs.argmax(dim=1)

            correct += (preds == labels).sum().item()
            total += labels.size(0)

    avg_loss = total_loss / total if total else 0.0
    acc = correct / total if total else 0.0

    return avg_loss, acc


def collect_predictions(model, loader, device):
    model.eval()

    y_true = []
    y_pred = []

    with torch.no_grad():
        for images, labels in loader:
            images = images.to(device)

            outputs = model(images)
            preds = outputs.argmax(dim=1).detach().cpu().tolist()

            y_pred.extend(preds)
            y_true.extend(labels.tolist())

    return y_true, y_pred


def train_model(
    model_type: str = "cnn",
    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 = 0.001,
    weight_decay: float = 0.0001,
    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)

    if model_type == "resnet18":
        model = ResNet18Classifier(
            num_classes=num_classes,
            dropout=dropout,
            fc_dim=fc_dim,
        ).to(device)
    else:
        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(
        filter(lambda p: p.requires_grad, model.parameters()),
        lr=learning_rate,
        weight_decay=weight_decay,
    )

    # Réduit le LR de moitié si val_loss ne s'améliore pas pendant 8 époques
    # patience élevée car le val set est très petit (bruit important)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(
        optimizer,
        mode="min",
        factor=0.5,
        patience=8,
        min_lr=learning_rate * 0.2,
    )

    history = []
    logs = []
    start_time = time.time()

    best_val_loss = float("inf")
    best_state_dict = None

    for epoch in range(1, epochs + 1):
        model.train()

        running_loss = 0.0
        total = 0
        correct = 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()

            # Important: prevents unstable fine-tuning / exploding gradients
            torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)

            optimizer.step()

            running_loss += loss.item() * images.size(0)

            preds = outputs.argmax(dim=1)
            correct += (preds == 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_dict = {
                k: v.detach().cpu().clone()
                for k, v in model.state_dict().items()
            }

        row = {
            "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),
        }

        history.append(row)

        logs.append(
            f"Époque {epoch}/{epochs} | "
            f"perte entraînement={train_loss:.4f}, précision entraînement={train_acc:.4f}, "
            f"perte validation={val_loss:.4f}, précision validation={val_acc:.4f}, "
            f"lr={current_lr:.6f}"
        )

    if best_state_dict is not None:
        model.load_state_dict(best_state_dict)

    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() - start_time

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

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

    if model_type == "resnet18":
        architecture = "ResNet18 pré-entraîné (layer4 + classifieur)"
    else:
        architecture = f"CNN simple ({num_conv_blocks} blocs, filtres={base_filters}, noyau={kernel_size}x{kernel_size})"

    config = {
        "dataset_name": DATASET_DISPLAY_NAME,
        "model_type": model_type,
        "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)

    logs.append("")
    logs.append("Entraînement terminé.")
    logs.append(f"Modèle sauvegardé : {model_name}")
    logs.append(f"Appareil utilisé : {device}")
    logs.append(f"Architecture : {architecture}")
    logs.append(f"Nombre total de paramètres : {total_params}")
    logs.append(f"Paramètres entraînables : {trainable_params}")
    logs.append(f"Perte test cross-entropy : {test_loss:.4f}")
    logs.append(f"Accuracy test : {test_acc:.4f}")
    logs.append(f"F1 macro test : {metrics['f1_macro']:.4f}")
    logs.append(f"F1 pondéré test : {metrics['f1_weighted']:.4f}")
    logs.append(f"Temps écoulé : {elapsed:.1f}s")

    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,
    }


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

    device = get_runtime_device()
    model, meta = load_model(model_name, device)

    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)

    summary = {
        "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),
    }

    return summary, metrics["classification_report"], metrics["confusion_matrix"], cm_path