Perform depth regression from unimodal depth distribution, namely compute the expectation and standard deviation of the unimodal distribution, the difference of this function and its ancestor in `enerf_utils.py: depth_regression()` is the additional `Softplus()` activation b
(density: torch.Tensor, depth_hypo: torch.Tensor, is_debug: bool = False,
save_dir: str = None, level: int = 0)
| 131 | |
| 132 | |
| 133 | def unimodal_depth_regression(density: torch.Tensor, depth_hypo: torch.Tensor, is_debug: bool = False, |
| 134 | save_dir: str = None, level: int = 0): |
| 135 | """ Perform depth regression from unimodal depth distribution, namely compute the expectation |
| 136 | and standard deviation of the unimodal distribution, the difference of this function and its |
| 137 | ancestor in `enerf_utils.py: depth_regression()` is the additional `Softplus()` activation |
| 138 | before `softmax()` operation. |
| 139 | Args: |
| 140 | density: (torch.Tensor), (B, D, H, W), raw density of each pixel and each depth |
| 141 | depth_hypo: (torch.Tensor), (B, D, H, W), depth planes or depth of samples |
| 142 | Returns: |
| 143 | depth: (torch.Tensor), (B, H, W), expectation of the unimodal distribution, namely the surface depth |
| 144 | std: (torch.Tensor), (B, H, W), standard deviation of the unimodal distribution, possible range |
| 145 | """ |
| 146 | # Use `softplus()` activation first, TODO: determine whether it is useful |
| 147 | depth_prob = F.softplus(density) # (B, D, H, W) |
| 148 | |
| 149 | # # Do not use `softplus()` as additional activation |
| 150 | # depth_prob = density |
| 151 | |
| 152 | prob_volume = depth_prob.softmax(-3) # (B, D, H, W) |
| 153 | depth = (prob_volume * depth_hypo).sum(-3) # (B, H, W) |
| 154 | std = (prob_volume * (depth_hypo - depth.unsqueeze(-3))**2).sum(-3).clip(1e-10).sqrt() # (B, H, W) |
| 155 | |
| 156 | # TODO: debug, save some useful middle output for visualization |
| 157 | if is_debug: |
| 158 | np.save(f'{save_dir}/depth_hypo_{level}.npy', depth_hypo[0].permute(1, 2, 0).detach().cpu().numpy()) |
| 159 | np.save(f'{save_dir}/raw_output_{level}.npy', density[0].permute(1, 2, 0).detach().cpu().numpy()) |
| 160 | np.save(f'{save_dir}/depth_prob_{level}.npy', prob_volume[0].permute(1, 2, 0).detach().cpu().numpy()) |
| 161 | |
| 162 | return depth, std |
| 163 | |
| 164 | |
| 165 | def bimodal_depth_regression(density: torch.Tensor, z_vals: torch.Tensor, dist_default: float = 1.0, |