import json import re from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt RUNS = Path("runs") OUT = Path("results") OUT.mkdir(exist_ok=True) def load_runs(): runs = [] for d in sorted(RUNS.iterdir()): hist = d / "history.json" if not hist.exists(): continue m = re.fullmatch(r"depth(\d+)(_res)?(_nobn)?", d.name) depth = int(m.group(1)) residual = bool(m.group(2)) bn = not m.group(3) data = json.loads(hist.read_text()) runs.append({ "name": d.name, "depth": depth, "residual": residual, "bn": bn, "epochs": [h["epoch"] for h in data], "train_loss": [h["train_loss"] for h in data], "train_acc": [h["train_acc"] for h in data], "val_acc": [h["val_acc"] for h in data], }) return runs def style(r): color = "tab:blue" if not r["residual"] else "tab:red" ls = "-" if r["bn"] else "--" return color, ls def label(r): return f"depth{r['depth']}" + ("-res" if r["residual"] else "") def plot_curves(runs, bn, metric, ylabel, fname, logy=False): fig, ax = plt.subplots(figsize=(8, 5)) for r in sorted(runs, key=lambda r: (r["depth"], r["residual"])): if r["bn"] != bn: continue c, ls = style(r) ax.plot(r["epochs"], r[metric], ls, color=c, label=label(r), lw=1.5) ax.set_xlabel("epoch") ax.set_ylabel(ylabel) ax.set_title(f"{ylabel} vs epoch ({'with' if bn else 'without'} BatchNorm)") ax.legend(fontsize=8) ax.grid(alpha=0.3) if logy: ax.set_yscale("log") fig.tight_layout() fig.savefig(OUT / fname, dpi=150) plt.close(fig) def plot_summary(runs, fname): fig, axes = plt.subplots(1, 2, figsize=(11, 4)) for ax, bn, title in [(axes[0], True, "with BN"), (axes[1], False, "without BN")]: subset = [r for r in runs if r["bn"] == bn] depths = sorted({r["depth"] for r in subset}) width = 0.35 for i, res in enumerate([False, True]): vals = [] for d in depths: cand = [r for r in subset if r["depth"] == d and r["residual"] == res] vals.append(max(max(r["val_acc"]) for r in cand) if cand else 0) xs = [d + (i - 0.5) * width for d in depths] ax.bar(xs, vals, width, label="residual" if res else "plain", color="tab:red" if res else "tab:blue", alpha=0.8) ax.set_xticks(depths) ax.set_title(title) ax.set_xlabel("depth (blocks)") ax.set_ylabel("best val acc") ax.set_ylim(0, 1.05) ax.legend(fontsize=8) ax.grid(alpha=0.3, axis="y") fig.tight_layout() fig.savefig(OUT / fname, dpi=150) plt.close(fig) def main(): runs = load_runs() plot_curves(runs, bn=True, metric="val_acc", ylabel="val accuracy", fname="val_acc_bn.png") plot_curves(runs, bn=True, metric="train_loss", ylabel="train loss", fname="train_loss_bn.png", logy=True) plot_curves(runs, bn=False, metric="val_acc", ylabel="val accuracy", fname="val_acc_nobn.png") plot_curves(runs, bn=False, metric="train_loss", ylabel="train loss", fname="train_loss_nobn.png", logy=True) plot_summary(runs, "summary.png") print(f"saved plots to {OUT}") if __name__ == "__main__": main()