File size: 4,532 Bytes
50185e9 | 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 151 152 153 154 155 156 157 158 159 | """
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()
|