Datasets:
Formats:
parquet
Size:
1M - 10M
Tags:
gaussian-splatting
fault-tolerance
single-event-upset
reliability
radiance-fields
computer-graphics
License:
| """E12: real-world scene (pretrained 3DGS) generalization + renders. | |
| Loads a pretrained 3D Gaussian Splatting checkpoint (Inria .ply format), renders | |
| it from synthesized orbit cameras, and repeats the criticality and support-guard | |
| measurement on it. This checks that the concentration of risk and the guard | |
| generalize beyond the trained synthetic scenes, and leaves high-resolution renders | |
| (clean / faulted / guarded) for the manuscript. | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import numpy as np | |
| import torch | |
| from plyfile import PlyData | |
| import imageio.v2 as imageio | |
| import faultlib as F | |
| import gsmodel | |
| def load_ply(path): | |
| ply = PlyData.read(path)["vertex"] | |
| xyz = np.stack([ply["x"], ply["y"], ply["z"]], 1).astype(np.float32) | |
| opac = np.asarray(ply["opacity"], np.float32)[:, None] | |
| scales = np.stack([ply[f"scale_{i}"] for i in range(3)], 1).astype(np.float32) | |
| rots = np.stack([ply[f"rot_{i}"] for i in range(4)], 1).astype(np.float32) | |
| fdc = np.stack([ply[f"f_dc_{i}"] for i in range(3)], 1).astype(np.float32) # [N,3] | |
| rest_names = sorted([p.name for p in ply.properties if p.name.startswith("f_rest_")], | |
| key=lambda s: int(s.split("_")[-1])) | |
| frest = np.stack([ply[n] for n in rest_names], 1).astype(np.float32) # [N, 3*M] | |
| N = xyz.shape[0] | |
| M = frest.shape[1] // 3 | |
| sh_rest = frest.reshape(N, 3, M).transpose(0, 2, 1) # [N, M, 3] | |
| sh_deg = int(round((M + 1) ** 0.5)) - 1 | |
| dev = "cuda" | |
| params = { | |
| "means": torch.tensor(xyz, device=dev), | |
| "scales": torch.tensor(scales, device=dev), | |
| "quats": torch.tensor(rots, device=dev), | |
| "opacities": torch.tensor(opac.squeeze(1), device=dev), | |
| "sh0": torch.tensor(fdc, device=dev).reshape(N, 1, 3), | |
| "shN": torch.tensor(sh_rest, device=dev), | |
| } | |
| return params, sh_deg, N | |
| def orbit_cameras(means, n_views, W, H, fov_deg=60.0): | |
| # robust center/extent: ignore distant floaters via the median and a low quantile | |
| c = means.median(0).values | |
| X = (means - c) | |
| d = X.norm(dim=1) | |
| keep = d < torch.quantile(d, 0.6) # dense core only | |
| Xc = X[keep] | |
| cov = (Xc.T @ Xc) / Xc.shape[0] | |
| evals, evecs = torch.linalg.eigh(cov) | |
| up = evecs[:, 0] | |
| a1 = evecs[:, 2]; a2 = evecs[:, 1] # ground-plane axes | |
| radius = float(torch.quantile(d, 0.5).item()) * 1.2 | |
| elev = radius * 0.12 | |
| vms = [] | |
| for i in range(n_views): | |
| th = 2 * np.pi * i / n_views | |
| cam = c + radius * (np.cos(th) * a1 + np.sin(th) * a2) + elev * up | |
| z = (c - cam); z = z / z.norm() # forward (+z, OpenCV) | |
| x = torch.linalg.cross(z, up); x = x / x.norm() | |
| y = torch.linalg.cross(z, x) # down | |
| R = torch.stack([x, y, z], 0) # world->cam rotation (rows) | |
| t = -R @ cam | |
| vm = torch.eye(4, device=means.device) | |
| vm[:3, :3] = R; vm[:3, 3] = t | |
| vms.append(vm) | |
| vms = torch.stack(vms, 0) | |
| f = 0.5 * W / np.tan(0.5 * np.deg2rad(fov_deg)) | |
| K = torch.tensor([[f, 0, W / 2], [0, f, H / 2], [0, 0, 1]], device=means.device, dtype=torch.float32) | |
| Ks = K[None].repeat(n_views, 1, 1) | |
| return vms, Ks | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--ply", required=True) | |
| ap.add_argument("--name", default="truck") | |
| ap.add_argument("--out", default="/root/seu/results/realscene") | |
| ap.add_argument("--gen", default="/root/seu/results/generated") | |
| ap.add_argument("--W", type=int, default=800) | |
| ap.add_argument("--S", type=int, default=300) | |
| ap.add_argument("--views", type=int, default=8) | |
| args = ap.parse_args() | |
| os.makedirs(args.out, exist_ok=True); os.makedirs(args.gen, exist_ok=True) | |
| dev = "cuda" | |
| params, sh, N = load_ply(args.ply) | |
| W = H = args.W | |
| print(f"[{args.name}] N={N} sh_deg={sh}", flush=True) | |
| vms, Ks = orbit_cameras(params["means"], args.views, W, H) | |
| bounds = F.compute_bounds(params) | |
| stored, work = F.quantize_params(params, "fp32") | |
| # pretty clean renders | |
| clean, _ = F.render_views(work, vms, Ks, W, H, sh) | |
| for i in range(min(3, args.views)): | |
| imageio.imwrite(os.path.join(args.out, f"{args.name}_view{i}.png"), | |
| (clean[i].cpu().numpy() * 255).astype(np.uint8)) | |
| # a clean/faulted/guarded triptych on a chosen view, using a large-footprint scale-sign flip | |
| v = 0 | |
| rng = np.random.default_rng(0) | |
| best = (-1, None) | |
| sc = work["scales"] | |
| for _ in range(150): | |
| g = int(rng.integers(0, N)); flat = g * 3 | |
| cv, _ = F.flip_one(stored["scales"], sc, flat, 31, "fp32") | |
| img, _ = F.render_views(work, vms[v:v + 1], Ks[v:v + 1], W, H, sh) | |
| F.restore_one(sc, flat, cv) | |
| fp = ((img[0] - clean[v]).abs().amax(-1) > 1 / 255).float().mean().item() | |
| if fp > best[0]: | |
| best = (fp, g) | |
| g = best[1]; flat = g * 3 | |
| cv, _ = F.flip_one(stored["scales"], sc, flat, 31, "fp32") | |
| faulted, _ = F.render_views(work, vms[v:v + 1], Ks[v:v + 1], W, H, sh) | |
| gw = F.apply_guard(work, bounds) | |
| guarded, _ = F.render_views(gw, vms[v:v + 1], Ks[v:v + 1], W, H, sh) | |
| F.restore_one(sc, flat, cv) | |
| trip = torch.cat([clean[v], faulted[0], guarded[0]], dim=1).clamp(0, 1) | |
| imageio.imwrite(os.path.join(args.gen, f"fig_realscene.png"), | |
| (trip.cpu().numpy() * 255).astype(np.uint8)) | |
| # criticality on the real scene, measured against a multi-view clean reference | |
| clean_small, _ = F.render_views(work, vms[:4], Ks[:4], W, H, sh) | |
| def footprint(rp): | |
| img, cat = F.render_views(rp, vms[:4], Ks[:4], W, H, sh) | |
| return ((img - clean_small).abs().amax(-1) > 1 / 255).float().mean().item(), bool(cat) | |
| # (a) dominant scale sign-bit case, no guard vs guard | |
| sign_ng, sign_g = [], [] | |
| for _ in range(150): | |
| g = int(rng.integers(0, N)); flat = g * 3 | |
| cv, _ = F.flip_one(stored["scales"], work["scales"], flat, 31, "fp32") | |
| f_ng, _ = footprint(work) | |
| f_g, _ = footprint(F.apply_guard(work, bounds)) | |
| F.restore_one(work["scales"], flat, cv) | |
| sign_ng.append(f_ng); sign_g.append(f_g) | |
| # (b) overall uniform-random single-bit catastrophe rate, no guard vs guard | |
| FIELDS = ["means", "scales", "quats", "opacities", "sh0", "shN"] | |
| comps = {f: work[f].reshape(N, -1).shape[1] for f in FIELDS} | |
| cats_ng, cats_g = [], [] | |
| for _ in range(args.S): | |
| field = FIELDS[int(rng.integers(0, 6))] | |
| flat = int(rng.integers(0, N * comps[field])); bit = int(rng.integers(0, 32)) | |
| cv, _ = F.flip_one(stored[field], work[field], flat, bit, "fp32") | |
| f_ng, c_ng = footprint(work) | |
| f_g, c_g = footprint(F.apply_guard(work, bounds)) | |
| F.restore_one(work[field], flat, cv) | |
| cats_ng.append(int(c_ng or f_ng > 0.01)); cats_g.append(int(c_g or f_g > 0.01)) | |
| res = {"name": args.name, "N": int(N), "sh_degree": sh, | |
| "cat_rate_noguard": float(np.mean(cats_ng)), "cat_rate_guard": float(np.mean(cats_g)), | |
| "scalesign_foot_noguard": float(np.mean(sign_ng) * 100), | |
| "scalesign_foot_guard": float(np.mean(sign_g) * 100), | |
| "scalesign_p99_noguard": float(np.percentile(sign_ng, 99) * 100), | |
| "n_samples": args.S} | |
| json.dump(res, open(os.path.join(args.out, f"realscene_{args.name}.json"), "w"), indent=2) | |
| print("REALSCENE", res, flush=True) | |
| if __name__ == "__main__": | |
| main() | |