gen_final.py 9.46 KB
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
import torch
Nianchen Deng's avatar
Nianchen Deng committed
2
import math
Nianchen Deng's avatar
sync    
Nianchen Deng committed
3
from torch import nn
Nianchen Deng's avatar
Nianchen Deng committed
4
5
import torch.nn.functional as nn_f
from typing import Any, List, Mapping, Tuple
Nianchen Deng's avatar
sync    
Nianchen Deng committed
6
7
from . import view
from . import refine
Nianchen Deng's avatar
Nianchen Deng committed
8
from . import util
Nianchen Deng's avatar
sync    
Nianchen Deng committed
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
from .foveation import Foveation
from .simple_perf import SimplePerf


class GenFinal(object):

    def __init__(self, layers_fov: List[float],
                 layers_res: List[Tuple[int, int]],
                 full_res: Tuple[int, int],
                 fovea_net: nn.Module,
                 periph_net: nn.Module,
                 device: torch.device = None) -> None:
        super().__init__()
        self.layer_cams = [
            view.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.full_cam = view.CameraParam({
            'fov': layers_fov[-1],
            'cx': 0.5,
            'cy': 0.5,
            'normalized': True
        }, full_res, device=device)
        self.fovea_net = fovea_net.to(device)
        self.periph_net = periph_net.to(device)
Nianchen Deng's avatar
Nianchen Deng committed
39
        self.foveation = Foveation(layers_fov, full_res, device=device)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
40
41
42
43
44
45
46
47
48
49
50
51
        self.device = device

    def to(self, device: torch.device):
        self.fovea_net.to(device)
        self.periph_net.to(device)
        self.foveation.to(device)
        self.full_cam.to(device)
        for cam in self.layer_cams:
            cam.to(device)
        self.device = device
        return self

Nianchen Deng's avatar
Nianchen Deng committed
52
53
54
55
56
57
58
59
60
    def __call__(self, *args: Any, **kwds: Any) -> Any:
        return self.gen(*args, **kwds)

    def gen(self, gaze, trans: view.Trans, *,
            mono_trans: view.Trans = None,
            shift: int = 0,
            warp_by_depth: bool = False,
            ret_raw=False,
            perf_time=False) -> Mapping[str, torch.Tensor]:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
61
62
63
        fovea_cam = self._adjust_cam(self.layer_cams[0], self.full_cam, gaze)
        mid_cam = self._adjust_cam(self.layer_cams[1], self.full_cam, gaze)
        periph_cam = self.layer_cams[2]
Nianchen Deng's avatar
Nianchen Deng committed
64
        trans_periph = mono_trans if mono_trans != None else trans
Nianchen Deng's avatar
sync    
Nianchen Deng committed
65
66
67

        perf = SimplePerf(True, True) if perf_time else None

Nianchen Deng's avatar
Nianchen Deng committed
68
        # *_rays_o, *_rays_d: (1, N, 3)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
69
        fovea_rays_o, fovea_rays_d = fovea_cam.get_global_rays(trans, True)
Nianchen Deng's avatar
Nianchen Deng committed
70
71
72
        mid_rays_o, mid_rays_d = mid_cam.get_global_rays(trans_periph, True)
        periph_rays_o, periph_rays_d = periph_cam.get_global_rays(
            trans_periph, True)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
73
74
75
76
77
78
79
        mid_periph_rays_o = torch.cat([mid_rays_o, periph_rays_o], 1)
        mid_periph_rays_d = torch.cat([mid_rays_d, periph_rays_d], 1)
        if perf_time:
            perf.Checkpoint('Get rays')

        perf1 = SimplePerf(True, True) if perf_time else None

Nianchen Deng's avatar
Nianchen Deng committed
80
81
82
        fovea_inferred, fovea_depth = self._infer(
            self.fovea_net, fovea_rays_o, fovea_rays_d, [fovea_cam.res], True)

Nianchen Deng's avatar
sync    
Nianchen Deng committed
83
84
        if perf_time:
            perf1.Checkpoint('Infer fovea')
Nianchen Deng's avatar
Nianchen Deng committed
85
86
87
        mid_inferred, mid_depth, periph_inferred, periph_depth = self._infer(
            self.periph_net, mid_periph_rays_o, mid_periph_rays_d,
            [mid_cam.res, periph_cam.res], True)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
88
89
90
91
92

        if perf_time:
            perf1.Checkpoint('Infer mid & periph')
            perf.Checkpoint('Infer')

Nianchen Deng's avatar
Nianchen Deng committed
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
        if mono_trans != None and shift == 0:  # do warp
            fovea_depth[torch.isnan(fovea_depth)] = 50
            mid_depth[torch.isnan(mid_depth)] = 50
            periph_depth[torch.isnan(periph_depth)] = 50

            if warp_by_depth:
                z_list = util.depth_sample((1, 50), 4, True)
                mid_inferred = self._warp(trans, mono_trans, mid_cam,
                                          z_list, mid_inferred, mid_depth)
                periph_inferred = self._warp(trans, mono_trans, periph_cam,
                                             z_list, periph_inferred, periph_depth)
                if perf_time:
                    perf.Checkpoint('Mono warp')
            else:   
                p = torch.tensor([[0, 0, torch.mean(fovea_depth)]],
                                device=self.device)
                p_ = trans.trans_point(mono_trans.trans_point(p), inverse=True)
                shift = self.full_cam.proj(
                    p_, center_as_origin=True)[..., 0].item()
                shift = round(shift)
                if perf_time:
                    perf.Checkpoint('Mono shift')

        fovea_refined = refine.grad_aware_median(fovea_inferred, 3, 3, True)
        fovea_refined = refine.constrast_enhance(fovea_refined, 3, 0.2)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
118
119
120
121
122
123
124
125
126
127
        mid_refined = refine.constrast_enhance(mid_inferred, 5, 0.2)
        periph_refined = refine.constrast_enhance(periph_inferred, 5, 0.2)

        if perf_time:
            perf.Checkpoint('Refine')

        blended = self.foveation.synthesis([
            fovea_refined,
            mid_refined,
            periph_refined
Nianchen Deng's avatar
Nianchen Deng committed
128
        ], (gaze[0], gaze[1]), [0, shift, shift] if shift != 0 else None)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
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

        if perf_time:
            perf.Checkpoint('Blend')

        if ret_raw:
            return {
                'fovea': fovea_refined,
                'mid': mid_refined,
                'periph': periph_refined,
                'blended': blended,
                'fovea_raw': fovea_inferred,
                'mid_raw': mid_inferred,
                'periph_raw': periph_inferred,
                'blended_raw': self.foveation.synthesis([
                    fovea_inferred,
                    mid_inferred,
                    periph_inferred
                ], (gaze[0], gaze[1]))
            }
        return {
            'fovea': fovea_refined,
            'mid': mid_refined,
            'periph': periph_refined,
            'blended': blended
        }

Nianchen Deng's avatar
Nianchen Deng committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    def _infer(self, net, rays_o: torch.Tensor, rays_d: torch.Tensor,
               res_list: List[Tuple[int, int]], ret_depth=False):
        if ret_depth:
            colors, depths = net(rays_o.view(-1, 3), rays_d.view(-1, 3),
                                 ret_depth=True)
        else:
            colors = net(rays_o.view(-1, 3), rays_d.view(-1, 3))
            depths = None
        images = []
        offset = 0
        for res in res_list:
            bound = offset + res[0] * res[1]
            images.append(colors[offset:bound].view(
                1, res[0], res[1], -1).permute(0, 3, 1, 2))
            if ret_depth:
                images.append(depths[offset:bound].view(
                    1, res[0], res[1]))
            offset = bound
        return tuple(images)

Nianchen Deng's avatar
sync    
Nianchen Deng committed
175
176
177
178
179
180
181
182
183
184
185
186
    def _adjust_cam(self, cam: view.CameraParam, full_cam: view.CameraParam,
                    gaze: Tuple[float, float]) -> view.CameraParam:
        fovea_offset = (
            (gaze[0]) / full_cam.f[0].item() * cam.f[0].item(),
            (gaze[1]) / full_cam.f[1].item() * cam.f[1].item()
        )
        return view.CameraParam({
            'fx': cam.f[0].item(),
            'fy': cam.f[1].item(),
            'cx': cam.c[0].item() - fovea_offset[0],
            'cy': cam.c[1].item() - fovea_offset[1]
        }, cam.res, device=self.device)
Nianchen Deng's avatar
Nianchen Deng committed
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

    def _warp(self, trans: view.Trans, trans0: view.Trans,
              cam: view.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)