File size: 9,430 Bytes
7405e85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import torch
import torch.nn.functional as F
from einops import rearrange, repeat
from torch import nn

from ....geometry.projection import (
    homogenize_points,
    project,
    sample_image_grid,
    transform_cam2world,
    transform_world2cam,
    unproject,
)

class DepthGuidedMVRefiner(nn.Module):
    def __init__(
        self,
        feature_channels: int,
        hidden_channels: int | None = None,
        concat_ref_depth: bool = False,
    ) -> None:
        super().__init__()
        hidden_channels = feature_channels if hidden_channels is None else hidden_channels
        depth_channels = max(hidden_channels // 4, 16)
        self.concat_ref_depth = concat_ref_depth

        self.depth_encoder = nn.Sequential(
            nn.Conv2d(
                1,
                depth_channels,
                1,
                1,
                0,
            ),
            nn.GELU(),
            nn.Conv2d(
                depth_channels,
                depth_channels,
                1,
                1,
                0,
            ),
            nn.GELU(),
        )
        self.feature_encoder = nn.Sequential(
            nn.Conv2d(
                feature_channels * 2
                + depth_channels * (2 if self.concat_ref_depth else 1),
                hidden_channels,
                3,
                1,
                1,
                padding_mode="replicate",
            ),
            nn.GELU(),
            nn.Conv2d(
                hidden_channels,
                hidden_channels,
                3,
                1,
                1,
                padding_mode="replicate",
            ),
            nn.GELU(),
        )
        self.post_fuse = nn.Sequential(
            nn.Conv2d(
                feature_channels + hidden_channels + depth_channels * int(self.concat_ref_depth),
                hidden_channels,
                3,
                1,
                1,
                padding_mode="replicate",
            ),
            nn.GELU(),
            nn.Conv2d(
                hidden_channels,
                feature_channels,
                3,
                1,
                1,
                padding_mode="replicate",
            ),
        )
        nn.init.zeros_(self.post_fuse[-1].weight)
        nn.init.zeros_(self.post_fuse[-1].bias)

    @staticmethod
    def _normalized_coords_to_grid(
        xy: torch.Tensor,
        h: int,
        w: int,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        x_pixel = xy[..., 0] * w - 0.5
        y_pixel = xy[..., 1] * h - 0.5

        if w > 1:
            x_grid = 2 * x_pixel / (w - 1) - 1
        else:
            x_grid = torch.zeros_like(x_pixel)

        if h > 1:
            y_grid = 2 * y_pixel / (h - 1) - 1
        else:
            y_grid = torch.zeros_like(y_pixel)

        grid = torch.stack((x_grid, y_grid), dim=-1)
        in_bounds = (
            (x_pixel >= 0)
            & (x_pixel <= (w - 1))
            & (y_pixel >= 0)
            & (y_pixel <= (h - 1))
        )
        return grid, in_bounds

    def forward(
        self,
        features: torch.Tensor,
        depth: torch.Tensor,
        intrinsics: torch.Tensor,
        extrinsics: torch.Tensor,
        source_view_indices: torch.Tensor | None = None,
    ) -> torch.Tensor:
        b, v, c, h, w = features.shape
        if v <= 1:
            return features

        depth = depth.detach()
        intrinsics = intrinsics.detach()
        extrinsics = extrinsics.detach()
        feature_dtype = features.dtype

        xy_ref, _ = sample_image_grid((h, w), device=features.device)
        xy_ref = repeat(xy_ref, "h w xy -> b h w xy", b=b)

        refined_features = []
        geom_eps = torch.finfo(torch.float32).eps
        count_eps = torch.finfo(features.dtype).eps
        batch_indices = torch.arange(b, device=features.device)
        if source_view_indices is not None:
            if source_view_indices.shape[:2] != (b, v):
                raise ValueError(
                    "source_view_indices must have shape [B, V, M], got "
                    f"{tuple(source_view_indices.shape)} for features "
                    f"{tuple(features.shape)}"
                )
            source_view_indices = source_view_indices.to(
                device=features.device,
                dtype=torch.long,
            )

        for ref_idx in range(v):
            ref_features = features[:, ref_idx]
            ref_depth = depth[:, ref_idx].float()
            ref_intrinsics = intrinsics[:, ref_idx].float()
            ref_extrinsics = extrinsics[:, ref_idx].float()
            ref_depth_repr = None
            if self.concat_ref_depth:
                ref_depth_input = ref_depth.unsqueeze(1).clamp_min(geom_eps).reciprocal()
                ref_depth_repr = self.depth_encoder(ref_depth_input.to(feature_dtype))

            with torch.no_grad(), torch.autocast(
                device_type=features.device.type, enabled=False
            ):
                ref_points_cam = unproject(
                    xy_ref,
                    ref_depth,
                    ref_intrinsics[:, None, None],
                )
                ref_points_world = transform_cam2world(
                    homogenize_points(ref_points_cam),
                    ref_extrinsics[:, None, None],
                )[..., :-1]

            fused_sum = None
            valid_count = None

            if source_view_indices is None:
                selected_src_indices = [
                    src_idx for src_idx in range(v) if src_idx != ref_idx
                ]
            else:
                selected_src_indices = source_view_indices[:, ref_idx].unbind(dim=-1)

            for src_idx in selected_src_indices:
                if isinstance(src_idx, int):
                    src_features = features[:, src_idx]
                    src_extrinsics = extrinsics[:, src_idx].float()[:, None, None]
                    src_intrinsics = intrinsics[:, src_idx].float()[:, None, None]
                else:
                    src_features = features[batch_indices, src_idx]
                    src_extrinsics = (
                        extrinsics[batch_indices, src_idx].float()[:, None, None]
                    )
                    src_intrinsics = (
                        intrinsics[batch_indices, src_idx].float()[:, None, None]
                    )

                with torch.no_grad(), torch.autocast(
                    device_type=features.device.type, enabled=False
                ):
                    src_xy, src_in_front = project(
                        ref_points_world,
                        src_extrinsics,
                        src_intrinsics,
                    )
                    src_grid, src_in_bounds = self._normalized_coords_to_grid(
                        src_xy, h, w
                    )
                    valid_mask_fp32 = (src_in_front & src_in_bounds).unsqueeze(1).float()

                with torch.autocast(device_type=features.device.type, enabled=False):
                    warped_features = F.grid_sample(
                        src_features.float(),
                        src_grid,
                        mode="bilinear",
                        padding_mode="zeros",
                        align_corners=True,
                    )
                valid_mask = valid_mask_fp32.to(feature_dtype)
                warped_features = warped_features.to(feature_dtype)
                warped_features = warped_features * valid_mask

                with torch.no_grad(), torch.autocast(
                    device_type=features.device.type, enabled=False
                ):
                    src_points_cam = transform_world2cam(
                        homogenize_points(ref_points_world),
                        src_extrinsics,
                    )[..., :-1]
                    warped_depth = rearrange(
                        src_points_cam[..., -1], "b h w -> b 1 h w"
                    )
                    warped_depth_input = torch.where(
                        valid_mask_fp32 > 0,
                        warped_depth.clamp_min(geom_eps).reciprocal(),
                        torch.zeros_like(warped_depth),
                    )

                depth_repr = self.depth_encoder(warped_depth_input.to(feature_dtype))
                feature_inputs = [ref_features, warped_features]
                if ref_depth_repr is not None:
                    feature_inputs.append(ref_depth_repr)
                feature_inputs.append(depth_repr)
                value = self.feature_encoder(torch.cat(feature_inputs, dim=1)) * valid_mask

                if fused_sum is None:
                    fused_sum = value
                    valid_count = valid_mask
                else:
                    fused_sum = fused_sum + value
                    valid_count = valid_count + valid_mask

            if fused_sum is None or valid_count is None:
                refined_features.append(ref_features)
                continue

            fused = fused_sum / valid_count.clamp_min(count_eps)

            post_fuse_inputs = [ref_features, fused]
            if ref_depth_repr is not None:
                post_fuse_inputs.append(ref_depth_repr)
            residual = self.post_fuse(torch.cat(post_fuse_inputs, dim=1))
            refined_features.append(ref_features + residual)

        return torch.stack(refined_features, dim=1)