Transforms model's predictions to semantically meaningful values. Args: raw: [num_rays, num_samples along ray, 4]. Prediction from model. z_vals: [num_rays, num_samples along ray]. Integration time. rays_d: [num_rays, 3]. Direction of each ray. Returns:
(raw, z_vals, rays_d, raw_noise_std=0, white_bkgd=False, pytest=False)
| 270 | |
| 271 | |
| 272 | def raw2outputs(raw, z_vals, rays_d, raw_noise_std=0, white_bkgd=False, pytest=False): |
| 273 | """Transforms model's predictions to semantically meaningful values. |
| 274 | Args: |
| 275 | raw: [num_rays, num_samples along ray, 4]. Prediction from model. |
| 276 | z_vals: [num_rays, num_samples along ray]. Integration time. |
| 277 | rays_d: [num_rays, 3]. Direction of each ray. |
| 278 | Returns: |
| 279 | rgb_map: [num_rays, 3]. Estimated RGB color of a ray. |
| 280 | disp_map: [num_rays]. Disparity map. Inverse of depth map. |
| 281 | acc_map: [num_rays]. Sum of weights along each ray. |
| 282 | weights: [num_rays, num_samples]. Weights assigned to each sampled color. |
| 283 | depth_map: [num_rays]. Estimated distance to object. |
| 284 | """ |
| 285 | raw2alpha = lambda raw, dists, act_fn=F.relu: 1.-torch.exp(-act_fn(raw)*dists) |
| 286 | |
| 287 | dists = z_vals[...,1:] - z_vals[...,:-1] |
| 288 | dists = torch.cat([dists, torch.Tensor([1e10]).expand(dists[...,:1].shape)], -1) # [N_rays, N_samples] |
| 289 | |
| 290 | dists = dists * torch.norm(rays_d[...,None,:], dim=-1) |
| 291 | |
| 292 | rgb = torch.sigmoid(raw[...,:3]) # [N_rays, N_samples, 3] |
| 293 | noise = 0. |
| 294 | if raw_noise_std > 0.: |
| 295 | noise = torch.randn(raw[...,3].shape) * raw_noise_std |
| 296 | |
| 297 | # Overwrite randomly sampled data if pytest |
| 298 | if pytest: |
| 299 | np.random.seed(0) |
| 300 | noise = np.random.rand(*list(raw[...,3].shape)) * raw_noise_std |
| 301 | noise = torch.Tensor(noise) |
| 302 | |
| 303 | alpha = raw2alpha(raw[...,3] + noise, dists) # [N_rays, N_samples] |
| 304 | weights = alpha * torch.cumprod(torch.cat([torch.ones((alpha.shape[0], 1)), 1.-alpha + 1e-10], -1), -1)[:, :-1] |
| 305 | |
| 306 | rgb_map = torch.sum(weights[...,None] * rgb, -2) # [N_rays, 3] |
| 307 | uncert_map = torch.sum(weights * weights * raw[..., 4], -1) |
| 308 | |
| 309 | depth_map = torch.sum(weights * z_vals, -1) |
| 310 | disp_map = 1./torch.max(1e-10 * torch.ones_like(depth_map), depth_map / torch.sum(weights, -1)) |
| 311 | acc_map = torch.sum(weights, -1) |
| 312 | |
| 313 | if white_bkgd: |
| 314 | rgb_map = rgb_map + (1.-acc_map[...,None]) |
| 315 | |
| 316 | return rgb_map, disp_map, acc_map, weights, depth_map, uncert_map, F.relu(raw[...,3] + noise).mean(-1) |
| 317 | |
| 318 | |
| 319 | def render_rays(ray_batch, |