-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotting.py
More file actions
118 lines (91 loc) · 3.45 KB
/
Copy pathplotting.py
File metadata and controls
118 lines (91 loc) · 3.45 KB
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
"""
Utilisaiton : python plotting.py --log_dir logs/mlp_debug/version_4 --out_dir plots
"""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
import pandas as pd
import torch
import matplotlib.pyplot as plt
def _find_metrics_csv(log_dir: Path) -> Path:
candidates = list(log_dir.rglob("metrics.csv"))
if not candidates:
raise FileNotFoundError(f"metrics.csv introuvable dans {log_dir}")
candidates.sort(key=lambda p: p.stat().st_mtime, reverse=True)
return candidates[0]
def _epoch_series(df: pd.DataFrame, col: str) -> pd.Series | None:
if col not in df.columns:
return None
s = df[["epoch", col]].dropna()
if s.empty:
return None
return s.groupby("epoch")[col].mean()
def plot_curves(metrics_csv: Path, out_dir: Path | None) -> None:
df = pd.read_csv(metrics_csv)
if "epoch" not in df.columns:
raise RuntimeError(f"Colonne 'epoch' introuvable dans {metrics_csv}")
# --- mIoU ---
train_miou = _epoch_series(df, "train/mIoU")
val_miou = _epoch_series(df, "val/mIoU")
plt.figure()
if train_miou is not None:
plt.plot(train_miou.index.values, train_miou.values, label="train")
if val_miou is not None:
plt.plot(val_miou.index.values, val_miou.values, label="val")
plt.xlabel("Epoch")
plt.ylabel("mIoU")
plt.title("mIoU vs Epoch")
plt.legend()
if out_dir is not None:
out_dir.mkdir(parents=True, exist_ok=True)
plt.savefig(out_dir / "miou_vs_epoch.png", dpi=150)
# --- F1 macro ---
train_f1 = _epoch_series(df, "train/F1_macro")
val_f1 = _epoch_series(df, "val/F1_macro")
plt.figure()
if train_f1 is not None:
plt.plot(train_f1.index.values, train_f1.values, label="train")
if val_f1 is not None:
plt.plot(val_f1.index.values, val_f1.values, label="val")
plt.xlabel("Epoch")
plt.ylabel("F1 (macro)")
plt.title("F1 macro vs Epoch")
plt.legend()
if out_dir is not None:
out_dir.mkdir(parents=True, exist_ok=True)
plt.savefig(out_dir / "f1_macro_vs_epoch.png", dpi=150)
plt.show()
def plot_confmat(log_dir: Path, out_dir: Path | None) -> None:
candidates = list(log_dir.rglob("confmat_final.pt"))
if not candidates:
raise FileNotFoundError(f"confmat_final.pt introuvable dans {log_dir}")
candidates.sort(key=lambda p: p.stat().st_mtime, reverse=True)
conf_path = candidates[0]
conf = torch.load(conf_path, map_location="cpu")
conf = conf.detach().cpu().numpy().astype(np.int64)
plt.figure()
plt.imshow(conf)
plt.colorbar()
plt.title("Confusion matrix (final test)")
plt.xlabel("Predicted class")
plt.ylabel("True class")
if out_dir is not None:
out_dir.mkdir(parents=True, exist_ok=True)
plt.savefig(out_dir / "confmat_final.png", dpi=150)
plt.show()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--log_dir", type=str, required=True)
parser.add_argument("--out_dir", type=str, default=None)
parser.add_argument("--no_confmat", action="store_true")
args = parser.parse_args()
log_dir = Path(args.log_dir)
out_dir = Path(args.out_dir) if args.out_dir is not None else None
metrics_csv = _find_metrics_csv(log_dir)
print("Using metrics:", metrics_csv)
plot_curves(metrics_csv, out_dir)
if not args.no_confmat:
plot_confmat(log_dir, out_dir)
if __name__ == "__main__":
main()