in models/nerf_utils.py [0:0]
def sample_pdf_2(bins, weights, num_samples, det=False):
"""sample_pdf function from another concurrent pytorch implementation
by yenchenlin (https://github.com/yenchenlin/nerf-pytorch).
"""
weights = weights + 1e-5
pdf = weights / torch.sum(weights, dim=-1, keepdim=True)
cdf = torch.cumsum(pdf, dim=-1)
cdf = torch.cat([torch.zeros_like(cdf[..., :1]), cdf], dim=-1) # (batchsize, len(bins))
# Take uniform samples
if det:
u = torch.linspace(0.0, 1.0, steps=num_samples, dtype=weights.dtype, device=weights.device)
u = u.expand(list(cdf.shape[:-1]) + [num_samples])
else:
u = torch.rand(
list(cdf.shape[:-1]) + [num_samples],
dtype=weights.dtype,
device=weights.device,
)
# Invert CDF
u = u.contiguous()
cdf = cdf.contiguous()
inds = torch.searchsorted(cdf, u, right=True)
below = torch.max(torch.zeros_like(inds - 1), inds - 1)
above = torch.min((cdf.shape[-1] - 1) * torch.ones_like(inds), inds)
inds_g = torch.stack((below, above), dim=-1) # (batchsize, num_samples, 2)
matched_shape = (inds_g.shape[0], inds_g.shape[1], cdf.shape[-1])
cdf_g = torch.gather(cdf.unsqueeze(1).expand(matched_shape), 2, inds_g)
bins_g = torch.gather(bins.unsqueeze(1).expand(matched_shape), 2, inds_g)
denom = cdf_g[..., 1] - cdf_g[..., 0]
denom = torch.where(denom < 1e-5, torch.ones_like(denom), denom)
t = (u - cdf_g[..., 0]) / denom
samples = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0])
return samples