File size: 4,418 Bytes
1b70a0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Evaluate continuous-domain sensing error on a trained JointDualSIM checkpoint."""
import argparse, math, torch
from rate_aware_gen import make_config
from joint_dual_sim import JointDualSIM, PortReadout, soft_position_estimate

def make_orthogonal_pilots(K, T, device):
    from updated_SIM_0413_multi_user import hadamard_matrix
    H = hadamard_matrix(max(T, K), device)
    return H[:K, :T]

def main():
    pa = argparse.ArgumentParser()
    pa.add_argument("--ckpt", required=True)
    pa.add_argument("--cache", required=True)
    pa.add_argument("--layout", default="LLNLNLN")
    pa.add_argument("--share-mask", default="1100000")
    pa.add_argument("--scale", default="large")
    pa.add_argument("--device", default="cuda")
    pa.add_argument("--batch-size", type=int, default=96)
    args = pa.parse_args()

    device = torch.device(args.device)
    d = torch.load(args.cache, weights_only=False, map_location="cpu")
    geos = d['geos']
    M, K = geos.shape[0], geos.shape[1] // 2
    cfg = make_config(args.scale, K)
    r_all = geos[:, ::2] * cfg.r_max
    az_all = geos[:, 1::2] * math.pi
    g = torch.Generator().manual_seed(2027)
    perm = torch.randperm(M, generator=g)
    n_tr = int(0.8 * M)
    te = perm[n_tr:]

    sim = JointDualSIM(cfg, layout=args.layout,
                       share_mask=tuple(int(c)==1 for c in args.share_mask)).to(device)
    n_bins = cfg.S * cfg.Q
    head = PortReadout(n_bins).to(device)
    ckpt = torch.load(args.ckpt, weights_only=False, map_location=device)
    sim.load_state_dict(ckpt['sim'])
    head.load_state_dict(ckpt['head'])
    sim.eval(); head.eval()

    pilot = make_orthogonal_pilots(K, cfg.T, device)
    r_te = r_all[te].to(device)
    az_te = az_all[te].to(device)

    az_errs, r_errs, r_trues, top1_correct, top3_correct = [], [], [], [], []
    with torch.no_grad():
        for s in range(0, len(te), args.batch_size):
            r_b = r_te[s:s+args.batch_size]
            az_b = az_te[s:s+args.batch_size]
            B = r_b.shape[0]
            for k in range(K):
                x = pilot[k:k+1].expand(B, -1)
                y = sim.forward_ul_signal(r_b[:, k], az_b[:, k], x)
                logits = head(y)
                r_hat, az_hat = soft_position_estimate(logits, cfg)
                r_errs.append((r_hat - r_b[:, k]).abs().cpu())
                az_errs.append(((az_hat - az_b[:, k]).abs() * 180.0 / math.pi).cpu())
                r_trues.append(r_b[:, k].cpu())
                # bin acc
                az_deg = az_b[:, k] * 180.0 / math.pi
                s_true = ((az_deg - cfg.az_min_deg)/(cfg.az_max_deg - cfg.az_min_deg)*cfg.S).long().clamp(0, cfg.S-1)
                q_true = ((r_b[:, k] - cfg.r_min)/(cfg.r_max - cfg.r_min)*cfg.Q).long().clamp(0, cfg.Q-1)
                tb = q_true * cfg.S + s_true
                top1_correct.append((logits.argmax(-1) == tb).float())
                top3 = logits.topk(3, dim=-1).indices  # (B, 3)
                top3_correct.append((top3 == tb.unsqueeze(-1)).any(-1).float())

    r_err = torch.cat(r_errs)
    az_err = torch.cat(az_errs)
    r_true = torch.cat(r_trues)
    az_err_rad = az_err * math.pi / 180.0
    pos_err_approx = torch.sqrt(r_err**2 + (r_true * az_err_rad)**2)
    top1 = torch.cat(top1_correct).mean().item()
    top3 = torch.cat(top3_correct).mean().item()
    print(f"Samples: {len(r_err)} ({K} users × {len(te)} groups)")
    print(f"\nRange error |r̂ - r|:")
    print(f"  mean={r_err.mean():.3f} m   median={r_err.median():.3f} m   "
          f"CEP50={r_err.quantile(0.5):.3f} m   CEP90={r_err.quantile(0.9):.3f} m")
    print(f"  bin width = {(cfg.r_max-cfg.r_min)/cfg.Q:.3f} m")
    print(f"\nAzimuth error |âz - az|:")
    print(f"  mean={az_err.mean():.3f}°   median={az_err.median():.3f}°   "
          f"CEP50={az_err.quantile(0.5):.3f}°   CEP90={az_err.quantile(0.9):.3f}°")
    print(f"  bin width = {(cfg.az_max_deg-cfg.az_min_deg)/cfg.S:.3f}°")
    print(f"\n2D position error (sqrt(Δr² + (r·Δaz)²)):")
    print(f"  mean={pos_err_approx.mean():.3f} m   median={pos_err_approx.median():.3f} m")
    print(f"  CEP50={pos_err_approx.quantile(0.5):.3f} m   CEP90={pos_err_approx.quantile(0.9):.3f} m")
    print(f"\nHard top-1 bin acc: {top1*100:.2f}%   top-3 bin acc: {top3*100:.2f}%")
    print(f"Random top-1 = {100/n_bins:.2f}%   ({n_bins} bins: S={cfg.S} × Q={cfg.Q})")

if __name__ == "__main__":
    main()