Add 'inference' mode to pipeline (#956)

* cleanup test mode

* cleanup eval loading

* change default

* cleanup

* cleanup

* comment
This commit is contained in:
Nikhil Mohan authored and GitHub committed 2022-11-14 16:56:36 +00:00
1 parent b67f96fc56
commit b5c34f0154
7 files changed
+64 -38

No files matched your search

+22 -16
View File
@@ -4,13 +4,14 @@ render.py
"""
from __future__ import annotations
import dataclasses
import json
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
from typing import List, Optional
import mediapy as media
import numpy as np
import torch
import tyro
from rich.console import Console
@@ -38,7 +39,7 @@ def _render_trajectory_video(
pipeline: Pipeline,
cameras: Cameras,
output_filename: Path,
rendered_output_name: str,
rendered_output_names: List[str],
rendered_resolution_scaling_factor: float = 1.0,
seconds: float = 5.0,
output_format: Literal["images", "video"] = "video",
@@ -49,7 +50,7 @@ def _render_trajectory_video(
pipeline: Pipeline to evaluate with.
cameras: Cameras to render.
output_filename: Name of the output file.
rendered_output_name: Name of the renderer output to use.
rendered_output_names: List of outputs to visualise.
rendered_resolution_scaling_factor: Scaling factor to apply to the camera image resolution.
seconds: Length of output video.
output_format: How to save output data.
@@ -73,16 +74,20 @@ def _render_trajectory_video(
camera_ray_bundle = cameras.generate_rays(camera_indices=camera_idx).to(pipeline.device)
with torch.no_grad():
outputs = pipeline.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
if rendered_output_name not in outputs:
CONSOLE.rule("Error", style="red")
CONSOLE.print(f"Could not find {rendered_output_name} in the model outputs", justify="center")
CONSOLE.print(f"Please set --rendered_output_name to one of: {outputs.keys()}", justify="center")
sys.exit(1)
image = outputs[rendered_output_name].cpu().numpy()
render_image = []
for rendered_output_name in rendered_output_names:
if rendered_output_name not in outputs:
CONSOLE.rule("Error", style="red")
CONSOLE.print(f"Could not find {rendered_output_name} in the model outputs", justify="center")
CONSOLE.print(f"Please set --rendered_output_name to one of: {outputs.keys()}", justify="center")
sys.exit(1)
output_image = outputs[rendered_output_name].cpu().numpy()
render_image.append(output_image)
render_image = np.concatenate(render_image, axis=1)
if output_format == "images":
media.write_image(output_image_dir / f"{camera_idx:05d}.png", image)
media.write_image(output_image_dir / f"{camera_idx:05d}.png", render_image)
else:
images.append(image)
images.append(render_image)
if output_format == "video":
fps = len(images) / seconds
@@ -94,14 +99,14 @@ def _render_trajectory_video(
CONSOLE.print(f"[green]Saved video to {output_filename}", justify="center")
@dataclasses.dataclass
@dataclass
class RenderTrajectory:
"""Load a checkpoint, render a trajectory, and save to a video file."""
# Path to config YAML file.
load_config: Path
# Name of the renderer output to use. rgb, depth, etc.
rendered_output_name: str = "rgb"
# Name of the renderer outputs to use. rgb, depth, etc. concatenates them along y axis
rendered_output_names: List[str] = field(default_factory=lambda: ["rgb"])
# Trajectory to render.
traj: Literal["spiral", "filename"] = "spiral"
# Scaling factor to apply to the camera image resolution.
@@ -122,6 +127,7 @@ class RenderTrajectory:
_, pipeline, _ = eval_setup(
self.load_config,
eval_num_rays_per_chunk=self.eval_num_rays_per_chunk,
test_mode="test" if self.traj == "spiral" else "inference",
)
install_checks.check_ffmpeg_installed()
@@ -145,7 +151,7 @@ class RenderTrajectory:
pipeline,
camera_path,
output_filename=self.output_path,
rendered_output_name=self.rendered_output_name,
rendered_output_names=self.rendered_output_names,
rendered_resolution_scaling_factor=1.0 / self.downscale_factor,
seconds=seconds,
output_format=self.output_format,