(self,
ts_w,
grid,
encode_w,
step=0,
flows=None)
| 175 | return deformed_grid |
| 176 | |
| 177 | def forward(self, |
| 178 | ts_w, |
| 179 | grid, |
| 180 | encode_w, |
| 181 | step=0, |
| 182 | flows=None): |
| 183 | # grid -> positional encoding |
| 184 | # ts_w -> embedding |
| 185 | grid = rearrange(grid, 'b n c -> (b n) c') |
| 186 | results_list = [] |
| 187 | flow_loss_list = [] |
| 188 | deform_list = [] |
| 189 | for i in range(self.num_models): |
| 190 | deformed_grid = self.deform_pts(ts_w, grid, encode_w, step, i) # [batch * num_pixels, 2] |
| 191 | deform_list.append(deformed_grid) |
| 192 | # Compute optical flow loss. |
| 193 | flow_loss = 0 |
| 194 | if self.hparams.flow_loss > 0 and not self.hparams.test: |
| 195 | if flows.max() > -1e2 and step > self.hparams.flow_step: |
| 196 | grid_new = grid + flows.squeeze(0) |
| 197 | deformed_grid_new = self.deform_pts( |
| 198 | ts_w + 1, grid_new, encode_w, step, i) |
| 199 | flow_loss = (deformed_grid_new, deformed_grid) |
| 200 | flow_loss_list.append(flow_loss) |
| 201 | if self.hparams.vid_hash: |
| 202 | pe_deformed_grid = (deformed_grid + 0.3) / 1.6 |
| 203 | else: |
| 204 | pe_deformed_grid = self.embeddings['xyz'](deformed_grid) |
| 205 | if not self.training and self.hparams.canonical_dir is not None: |
| 206 | w, h = self.img_wh |
| 207 | canonical_img = self.canonical_img.squeeze(0) |
| 208 | h_c, w_c = canonical_img.shape[1:3] |
| 209 | grid_new = deformed_grid.clone() |
| 210 | grid_new[..., 1] = (2 * deformed_grid[..., 0] - 1) * h / h_c |
| 211 | grid_new[..., 0] = (2 * deformed_grid[..., 1] - 1) * w / w_c |
| 212 | if len(canonical_img.shape) == 3: |
| 213 | canonical_img = canonical_img.unsqueeze(0) |
| 214 | results = torch.nn.functional.grid_sample( |
| 215 | canonical_img[i:i + 1].permute(0, 3, 1, 2), |
| 216 | grid_new.unsqueeze(1).unsqueeze(0), |
| 217 | mode='bilinear', |
| 218 | padding_mode='border') |
| 219 | results = results.squeeze().permute(1,0) |
| 220 | else: |
| 221 | results = self.models[f'implicit_video_{i}'](pe_deformed_grid) |
| 222 | |
| 223 | results_list.append(results) |
| 224 | |
| 225 | ret = edict(rgbs=results_list, |
| 226 | flow_loss=flow_loss_list, |
| 227 | deform=deform_list) |
| 228 | |
| 229 | return ret |
| 230 | |
| 231 | def setup(self, stage): |
| 232 | if not self.hparams.test: |
no test coverage detected