| """ |
| Ablate quantized reconstruction for 3DGS PLY files. |
| |
| This script builds multiple reconstruction variants from one fine PLY: |
| - all: quantize scale + rotation + dc + sh |
| - only_scale: quantize only scale |
| - only_rotation |
| - only_dc |
| - only_sh |
| - no_scale: keep scale original, quantize the others |
| - no_rotation |
| - no_dc |
| - no_sh |
| |
| The goal is to visually isolate which quantized attribute introduces |
| the strongest artifacts such as spikes, blurring, or ghosting. |
| """ |
|
|
| import argparse |
| import os |
| from typing import Dict, Iterable, Set |
|
|
| import numpy as np |
|
|
|
|
| FIELD_NAMES = ("scale", "rotation", "dc", "sh") |
|
|
| MODE_MAP: Dict[str, Set[str]] = { |
| "all": {"scale", "rotation", "dc", "sh"}, |
| "only_scale": {"scale"}, |
| "only_rotation": {"rotation"}, |
| "only_dc": {"dc"}, |
| "only_sh": {"sh"}, |
| "no_scale": {"rotation", "dc", "sh"}, |
| "no_rotation": {"scale", "dc", "sh"}, |
| "no_dc": {"scale", "rotation", "sh"}, |
| "no_sh": {"scale", "rotation", "dc"}, |
| } |
|
|
|
|
| def _quantize_or_copy(name: str, |
| features: np.ndarray, |
| codebook: np.ndarray, |
| enabled: bool, |
| quantize_fn) -> dict: |
| if enabled: |
| idx, recon, stats = quantize_fn(features, codebook, name) |
| return { |
| "indices": idx, |
| "reconstructed": recon.astype(np.float32), |
| "stats": stats, |
| "quantized": True, |
| } |
|
|
| return { |
| "indices": None, |
| "reconstructed": features.astype(np.float32, copy=True), |
| "stats": None, |
| "quantized": False, |
| } |
|
|
|
|
| def build_variant_results(data: dict, |
| codebooks: dict, |
| quantized_fields: Set[str], |
| quantize_fn) -> dict: |
| feature_map = { |
| "scale": data["scales"], |
| "rotation": data["rotations"], |
| "dc": data["dc"], |
| "sh": data["sh_rest"], |
| } |
|
|
| results = {} |
| for name in FIELD_NAMES: |
| results[name] = _quantize_or_copy( |
| name=name, |
| features=feature_map[name], |
| codebook=codebooks[name], |
| enabled=(name in quantized_fields), |
| quantize_fn=quantize_fn, |
| ) |
| return results |
|
|
|
|
| def print_mode_summary(mode: str, results: dict) -> None: |
| print(f"\n{'=' * 72}") |
| print(f"[mode] {mode}") |
| print(f"{'field':<12s} {'quantized':<10s} {'rmse':>12s} {'max_err':>12s} {'usage':>10s}") |
| print(f"{'-' * 72}") |
| for name in FIELD_NAMES: |
| res = results[name] |
| if res["stats"] is None: |
| print(f"{name:<12s} {'no':<10s} {'-':>12s} {'-':>12s} {'-':>10s}") |
| else: |
| s = res["stats"] |
| print( |
| f"{name:<12s} {'yes':<10s} " |
| f"{s['rmse']:>12.6f} {s['max_err']:>12.6f} " |
| f"{s['cluster_usage']:>10d}" |
| ) |
| print(f"{'=' * 72}") |
|
|
|
|
| def parse_args(): |
| parser = argparse.ArgumentParser( |
| description="Generate ablated quantized reconstruction PLY variants." |
| ) |
| parser.add_argument("ply_path", type=str, help="Path to the fine/original PLY.") |
| parser.add_argument( |
| "--codebook_dir", |
| type=str, |
| default="./codebooks", |
| help="Directory containing scale/rotation/dc/sh codebooks.", |
| ) |
| parser.add_argument( |
| "--save_dir", |
| type=str, |
| default="./ablate_quantized", |
| help="Output directory for ablation PLY files.", |
| ) |
| parser.add_argument( |
| "--modes", |
| nargs="+", |
| default=["all", "only_scale", "only_rotation", "only_dc", "only_sh"], |
| choices=sorted(MODE_MAP.keys()), |
| help="Which ablation variants to write.", |
| ) |
| return parser.parse_args() |
|
|
|
|
| def main(): |
| args = parse_args() |
| from quantize import read_ply, load_codebook, quantize, save_reconstructed_ply |
|
|
| os.makedirs(args.save_dir, exist_ok=True) |
|
|
| data = read_ply(args.ply_path) |
|
|
| codebooks = { |
| name: load_codebook(args.codebook_dir, name) |
| for name in FIELD_NAMES |
| } |
|
|
| scene_name = os.path.splitext(os.path.basename(args.ply_path))[0] |
|
|
| for mode in args.modes: |
| quantized_fields = MODE_MAP[mode] |
| results = build_variant_results(data, codebooks, quantized_fields, quantize) |
| print_mode_summary(mode, results) |
|
|
| out_path = os.path.join(args.save_dir, f"{scene_name}_{mode}.ply") |
| save_reconstructed_ply(out_path, data, results) |
| print(f"[save] {out_path}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|