"""The validation report — one self-contained HTML file per run.
"Validate estimated structural heights against reference datasets" is an explicit
deliverable, and "RMSE, MAE, and correlation against LiDAR/reference data,
including performance stability across urban, sparse, hilly, and forested
landscapes" is the 50 % accuracy rubric verbatim. This renders exactly that, from
`metrics.json`, with every figure inlined as a data URI so the file can be
emailed, printed to PDF, or opened on an air-gapped machine with no server and no
sibling directory.
It is deliberately blunt about what is *not* known: the class-id mapping is
unverified, the landscape labels are heuristics, and any number that came from a
proxy nDSM says so. A panel that catches an overclaim discounts everything else
you said; a report that flags its own limits does the opposite.
"""
from __future__ import annotations
import base64
import html
import json
from datetime import datetime, timezone
from pathlib import Path
_CSS = """
:root{color-scheme:light}
*{box-sizing:border-box}
body{margin:0;font:14px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",system-ui,sans-serif;
color:#1b1f24;background:#f6f7f9}
.wrap{max-width:1000px;margin:0 auto;padding:32px 20px 80px}
h1{font-size:26px;margin:0 0 4px}
h2{font-size:17px;margin:34px 0 10px;padding-bottom:6px;border-bottom:1px solid #e1e4e8}
h3{font-size:14px;margin:20px 0 6px;color:#39424c}
.sub{color:#6a737d;margin:0 0 22px}
.cards{display:flex;flex-wrap:wrap;gap:12px;margin:16px 0}
.card{flex:1 1 150px;background:#fff;border:1px solid #e1e4e8;border-radius:10px;padding:14px 16px}
.card .k{color:#6a737d;font-size:11px;text-transform:uppercase;letter-spacing:.05em}
.card .v{font-size:24px;font-weight:650;margin-top:2px;font-variant-numeric:tabular-nums}
.card .u{font-size:13px;font-weight:400;color:#6a737d}
.card.warn{border-color:#f0b429;background:#fffaf0}
table{border-collapse:collapse;width:100%;background:#fff;border:1px solid #e1e4e8;
border-radius:10px;overflow:hidden;font-variant-numeric:tabular-nums}
th,td{padding:7px 11px;text-align:right;border-bottom:1px solid #eef0f2}
th:first-child,td:first-child{text-align:left}
thead th{background:#f0f2f5;font-weight:600;font-size:12px;color:#39424c}
tbody tr:last-child td{border-bottom:0}
figure{margin:16px 0;background:#fff;border:1px solid #e1e4e8;border-radius:10px;padding:12px}
figure img{width:100%;display:block;border-radius:6px}
figcaption{color:#6a737d;font-size:12px;margin-top:8px}
.note{background:#fff8e6;border:1px solid #f0d9a0;border-left:3px solid #f0b429;
border-radius:6px;padding:10px 14px;margin:14px 0;font-size:13px}
.ok{background:#eefaf0;border-color:#bfe5c8;border-left-color:#34a853}
code{background:#eef0f2;padding:1px 5px;border-radius:4px;font-size:12.5px}
footer{margin-top:40px;color:#6a737d;font-size:12px;border-top:1px solid #e1e4e8;padding-top:14px}
@media print{body{background:#fff}.wrap{max-width:none}figure,table,.card{break-inside:avoid}}
"""
def _img(path: Path) -> str:
b = base64.b64encode(Path(path).read_bytes()).decode("ascii")
return f"data:image/png;base64,{b}"
def _fig(path: Path, caption: str) -> str:
if not Path(path).is_file():
return ""
return (f''
f'{caption}')
def _card(k, v, unit="", warn=False) -> str:
cls = "card warn" if warn else "card"
u = f' {html.escape(unit)}' if unit else ""
return (f'
{html.escape(k)}
'
f'
{html.escape(str(v))}{u}
')
def _num(x, nd=3, dash="—"):
return dash if x is None else f"{float(x):.{nd}f}"
def _table(headers, rows) -> str:
h = "".join(f"
{html.escape(str(x))}
" for x in headers)
b = "".join("
" + "".join(f"
{c}
" for c in r) + "
" for r in rows)
return f"
{h}
{b}
"
# ---------------------------------------------------------------------
def _pick_result(metrics: dict) -> tuple[str, dict]:
for key, label in (("final_sliding_tta", "full-tile sliding window + TTA, native GSD"),
("final_tta", "centre crop + TTA"),
("final_plain", "centre crop")):
if metrics.get(key):
return label, metrics[key]
for h in reversed(metrics.get("history") or []):
if h.get("val"):
return f"last validation (epoch {h['epoch']})", h["val"]
return "no result", {}
def build(run_dir: str | Path, out_path: str | Path | None = None,
fig_dir: str | Path | None = None, title: str = "DepthWizard v4") -> Path:
run_dir = Path(run_dir)
metrics = json.loads((run_dir / "metrics.json").read_text())
figs = Path(fig_dir) if fig_dir else run_dir / "figures"
out_path = Path(out_path) if out_path else run_dir / "validation_report.html"
cfg = metrics.get("config") or {}
pre = metrics.get("preproc") or {}
label, res = _pick_result(metrics)
g = res.get("global") or {}
parts = [f"
Read balanced RMSE and the two bias numbers, not '
'the headline alone. A global RMSE is dominated by flat ground — roughly '
'two thirds of an aerial scene — so a model that underestimates every tall '
'building still posts a good one. A large negative tall bias means '
'structures are being compressed; a large positive flat bias means '
'terrain is being invented from image texture, which is the failure that '
'turns a flythrough into a crumpled mountain range.
')
# ---- landscape stability -----------------------------------------
per_l = res.get("per_landscape") or {}
if per_l:
parts.append("
Stability across landscapes
")
parts.append("
The rubric grades stability across urban, sparse, hilly and "
"forested scenes. No dataset here ships that label, so each "
"validation tile is classified from its own ground-truth height "
"field (relief, tall-pixel fraction, canopy roughness — see "
"eval/landscape.py). The rule and its descriptors are "
"in metrics.json; these are heuristics, not "
"annotations.
")
parts.append(_table(
["landscape", "tiles", "pixels", "RMSE (m)", "MAE (m)", "bias (m)", "r"],
[[k, v.get("tiles", "—"), f"{v.get('n', 0):,}", _num(v.get("rmse_m")),
_num(v.get("mae_m")), _num(v.get("bias_m"), 2), _num(v.get("pearson_r"))]
for k, v in sorted(per_l.items(), key=lambda kv: -kv[1].get("rmse_m", 0))]))
parts.append(_fig(figs / "per_landscape.png",
"RMSE by landscape class; dashed line is the global RMSE."))
# ---- height strata -------------------------------------------------
per_s = {k: v for k, v in (res.get("per_stratum") or {}).items() if v.get("n")}
if per_s:
parts.append("
Error by height stratum
")
parts.append(_table(
["stratum", "pixels", "RMSE (m)", "MAE (m)", "bias (m)"],
[[k, f"{v['n']:,}", _num(v.get("rmse_m")), _num(v.get("mae_m")),
_num(v.get("bias_m"), 2)] for k, v in per_s.items()]))
parts.append(_fig(figs / "per_stratum.png",
"Per-stratum RMSE and signed bias. Negative bias in the top "
"stratum is tall-structure underestimation."))
# ---- distributions --------------------------------------------------
dist = "".join([
_fig(figs / "scatter.png",
"Predicted against reference height. Mass below y=x at the high end is "
"compression of tall structures."),
_fig(figs / "error_hist.png",
"Signed error, split into flat ground and tall structures — the two "
"regimes fail in opposite directions and a single histogram hides it."),
])
if dist:
parts.append("
Error distribution
" + dist)
# ---- qualitative -----------------------------------------------------
qual = "".join([
_fig(figs / "hillshade.png",
"Hillshade of the predicted and reference surfaces. This is the shape "
"the 3D flythrough renders; flat ground must read as flat."),
_fig(figs / "qualitative.png", "RGB, prediction, reference, absolute error."),
])
if qual:
parts.append("
Qualitative
" + qual)
# ---- training --------------------------------------------------------
parts.append("
Training
")
parts.append(_fig(figs / "curves.png",
"Loss and validation error. The dashed line is where the "
"encoder was unfrozen."))
parts.append(_table(
["setting", "value"],
[[k, html.escape(str(cfg.get(k, "—")))] for k in (
"encoder_model_id", "datasets", "epochs", "freeze_epochs", "batch_size",
"grad_accum", "learning_rate", "encoder_lr", "llrd", "tile_size",
"canonical_gsd_m", "stratum_balance_beta", "w_consistency", "ema_decay")]))
# ---- the inference contract -------------------------------------------
if pre:
parts.append("
Inference contract
")
parts.append("
Serialised into every checkpoint, and read from the "
"checkpoint (never from a config file) by "
"infer/predict.py. Training and inference build "
"their tensors with the same code, so a validation number "
"measures the path the demo actually runs.
")
parts.append(_table(["field", "value"],
[[k, html.escape(str(v))] for k, v in pre.items()]))
# ---- per class, with the caveat attached ------------------------------
per_c = res.get("per_class") or {}
if per_c:
stats = res.get("class_stats") or {}
parts.append("
Per semantic class
")
parts.append('
Class ids are reported, not names. The '
'name order asserted by v1/v2 is contradicted by the measured '
'histogram (one supposed "bridge" id is 16 % of all pixels; '
'"ground" is 0.1 %), so the mapping is unverified. Do not attach '
'land-cover names to these rows until it is pinned to the GAMUS '
'paper.
')
parts.append(_table(
["class id", "pixel share", "mean GT height (m)", "RMSE (m)", "MAE (m)"],
[[k, f"{(stats.get(k, {}).get('px_frac') or 0) * 100:.1f}%",
_num((stats.get(k, {}) or {}).get("mean_gt_height_m"), 2),
_num(v.get("rmse_m")), _num(v.get("mae_m"))]
for k, v in per_c.items()]))
# ---- limits ------------------------------------------------------------
parts.append("
What this report does not establish
")
parts.append("
"
"
Every labelled training source is foreign (US, German, "
"synthetic). No open dataset pairs Indian RGB with per-pixel "
"heights; the Indian branch here is unlabeled mean-teacher "
"adaptation, which reduces the domain gap but is not evidence of "
"Indian accuracy. Numbers on ISRO imagery have to be measured on "
"ISRO imagery.
"
"
Landscape labels are heuristics derived from GT geometry, not "
"annotations.
"
"
Semantic class ids are unverified (above).
"
"
A GeoNRW-derived nDSM is a morphological proxy, not a survey "
"product; treat any GeoNRW row as auxiliary.