Integrating WebRTC into viewer (#73)

* restructuring for webrtc integration

* some webrtc code but not working

* webrtc connects when muted

* the flag is real!

* updating camera in websocket bridge. moved webrtc to existing websocket connection instead of http request. need to change video track

* rudimentary adding real-time imaging

* added free TURN server so code works everywhere

* slow rendering works during training with the viewer

* fix tensor device and update default model save dir

* forgot to include trainer updates

* removing some code that isn't needed

* quick fix to bins. not the most accurate however

* cleaned up code and added some config logic

* black

* adding license

Co-authored-by: evonneng <evonne_ng@berkeley.edu>
This commit is contained in:
Ethan Weberandevonneng authored and GitHub committed 2022-07-05 14:52:49 -07:00
1 parent e507c97e3e
commit bf13ed3cf3
28 files changed
+8923 -917

No files matched your search

+88 -8
View File
@@ -17,13 +17,16 @@ Code to train model.
"""
import logging
import os
import random
from typing import Dict
import torch
import torch.distributed as dist
import umsgpack
from torch.nn.parallel import DistributedDataParallel as DDP
from torchtyping import TensorType
from pyrad.cameras.cameras import get_camera, get_intrinsics_from_intrinsics_matrix
from pyrad.cameras.rays import RayBundle
from pyrad.data.dataloader import EvalDataloader, setup_dataset_eval, setup_dataset_train
from pyrad.graphs.base import setup_graph
@@ -32,6 +35,9 @@ from pyrad.utils import profiler, writer
from pyrad.utils.config import Config
from pyrad.utils.decorators import check_main_thread
from pyrad.utils.writer import EventName, TimeWriter
from pyrad.viewer.backend import vis_utils
from pyrad.viewer.backend.utils import get_intrinsics_matrix_and_camera_to_world_h
from pyrad.viewer.backend.visualizer import ViewerWindow, Visualizer
logging.getLogger("PIL").setLevel(logging.WARNING)
@@ -40,10 +46,9 @@ class Trainer:
"""Training class
Args:
config (Config): _description_
local_rank (int, optional): _description_. Defaults to 0.
world_size (int, optional): _description_. Defaults to 1.
cpu (bool, optional): Whether or not to use the CPU.
config (Config): The configuration object.
local_rank (int, optional): Local rank of the process. Defaults to 0.
world_size (int, optional): World size of the process. Defaults to 1.
"""
def __init__(self, config: Config, local_rank: int = 0, world_size: int = 1):
@@ -52,6 +57,7 @@ class Trainer:
self.world_size = world_size
self.device = "cpu" if world_size == 0 else f"cuda:{local_rank}"
# dataset variables
self.dataset_inputs_train = None
self.dataloader_train = None
self.dataloader_eval = None
# model variables
@@ -62,11 +68,20 @@ class Trainer:
writer.setup_event_writers(config.logging, max_iter=config.trainer.max_num_iterations)
profiler.setup_profiler(config.logging)
self.vis = None
if self.config.viewer.enable:
window = ViewerWindow(zmq_url=self.config.viewer.zmq_url)
self.vis = Visualizer(window=window)
logging.info("Connected to viewer at %s", self.config.viewer.zmq_url)
self.vis.delete()
else:
logging.info("Continuing without viewer.")
def setup(self, test_mode=False):
"""Setup the Trainer by calling other setup functions."""
dataset_inputs_train, self.dataloader_train = setup_dataset_train(self.config.data, device=self.device)
self.dataset_inputs_train, self.dataloader_train = setup_dataset_train(self.config.data, device=self.device)
_, self.dataloader_eval = setup_dataset_eval(self.config.data, test_mode=test_mode, device=self.device)
self.graph = setup_graph(self.config.graph, dataset_inputs_train, device=self.device)
self.graph = setup_graph(self.config.graph, self.dataset_inputs_train, device=self.device)
self.optimizers = setup_optimizers(self.config.optimizers, self.graph.get_param_groups())
if self.config.trainer.resume_train.load_dir:
@@ -90,7 +105,11 @@ class Trainer:
return loss_sum
def train(self) -> None:
"""_summary_"""
"""Train the model."""
if self.vis:
self.draw_scene_in_viewer()
with TimeWriter(writer, EventName.TOTAL_TRAIN_TIME):
num_iterations = self.config.trainer.max_num_iterations
iter_dataloader_train = iter(self.dataloader_train)
@@ -106,6 +125,13 @@ class Trainer:
writer.put_dict(name="Loss/train-loss_dict", scalar_dict=loss_dict, step=step)
if step != 0 and self.config.trainer.steps_per_save and step % self.config.trainer.steps_per_save == 0:
self._save_checkpoint(self.config.trainer.model_dir, step)
if (
self.vis
and step != 0
and self.config.viewer.steps_per_render_image
and step % self.config.viewer.steps_per_render_image == 0
):
_ = self.render_image_in_viewer()
if step % self.config.trainer.steps_per_test == 0:
self.eval_with_dataloader(self.dataloader_eval, step=step)
self._write_out_storage(step)
@@ -128,7 +154,7 @@ class Trainer:
def _load_checkpoint(self) -> None:
"""helper function to load graph and optimizer from prespecified checkpoint"""
load_config = self.config.resume_train
load_config = self.config.trainer.resume_train
load_path = os.path.join(load_config.load_dir, f"step-{load_config.load_step:09d}.ckpt")
assert os.path.exists(load_path), f"Checkpoint {load_path} does not exist"
loaded_state = torch.load(load_path, map_location="cpu")
@@ -200,6 +226,60 @@ class Trainer:
self.graph.train()
return psnr
def draw_scene_in_viewer(self):
"""Draw some images and the scene aabb in the viewer."""
image_dataset_train = self.dataloader_train.image_sampler.dataset
dataset_inputs = self.dataset_inputs_train
indices = random.sample(range(len(image_dataset_train)), k=10)
for idx in indices:
image = image_dataset_train[idx]["image"]
camera = get_camera(dataset_inputs.intrinsics[idx], dataset_inputs.camera_to_world[idx], None)
pose = camera.get_camera_to_world().double().numpy()
K = camera.get_intrinsics_matrix().double().numpy()
vis_utils.draw_camera_frustum(
self.vis,
image=(image.double().numpy() * 255.0),
pose=pose,
K=K,
height=1.0,
name=f"image_dataset_train/{idx:06d}",
displayed_focal_length=0.5,
realistic=False,
)
aabb = dataset_inputs.scene_bounds.aabb
vis_utils.draw_aabb(self.vis, aabb, name="dataset_inputs_train/scene_bounds/aabb")
@profiler.time_function
def render_image_in_viewer(self):
"""
Draw an image using the current camera pose from the viewer.
The image is sent of a TCP connection and then uses WebRTC to send it to the viewer.
"""
data = self.vis["/Cameras/Main Camera"].get_object()
message = umsgpack.unpackb(data)
camera_object = message["object"]["object"]
image_height = self.config.viewer.render_image_height
intrinsics_matrix, camera_to_world_h = get_intrinsics_matrix_and_camera_to_world_h(
camera_object, image_height=image_height
)
camera_to_world = camera_to_world_h[:3, :]
intrinsics = get_intrinsics_from_intrinsics_matrix(intrinsics_matrix)
camera = get_camera(intrinsics, camera_to_world)
camera_ray_bundle = camera.get_camera_ray_bundle(device=self.device)
camera_ray_bundle.num_rays_per_chunk = self.config.viewer.num_rays_per_chunk
self.graph.eval()
outputs = self.graph.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
self.graph.train()
# gross hack to get the image key, depending on which keys the graph uses
rgb_key = "rgb" if "rgb" in outputs else "rgb_fine"
# TODO: make it such that the TCP connection doesn't need float64
image = outputs[rgb_key].cpu().numpy().astype("float64") * 255
self.vis["/Cameras/Main Camera"].set_image(image)
return outputs
def eval_with_dataloader(self, dataloader: EvalDataloader, step: int = None) -> None:
"""Run evaluation with a given dataloader.