MCPcopy Create free account
hub / github.com/DSL-Lab/StreamSplat / forward

Method forward

model/splat_model.py:169–258  ·  view source on GitHub ↗
(self, data, step_ratio=0.0)

Source from the content-addressed store, hash-verified

167
168
169 def forward(self, data, step_ratio=0.0):
170 # data: [B, V, C, H, W]
171 input_frames = data['frames'] # [B, V, C, H, W], input features
172 input_depths = data['depths'] # [B, V, C, H, W], input features
173 timestamps = data['timestamps'] # [B, V], input timestamps
174 supv_masks = data['supv_masks'] # [B, V], input timestamps
175 if supv_masks is None:
176 supv_masks = torch.ones_like(input_depths, device=timestamps.device).bool()
177 timestamps = torch.as_tensor(timestamps, dtype=torch.float32, device=input_frames.device)
178 timestamps = timestamps / timestamps[..., -1].unsqueeze(-1)
179 anchor_time = torch.tensor([0.0, 1.0], device=input_frames.device)
180 supv_masks = (supv_masks > 0)
181
182 max_depth = input_depths.flatten(2).max(dim=2)[0][:, :, None, None, None]
183 min_depth = input_depths.flatten(2).min(dim=2)[0][:, :, None, None, None]
184 target_depth = input_depths # without normalization
185 input_depths = (input_depths - min_depth) / (max_depth - min_depth)
186
187 results = {}
188
189 # predict gaussians
190 decoder_out = self.forward_gaussians(input_frames, input_depths, timestamps) # dict
191 # pdb.set_trace()
192
193 with autocast('cuda', enabled=False):
194 render_pkg = self.gaussian_renderer(decoder_out["pred_gs"], self.background,
195 opt=self.opt, timestamps=timestamps,
196 anchor_time=anchor_time,
197 training=self.training,
198 )
199
200 loss, mse_loss, supv_mse_loss, depth_loss, loss_lpips, psnr, metrics = self.compute_losses(input_frames, target_depth, supv_masks, render_pkg)
201
202 if hasattr(self.opt, 'fix_opacity') and self.opt.fix_opacity:
203 render_pkg_fix = self.gaussian_renderer(decoder_out["pred_gs"], self.background,
204 opt=self.opt, timestamps=timestamps,
205 anchor_time=anchor_time,
206 override_opacity=True, training=self.training,
207 )
208 loss_fix, mse_loss_fix, supv_mse_loss_fix, depth_loss_fix, loss_lpips_fix, psnr_fix, metrics_fix = self.compute_losses(input_frames, target_depth, supv_masks, render_pkg_fix)
209 loss = (loss + loss_fix) * 0.5
210 else:
211 render_pkg_fix = None
212 mse_loss_fix = torch.zeros_like(mse_loss)
213 depth_loss_fix = torch.zeros_like(depth_loss)
214 loss_lpips_fix = torch.zeros_like(loss_lpips)
215 supv_mse_loss_fix = torch.zeros_like(supv_mse_loss)
216 psnr_fix = torch.zeros_like(psnr)
217
218 pred_depth = render_pkg["depth"]
219 depth_mask = pred_depth >= 0.2 # [B, V, 1, H, W]
220 pred_depth[~depth_mask] += 10 # set invalid depth to max depth
221 pred_depth = 1.0 / (pred_depth + 1e-8)
222 B, V, C, H, W = pred_depth.shape
223 # Reshape to treat each depth map independently for min/max calculation
224 reshaped_depth = pred_depth.view(B, V*C * H * W) # Shape [B*V, H*W]
225 min_vals = reshaped_depth.min(dim=1, keepdim=True)[0] # Shape [B*V, 1]
226 max_vals = reshaped_depth.max(dim=1, keepdim=True)[0] # Shape [B*V, 1]

Callers

nothing calls this directly

Calls 2

forward_gaussiansMethod · 0.95
compute_lossesMethod · 0.95

Tested by

no test coverage detected