Shortcuts

mmagic.models.editors.eg3d.renderer

The renderer is a module that takes in rays, decides where to sample along each ray, and computes pixel colors using the volume rendering equation.

Module Contents

Classes

EG3DRenderer

Renderer for EG3D. This class samples render points on each input ray

EG3DDecoder

Decoder for EG3D model.

class mmagic.models.editors.eg3d.renderer.EG3DRenderer(decoder_cfg: dict, ray_start: float, ray_end: float, box_warp: float = 1, depth_resolution: int = 64, depth_resolution_importance: int = 64, density_noise: float = 0, clamp_mode: str = 'softplus', white_back: bool = True, projection_mode: str = 'Official')[源代码]

Bases: mmengine.model.BaseModule

Renderer for EG3D. This class samples render points on each input ray and interpolate the triplane feature corresponding to the points’ coordinates. Then, predict each point’s RGB feature and density (sigma) by a neural network and calculate the RGB feature of each ray by integration. Different from typical NeRF models, the decoder of EG3DRenderer takes triplane feature of each points as input instead of positional encoding of the coordinates.

参数
  • decoder_cfg (dict) – The config to build neural renderer.

  • ray_start (float) – The start position of all rays.

  • ray_end (float) – The end position of all rays.

  • box_warp (float) – The side length of the cube spanned by the triplanes. The box is axis-aligned, centered at the origin. The range of each axis is [-box_warp/2, box_warp/2]. If box_warp=1.8, it has vertices at the range of axis is [-0.9, 0.9]. Defaults to 1.

  • depth_resolution (int) – Resolution of depth, as well as the number of points per ray. Defaults to 64.

  • depth_resolution_importance (int) – Resolution of depth in hierarchical sampling. Defaults to 64.

  • clamp_mode (str) – The clamp mode for density predicted by neural renderer. Defaults to ‘softplus’.

  • white_back (bool) – Whether render a white background. Defaults to True.

  • projection_mode (str) – The projection method to mapping coordinates of render points to plane feature. The usage of this argument please refer to self.project_onto_planes() and https://github.com/NVlabs/eg3d/issues/67. Defaults to ‘Official’.

get_value(target: str, render_kwargs: Optional[dict] = None) Any[源代码]

Get value of target field.

参数
  • target (str) – The key of the target field.

  • render_kwargs (Optional[dict], optional) – The input key word arguments dict. Defaults to None.

返回

The default value of target field.

返回类型

Any

forward(planes: torch.Tensor, ray_origins: torch.Tensor, ray_directions: torch.Tensor, render_kwargs: Optional[dict] = None) Tuple[torch.Tensor][源代码]

Render 2D RGB feature, weighed depth and weights with the passed triplane features and rays. ‘weights’ denotes w in Equation 5 of the NeRF’s paper.

参数
  • planes (torch.Tensor) – The triplane features shape like (bz, 3, TriPlane_feat, TriPlane_res, TriPlane_res).

  • ray_origins (torch.Tensor) – The original of each ray to render, shape like (bz, NeRF_res * NeRF_res, 3).

  • ray_directions (torch.Tensor) – The direction vector of each ray to render, shape like (bz, NeRF_res * NeRF_res, 3).

  • render_kwargs (Optional[dict], optional) – The specific kwargs for rendering. Defaults to None.

返回

Renderer RGB feature, weighted depths and

weights.

返回类型

Tuple[torch.Tensor]

sample_stratified(ray_origins: torch.Tensor, ray_start: Union[float, torch.Tensor], ray_end: Union[float, torch.Tensor], depth_resolution: int) torch.Tensor[源代码]

Return depths of approximately uniformly spaced samples along rays.

参数
  • ray_origins (torch.Tensor) – The original of each ray, shape like (bz, NeRF_res * NeRF_res, 3). Only used to provide device and shape info.

  • ray_start (Union[float, torch.Tensor]) – The start position of rays. If a float is passed, all rays will have the same start distance.

  • ray_end (Union[float, torch.Tensor]) – The end position of rays. If a float is passed, all rays will have the same end distance.

  • depth_resolution (int) – Resolution of depth, as well as the number of points per ray.

返回

The sampled coarse depth shape like

(bz, NeRF_res * NeRF_res, 1).

返回类型

torch.Tensor

neural_rendering(planes: torch.Tensor, sample_coordinates: float, density_noise: float, box_warp: float) dict[源代码]

Predict RGB features and densities of the coordinates by neural renderer model and the triplane input.

参数
  • planes (torch.Tensor) – Triplane feature shape like (bz, 3, TriPlane_feat, TriPlane_res, TriPlane_res).

  • sample_coordinates (torch.Tensor) – Coordinates of the sampling points, shape like (bz, N_depth * NeRF_res * NeRF_res, 1).

  • density_noise (float) – Strength of noise add to the predicted density.

  • box_warp (float) – The side length of the cube spanned by the triplanes.

返回

A dict contains RGB features (‘rgb’) and densities (‘sigma’).

返回类型

dict

sample_from_planes(plane_features: torch.Tensor, coordinates: torch.Tensor, interp_mode: str = 'bilinear', box_warp: float = None) torch.Tensor[源代码]

Sample from feature from triplane feature with the passed coordinates of render points.

参数
  • plane_features (torch.Tensor) – The triplane feature.

  • coordinates (torch.Tensor) – The coordinates of points to render.

  • interp_mode (str) – The interpolation mode to sample feature from triplane.

  • box_warp (float) – The side length of the cube spanned by the triplanes.

返回

The sampled triplane feature of the render points.

返回类型

torch.Tensor

project_onto_planes(coordinates: torch.Tensor) torch.Tensor[源代码]

Project 3D points to plane formed by coordinate axes. In this function, we use indexing operation to replace matrix multiplication to achieve higher calculation performance.

In the original implementation, the mapping matrix is incorrect. Therefore we support users to define projection_mode to control projection behavior in the initialization function of EG3DRenderer. If you want to run inference with the official pretrained model, please remember to set projection_mode = ‘official’. More information please refer to https://github.com/NVlabs/eg3d/issues/67.

If the project mode official, the equivalent projection matrix is inverse matrix of:

[[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 0, 1], [1, 0, 0], [0, 1, 0]]]

Otherwise, the equivalent projection matrix is inverse matrix of:

[[[1, 0, 0], [0, 1, 0], [0, 0, 1]], [[1, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 0, 1], [0, 1, 0], [1, 0, 0]]]

参数

coordinates (torch.Tensor) – The coordinates of the render points. shape like (bz, NeRF_res * NeRF_res * N_depth, 3).

返回

The projected coordinates.

返回类型

torch.Tensor

unify_samples(depths_c: torch.Tensor, colors_c: torch.Tensor, densities_c: torch.Tensor, depths_f: torch.Tensor, colors_f: torch.Tensor, densities_f: torch.Tensor) Tuple[torch.Tensor][源代码]

Sort and merge coarse samples and fine samples.

参数
  • depths_c (torch.Tensor) – Coarse depths shape like (bz, NeRF_res * NeRF_res, N_depth, 1).

  • colors_c (torch.Tensor) – Coarse color features shape like (bz, NeRF_res * NeRF_res, N_depth, N_feat).

  • densities_c (torch.Tensor) – Coarse densities shape like (bz, NeRF_res * NeRF_res, N_depth, 1).

  • depths_f (torch.Tensor) – Fine depths shape like (bz, NeRF_res * NeRF_res, N_depth_fine, 1).

  • colors_f (torch.Tensor) – Fine colors features shape like (bz, NeRF_res * NeRF_res, N_depth_fine, N_feat).

  • densities_f (torch.Tensor) – Fine densities shape like (bz, NeRF_res * NeRF_res, N_depth_fine, 1).

返回

Unified depths, color features and densities.

The third dimension of returns are N_depth + N_depth_fine.

返回类型

Tuple[torch.Tensor]

volume_rendering(colors: torch.Tensor, densities: torch.Tensor, depths: torch.Tensor) Tuple[torch.Tensor][源代码]

Volume rendering.

参数
  • colors (torch.Tensor) – Color feature for each points. Shape like (bz, N_points, N_depth, N_feature).

  • densities (torch.Tensor) – Density for each points. Shape like (bz, N_points, N_depth, 1).

  • depths (torch.Tensor) – Depths for each points. Shape like (bz, N_points, N_depth, 1).

返回

A tuple of color feature

(bz, N_points, N_feature), weighted depth (bz, N_points, 1) and weight (bz, N_points, N_depth-1, 1).

返回类型

Tuple[torch.Tensor]

sample_importance(z_vals: torch.Tensor, weights: torch.Tensor, N_importance: int) torch.Tensor[源代码]

Return depths of importance sampled points along rays.

参数
  • z_vals (torch.Tensor) – Coarse Z value (depth). Shape like (bz, N_points, N_depth, N_feature).

  • weights (torch.Tensor) – Weights of the coarse samples. Shape like (bz, N_points, N_depths-1, 1).

  • N_importance (int) – Number of samples to resample.

class mmagic.models.editors.eg3d.renderer.EG3DDecoder(in_channels: int, out_channels: int = 32, hidden_channels: int = 64, lr_multiplier: float = 1, rgb_padding: float = 0.001)[源代码]

Bases: mmengine.model.BaseModule

Decoder for EG3D model.

参数
  • in_channels (int) – The number of input channels.

  • out_channels (int) – The number of output channels. Defaults to 32.

  • hidden_channels (int) – The number of channels of hidden layer. Defaults to 64.

  • lr_multiplier (float, optional) – Equalized learning rate multiplier. Defaults to 1.

  • rgb_padding (float) – Padding for RGB output. Defaults to 0.001.

forward(sampled_features: torch.Tensor) dict[源代码]

Forward function.

参数

sampled_features (torch.Tensor) – The sampled triplane feature for each points. Shape like (batch_size, xxx, xxx, n_ch).

返回

A dict contains rgb feature and sigma value for each point.

返回类型

dict

Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.