examples/nerf.py
def batched_forward(
    net,
    ray_bundle: RayBundle,
    n_batches: int = 16,
    path = None,
    **kwargs,
):
    """
    This function is used to allow for memory efficient processing
    of input rays. The input rays are first split to `n_batches`
    chunks and passed through the `self.forward` function one at a time
    in a for loop. Combined with disabling Pytorch gradient caching
    (`torch.no_grad()`), this allows for rendering large batches
    of rays that do not all fit into GPU memory in a single forward pass.
    In our case, batched_forward is used to export a fully-sized render
    of the radiance field for visualisation purposes.

    Args:
        ray_bundle: A RayBundle object containing the following variables:
            origins: A tensor of shape `(minibatch, ..., 3)` denoting the
                origins of the sampling rays in world coords.
            directions: A tensor of shape `(minibatch, ..., 3)`
                containing the direction vectors of sampling rays in world coords.
            lengths: A tensor of shape `(minibatch, ..., num_points_per_ray)`
                containing the lengths at which the rays are sampled.
        n_batches: Specifies the number of batches the input rays are split into.
            The larger the number of batches, the smaller the memory footprint
            and the lower the processing speed.

    Returns:
        rays_densities: A tensor of shape `(minibatch, ..., num_points_per_ray, 1)`
            denoting the opacitiy of each ray point.
        rays_colors: A tensor of shape `(minibatch, ..., num_points_per_ray, 3)`
            denoting the color of each ray point.

    """

    # Parse out shapes needed for tensor reshaping in this function.
    n_pts_per_ray = ray_bundle.lengths.shape[-1]
    spatial_size = [*ray_bundle.origins.shape[:-1], n_pts_per_ray]

    # Split the rays to `n_batches` batches.
    tot_samples = ray_bundle.origins.shape[:-1].numel()
    batches = torch.chunk(torch.arange(tot_samples), n_batches)

    # For each batch, execute the standard forward pass.
    batch_outputs = [
        net(
            RayBundle(
                origins=ray_bundle.origins.view(-1, 3)[batch_idx],
                directions=ray_bundle.directions.view(-1, 3)[batch_idx],
                lengths=ray_bundle.lengths.view(-1, n_pts_per_ray)[batch_idx],
                xys=None,
            ), path=None if path is None else f"{path}/batch{i}"
        ) for i, batch_idx in enumerate(batches)
    ]

    # Concatenate the per-batch rays_densities and rays_colors
    # and reshape according to the sizes of the inputs.
    rays_densities, rays_colors = [
        torch.cat(
            [batch_output[output_i] for batch_output in batch_outputs], dim=0
        ).view(*spatial_size, -1) for output_i in (0, 1)
    ]
    if path is not None:
        torch.save(spatial_size, f"{path}/spatial_size.pt")
    return rays_densities, rays_colors

    This function is used to allow for memory efficient processing
    of input rays. The input rays are first split to `n_batches`
    chunks and passed through the `self.forward` function one at a time
    in a for loop. Combined with disabling Pytorch gradient caching
    (`torch.no_grad()`), this allows for rendering large batches
    of rays that do not all fit into GPU memory in a single forward pass.
    In our case, batched_forward is used to export a fully-sized render
    of the radiance field for visualisation purposes.

    Args:
        ray_bundle: A RayBundle object containing the following variables:
            origins: A tensor of shape `(minibatch, ..., 3)` denoting the
                origins of the sampling rays in world coords.
            directions: A tensor of shape `(minibatch, ..., 3)`
                containing the direction vectors of sampling rays in world coords.
            lengths: A tensor of shape `(minibatch, ..., num_points_per_ray)`
                containing the lengths at which the rays are sampled.
        n_batches: Specifies the number of batches the input rays are split into.
            The larger the number of batches, the smaller the memory footprint
            and the lower the processing speed.

    Returns:
        rays_densities: A tensor of shape `(minibatch, ..., num_points_per_ray, 1)`
            denoting the opacitiy of each ray point.
        rays_colors: A tensor of shape `(minibatch, ..., num_points_per_ray, 3)`
            denoting the color of each ray point.

    
This function is used to allow for memory efficient processing
of input rays. The input rays are first split to `n_batches`
chunks and passed through the `self.forward` function one at a time
in a for loop. Combined with disabling Pytorch gradient caching
(`torch.no_grad()`), this allows for rendering large batches
of rays that do not all fit into GPU memory in a single forward pass.
In our case, batched_forward is used to export a fully-sized render
of the radiance field for visualisation purposes.
This function is used to allow for memory efficient processing of input rays . The input rays are first split to ` n_batches ` chunks and passed through the ` self . forward ` function one at a time in a for loop . Combined with disabling Pytorch gradient caching ( ` torch . no_grad () ` ) this allows for rendering large batches of rays that do not all fit into GPU memory in a single forward pass . In our case batched_forward is used to export a fully - sized render of the radiance field for visualisation purposes .
def batched_forward(
    net,
    ray_bundle: RayBundle,
    n_batches: int = 16,
    path = None,
    **kwargs,
):
    n_pts_per_ray = ray_bundle.lengths.shape[-1]
    spatial_size = [*ray_bundle.origins.shape[:-1], n_pts_per_ray]
    tot_samples = ray_bundle.origins.shape[:-1].numel()
    batches = torch.chunk(torch.arange(tot_samples), n_batches)
    batch_outputs = [
        net(
            RayBundle(
                origins=ray_bundle.origins.view(-1, 3)[batch_idx],
                directions=ray_bundle.directions.view(-1, 3)[batch_idx],
                lengths=ray_bundle.lengths.view(-1, n_pts_per_ray)[batch_idx],
                xys=None,
            ), path=None if path is None else f"{path}/batch{i}"
        ) for i, batch_idx in enumerate(batches)
    ]
    rays_densities, rays_colors = [
        torch.cat(
            [batch_output[output_i] for batch_output in batch_outputs], dim=0
        ).view(*spatial_size, -1) for output_i in (0, 1)
    ]
    if path is not None:
        torch.save(spatial_size, f"{path}/spatial_size.pt")
    return rays_densities, rays_colors
def batched_forward ( net , ray_bundle : RayBundle , n_batches : int = 16 , path = None , ** kwargs , ) : n_pts_per_ray = ray_bundle . lengths . shape [ - 1 ] spatial_size = [ * ray_bundle . origins . shape [ : - 1 ] , n_pts_per_ray ] tot_samples = ray_bundle . origins . shape [ : - 1 ] . numel ( ) batches = torch . chunk ( torch . arange ( tot_samples ) , n_batches ) batch_outputs = [ net ( RayBundle ( origins = ray_bundle . origins . view ( - 1 , 3 ) [ batch_idx ] , directions = ray_bundle . directions . view ( - 1 , 3 ) [ batch_idx ] , lengths = ray_bundle . lengths . view ( - 1 , n_pts_per_ray ) [ batch_idx ] , xys = None , ) , path = None if path is None else f"{path}/batch{i}" ) for i , batch_idx in enumerate ( batches ) ] rays_densities , rays_colors = [ torch . cat ( [ batch_output [ output_i ] for batch_output in batch_outputs ] , dim = 0 ) . view ( * spatial_size , - 1 ) for output_i in ( 0 , 1 ) ] if path is not None : torch . save ( spatial_size , f"{path}/spatial_size.pt" ) return rays_densities , rays_colors
This function is used to allow for memory efficient processing
of input rays.
This function is used to allow for memory efficient processing of input rays .
"""
    This function is used to allow for memory efficient processing
    of input rays. The input rays are first split to `n_batches`
    chunks and passed through the `self.forward` function one at a time
    in a for loop. Combined with disabling Pytorch gradient caching
    (`torch.no_grad()`), this allows for rendering large batches
    of rays that do not all fit into GPU memory in a single forward pass.
    In our case, batched_forward is used to export a fully-sized render
    of the radiance field for visualisation purposes.

    Args:
        ray_bundle: A RayBundle object containing the following variables:
            origins: A tensor of shape `(minibatch, ..., 3)` denoting the
                origins of the sampling rays in world coords.
            directions: A tensor of shape `(minibatch, ..., 3)`
                containing the direction vectors of sampling rays in world coords.
            lengths: A tensor of shape `(minibatch, ..., num_points_per_ray)`
                containing the lengths at which the rays are sampled.
        n_batches: Specifies the number of batches the input rays are split into.
            The larger the number of batches, the smaller the memory footprint
            and the lower the processing speed.

    Returns:
        rays_densities: A tensor of shape `(minibatch, ..., num_points_per_ray, 1)`
            denoting the opacitiy of each ray point.
        rays_colors: A tensor of shape `(minibatch, ..., num_points_per_ray, 3)`
            denoting the color of each ray point.

    """ # Parse out shapes needed for tensor reshaping in this function. # Split the rays to `n_batches` batches. # For each batch, execute the standard forward pass. # Concatenate the per-batch rays_densities and rays_colors # and reshape according to the sizes of the inputs.
A tensor of shape `(minibatch, ..., num_points_per_ray, 1)`
denoting the opacitiy of each ray point.
rays_colors: A tensor of shape `(minibatch, ..., num_points_per_ray, 3)`
denoting the color of each ray point.
A tensor of shape ` ( minibatch ... num_points_per_ray 1 ) ` denoting the opacitiy of each ray point . rays_colors : A tensor of shape ` ( minibatch ... num_points_per_ray 3 ) ` denoting the color of each ray point .
rays_densities
net
ray_bundle
RayBundle
A RayBundle object containing the following variables:
origins: A tensor of shape `(minibatch, ..., 3)` denoting the
origins of the sampling rays in world coords.
directions: A tensor of shape `(minibatch, ..., 3)`
containing the direction vectors of sampling rays in world coords.
lengths: A tensor of shape `(minibatch, ..., num_points_per_ray)`
containing the lengths at which the rays are sampled.
A RayBundle object containing the following variables : origins : A tensor of shape ` ( minibatch ... 3 ) ` denoting the origins of the sampling rays in world coords . directions : A tensor of shape ` ( minibatch ... 3 ) ` containing the direction vectors of sampling rays in world coords . lengths : A tensor of shape ` ( minibatch ... num_points_per_ray ) ` containing the lengths at which the rays are sampled .
n_batches
int
Specifies the number of batches the input rays are split into.
The larger the number of batches, the smaller the memory footprint
and the lower the processing speed.
Specifies the number of batches the input rays are split into . The larger the number of batches the smaller the memory footprint and the lower the processing speed .
path