|
10 | 10 | import numpy as np
|
11 | 11 | import torch
|
12 | 12 | from common_testing import TestCaseMixin
|
13 |
| -from pytorch3d.renderer.utils import TensorProperties |
| 13 | +from pytorch3d.ops import eyes |
| 14 | +from pytorch3d.renderer import ( |
| 15 | + PerspectiveCameras, |
| 16 | + AlphaCompositor, |
| 17 | + PointsRenderer, |
| 18 | + PointsRasterizationSettings, |
| 19 | + PointsRasterizer, |
| 20 | +) |
| 21 | +from pytorch3d.renderer.utils import ( |
| 22 | + TensorProperties, |
| 23 | + ndc_to_grid_sample_coords, |
| 24 | + ndc_grid_sample, |
| 25 | +) |
| 26 | +from pytorch3d.structures import Pointclouds |
14 | 27 |
|
15 | 28 |
|
16 | 29 | # Example class for testing
|
@@ -96,3 +109,165 @@ def test_gather_props(self):
|
96 | 109 | # the input.
|
97 | 110 | self.assertClose(test_class_gathered.x[inds].mean(dim=0), x[i, ...])
|
98 | 111 | self.assertClose(test_class_gathered.y[inds].mean(dim=0), y[i, ...])
|
| 112 | + |
| 113 | + def test_ndc_grid_sample_rendering(self): |
| 114 | + """ |
| 115 | + Use PyTorch3D point renderer to render a colored point cloud, then |
| 116 | + sample the image at the locations of the point projections with |
| 117 | + `ndc_grid_sample`. Finally, assert that the sampled colors are equal to the |
| 118 | + original point cloud colors. |
| 119 | +
|
| 120 | + Note that, in order to ensure correctness, we use a nearest-neighbor |
| 121 | + assignment point renderer (i.e. no soft splatting). |
| 122 | + """ |
| 123 | + |
| 124 | + # generate a bunch of 3D points on a regular grid lying in the z-plane |
| 125 | + n_grid_pts = 10 |
| 126 | + grid_scale = 0.9 |
| 127 | + z_plane = 2.0 |
| 128 | + image_size = [128, 128] |
| 129 | + point_radius = 0.015 |
| 130 | + n_pts = n_grid_pts * n_grid_pts |
| 131 | + pts = torch.stack( |
| 132 | + torch.meshgrid( |
| 133 | + [torch.linspace(-grid_scale, grid_scale, n_grid_pts)] * 2, indexing="ij" |
| 134 | + ), |
| 135 | + dim=-1, |
| 136 | + ) |
| 137 | + pts = torch.cat([pts, z_plane * torch.ones_like(pts[..., :1])], dim=-1) |
| 138 | + pts = pts.reshape(1, n_pts, 3) |
| 139 | + |
| 140 | + # color the points randomly |
| 141 | + pts_colors = torch.rand(1, n_pts, 3) |
| 142 | + |
| 143 | + # make trivial rendering cameras |
| 144 | + cameras = PerspectiveCameras( |
| 145 | + R=eyes(dim=3, N=1), |
| 146 | + device=pts.device, |
| 147 | + T=torch.zeros(1, 3, dtype=torch.float32, device=pts.device), |
| 148 | + ) |
| 149 | + |
| 150 | + # render the point cloud |
| 151 | + pcl = Pointclouds(points=pts, features=pts_colors) |
| 152 | + renderer = NearestNeighborPointsRenderer( |
| 153 | + rasterizer=PointsRasterizer( |
| 154 | + cameras=cameras, |
| 155 | + raster_settings=PointsRasterizationSettings( |
| 156 | + image_size=image_size, |
| 157 | + radius=point_radius, |
| 158 | + points_per_pixel=1, |
| 159 | + ), |
| 160 | + ), |
| 161 | + compositor=AlphaCompositor(), |
| 162 | + ) |
| 163 | + im_render = renderer(pcl) |
| 164 | + |
| 165 | + # sample the render at projected pts |
| 166 | + pts_proj = cameras.transform_points(pcl.points_padded())[..., :2] |
| 167 | + pts_colors_sampled = ndc_grid_sample( |
| 168 | + im_render, |
| 169 | + pts_proj, |
| 170 | + mode="nearest", |
| 171 | + align_corners=False, |
| 172 | + ).permute(0, 2, 1) |
| 173 | + |
| 174 | + # assert that the samples are the same as original points |
| 175 | + self.assertClose(pts_colors, pts_colors_sampled, atol=1e-4) |
| 176 | + |
| 177 | + def test_ndc_to_grid_sample_coords(self): |
| 178 | + """ |
| 179 | + Test the conversion from ndc to grid_sample coords by comparing |
| 180 | + to known conversion results. |
| 181 | + """ |
| 182 | + |
| 183 | + # square image tests |
| 184 | + image_size_square = [100, 100] |
| 185 | + xy_ndc_gs_square = torch.FloatTensor( |
| 186 | + [ |
| 187 | + # 4 corners |
| 188 | + [[-1.0, -1.0], [1.0, 1.0]], |
| 189 | + [[1.0, 1.0], [-1.0, -1.0]], |
| 190 | + [[1.0, -1.0], [-1.0, 1.0]], |
| 191 | + [[1.0, 1.0], [-1.0, -1.0]], |
| 192 | + # center |
| 193 | + [[0.0, 0.0], [0.0, 0.0]], |
| 194 | + ] |
| 195 | + ) |
| 196 | + |
| 197 | + # non-batched version |
| 198 | + for xy_ndc, xy_gs in xy_ndc_gs_square: |
| 199 | + xy_gs_predicted = ndc_to_grid_sample_coords( |
| 200 | + xy_ndc, |
| 201 | + image_size_square, |
| 202 | + ) |
| 203 | + self.assertClose(xy_gs_predicted, xy_gs) |
| 204 | + |
| 205 | + # batched version |
| 206 | + xy_ndc, xy_gs = xy_ndc_gs_square[:, 0], xy_ndc_gs_square[:, 1] |
| 207 | + xy_gs_predicted = ndc_to_grid_sample_coords( |
| 208 | + xy_ndc, |
| 209 | + image_size_square, |
| 210 | + ) |
| 211 | + self.assertClose(xy_gs_predicted, xy_gs) |
| 212 | + |
| 213 | + # non-square image tests |
| 214 | + image_size = [100, 200] |
| 215 | + xy_ndc_gs = torch.FloatTensor( |
| 216 | + [ |
| 217 | + # 4 corners |
| 218 | + [[-2.0, -1.0], [1.0, 1.0]], |
| 219 | + [[2.0, -1.0], [-1.0, 1.0]], |
| 220 | + [[-2.0, 1.0], [1.0, -1.0]], |
| 221 | + [[2.0, 1.0], [-1.0, -1.0]], |
| 222 | + # center |
| 223 | + [[0.0, 0.0], [0.0, 0.0]], |
| 224 | + # non-corner points |
| 225 | + [[4.0, 0.5], [-2.0, -0.5]], |
| 226 | + [[1.0, -0.5], [-0.5, 0.5]], |
| 227 | + ] |
| 228 | + ) |
| 229 | + |
| 230 | + # check both H > W and W > H |
| 231 | + for flip_axes in [False, True]: |
| 232 | + |
| 233 | + # non-batched version |
| 234 | + for xy_ndc, xy_gs in xy_ndc_gs: |
| 235 | + xy_gs_predicted = ndc_to_grid_sample_coords( |
| 236 | + xy_ndc.flip(dims=(-1,)) if flip_axes else xy_ndc, |
| 237 | + list(reversed(image_size)) if flip_axes else image_size, |
| 238 | + ) |
| 239 | + self.assertClose( |
| 240 | + xy_gs_predicted, |
| 241 | + xy_gs.flip(dims=(-1,)) if flip_axes else xy_gs, |
| 242 | + ) |
| 243 | + |
| 244 | + # batched version |
| 245 | + xy_ndc, xy_gs = xy_ndc_gs[:, 0], xy_ndc_gs[:, 1] |
| 246 | + xy_gs_predicted = ndc_to_grid_sample_coords( |
| 247 | + xy_ndc.flip(dims=(-1,)) if flip_axes else xy_ndc, |
| 248 | + list(reversed(image_size)) if flip_axes else image_size, |
| 249 | + ) |
| 250 | + self.assertClose( |
| 251 | + xy_gs_predicted, |
| 252 | + xy_gs.flip(dims=(-1,)) if flip_axes else xy_gs, |
| 253 | + ) |
| 254 | + |
| 255 | + |
| 256 | +class NearestNeighborPointsRenderer(PointsRenderer): |
| 257 | + """ |
| 258 | + A class for rendering a batch of points by a trivial nearest |
| 259 | + neighbor assignment. |
| 260 | + """ |
| 261 | + |
| 262 | + def forward(self, point_clouds, **kwargs) -> torch.Tensor: |
| 263 | + fragments = self.rasterizer(point_clouds, **kwargs) |
| 264 | + # set all weights trivially to one |
| 265 | + dists2 = fragments.dists.permute(0, 3, 1, 2) |
| 266 | + weights = torch.ones_like(dists2) |
| 267 | + images = self.compositor( |
| 268 | + fragments.idx.long().permute(0, 3, 1, 2), |
| 269 | + weights, |
| 270 | + point_clouds.features_packed().permute(1, 0), |
| 271 | + **kwargs, |
| 272 | + ) |
| 273 | + return images |
0 commit comments