Download scripts/inference.py from OneScience-Group/SirenFNO: direct link, hf CLI and curl.
- Browser
- Download file 4.37 kB
-
https://huggingface.co/OneScience-Group/SirenFNO/resolve/main/scripts/inference.py
- Command line
-
hf download hf://OneScience-Group/SirenFNO/scripts/inference.py
-
curl -L -o inference.py https://huggingface.co/OneScience-Group/SirenFNO/resolve/main/scripts/inference.py
4.37 kB
| """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() | |