TwinWorld_Datasets / script /render_test_poses.py
Zqlin's picture
Upload folder using huggingface_hub (part 2)
c4a493a verified
Raw
History Blame Contribute Delete
5.05 kB
"""
EXAMPLE ONLY. Renders a trained 3D Gaussian Splatting model at this
challenge's test camera poses, producing images that go straight into a
submission's rgb/ folder.
This script targets the vanilla method from
https://github.com/graphdeco-inria/gaussian-splatting: it must be run from
inside a gaussian-splatting (or API-compatible fork) checkout, next to
train.py/render.py, since it imports that repo's
scene/gaussian_renderer/utils/arguments modules. If your submission was
produced with a different method/codebase, this script will not run as-is --
adapt the model loading and rendering calls to your own repo, keeping the
same camera-pose parsing (cameras.txt/images.txt) and output naming
(<output_dir>/rgb/<frame_id>.png) shown below.
Output:
<output_dir>/rgb/<frame_id>.png
<frame_id> is the filename stem taken from the pose file's own image names
(images.txt NAME column) -- the same stem the scoring program matches
submitted renderings against.
Usage Example (scene_000):
python render_test_poses.py \\
--model_ply <model_path>/point_cloud/iteration_30000/point_cloud.ply \\
--camera_pose_dir Data_TUM/scene_000/test/sparse/0 \\
--output_dir <submission_dir>/scene_000
"""
import torch
import torchvision
from pathlib import Path
from argparse import ArgumentParser
from scene.colmap_loader import read_extrinsics_text, read_intrinsics_text, qvec2rotmat
from scene.gaussian_model import GaussianModel
from scene.cameras import MiniCam
from gaussian_renderer import render
from utils.graphics_utils import getWorld2View2, getProjectionMatrix, focal2fov
from arguments import PipelineParams
def build_minicam(qvec, tvec, width, height, fx, fy, resolution_scale, znear=0.01, zfar=100.0):
R = qvec2rotmat(qvec).transpose()
T = tvec
out_w = max(1, round(width / resolution_scale))
out_h = max(1, round(height / resolution_scale))
FoVx = focal2fov(fx, width)
FoVy = focal2fov(fy, height)
world_view_transform = torch.tensor(getWorld2View2(R, T)).transpose(0, 1).cuda()
projection_matrix = getProjectionMatrix(znear=znear, zfar=zfar, fovX=FoVx, fovY=FoVy).transpose(0, 1).cuda()
full_proj_transform = world_view_transform.unsqueeze(0).bmm(projection_matrix.unsqueeze(0)).squeeze(0)
return MiniCam(out_w, out_h, FoVy, FoVx, znear, zfar, world_view_transform, full_proj_transform)
def load_test_poses(camera_pose_dir: Path):
cameras = read_intrinsics_text(camera_pose_dir / "cameras.txt")
images = read_extrinsics_text(camera_pose_dir / "images.txt")
entries = []
for image in images.values():
cam = cameras[image.camera_id]
assert cam.model == "PINHOLE", "only undistorted PINHOLE cameras are supported"
fx, fy = cam.params[0], cam.params[1]
entries.append((image.name, image.qvec, image.tvec, cam.width, cam.height, fx, fy))
entries.sort(key=lambda e: e[0])
return entries
def main():
parser = ArgumentParser(description="Render a trained 3DGS model at the challenge's test camera poses")
parser.add_argument("--model_ply", required=True, type=Path,
help="trained point_cloud.ply, e.g. <model_path>/point_cloud/iteration_30000/point_cloud.ply")
parser.add_argument("--camera_pose_dir", required=True, type=Path,
help="folder containing cameras.txt and images.txt for the test poses, "
"e.g. Data_TUM/scene_000/test/sparse/0")
parser.add_argument("--output_dir", required=True, type=Path,
help="renders are written to <output_dir>/rgb/<frame_id>.png")
parser.add_argument("--sh_degree", type=int, default=3)
parser.add_argument("--resolution_scale", type=float, default=1.0,
help="downsample factor applied to the cameras.txt WIDTH/HEIGHT; must match "
"the reference image size the submission is scored against")
parser.add_argument("--white_background", action="store_true")
pipeline_params = PipelineParams(parser)
args = parser.parse_args()
pipeline = pipeline_params.extract(args)
gaussians = GaussianModel(args.sh_degree)
gaussians.load_ply(str(args.model_ply))
bg_color = [1, 1, 1] if args.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
rgb_dir = args.output_dir / "rgb"
rgb_dir.mkdir(parents=True, exist_ok=True)
poses = load_test_poses(args.camera_pose_dir)
print(f"Rendering {len(poses)} test views from {args.camera_pose_dir}")
with torch.no_grad():
for name, qvec, tvec, width, height, fx, fy in poses:
cam = build_minicam(qvec, tvec, width, height, fx, fy, args.resolution_scale)
image = torch.clamp(render(cam, gaussians, pipeline, background)["render"], 0.0, 1.0)
stem = Path(name).stem
torchvision.utils.save_image(image, rgb_dir / f"{stem}.png")
print(f"Done. renders -> {rgb_dir}")
if __name__ == "__main__":
main()