File size: 5,527 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | """Evaluate RMSE / CDF for a sensing-only checkpoint.
Loads ckpt, reuses the same train/test split (seed 2027, 80/20), runs UL
forward, and reports for each user:
- range RMSE (m), azimuth RMSE (deg), Cartesian RMSE (m)
- CDF percentiles 50/67/90/95
- argmax-bin vs soft-expected estimators
Usage:
python eval_rmse.py --ckpt experiments_v2/checkpoints/phys_layout6L5N.pt
"""
import argparse, math, torch
from torch.nn.functional import softmax
from joint_dual_sim import (
JointDualSIM, PortReadout, PerBinPort, LinearReadout,
soft_position_estimate, make_range_edges,
)
from rate_aware_gen import make_config
from updated_SIM_0413_multi_user import hadamard_matrix
def polar_to_xy(r, az):
return r * torch.cos(az), r * torch.sin(az)
def argmax_position(logits, cfg, device):
idx = logits.argmax(dim=-1)
s = idx % cfg.S
q = idx // cfg.S
az_edges = torch.linspace(cfg.az_min_deg, cfg.az_max_deg, cfg.S + 1, device=device)
r_edges = make_range_edges(cfg, device)
az_hat = (az_edges[:-1] + az_edges[1:]) / 2.0 * math.pi / 180.0
r_hat = (r_edges[:-1] + r_edges[1:]) / 2.0
return r_hat[q], az_hat[s]
def percentiles(err):
err = err.sort().values
n = err.numel()
return {p: err[min(n - 1, int(n * p / 100))].item() for p in (50, 67, 90, 95)}
def main():
pa = argparse.ArgumentParser()
pa.add_argument("--ckpt", required=True)
pa.add_argument("--cache", default="experiments_v2/caches/v2_K2_15k.pt")
pa.add_argument("--scale", default="large")
pa.add_argument("--device", default="cuda:0")
pa.add_argument("--batch-size", type=int, default=64)
args = pa.parse_args()
device = torch.device(args.device if torch.cuda.is_available() else "cpu")
print(f"Device: {device}")
ckpt = torch.load(args.ckpt, weights_only=False, map_location="cpu")
cfg_dict = ckpt['cfg']
print(f"Loaded ckpt: {args.ckpt}")
print(f" layout={ckpt['layout']} share_mask={ckpt['share_mask']} "
f"readout={ckpt['readout']} range_grid={ckpt['range_grid']}")
d = torch.load(args.cache, weights_only=False, map_location="cpu")
geos, full_rates = d['geos'], d['rates']
M = geos.shape[0]
K = geos.shape[1] // 2
cfg = make_config(args.scale, K)
cfg.range_grid = ckpt['range_grid']
for k, v in cfg_dict.items():
setattr(cfg, k, v)
Pt_dBm_ul = getattr(cfg, 'Pt_UE_dBm', cfg.Pt_dBm)
pt_w = 10 ** (Pt_dBm_ul / 10) / 1000
sqrt_pt = math.sqrt(pt_w)
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:]
print(f" test set size: {len(te)}")
sim = JointDualSIM(cfg, layout=ckpt['layout'],
share_mask=tuple(int(c) == 1 for c in ckpt['share_mask'])).to(device)
sim.load_state_dict(ckpt['sim'])
sim.eval()
n_bins = cfg.S * cfg.Q
if ckpt['readout'] == "port":
head = PortReadout(n_bins).to(device)
elif ckpt['readout'] == "perbin":
head = PerBinPort(n_bins).to(device)
else:
head = LinearReadout(n_bins).to(device)
head.load_state_dict(ckpt['head'])
head.eval()
pilot_H = hadamard_matrix(max(cfg.T, K), device)
pilot = pilot_H[:K, :cfg.T].to(torch.float32) * sqrt_pt # (K, T)
r_true_all, az_true_all = [], []
r_soft_all, az_soft_all = [], []
r_arg_all, az_arg_all = [], []
with torch.no_grad():
for s_ in range(0, len(te), args.batch_size):
idx = te[s_:s_ + args.batch_size]
r_b = r_all[idx].to(device)
az_b = az_all[idx].to(device)
B = r_b.shape[0]
for k in range(K):
x_pilot = pilot[k:k+1].expand(B, -1)
y = sim.forward_ul_signal(r_b[:, k], az_b[:, k], x_pilot)
logits = head(y.abs())
r_s, az_s = soft_position_estimate(logits, cfg)
r_a, az_a = argmax_position(logits, cfg, device)
r_true_all.append(r_b[:, k]); az_true_all.append(az_b[:, k])
r_soft_all.append(r_s); az_soft_all.append(az_s)
r_arg_all.append(r_a); az_arg_all.append(az_a)
r_true = torch.cat(r_true_all)
az_true = torch.cat(az_true_all)
r_s_ = torch.cat(r_soft_all); az_s_ = torch.cat(az_soft_all)
r_a_ = torch.cat(r_arg_all); az_a_ = torch.cat(az_arg_all)
def report(tag, r_hat, az_hat):
x_t, y_t = polar_to_xy(r_true, az_true)
x_h, y_h = polar_to_xy(r_hat, az_hat)
pos_err = torch.sqrt((x_t - x_h)**2 + (y_t - y_h)**2)
r_rmse = (r_true - r_hat).pow(2).mean().sqrt().item()
az_rmse_deg = ((az_true - r_hat * 0 - az_hat) * 180 / math.pi).pow(2).mean().sqrt().item()
pos_rmse = pos_err.pow(2).mean().sqrt().item()
pcts = percentiles(pos_err)
print(f"\n[{tag}] range RMSE = {r_rmse:.3f} m az RMSE = {az_rmse_deg:.2f}° "
f"pos RMSE = {pos_rmse:.3f} m")
print(f" pos err CDF — p50={pcts[50]:.2f}m p67={pcts[67]:.2f}m "
f"p90={pcts[90]:.2f}m p95={pcts[95]:.2f}m")
print(f"\n=== Eval ({len(te)} test samples × {K} users = {r_true.numel()} estimates) ===")
print(f" bin sizes: az = {(cfg.az_max_deg - cfg.az_min_deg)/cfg.S:.2f}° "
f"r ≈ {(cfg.r_max - cfg.r_min)/cfg.Q:.2f} m")
report("argmax (hard)", r_a_, az_a_)
report("soft expected", r_s_, az_s_)
if __name__ == "__main__":
main()
|