Repository navigation
Expand file tree
/
Copy pathreport.py
More file actions
571 lines (526 loc) · 23.2 KB
/
Copy pathreport.py
File metadata and controls
571 lines (526 loc) · 23.2 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
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
"""Build a retained, quantitative report from completed benchmark seed runs."""
import argparse
import json
import math
from pathlib import Path
from statistics import mean, stdev
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
import numpy as np # noqa: E402
METRICS = {
"accuracy": ("Top-1 accuracy", True),
"balanced_accuracy": ("Balanced accuracy", True),
"macro_precision": ("Macro precision", True),
"macro_recall": ("Macro recall", True),
"macro_f1": ("Macro F1", True),
"weighted_f1": ("Weighted F1", True),
"top3_accuracy": ("Top-3 accuracy", True),
"top5_accuracy": ("Top-5 accuracy", True),
"negative_log_likelihood": ("Negative log likelihood", False),
"brier_score": ("Multiclass Brier score", False),
"ece": ("Expected calibration error", True),
"mce": ("Maximum calibration error", True),
"mean_confidence": ("Mean confidence", True),
"confidence_correct": ("Confidence when correct", True),
"confidence_incorrect": ("Confidence when incorrect", True),
"multiclass_roc_auc_ovr_macro": ("Macro one-vs-rest ROC AUC", False),
"average_precision_macro": ("Macro average precision", False),
}
def _read(path: Path):
return json.loads(path.read_text())
def _numeric(value) -> bool:
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
def _flatten_scalars(value: dict, prefix: str = "") -> dict:
result = {}
for key, item in value.items():
name = f"{prefix}.{key}" if prefix else key
if isinstance(item, dict):
result.update(_flatten_scalars(item, name))
elif _numeric(item):
result[name] = item
return result
def _aggregate(values: list[float]) -> dict:
return {
"count": len(values),
"mean": mean(values),
"sample_std": stdev(values) if len(values) > 1 else None,
"min": min(values),
"max": max(values),
}
def _format(value, percent: bool = False) -> str:
if not _numeric(value):
return "—"
return f"{100 * value:.2f}%" if percent else f"{value:.4f}"
def _mib(value) -> str:
return _format(value / 2**20) if _numeric(value) else "—"
def _ci(value: dict | None, percent: bool = True) -> str:
if not value:
return "—"
return f"[{_format(value.get('lower'), percent)}, {_format(value.get('upper'), percent)}]"
def _load_runs(output_dir: Path) -> list[dict]:
runs = []
for directory in output_dir.glob("seed-*"):
if not directory.is_dir() or not directory.name[5:].isdigit():
continue
required = [
directory / name for name in ("metrics.json", "performance.json", "history.json")
]
if not all(path.is_file() for path in required):
continue
metrics, performance, history = map(_read, required)
if "test" not in metrics or not history:
continue
runs.append(
{
"seed": int(directory.name[5:]),
"directory": directory,
"metrics": metrics,
"performance": performance,
"history": history,
}
)
return sorted(runs, key=lambda run: run["seed"])
def _save_figure(fig, directory: Path, name: str) -> str:
fig.savefig(directory / name, dpi=180, bbox_inches="tight", facecolor="white")
plt.close(fig)
return f"figures/{name}"
def _learning_curves(runs: list[dict], directory: Path) -> str:
fig, axes = plt.subplots(1, 2, figsize=(11, 4), layout="constrained")
colors = plt.get_cmap("tab10").colors
for index, run in enumerate(runs):
history = run["history"]
epochs = [row["epoch"] for row in history]
color = colors[index % len(colors)]
for ax, metric, scale in zip(axes, ("loss", "accuracy"), (1, 100), strict=True):
ax.plot(
epochs,
[scale * row[f"train_{metric}"] for row in history],
color=color,
linestyle="--",
label=f"Seed {run['seed']} train",
)
ax.plot(
epochs,
[scale * row[f"val_{metric}"] for row in history],
color=color,
label=f"Seed {run['seed']} validation",
)
ax.set_xlabel("Epoch")
ax.grid(alpha=0.2)
axes[0].set_ylabel("Cross-entropy loss")
axes[1].set_ylabel("Accuracy (%)")
axes[0].set_title("Loss during training")
axes[1].set_title("Accuracy during training")
axes[1].legend(fontsize=8)
return _save_figure(fig, directory, "learning_curves.png")
def _confusion_plot(test: dict, directory: Path, seed: int) -> str:
matrix = np.asarray(test["confusion_matrix"], dtype=float)
support = matrix.sum(axis=1, keepdims=True)
normalized = np.divide(matrix, support, out=np.zeros_like(matrix), where=support != 0)
names = test["class_names"]
fig, ax = plt.subplots(figsize=(9, 8), layout="constrained")
display = ax.imshow(100 * normalized, cmap="Blues", vmin=0, vmax=100)
ax.set_xticks(range(len(names)), names, rotation=45, ha="right")
ax.set_yticks(range(len(names)), names)
ax.set_xlabel("Predicted class")
ax.set_ylabel("Actual class")
ax.set_title(f"Test confusion matrix · seed {seed}\nRows normalized to 100%")
for row in range(len(names)):
for column in range(len(names)):
value = 100 * normalized[row, column]
ax.text(
column,
row,
f"{value:.1f}",
ha="center",
va="center",
fontsize=8,
color="white" if value > 55 else "black",
)
fig.colorbar(display, ax=ax, label="Percentage of actual class")
return _save_figure(fig, directory, "confusion_matrix.png")
def _reliability_plot(test: dict, directory: Path, seed: int) -> str:
bins = test["calibration_bins"]
nonempty = [item for item in bins if item["count"] > 0]
fig, axes = plt.subplots(
2, 1, figsize=(6.5, 7), sharex=True, height_ratios=[3, 1], layout="constrained"
)
axes[0].plot([0, 1], [0, 1], "--", color="0.5", label="Perfect calibration")
axes[0].plot(
[item["confidence"] for item in nonempty],
[item["accuracy"] for item in nonempty],
"o-",
color="#176b91",
label="Observed test accuracy",
)
axes[0].set(xlim=(0, 1), ylim=(0, 1), ylabel="Fraction correct")
axes[0].set_title(f"Test reliability · seed {seed}")
axes[0].legend(fontsize=9)
axes[0].grid(alpha=0.2)
axes[1].bar(
[(item["lower"] + item["upper"]) / 2 for item in bins],
[item["count"] for item in bins],
width=[0.9 * (item["upper"] - item["lower"]) for item in bins],
color="#176b91",
)
axes[1].set(xlabel="Predicted top-class probability", ylabel="Images")
return _save_figure(fig, directory, "reliability.png")
def _class_f1_plot(runs: list[dict], directory: Path) -> str:
names = runs[0]["metrics"]["test"]["class_names"]
values = np.asarray(
[[row["f1"] for row in run["metrics"]["test"]["per_class"]] for run in runs]
)
fig, ax = plt.subplots(figsize=(10, 4.5), layout="constrained")
x = np.arange(len(names))
errors = 100 * values.std(axis=0, ddof=1) if len(runs) > 1 else None
ax.bar(x, 100 * values.mean(axis=0), yerr=errors, capsize=4, color="#176b91", alpha=0.8)
if len(runs) > 1:
for row in values:
ax.scatter(x, 100 * row, s=18, color="#d97706", zorder=3)
ax.set_xticks(x, names, rotation=35, ha="right")
ax.set(ylabel="F1 (%)", ylim=(0, 100))
ax.set_title(
"Test per-class F1 · mean ± seed sample SD" if len(runs) > 1 else "Test per-class F1"
)
ax.grid(axis="y", alpha=0.2)
return _save_figure(fig, directory, "per_class_f1.png")
def _seed_plot(runs: list[dict], directory: Path) -> str:
fig, ax = plt.subplots(figsize=(7, 4.5), layout="constrained")
x = np.arange(len(runs))
for offset, split, color in ((-0.18, "validation", "#176b91"), (0.18, "test", "#d97706")):
ax.bar(
x + offset,
[100 * run["metrics"][split]["accuracy"] for run in runs],
width=0.34,
label=split.title(),
color=color,
)
ax.set_xticks(x, [str(run["seed"]) for run in runs])
ax.set(xlabel="Training seed", ylabel="Accuracy (%)", ylim=(0, 100))
ax.set_title("Selected-checkpoint accuracy by seed")
ax.grid(axis="y", alpha=0.2)
ax.legend()
return _save_figure(fig, directory, "accuracy_seed_comparison.png")
def _summary(runs: list[dict], representative_seed: int, figures: list[str]) -> dict:
per_seed = []
for run in runs:
best = max(run["history"], key=lambda row: (row["val_accuracy"], -row["val_loss"]))
per_seed.append(
{
"seed": run["seed"],
"selected_epoch": run["performance"].get("best_epoch", best["epoch"]),
**_flatten_scalars(run["metrics"]),
**_flatten_scalars(run["performance"], "performance"),
}
)
keys = sorted(
key
for key in set().union(*(row.keys() for row in per_seed))
if not any(part.endswith("_ci95") for part in key.split("."))
and key.rsplit(".", 1)[-1] not in {"seed", "resamples", "confidence_level"}
)
aggregates = {key: _aggregate([row[key] for row in per_seed if key in row]) for key in keys}
return {
"schema_version": 1,
"seed_count": len(runs),
"seeds": [run["seed"] for run in runs],
"representative_seed": representative_seed,
"aggregate": aggregates,
"per_seed": per_seed,
"artifacts": {"report": "report.md", "figures": figures},
}
def _metric_table(summary: dict, split: str) -> list[str]:
lines = ["| Metric | Mean | Sample SD | Minimum | Maximum |", "|---|---:|---:|---:|---:|"]
for key, (label, percent) in METRICS.items():
stats = summary["aggregate"].get(f"{split}.{key}")
if stats is None:
continue
values = " | ".join(
_format(stats[name], percent) for name in ("mean", "sample_std", "min", "max")
)
lines.append(f"| {label} | {values} |")
return lines
def _report_lines(
manifest: dict, runs: list[dict], representative: dict, summary: dict
) -> list[str]:
seed = representative["seed"]
test = representative["metrics"]["test"]
protocol = manifest.get("protocol", {})
expected = protocol.get("seeds", summary["seeds"])
lines = [
"# Simple CNN · CIFAR-10 baseline",
"",
f"This report contains **{len(runs)} completed training seeds**: "
+ ", ".join(str(run["seed"]) for run in runs)
+ ". "
+ f"Detailed example-level diagnostics use representative seed **{seed}** "
+ "(seed 42 when available; otherwise the lowest completed seed).",
"",
]
missing = sorted(set(expected) - set(summary["seeds"]))
if missing:
lines += [
f"**Incomplete run set:** planned seeds {missing} are missing from this report.",
"",
]
lines += [
"## Evaluation protocol",
"",
"The CNN is trained from randomly initialized weights, with no pretraining. "
"The official CIFAR-10 training set is split into training and validation subsets; "
"the official test set remains separate. "
"The same saved split is used across training seeds. "
"Within each seed, the checkpoint is selected by highest validation accuracy, "
"breaking ties by lower validation loss. Test results do not select checkpoints or seeds.",
"",
"Final train, validation, and test metrics below evaluate the selected checkpoint with "
"evaluation preprocessing. Epoch training metrics use augmented images and mix "
"successive model states, so they need not equal final training metrics.",
"",
"| Split | Images (representative seed) |",
"|---|---:|",
]
for split in ("train", "validation", "test"):
lines.append(f"| {split.title()} | {representative['metrics'][split]['num_examples']:,} |")
lines += [
"",
"Uniform random top-1 guessing has expected accuracy **10%** for this 10-class benchmark. "
"This is a reference point, not a trained competing model.",
"",
"## Test performance across seeds",
"",
"Every seed uses the same test images. Means and sample standard deviations describe "
"variation across training seeds; repeated test predictions are not independent test sets. "
"Standard deviations of percentage metrics are shown in percentage points. "
"A small number of seeds provides only a preliminary estimate of training variability.",
"",
*_metric_table(summary, "test"),
"",
"Lower negative log likelihood, Brier score, ECE, and MCE are better. "
"Brier score sums squared probability error over classes, then averages over images. "
"ECE is the count-weighted absolute accuracy–confidence gap over 15 equal-width bins; "
"MCE is the largest nonempty-bin gap. Both depend on the binning. ROC AUC and average "
"precision use one-vs-rest scores averaged equally over classes. "
"Undefined metrics appear as —.",
"",
"## Individual runs and uncertainty",
"",
"| Seed | Selected epoch | Validation accuracy | Test accuracy | "
"Accuracy 95% CI | Macro F1 | Macro F1 95% CI |",
"|---|---:|---:|---:|---|---:|---|",
]
for run, row in zip(runs, summary["per_seed"], strict=True):
metrics = run["metrics"]
result = metrics["test"]
lines.append(
f"| {run['seed']} | {row['selected_epoch']} | "
f"{_format(metrics['validation']['accuracy'], True)} | "
f"{_format(result['accuracy'], True)} | {_ci(result.get('accuracy_ci95'))} | "
f"{_format(result['macro_f1'], True)} | {_ci(result.get('macro_f1_ci95'))} |"
)
bootstrap = test.get("macro_f1_ci95", {})
lines += [
"",
"Accuracy intervals use the 95% Wilson method. Macro F1 intervals use a percentile "
f"bootstrap over test examples ({bootstrap.get('resamples', 'recorded')} resamples; "
"the random seed and exact method are saved in each metrics.json). These intervals "
"describe test-sample uncertainty conditional on a fitted model and an independent, "
"representative sampling assumption; they do not include training-seed variability or "
"distribution shift. They are not intervals for the cross-seed mean.",
"",
"## Fit and learning curves",
"",
"| Seed | Final train accuracy | Validation accuracy | Test accuracy | "
"Train − validation gap |",
"|---|---:|---:|---:|---:|",
]
for run in runs:
metrics = run["metrics"]
gap = metrics["train"]["accuracy"] - metrics["validation"]["accuracy"]
lines.append(
f"| {run['seed']} | {_format(metrics['train']['accuracy'], True)} | "
f"{_format(metrics['validation']['accuracy'], True)} | "
f"{_format(metrics['test']['accuracy'], True)} | {100 * gap:.2f} pp |"
)
lines += ["", "", ""]
if len(runs) > 1:
lines += [
"",
"",
]
lines += [
"## Class-level performance",
"",
f"The table reports seed {seed}; the figure summarizes all completed seeds.",
"",
"| Class | Support | Precision | Recall | F1 | ROC AUC | Average precision |",
"|---|---:|---:|---:|---:|---:|---:|",
]
for row in test["per_class"]:
lines.append(
f"| {row['class']} | {row['support']} | {_format(row['precision'], True)} | "
f"{_format(row['recall'], True)} | {_format(row['f1'], True)} | "
f"{_format(row.get('roc_auc'))} | {_format(row.get('average_precision'))} |"
)
lines += [
"",
"",
"",
"",
"",
f"Most frequent directed errors for seed {seed}:",
"",
"| Actual → predicted | Images | Percentage of actual class |",
"|---|---:|---:|",
]
matrix = np.asarray(test["confusion_matrix"])
pairs = sorted(
(
(int(matrix[i, j]), i, j)
for i in range(len(matrix))
for j in range(len(matrix))
if i != j
),
reverse=True,
)
names = test["class_names"]
for count, actual, predicted in pairs[:10]:
if count > 0:
lines.append(
f"| {names[actual]} → {names[predicted]} | {count} | "
f"{100 * count / matrix[actual].sum():.2f}% |"
)
lines += [
"",
"## Confidence and calibration",
"",
f"For seed {seed}, mean top-class confidence is "
f"{_format(test.get('mean_confidence'), True)}. "
f"It is {_format(test.get('confidence_correct'), True)} on correct predictions and "
f"{_format(test.get('confidence_incorrect'), True)} on incorrect predictions. "
"Confidence is a model probability, not a guarantee of correctness. "
"The reliability plot shows observed accuracy against mean confidence "
"in each nonempty bin; the lower panel shows how many images fall in each bin. "
"No post-hoc calibration is fitted.",
"",
"",
"",
"## Runtime and resource use",
"",
"| Seed | Train (s) | Final evaluation (s) | Parameters | "
"Checkpoint (MiB) | Peak process RSS (MiB) |",
"|---|---:|---:|---:|---:|---:|",
]
for run in runs:
perf = run["performance"]
lines.append(
f"| {run['seed']} | {_format(perf.get('training_seconds'))} | "
f"{_format(perf.get('final_evaluation_seconds'))} | "
f"{perf.get('parameter_count', '—')} | "
f"{_mib(perf.get('checkpoint_bytes'))} | "
f"{_mib(perf.get('peak_rss_bytes'))} |"
)
lines += [
"",
"| Seed | Batch size | Median batch latency (ms) | P95 batch latency (ms) | Images/s |",
"|---|---:|---:|---:|---:|",
]
for run in runs:
for batch, perf in run["performance"].get("inference", {}).items():
lines.append(
f"| {run['seed']} | {batch.removeprefix('batch')} | "
f"{_format(perf.get('median_ms'))} | {_format(perf.get('p95_ms'))} | "
f"{_format(perf.get('images_per_second'))} |"
)
lines += [
"",
"Timing is specific to the recorded host, device, thread count, batch size, warmup, and "
"measurement procedure. Hosted runners can differ between seeds; consult each seed's "
"environment in manifest.json before comparing speed. Latency measurements cover the "
"model forward pass described in performance.json, "
"not an end-to-end image-serving pipeline. "
"Peak RSS is the process high-water mark, including data and evaluation allocations; "
"it is not an isolated model-memory measurement. Per-epoch training and validation timings "
"are retained in history.json.",
"",
"## Retained evidence and future comparisons",
"",
"- [Machine-readable summary](summary.json): "
"per-seed scalar values and cross-seed aggregates.",
"- [Manifest](manifest.json): source revision, protocol, configuration, package versions, "
"and hardware/software environment.",
"- [Split indices](split_indices.npz): exact training and validation memberships.",
]
for run in runs:
prefix = run["directory"].name
lines += [
f"- Seed {run['seed']}: [metrics]({prefix}/metrics.json), "
f"[history]({prefix}/history.json), [performance]({prefix}/performance.json), "
f"[checkpoint]({prefix}/best.pt), "
f"[train predictions]({prefix}/predictions_train.npz), "
f"[validation predictions]({prefix}/predictions_validation.npz), "
f"[test predictions]({prefix}/predictions_test.npz).",
]
lines += [
"",
"Prediction archives retain labels, class probabilities, and example indices. "
"A future model can use these same splits, seeds, evaluation code, and indexed test "
"examples for paired comparisons. Fix the next experiment's protocol before examining "
"its test results, select checkpoints using validation only, and report both paired "
"test-example uncertainty and variability across training seeds. Repeated experiment "
"selection based on this test set will turn it into development data.",
"",
f"This is a small CNN baseline at a fixed {protocol.get('epochs', 'recorded')}-epoch "
"training budget, not an estimate of the architecture's maximum attainable accuracy. "
"It does not establish "
"optimal hyperparameters, convergence, state-of-the-art performance, robustness to "
"distribution shift, or production suitability.",
"",
"## Recorded protocol",
"",
"```json",
json.dumps(protocol, indent=2, sort_keys=True),
"```",
"",
"## Source provenance",
"",
"```json",
json.dumps(manifest.get("source", {}), indent=2, sort_keys=True),
"```",
"",
]
return lines
def build_report(output_dir: Path) -> None:
"""Write report.md, summary.json, and plots using only retained run results."""
output_dir = Path(output_dir)
manifest = _read(output_dir / "manifest.json")
runs = _load_runs(output_dir)
if not runs:
raise ValueError(
"No completed seed runs found: expected metrics, performance, and history."
)
representative = next((run for run in runs if run["seed"] == 42), runs[0])
directory = output_dir / "figures"
directory.mkdir(exist_ok=True)
plt.rcParams.update({"font.size": 10, "axes.spines.top": False, "axes.spines.right": False})
test = representative["metrics"]["test"]
figures = [
_learning_curves(runs, directory),
_confusion_plot(test, directory, representative["seed"]),
_reliability_plot(test, directory, representative["seed"]),
_class_f1_plot(runs, directory),
]
if len(runs) > 1:
figures.append(_seed_plot(runs, directory))
summary = _summary(runs, representative["seed"], figures)
(output_dir / "summary.json").write_text(json.dumps(summary, indent=2, allow_nan=False) + "\n")
lines = _report_lines(manifest, runs, representative, summary)
(output_dir / "report.md").write_text("\n".join(lines))
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output-dir", type=Path, required=True)
args = parser.parse_args()
build_report(args.output_dir)
if __name__ == "__main__":
main()