fnr.py 12.5 KB
Newer Older
1
2
3
4
5
import torch
import torch.nn.functional as nn_f
from typing import Any, List, Mapping, Tuple
from torch import nn
from utils.view import *
Nianchen Deng's avatar
sync    
Nianchen Deng committed
6
from utils import math
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
from .post_process import *
from .foveation import Foveation


class FoveatedNeuralRenderer(object):

    def __init__(self, layers_fov: List[float],
                 layers_res: List[Tuple[int, int]],
                 layers_net: nn.ModuleList,
                 output_res: Tuple[int, int], *,
                 device: torch.device = None):
        super().__init__()
        self.layers_net = layers_net.to(device=device)
        self.layers_cam = [
            CameraParam({
                'fov': layers_fov[i],
                'cx': 0.5,
                'cy': 0.5,
                'normalized': True
            }, layers_res[i], device=device)
            for i in range(len(layers_fov))
        ]
        self.cam = CameraParam({
            'fov': layers_fov[-1],
            'cx': 0.5,
            'cy': 0.5,
            'normalized': True
        }, output_res, device=device)
        self.foveation = Foveation(layers_fov, layers_res, output_res, device=device)
        self.device = device

    def to(self, device: torch.device):
        self.layers_net.to(device)
        self.foveation.to(device)
        self.cam.to(device)
        for cam in self.layers_cam:
            cam.to(device)
        self.device = device
        return self

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        return self.render(*args, **kwds)

    def render(self, view: Trans, gaze, right_gaze=None, *,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
51
52
               stereo_disparity=0,
               using_mask=True,
Nianchen Deng's avatar
Nianchen Deng committed
53
               mono_periph_mode=0,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
54
               ret_raw=False) -> Union[Mapping[str, torch.Tensor], Tuple[Mapping[str, torch.Tensor]]]:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
55
        if stereo_disparity > math.tiny:
56
            left_view = Trans(
Nianchen Deng's avatar
sync    
Nianchen Deng committed
57
                view.trans_point(torch.tensor([-stereo_disparity / 2, 0, 0], device=self.device)),
58
59
                view.r)
            right_view = Trans(
Nianchen Deng's avatar
sync    
Nianchen Deng committed
60
                view.trans_point(torch.tensor([stereo_disparity / 2, 0, 0], device=self.device)),
61
62
63
                view.r)
            left_gaze = gaze
            right_gaze = gaze if right_gaze is None else right_gaze
Nianchen Deng's avatar
sync    
Nianchen Deng committed
64

Nianchen Deng's avatar
Nianchen Deng committed
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
            layers_mask = self.foveation.get_layers_mask() if using_mask else [None] * 3
            left_shifts = None
            right_shifts = None

            if using_mask and mono_periph_mode != 0:
                fovea_left = self._render(self.layers_net[0], self.layers_cam[0], left_view, left_gaze,
                                          layer_mask=layers_mask[0])['color']
                fovea_right = self._render(self.layers_net[0], self.layers_cam[0], right_view, right_gaze,
                                           layer_mask=layers_mask[0])['color']
                if mono_periph_mode == 3:
                    mid = self._render(self.layers_net[1], self.layers_cam[1], view,
                                       ((left_gaze[0] + right_gaze[0]) // 2, left_gaze[1]),
                                       layer_mask=layers_mask[1])['color']
                    periph = self._render(self.layers_net[2], self.layers_cam[2], view)['color']
                    raw_left = [fovea_left, mid, periph]
                    raw_right = [fovea_right, mid, periph]
                    shift = int(left_gaze[0] - right_gaze[0]) // 2
                    left_shifts = [0, 0, shift]
                    right_shifts = [0, 0, -shift]
                else:
                    mid_left = self._render_mid(self.layers_net[1], self.layers_cam[1], left_view, left_gaze,
                                                layer_mask=layers_mask[1], mono_view=view,
                                                blend_view=mono_periph_mode == 1)['color']
                    mid_right = self._render_mid(self.layers_net[1], self.layers_cam[1], right_view, right_gaze,
                                                 layer_mask=layers_mask[1], mono_view=view,
                                                 blend_view=mono_periph_mode == 1)['color']
                    periph = self._render(self.layers_net[2], self.layers_cam[2], view)['color']
                    raw_left = [fovea_left, mid_left, periph]
                    raw_right = [fovea_right, mid_right, periph]
            else:
                raw_left = [
                    self._render(self.layers_net[i], self.layers_cam[i], left_view,
                                 left_gaze if i < 2 else None,
                                 layer_mask=layers_mask[i])['color']
                    for i in range(3)
                ]
                raw_right = [
                    self._render(self.layers_net[i], self.layers_cam[i], right_view,
                                 right_gaze if i < 2 else None,
                                 layer_mask=layers_mask[i])['color']
                    for i in range(3)
                ]
            return self._gen_output(raw_left, left_gaze, left_shifts, ret_raw=ret_raw), \
                self._gen_output(raw_right, right_gaze, right_shifts, ret_raw=ret_raw)
109
        else:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
110
            layers_mask = self.foveation.get_layers_mask(gaze) if using_mask else None
111
            res_raw = [
Nianchen Deng's avatar
sync    
Nianchen Deng committed
112
113
                self._render(self.layers_net[i], self.layers_cam[i], view, gaze if i < 2 else None,
                             layer_mask=layers_mask[i] if layers_mask is not None else None)['color']
114
115
                for i in range(3)
            ]
Nianchen Deng's avatar
Nianchen Deng committed
116
            return self._gen_output(res_raw, gaze, ret_raw=ret_raw)
117

Nianchen Deng's avatar
sync    
Nianchen Deng committed
118
119
120
    def _render(self, net, cam: CameraParam, view: Trans, gaze=None, *,
                ret_depth=False,
                layer_mask=None) -> Mapping[str, torch.Tensor]:
121
122
        if gaze is not None:
            cam = self._adjust_cam(cam, gaze)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
123
124
125
126
127
        rays_o, rays_d = cam.get_global_rays(view, False)  # (1, H, W, 3)
        if layer_mask is not None:
            infer_mask = layer_mask >= 0
            rays_o = rays_o[:, infer_mask]
            rays_d = rays_d[:, infer_mask]
128
129
            net_output = net(rays_o.view(-1, 3), rays_d.view(-1, 3), ret_depth=ret_depth)
            ret = {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
130
                'color': torch.zeros(1, cam.res[0], cam.res[1], 3, device=self.device)
131
            }
Nianchen Deng's avatar
sync    
Nianchen Deng committed
132
            ret['color'][:, infer_mask] = net_output['color']
133
134
135
            ret['color'] = ret['color'].permute(0, 3, 1, 2)
            if ret_depth:
                ret['depth'] = torch.zeros(1, cam.res[0], cam.res[1])
Nianchen Deng's avatar
sync    
Nianchen Deng committed
136
                ret['depth'][:, infer_mask] = net_output['depth']
137
138
139
140
141
142
143
144
            return ret
        else:
            net_output = net(rays_o.view(-1, 3), rays_d.view(-1, 3), ret_depth=ret_depth)
            return {
                'color': net_output['color'].view(1, cam.res[0], cam.res[1], -1).permute(0, 3, 1, 2),
                'depth': net_output['depth'].view(1, cam.res[0], cam.res[1]) if ret_depth else None
            }

Nianchen Deng's avatar
Nianchen Deng committed
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
    def _render_mid(self, net, cam: CameraParam, view: Trans, gaze=None, *,
                    layer_mask: torch.Tensor,
                    mono_view: Trans,
                    blend_view: bool,
                    ret_depth=False) -> Mapping[str, torch.Tensor]:
        """
        [summary]

        :param net: [description]
        :param cam: [description]
        :param view: [description]
        :param layer_mask: [description]
        :param mono_view: [description]
        :param gaze: [description], defaults to None
        :param ret_depth: [description], defaults to False
        :return: [description]
        """
        if gaze is not None:
            cam = self._adjust_cam(cam, gaze)

        k = layer_mask[None, ..., None].clamp(1 if blend_view else 2, 2) - 1  # (1, H, W, 1)
        rays_o = (1 - k) * view.t + k * mono_view.t  # (1, H, W, 3)
        rays_d = view.trans_vector(cam.get_local_rays())  # (1, H, W, 3)

        if layer_mask is not None:
            infer_mask = layer_mask >= 0
            rays_o = rays_o[:, infer_mask]
            rays_d = rays_d[:, infer_mask]
            net_output = net(rays_o.view(-1, 3), rays_d.view(-1, 3), ret_depth=ret_depth)
            ret = {
                'color': torch.zeros(1, cam.res[0], cam.res[1], 3, device=self.device)
            }
            ret['color'][:, infer_mask] = net_output['color']
            ret['color'] = ret['color'].permute(0, 3, 1, 2)
            if ret_depth:
                ret['depth'] = torch.zeros(1, cam.res[0], cam.res[1])
                ret['depth'][:, infer_mask] = net_output['depth']
            return ret
        else:
            net_output = net(rays_o.view(-1, 3), rays_d.view(-1, 3), ret_depth=ret_depth)
            return {
                'color': net_output['color'].view(1, cam.res[0], cam.res[1], -1).permute(0, 3, 1, 2),
                'depth': net_output['depth'].view(1, cam.res[0], cam.res[1]) if ret_depth else None
            }

    def _gen_output(self, layers_img: List[torch.Tensor], gaze: Tuple[float, float], shifts=None, ret_raw=False) -> Mapping[str, torch.Tensor]:
191
        refined = self._post_process(layers_img)
Nianchen Deng's avatar
Nianchen Deng committed
192
        blended = self.foveation.synthesis(refined, gaze, shifts)
193
194
195
196
197
        ret = {
            'layers_img': refined,
            'blended': blended
        }
        if ret_raw:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
198
            ret['layers_raw'] = layers_img
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
268
269
270
271
272
            ret['blended_raw'] = self.foveation.synthesis(layers_img, gaze)
        return ret

    def _post_process(self, layers_img: List[torch.Tensor]) -> List[torch.Tensor]:
        return [
            #grad_aware_median(constrast_enhance(layers_img[0], 3, 0.2), 3, 3, True),
            constrast_enhance(layers_img[0], 3, 0.2),
            constrast_enhance(layers_img[1], 5, 0.2),
            constrast_enhance(layers_img[2], 5, 0.2)
        ]

    def _adjust_cam(self, layer_cam: CameraParam, gaze: Tuple[float, float]) -> CameraParam:
        fovea_offset = (
            (gaze[0]) / self.cam.f[0].item() * layer_cam.f[0].item(),
            (gaze[1]) / self.cam.f[1].item() * layer_cam.f[1].item()
        )
        return CameraParam({
            'fx': layer_cam.f[0].item(),
            'fy': layer_cam.f[1].item(),
            'cx': layer_cam.c[0].item() - fovea_offset[0],
            'cy': layer_cam.c[1].item() - fovea_offset[1]
        }, layer_cam.res, device=self.device)

    def _warp(self, trans: Trans, trans0: Trans,
              cam: CameraParam, z_list: torch.Tensor,
              image: torch.Tensor, depthmap: torch.Tensor) -> torch.Tensor:
        """
        [summary]

        :param trans: [description]
        :param trans0: [description]
        :param cam: [description]
        :param z_list: [description]
        :param image `Tensor(B, C, H, W)`:
        :param depthmap `Tensor(B, H, W)`:
        :return `Tensor(B, C, H, W)`:
        """
        B = image.size(0)
        rays_d = cam.get_global_rays(trans, norm=False)[1]  # (1, H, W, 3)
        rays_d_0 = trans0.trans_vector(rays_d, True)[0]  # (1, H, W, 3)
        t_0 = trans0.trans_point(trans.t, True)[0]  # (1, 3)
        q1_0 = torch.empty(B, cam.res[0], cam.res[1],
                           3, device=cam.device)  # near
        q2_0 = torch.empty(B, cam.res[0], cam.res[1],
                           3, device=cam.device)  # far
        determined = torch.zeros(B, cam.res[0], cam.res[1], 1,
                                 dtype=torch.bool, device=cam.device)
        for z in z_list:
            p_0 = rays_d_0 * z + t_0  # (1, H, W, 3)
            d_of_p_0 = torch.norm(p_0 - trans0.t, dim=-1,
                                  keepdim=True)  # (1, H, W, 1)
            v_of_p_0 = p_0 / d_of_p_0  # (1, H, W, 3)
            coords = cam.proj(p_0, True) * 2 - 1  # (1, H, W, 2)
            d = nn_f.grid_sample(
                depthmap[:, None, :, :],
                coords.expand(B, -1, -1, -1)).permute(0, 2, 3, 1)  # (B, H, W, 1)
            q = v_of_p_0 * d  # (B, H, W, 3)
            near_selector = d < d_of_p_0
            # Fill q2(far) when undetermined and d > d_of_p_0
            q2_selector = (~determined & ~near_selector).expand(-1, -1, -1, 3)
            q2_0[q2_selector] = q[q2_selector]
            # Fill q1(near) when undetermined and d <= d_of_p_0
            q1_selector = (~determined & near_selector).expand(-1, -1, -1, 3)
            q1_0[q1_selector] = q[q1_selector]
            # Mark as determined for d0 <= d
            determined[near_selector] = True

        # Compute intersection x of q1-q2 and rays (in trans0 space)
        k = torch.cross(q1_0 - t_0, rays_d_0, dim=-1).norm(dim=-1, keepdim=True) / \
            torch.cross(rays_d_0, q2_0 - t_0, dim=-1).norm(dim=-
                                                           1, keepdim=True)  # (B, H, W, 1)
        x_0 = (q2_0 - q1_0) * k / (k + 1) + q1_0
        coords = cam.proj(x_0, True) * 2 - 1  # (B, H, W, 2)
        return nn_f.grid_sample(image, coords)