File size: 11,376 Bytes
81ecb2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.transforms import Compose, Resize, CenterCrop, Normalize, InterpolationMode

import open_clip
from dva.io import load_from_config

def sample_orbit_traj(radius, height, start_theta, end_theta, num_points, world_up=torch.Tensor([0, 1, 0])):
    # return [num_points, 3, 4]
    angles = torch.rand((num_points, )) * (end_theta - start_theta) + start_theta
    return get_pose_on_orbit(radius=radius, height=height, angles=angles, world_up=world_up)

def get_pose_on_orbit(radius, height, angles, world_up=torch.Tensor([0, 1, 0])):
    num_points = angles.shape[0]
    x = radius * torch.cos(angles)
    h = torch.ones((num_points,)) * height
    z = radius * torch.sin(angles)
    position = torch.stack([x, h, z], dim=-1)
    forward = position / torch.norm(position, p=2, dim=-1, keepdim=True)
    right = -torch.cross(world_up[None, ...], forward)
    right /= torch.norm(right, dim=-1, keepdim=True)
    up = torch.cross(forward, right)
    up /= torch.norm(up, p=2, dim=-1, keepdim=True)
    rotation = torch.stack([right, up, forward], dim=1)
    translation = torch.Tensor([0, 0, radius])[None, :, None].repeat(num_points, 1, 1)
    return torch.concat([rotation, translation], dim=2)

class DummyImageConditioner(nn.Module):
    def __init__(
        self,
        num_prims,
        dim_feat,
        prim_shape,
        encoder_config,
        sample_view=False,
        sample_start=torch.pi*0.25,
        sample_end=torch.pi*0.75,
    ):
        super().__init__()

        self.num_prims = num_prims
        self.dim_feat = dim_feat
        self.prim_shape = prim_shape
        self.sample_view = sample_view
        self.sample_start = sample_start
        self.sample_end = sample_end
        self.encoder = None

    @torch.no_grad()
    def forward(self, batch, rm, amp, precision_dtype=torch.float32):
        return batch['cond']

class ImageConditioner(nn.Module):
    def __init__(
        self,
        num_prims,
        dim_feat,
        prim_shape,
        encoder_config,
        sample_view=False,
        sample_start=torch.pi*0.25,
        sample_end=torch.pi*0.75,
    ):
        super().__init__()

        self.num_prims = num_prims
        self.dim_feat = dim_feat
        self.prim_shape = prim_shape
        self.sample_view = sample_view
        self.sample_start = sample_start
        self.sample_end = sample_end
        self.encoder = load_from_config(encoder_config)
    
    def sdf2alpha(self, sdf):
        return torch.exp(-(sdf / 0.005) ** 2)

    @torch.no_grad()
    def forward(self, batch, rm, amp, precision_dtype=torch.float32):
        # TODO: replace with real rendering process in primsdf
        assert 'input_param' in batch, "No parameters in current batch for rendering image conditions"
        prim_volume = batch['input_param']
        bs = prim_volume.shape[0]
        preds = {}
        geo_start_index = 4
        geo_end_index = geo_start_index + self.prim_shape ** 3 # non-inclusive
        tex_start_index = geo_end_index
        tex_end_index = tex_start_index + self.prim_shape ** 3 * 3 # non-inclusive
        feat_geo = prim_volume[:, :, geo_start_index: geo_end_index]
        feat_tex = prim_volume[:, :, tex_start_index: tex_end_index]
        prim_alpha = self.sdf2alpha(feat_geo).reshape(bs, self.num_prims, 1, self.prim_shape, self.prim_shape, self.prim_shape) * 255
        prim_rgb = feat_tex.reshape(bs, self.num_prims, 3, self.prim_shape, self.prim_shape, self.prim_shape) * 255
        preds['prim_rgba'] = torch.concat([prim_rgb, prim_alpha], dim=2)
        pos = prim_volume[:, :, 1:4]
        scale = prim_volume[:, :, 0:1]
        preds['prim_pos'] = pos.reshape(bs, self.num_prims, 3) * rm.volradius
        preds['prim_rot'] = torch.eye(3).to(preds['prim_pos'])[None, None, ...].repeat(bs, self.num_prims, 1, 1)
        preds['prim_scale'] = (1 / scale.reshape(bs, self.num_prims, 1).repeat(1, 1, 3))
        if not self.sample_view:
            preds['Rt'] = torch.Tensor([
                [
                    1.0,
                    0.0,
                    0.0,
                    0.0 * rm.volradius
                ],
                [
                    0.0,
                    -1.0,
                    0.0,
                    0.0 * rm.volradius
                ],
                [
                    0.0,
                    0.0,
                    -1.0,
                    5 * rm.volradius
                ]
                ]).to(prim_volume)[None, ...].repeat(bs, 1, 1)
        else:
            preds['Rt'] = sample_orbit_traj(radius=5*rm.volradius, height=0, start_theta=self.sample_start, end_theta=self.sample_end, num_points=bs).to(prim_volume)
        preds['K'] = torch.Tensor([
            [
                2084.9526697685183,
                0.0,
                512.0
            ],
            [
                0.0,
                2084.9526697685183,
                512.0
            ],
            [
                0.0,
                0.0,
                1.0
            ]]).to(prim_volume)[None, ...].repeat(bs, 1, 1)
        ratio_h = rm.image_height / 1024.
        ratio_w = rm.image_width / 1024.
        preds['K'][:, 0:1, :] *= ratio_h
        preds['K'][:, 1:2, :] *= ratio_w
        rm_preds = rm(
            prim_rgba=preds["prim_rgba"],
            prim_pos=preds["prim_pos"],
            prim_scale=preds["prim_scale"],
            prim_rot=preds["prim_rot"],
            RT=preds["Rt"],
            K=preds["K"],
        )
        rendered_image = rm_preds['rgba_image'].permute(0, 2, 3, 1)[..., :3].contiguous()
        with torch.autocast(device_type='cuda', dtype=precision_dtype, enabled=amp):
            results = self.encoder(rendered_image)
        return results

class ImageMultiViewConditioner(nn.Module):
    def __init__(
        self,
        num_prims,
        dim_feat,
        prim_shape,
        encoder_config,
        sample_view=False,
        view_counts=4,
    ):
        super().__init__()

        self.num_prims = num_prims
        self.dim_feat = dim_feat
        self.prim_shape = prim_shape
        self.view_counts = view_counts
        view_angles = torch.linspace(0.5, 2.5, self.view_counts + 1) * torch.pi
        self.view_angles = view_angles[:-1]
        self.encoder = load_from_config(encoder_config)
    
    def sdf2alpha(self, sdf):
        return torch.exp(-(sdf / 0.005) ** 2)

    @torch.no_grad()
    def forward(self, batch, rm, amp, precision_dtype=torch.float32):
        # TODO: replace with real rendering process in primsdf
        assert 'input_param' in batch, "No parameters in current batch for rendering image conditions"
        prim_volume = batch['input_param']
        bs = prim_volume.shape[0]
        preds = {}
        geo_start_index = 4
        geo_end_index = geo_start_index + self.prim_shape ** 3 # non-inclusive
        tex_start_index = geo_end_index
        tex_end_index = tex_start_index + self.prim_shape ** 3 * 3 # non-inclusive
        feat_geo = prim_volume[:, :, geo_start_index: geo_end_index]
        feat_tex = prim_volume[:, :, tex_start_index: tex_end_index]
        prim_alpha = self.sdf2alpha(feat_geo).reshape(bs, self.num_prims, 1, self.prim_shape, self.prim_shape, self.prim_shape) * 255
        prim_rgb = feat_tex.reshape(bs, self.num_prims, 3, self.prim_shape, self.prim_shape, self.prim_shape) * 255
        preds['prim_rgba'] = torch.concat([prim_rgb, prim_alpha], dim=2)
        pos = prim_volume[:, :, 1:4]
        scale = prim_volume[:, :, 0:1]
        preds['prim_pos'] = pos.reshape(bs, self.num_prims, 3) * rm.volradius
        preds['prim_rot'] = torch.eye(3).to(preds['prim_pos'])[None, None, ...].repeat(bs, self.num_prims, 1, 1)
        preds['prim_scale'] = (1 / scale.reshape(bs, self.num_prims, 1).repeat(1, 1, 3))
        preds['K'] = torch.Tensor([
            [
                2084.9526697685183,
                0.0,
                512.0
            ],
            [
                0.0,
                2084.9526697685183,
                512.0
            ],
            [
                0.0,
                0.0,
                1.0
            ]]).to(prim_volume)[None, ...].repeat(bs, 1, 1)
        ratio_h = rm.image_height / 1024.
        ratio_w = rm.image_width / 1024.
        preds['K'][:, 0:1, :] *= ratio_h
        preds['K'][:, 1:2, :] *= ratio_w
        # we sample view according to view_counts
        cond_list = []
        for view_ang in self.view_angles:
            bs_view_ang = view_ang.repeat(bs,)
            preds['Rt'] = get_pose_on_orbit(radius=5*rm.volradius, height=0, angles=bs_view_ang).to(prim_volume)
            rm_preds = rm(
                prim_rgba=preds["prim_rgba"],
                prim_pos=preds["prim_pos"],
                prim_scale=preds["prim_scale"],
                prim_rot=preds["prim_rot"],
                RT=preds["Rt"],
                K=preds["K"],
            )
            rendered_image = rm_preds['rgba_image'].permute(0, 2, 3, 1)[..., :3].contiguous()
            with torch.autocast(device_type='cuda', dtype=precision_dtype, enabled=amp):
                results = self.encoder(rendered_image)
            cond_list.append(results)
        final_cond = torch.concat(cond_list, dim=1)
        return final_cond

class CLIPImageEncoder(nn.Module):
    def __init__(
        self,
        pretrained_path: str,
        model_spec: str = 'ViT-L-14',
    ):
        super().__init__()

        self.model, _, _ = open_clip.create_model_and_transforms(model_spec, pretrained=pretrained_path)
        self.model_resolution = self.model.visual.image_size
        self.preprocess = Compose([
            Resize(self.model_resolution, interpolation=InterpolationMode.BICUBIC),
            CenterCrop(self.model_resolution),
            Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
        ])
        self.model.eval()
        # self.tokenizer = open_clip.get_tokenizer(model_spec)

    @torch.no_grad()
    def forward(self, img):
        assert img.shape[-1] == 3
        img = img.permute(0, 3, 1, 2) / 255.
        image = self.preprocess(img)
        image_features = self.model.encode_image(image)
        image_features /= image_features.norm(dim=-1, keepdim=True)
        return image_features

class CLIPImageTokenEncoder(nn.Module):
    def __init__(
        self,
        pretrained_path: str,
        model_spec: str = 'ViT-L-14',
    ):
        super().__init__()

        self.model, _, _ = open_clip.create_model_and_transforms(model_spec, pretrained=pretrained_path)
        self.model.visual.output_tokens = True
        self.model_resolution = self.model.visual.image_size
        self.preprocess = Compose([
            Resize(self.model_resolution, interpolation=InterpolationMode.BICUBIC),
            CenterCrop(self.model_resolution),
            Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
        ])
        self.model.eval()

    @torch.no_grad()
    def forward(self, img):
        assert img.shape[-1] == 3
        img = img.permute(0, 3, 1, 2) / 255.
        image = self.preprocess(img)
        _, image_tokens = self.model.encode_image(image)
        # [B, T, D] - [B, 256, 1024]
        image_tokens /= image_tokens.norm(dim=-1, keepdim=True)
        return image_tokens