File size: 4,366 Bytes
10ab127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reload saved weights and evaluate 200 original-scale 32x32 pressure fields."""

import csv
from copy import deepcopy
from pathlib import Path

import torch

from common import (arguments, atomic_json, build_model, configure_runtime,
                    evaluate, load_config, load_data, relative_l2, sha256)


def main():
    parser = arguments(__doc__)
    parser.add_argument("--checkpoint", type=Path)
    args = parser.parse_args()
    config = load_config(args.config)
    device = configure_runtime(config)
    output = Path(config["output"]["root"])
    results = output / config["output"]["results"]
    checkpoint_path = args.checkpoint or output / config["output"]["checkpoint"]
    last = torch.load(output / config["output"]["last_checkpoint"], map_location="cpu", weights_only=False)
    assert last["epoch"] == config["training"]["epochs"] == 500
    checkpoint = torch.load(checkpoint_path, map_location="cpu", weights_only=False)
    trained_config = deepcopy(checkpoint["config"])
    trained_config["data"]["root"] = config["data"]["root"]
    trained_config["output"]["root"] = config["output"]["root"]
    if trained_config != config:
        raise ValueError("Inference configuration does not match trained checkpoint")
    data, manifest = load_data(config)
    for split, info in manifest["files"].items():
        assert info["sha256"] == checkpoint["data_sha256"][split], "Dataset changed after training"
    model = build_model(config, device)
    model.load_state_dict(checkpoint["model_state_dict"], strict=True)
    metrics, errors, predictions = evaluate(model, data["test_x"], data["test_y"],
        checkpoint["normalizer"], config["training"]["batch_size"], device, return_predictions=True)
    # Recompute using saved physical fields in float64, avoiding batch-average or
    # normalized-target metric ambiguities in the final reported value.
    errors64 = relative_l2(predictions.double(), data["test_y"].double())
    metrics["relative_l2_mean"] = errors64.mean().item()
    metrics["squared_relative_l2_mean"] = errors64.square().mean().item()
    metrics["relative_l2_std"] = errors64.std(correction=0).item()
    reference = config["paper_reference"]
    metrics.update({"n_test": len(errors), "resolution": [32, 32], "scale": "original pressure",
                    "aggregation": "arithmetic mean of per-sample Euclidean norm ratios",
                    "completed_epochs": last["epoch"], "selected_epoch": checkpoint["epoch"],
                    "checkpoint": str(checkpoint_path), "checkpoint_sha256": sha256(checkpoint_path),
                    "checkpoint_selection": checkpoint["selection_rule"], "paper_table2": reference,
                    "signed_gap": metrics["relative_l2_mean"] - reference,
                    "absolute_gap": abs(metrics["relative_l2_mean"] - reference),
                    "relative_gap_percent": 100 * (metrics["relative_l2_mean"] / reference - 1),
                    "last_epoch_test_relative_l2": last["test_metrics"]["relative_l2_mean"],
                    "test_used_for_selection": False,
                    "loss_metric_caveat": "Paper Eq15 is squared; this report's primary metric is unsquared as requested"})
    torch.save({"x": data["test_x"], "y": data["test_y"], "prediction": predictions,
                "sample_ids": torch.arange(len(errors)), "relative_l2": errors64,
                "checkpoint_sha256": metrics["checkpoint_sha256"]}, results / "predictions.pt")
    with (results / "per_sample_metrics.csv").open("w", newline="") as stream:
        writer = csv.writer(stream)
        writer.writerow(["sample_id", "relative_l2_original_scale", "squared_relative_l2_original_scale"])
        for index, error in enumerate(errors64.tolist()):
            writer.writerow([index, error, error * error])
    atomic_json(results / "metrics.json", metrics)
    print(f"RELOADED {checkpoint_path} epoch={checkpoint['epoch']}; actual training=500 epochs", flush=True)
    print(f"INFERENCE original-scale per-sample relative L2 mean={metrics['relative_l2_mean']:.10f}; "
          f"paper=0.0351; signed_gap={metrics['signed_gap']:+.10f}; "
          f"relative_gap={metrics['relative_gap_percent']:+.3f}%", flush=True)
    print(f"Squared relative L2 mean (paper Eq15)={metrics['squared_relative_l2_mean']:.10f}", flush=True)


if __name__ == "__main__":
    main()