pulsar_cam_unified.py 7.55 KB
Newer Older
Christoph Lassner's avatar
Christoph Lassner committed
1
#!/usr/bin/env python3
2
# Copyright (c) Meta Platforms, Inc. and affiliates.
Patrick Labatut's avatar
Patrick Labatut committed
3
4
5
6
7
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

Christoph Lassner's avatar
Christoph Lassner committed
8
9
10
11
12
13
14
15
16
"""
This example demonstrates camera parameter optimization with the pulsar
PyTorch3D interface. For this, a reference image has been pre-generated
(you can find it at `../../tests/pulsar/reference/examples_TestRenderer_test_cam.png`).
The same scene parameterization is loaded and the camera parameters
distorted. Gradient-based optimization is used to converge towards the
original camera parameters.
Output: cam-pt3d.gif
"""
Christoph Lassner's avatar
Christoph Lassner committed
17
import logging
18
from os import path
Christoph Lassner's avatar
Christoph Lassner committed
19
20
21
22
23

import cv2
import imageio
import numpy as np
import torch
24
from pytorch3d.renderer.cameras import PerspectiveCameras
Christoph Lassner's avatar
Christoph Lassner committed
25
26
27
28
29
30
31
32
33
34
from pytorch3d.renderer.points import (
    PointsRasterizationSettings,
    PointsRasterizer,
    PulsarPointsRenderer,
)
from pytorch3d.structures.pointclouds import Pointclouds
from pytorch3d.transforms import axis_angle_to_matrix
from torch import nn, optim


Christoph Lassner's avatar
Christoph Lassner committed
35
36
37
38
39
LOGGER = logging.getLogger(__name__)
N_POINTS = 20
WIDTH = 1_000
HEIGHT = 1_000
DEVICE = torch.device("cuda")
Christoph Lassner's avatar
Christoph Lassner committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59


class SceneModel(nn.Module):
    """
    A simple scene model to demonstrate use of pulsar in PyTorch modules.

    The scene model is parameterized with sphere locations (vert_pos),
    channel content (vert_col), radiuses (vert_rad), camera position (cam_pos),
    camera rotation (cam_rot) and sensor focal length and width (cam_sensor).

    The forward method of the model renders this scene description. Any
    of these parameters could instead be passed as inputs to the forward
    method and come from a different model.
    """

    def __init__(self):
        super(SceneModel, self).__init__()
        self.gamma = 0.1
        # Points.
        torch.manual_seed(1)
Christoph Lassner's avatar
Christoph Lassner committed
60
        vert_pos = torch.rand(N_POINTS, 3, dtype=torch.float32) * 10.0
Christoph Lassner's avatar
Christoph Lassner committed
61
62
63
64
65
66
        vert_pos[:, 2] += 25.0
        vert_pos[:, :2] -= 5.0
        self.register_parameter("vert_pos", nn.Parameter(vert_pos, requires_grad=False))
        self.register_parameter(
            "vert_col",
            nn.Parameter(
Christoph Lassner's avatar
Christoph Lassner committed
67
                torch.rand(N_POINTS, 3, dtype=torch.float32),
Christoph Lassner's avatar
Christoph Lassner committed
68
69
70
71
72
73
                requires_grad=False,
            ),
        )
        self.register_parameter(
            "vert_rad",
            nn.Parameter(
Christoph Lassner's avatar
Christoph Lassner committed
74
                torch.rand(N_POINTS, dtype=torch.float32),
Christoph Lassner's avatar
Christoph Lassner committed
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
                requires_grad=False,
            ),
        )
        self.register_parameter(
            "cam_pos",
            nn.Parameter(
                torch.tensor([0.1, 0.1, 0.0], dtype=torch.float32),
                requires_grad=True,
            ),
        )
        self.register_parameter(
            "cam_rot",
            # We're using the 6D rot. representation for better gradients.
            nn.Parameter(
                axis_angle_to_matrix(
                    torch.tensor(
                        [
                            [0.02, 0.02, 0.01],
                        ],
                        dtype=torch.float32,
                    )
                )[0],
                requires_grad=True,
            ),
        )
        self.register_parameter(
            "focal_length",
            nn.Parameter(
                torch.tensor(
                    [
                        4.8 * 2.0 / 2.0,
                    ],
                    dtype=torch.float32,
                ),
                requires_grad=True,
            ),
        )
        self.cameras = PerspectiveCameras(
            # The focal length must be double the size for PyTorch3D because of the NDC
            # coordinates spanning a range of two - and they must be normalized by the
            # sensor width (see the pulsar example). This means we need here
            # 5.0 * 2.0 / 2.0 to get the equivalent results as in pulsar.
            #
            # R, T and f are provided here, but will be provided again
            # at every call to the forward method. The reason are problems
            # with PyTorch which makes device placement for gradients problematic
            # for tensors which are themselves on a 'gradient path' but not
            # leafs in the calculation tree. This will be addressed by an architectural
            # change in PyTorch3D in the future. Until then, this workaround is
            # recommended.
            focal_length=self.focal_length,
            R=self.cam_rot[None, ...],
            T=self.cam_pos[None, ...],
Nikhila Ravi's avatar
Nikhila Ravi committed
128
            image_size=((HEIGHT, WIDTH),),
Christoph Lassner's avatar
Christoph Lassner committed
129
            device=DEVICE,
Christoph Lassner's avatar
Christoph Lassner committed
130
131
        )
        raster_settings = PointsRasterizationSettings(
Nikhila Ravi's avatar
Nikhila Ravi committed
132
            image_size=(HEIGHT, WIDTH),
Christoph Lassner's avatar
Christoph Lassner committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            radius=self.vert_rad,
        )
        rasterizer = PointsRasterizer(
            cameras=self.cameras, raster_settings=raster_settings
        )
        self.renderer = PulsarPointsRenderer(rasterizer=rasterizer)

    def forward(self):
        # The Pointclouds object creates copies of it's arguments - that's why
        # we have to create a new object in every forward step.
        pcl = Pointclouds(
            points=self.vert_pos[None, ...], features=self.vert_col[None, ...]
        )
        return self.renderer(
            pcl,
            gamma=(self.gamma,),
            zfar=(45.0,),
            znear=(1.0,),
            radius_world=True,
Christoph Lassner's avatar
Christoph Lassner committed
152
            bg_col=torch.ones((3,), dtype=torch.float32, device=DEVICE),
Christoph Lassner's avatar
Christoph Lassner committed
153
154
155
156
157
158
159
160
            # As mentioned above: workaround for device placement of gradients for
            # camera parameters.
            focal_length=self.focal_length,
            R=self.cam_rot[None, ...],
            T=self.cam_pos[None, ...],
        )[0]


Christoph Lassner's avatar
Christoph Lassner committed
161
162
163
def cli():
    """
    Camera optimization example using pulsar.
Christoph Lassner's avatar
Christoph Lassner committed
164

Christoph Lassner's avatar
Christoph Lassner committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    Writes to `cam.gif`.
    """
    LOGGER.info("Loading reference...")
    # Load reference.
    ref = (
        torch.from_numpy(
            imageio.imread(
                "../../tests/pulsar/reference/examples_TestRenderer_test_cam.png"
            )[:, ::-1, :].copy()
        ).to(torch.float32)
        / 255.0
    ).to(DEVICE)
    # Set up model.
    model = SceneModel().to(DEVICE)
    # Optimizer.
    optimizer = optim.SGD(
        [
            {"params": [model.cam_pos], "lr": 1e-4},
            {"params": [model.cam_rot], "lr": 5e-6},
            # Using a higher lr for the focal length here, because
            # the sensor width can not be optimized directly.
            {"params": [model.focal_length], "lr": 1e-3},
Christoph Lassner's avatar
Christoph Lassner committed
187
188
        ]
    )
Christoph Lassner's avatar
Christoph Lassner committed
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

    LOGGER.info("Writing video to `%s`.", path.abspath("cam-pt3d.gif"))
    writer = imageio.get_writer("cam-pt3d.gif", format="gif", fps=25)

    # Optimize.
    for i in range(300):
        optimizer.zero_grad()
        result = model()
        # Visualize.
        result_im = (result.cpu().detach().numpy() * 255).astype(np.uint8)
        cv2.imshow("opt", result_im[:, :, ::-1])
        writer.append_data(result_im)
        overlay_img = np.ascontiguousarray(
            ((result * 0.5 + ref * 0.5).cpu().detach().numpy() * 255).astype(np.uint8)[
                :, :, ::-1
            ]
        )
        overlay_img = cv2.putText(
            overlay_img,
            "Step %d" % (i),
            (10, 40),
            cv2.FONT_HERSHEY_SIMPLEX,
            1,
            (0, 0, 0),
            2,
            cv2.LINE_AA,
            False,
        )
        cv2.imshow("overlay", overlay_img)
        cv2.waitKey(1)
        # Update.
        loss = ((result - ref) ** 2).sum()
        LOGGER.info("loss %d: %f", i, loss.item())
        loss.backward()
        optimizer.step()
    writer.close()
    LOGGER.info("Done.")


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    cli()