File size: 1,684 Bytes
73d9e73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Show the saved SFNO training and inference summaries."""

import json
from pathlib import Path

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import torch


ROOT = Path(__file__).resolve().parents[1]


if __name__ == "__main__":
    result_dir = ROOT / "result"
    prediction = torch.load(result_dir / "prediction.pt", map_location="cpu", weights_only=True)
    target = torch.load(result_dir / "target.pt", map_location="cpu", weights_only=True)
    error = prediction - target
    rmse_by_step = torch.sqrt(error.square().mean(dim=(1, 2, 3)))
    pred_anom = prediction - prediction.mean(dim=(2, 3), keepdim=True)
    target_anom = target - target.mean(dim=(2, 3), keepdim=True)
    acc_by_step = (pred_anom * target_anom).sum(dim=(1, 2, 3)) / torch.sqrt(
        pred_anom.square().sum(dim=(1, 2, 3))
        * target_anom.square().sum(dim=(1, 2, 3))
    ).clamp_min(1e-12)
    metrics = {
        "rmse": float(torch.sqrt(error.square().mean())),
        "rmse_by_step": rmse_by_step.tolist(),
        "spatial_acc": float(acc_by_step.mean()),
        "spatial_acc_by_step": acc_by_step.tolist(),
    }
    (result_dir / "metrics.json").write_text(json.dumps(metrics, indent=2) + "\n")
    fig, axes = plt.subplots(1, 3, figsize=(12, 3.5))
    for ax, data, title in zip(
        axes,
        (target[0, 0], prediction[0, 0], error[0, 0]),
        ("Target", "Prediction", "Error"),
    ):
        image = ax.imshow(data, cmap="RdBu_r")
        ax.set_title(title)
        plt.colorbar(image, ax=ax, shrink=0.75)
    plt.tight_layout()
    plt.savefig(result_dir / "comparison.png", dpi=150)
    plt.close()
    print(json.dumps(metrics, indent=2))