Shortcuts

nerfacc.importance_sampling

nerfacc.importance_sampling(intervals, cdfs, n_intervals_per_ray, stratified=False)

Importance sampling that supports flattened tensor.

Given a set of intervals and the corresponding CDFs at the interval edges, this function performs inverse transform sampling to create a new set of intervals and samples. Stratified sampling is also supported.

Parameters:
  • intervals (RayIntervals) – A RayIntervals object that specifies the edges of the intervals along the rays.

  • cdfs (Tensor) – The CDFs at the interval edges. It has the same shape as intervals.vals.

  • n_intervals_per_ray (Union[Tensor, int]) – Resample each ray to have this many intervals. If it is a tensor, it must be of shape (n_rays,). If it is an int, it is broadcasted to all rays.

  • stratified (bool) – If True, perform stratified sampling.

Returns:

  • intervals: A RayIntervals object. If n_intervals_per_ray is an int, intervals.vals will has the shape of (n_rays, n_intervals_per_ray + 1). If n_intervals_per_ray is a tensor, we assume each ray results in a different number of intervals. In this case, intervals.vals will has the shape of (all_edges,), the attributes packed_info, ray_indices, is_left and is_right will be accessable.

  • samples: A RaySamples object. If n_intervals_per_ray is an int, samples.vals will has the shape of (n_rays, n_intervals_per_ray). If n_intervals_per_ray is a tensor, we assume each ray results in a different number of intervals. In this case, samples.vals will has the shape of (all_samples,), the attributes packed_info and ray_indices will be accessable.

Return type:

A tuple of {RayIntervals, RaySamples}

Example:

>>> intervals = RayIntervals(
...     vals=torch.tensor([0.0, 1.0, 0.0, 1.0, 2.0], device="cuda"),
...     packed_info=torch.tensor([[0, 2], [2, 3]], device="cuda"),
... )
>>> cdfs = torch.tensor([0.0, 0.5, 0.0, 0.5, 1.0], device="cuda")
>>> n_intervals_per_ray = 2
>>> intervals, samples = importance_sampling(intervals, cdfs, n_intervals_per_ray)
>>> intervals.vals
tensor([[0.0000, 0.5000, 1.0000],
        [0.0000, 1.0000, 2.0000]], device='cuda:0')
>>> samples.vals
tensor([[0.2500, 0.7500],
        [0.5000, 1.5000]], device='cuda:0')