ViewSuite-Habitat-GS / render /render_images.py
JamesK2W's picture
Add files using upload-large-folder tool
7b77e6d verified
Raw History Blame Contribute Delete
5.38 kB
#!/usr/bin/env python3
"""Re-create the images of the ViewSuite Habitat-GS corpus from the stored cameras.
The task files reference 256 x 256 PNG renders of 3D Gaussian-splatting scenes, which this repository does not
distribute (64 of the scenes are InteriorGS scenes, whose terms forbid redistribution). Every image is a
deterministic gsplat render (antialiased rasterization, as in the Habitat-GS simulator) of a scene of the
Habitat-GS scene release:
- perspective views (start, target, options): the camera in render/images.jsonl (`c2w`, OpenCV camera-to-world;
`K` for 256 x 256, 90 deg field of view);
- the top-down view of each scene: the height-clipped overhead splat of the code release
(`render_top_down`), from the overhead camera and the task-camera positions in render/top_down_inputs.jsonl.
python render/render_images.py --out corpus --gs-root <Habitat-GS scenes dir> --gpu 0
python render/render_images.py --out corpus --verify-only
Requirements: the ViewSuite code release on PYTHONPATH (view_agent) and its gsplat environment
(view_agent/envs/habitat_gs/scripts/install_env.sh), a CUDA GPU, and the Habitat-GS scenes laid out as
<gs-root>/{train,val}/<scene>/<scene>.gs.ply (the code release's download script). Read and accept the InteriorGS
Terms of Use before downloading the interior_* scenes. Prints a JSON summary; exits with status 1 if any image is
missing or differs.
pixel_sha256 = SHA-256 of the decoded image as height x width x 3 uint8 RGB bytes (row-major), so it does not
depend on the PNG encoder; file_sha256 is the hash of the original PNG file.
"""
import argparse
import collections
import hashlib
import json
import os
import sys
import numpy as np
from PIL import Image
def pixel_sha256(arr):
a = np.ascontiguousarray(np.asarray(arr)[..., :3].astype(np.uint8))
return hashlib.sha256(a.tobytes()).hexdigest()
def main():
here = os.path.dirname(os.path.abspath(__file__))
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--manifest", default=os.path.join(here, "images.jsonl"))
ap.add_argument("--top-down-inputs", default=os.path.join(here, "top_down_inputs.jsonl"))
ap.add_argument("--out", required=True, help="corpus directory: images are written below it at their paths")
ap.add_argument("--gs-root", default=None, help="Habitat-GS scenes directory ({train,val}/<scene>/<scene>.gs.ply)")
ap.add_argument("--gpu", type=int, default=0)
ap.add_argument("--scenes", default=None, help="comma-separated subset of scene ids")
ap.add_argument("--verify-only", action="store_true")
ap.add_argument("--overwrite", action="store_true", help="re-render images that already exist")
a = ap.parse_args()
jobs = [json.loads(line) for line in open(a.manifest)]
tops = {}
for line in open(a.top_down_inputs):
t = json.loads(line)
tops[t["path"]] = t
if a.scenes:
keep = set(a.scenes.split(","))
jobs = [j for j in jobs if j["scene_id"] in keep]
by_scene = collections.defaultdict(list)
for j in jobs:
by_scene[j["scene_id"]].append(j)
stats = collections.Counter()
bad = []
for scene in sorted(by_scene):
todo = [j for j in by_scene[scene]
if a.overwrite or not os.path.exists(os.path.join(a.out, j["path"]))]
if todo and not a.verify_only:
if not a.gs_root:
raise SystemExit("--gs-root is required to render")
from view_agent.envs.habitat_gs._common.splat import SplatScene
from view_agent.envs.habitat_gs._common.data_processing.topdown_splat import render_top_down
sc = SplatScene(scene, root=a.gs_root, device=f"cuda:{a.gpu}")
try:
for j in todo:
if j["kind"] == "top_down":
t = tops[j["path"]]
img, _K, _lineage = render_top_down(sc, np.asarray(t["c2w"]), np.asarray(t["view_positions"]),
output_size=j["width"], fov_deg=t["fov_deg"])
else:
img = sc.render(np.asarray(j["K"]), np.asarray(j["c2w"]), j["width"], j["height"])
dst = os.path.join(a.out, j["path"])
os.makedirs(os.path.dirname(dst), exist_ok=True)
Image.fromarray(np.asarray(img)[..., :3].astype(np.uint8)).save(dst)
stats["rendered"] += 1
finally:
sc.close()
for j in by_scene[scene]:
dst = os.path.join(a.out, j["path"])
if not os.path.exists(dst):
stats["missing"] += 1
bad.append({"path": j["path"], "problem": "missing"})
continue
with Image.open(dst) as im:
h = pixel_sha256(np.asarray(im.convert("RGB")))
if h == j["pixel_sha256"]:
stats["pixel_identical"] += 1
else:
stats["different"] += 1
bad.append({"path": j["path"], "problem": "pixels differ"})
summary = {"images": len(jobs), **stats, "problems": bad[:50]}
print(json.dumps(summary, indent=1))
return 0 if stats["pixel_identical"] == len(jobs) else 1
if __name__ == "__main__":
sys.exit(main())