Skip to content

Instantly share code, notes, and snippets.

View Mason-McGough's full-sized avatar
⛰️
Living in the right balance

Mason McGough Mason-McGough

⛰️
Living in the right balance
View GitHub Profile
@Mason-McGough
Mason-McGough / nerf_volume_render.py
Created April 23, 2022 17:41
Volume rendering function for NeRF.
def raw2outputs(
raw: torch.Tensor,
z_vals: torch.Tensor,
rays_d: torch.Tensor,
raw_noise_std: float = 0.0,
white_bkgd: bool = False
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
r"""
Convert the raw NeRF output into RGB and other maps.
"""
@Mason-McGough
Mason-McGough / nerf_positional_encoder.py
Last active April 23, 2022 17:40
Standard positional encoder for NeRF
class PositionalEncoder(nn.Module):
r"""
Sine-cosine positional encoder for input points.
"""
def __init__(
self,
d_input: int,
n_freqs: int,
log_space: bool = False
):
@Mason-McGough
Mason-McGough / streamvideo.py
Created September 17, 2021 00:14
Threaded frame grabbing and display for generic webcams or Intel RealSense camera
import time
import threading
import numpy as np
import cv2 as cv
def _is_window_open(win_name):
return cv.getWindowProperty(win_name, cv.WND_PROP_VISIBLE) > 0
class VideoGetter():
@Mason-McGough
Mason-McGough / calc_volume_weights.py
Last active June 25, 2021 18:34
GIRAFFE volume weights
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
weights = self.calc_volume_weights(di, ray_vector, sigma_sum)
feat_map = torch.sum(weights.unsqueeze(-1) * feat_weighted, dim=-2)
@Mason-McGough
Mason-McGough / composite.py
Created June 25, 2021 17:38
GIRAFFE composite
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
sigma_sum, feat_weighted = self.composite_function(sigma, feat)
@Mason-McGough
Mason-McGough / latent_code.py
Created June 25, 2021 17:38
GIRAFFE latent codes
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
z_shape_obj, z_app_obj, z_shape_bg, z_app_bg = latent_codes
@Mason-McGough
Mason-McGough / generator_dict.py
Last active June 25, 2021 18:24
GIRAFFE generator dict
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
from im2scene.giraffe.models import generator
# ...
generator_dict = {
'simple': generator.Generator,
}
@Mason-McGough
Mason-McGough / generator_feature_and_sigma.py
Created June 25, 2021 17:36
GIRAFFE feature and sigma fields
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
n_iter = n_boxes if not_render_background else n_boxes + 1
# ...
for i in range(n_iter):
if i < n_boxes: # Object
p_i, r_i = self.get_evaluation_points(pixels_world,
camera_world, di, transformations, i)
z_shape_i, z_app_i = z_shape_obj[:, i], z_app_obj[:, i]
feat_i, sigma_i = self.decoder(p_i, r_i, z_shape_i, z_app_i)
# ...
@Mason-McGough
Mason-McGough / generator_latent_codes.py
Last active June 25, 2021 17:41
GIRAFFE generator
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
class Generator(nn.Module):
# ...
def get_latent_codes(self, batch_size=32, tmp=1.):
z_dim, z_dim_bg = self.z_dim, self.z_dim_bg
n_boxes = self.get_n_boxes()
def sample_z(x): return self.sample_z(x, tmp=tmp)
z_shape_obj = sample_z((batch_size, n_boxes, z_dim))
z_app_obj = sample_z((batch_size, n_boxes, z_dim))
z_shape_bg = sample_z((batch_size, z_dim_bg))
@Mason-McGough
Mason-McGough / rendering_render_object_rotation.py
Last active June 25, 2021 17:33
GIRAFFE object rotation method
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
class Renderer(object):
# ...
def render_object_rotation(self, img_out_path, batch_size=15, n_steps=32):
gen = self.generator
bbox_generator = gen.bounding_box_generator
n_boxes = bbox_generator.n_boxes
# Set rotation range