from .gating import cap_scales, gate_predictions, mask_for_images from .mask_attention import mask_context from .visibility import ext_to_c2w, visibility def cameras(predictions, images): from dggt.utils.pose_enc import pose_encoding_to_extri_intri ext, K = pose_encoding_to_extri_intri(predictions['pose_enc'], images.shape[-2:]) return ext, K def observed_by_inputs(depth, K, c2w, mask, inputs, *, z_tol): V = None for i in range(0, inputs, 8): v = visibility(depth, K, c2w, mask, list(range(i, min(i + 8, inputs))), z_tol=z_tol)['V'] V = v if V is None else V | v return V def online_visibility(predictions, images, mask, *, z_tol=.05): mask = mask_for_images(mask, images) ext, K = cameras(predictions, images) return {'V': observed_by_inputs(predictions['depth'][..., 0], K, ext_to_c2w(ext), mask, mask.shape[1], z_tol=z_tol)} def render_visibility(input_depth, input_K, input_w2c, render_depth, render_K, render_w2c, mask, *, z_tol): import torch n = input_depth.shape[1] depth = torch.cat([input_depth, render_depth], 1) K = torch.cat([input_K, render_K], 1) c2w = ext_to_c2w(torch.cat([input_w2c, render_w2c], 1)) mask = mask[:, :1].expand(depth.shape).contiguous() return observed_by_inputs(depth, K, c2w, mask, n, z_tol=z_tol)[:, n:] def predict(model, images, occluder_mask, *, z_tol=None, attention_mask=None): options = model._options with mask_context(model, occluder_mask if attention_mask is None else attention_mask, images): predictions = model(images) visible = online_visibility(predictions, images, occluder_mask, z_tol=options['v_z_tol'] if z_tol is None else z_tol)['V'] if options['scale_cap'] > 0: predictions = cap_scales(predictions, options['scale_cap']) if options['observed_only']: predictions = gate_predictions(predictions, ~visible) return predictions, visible