def test_pinhole_camera_attributes_batch2(self): batch_size = 2 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width) assert pinhole.batch_size == batch_size assert pinhole.fx.shape[0] == batch_size assert pinhole.fy.shape[0] == batch_size assert pinhole.cx.shape[0] == batch_size assert pinhole.cy.shape[0] == batch_size assert pinhole.tx.shape[0] == batch_size assert pinhole.ty.shape[0] == batch_size assert pinhole.tz.shape[0] == batch_size assert pinhole.height.shape[0] == batch_size assert pinhole.width.shape[0] == batch_size assert pinhole.rt_matrix.shape == (batch_size, 3, 4) assert pinhole.camera_matrix.shape == (batch_size, 3, 3) assert pinhole.rotation_matrix.shape == (batch_size, 3, 3) assert pinhole.translation_vector.shape == (batch_size, 3, 1)
def test_pinhole_camera_translation_setters(self): batch_size = 1 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width) assert pinhole.tx.item() == tx assert pinhole.ty.item() == ty assert pinhole.tz.item() == tz # add offset pinhole.tx += 3. pinhole.ty += 2. pinhole.tz += 1. assert pinhole.tx.item() == tx + 3. assert pinhole.ty.item() == ty + 2. assert pinhole.tz.item() == tz + 1. # set to zero pinhole.tx = 0. pinhole.ty = 0. pinhole.tz = 0. assert pinhole.tx.item() == 0. assert pinhole.ty.item() == 0. assert pinhole.tz.item() == 0.
def test_smoke(self): intrinsics = torch.eye(4)[None] extrinsics = torch.eye(4)[None] height = torch.ones(1) width = torch.ones(1) pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width) assert isinstance(pinhole, tgm.PinholeCamera)
def test_pinhole_camera_scale_inplace(self): batch_size = 2 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 scale_val = 2.0 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width scale_factor = torch.ones(batch_size) * scale_val pinhole = tgm.PinholeCamera(intrinsics, extrinsics, height, width) pinhole_scale = pinhole.clone() pinhole_scale.scale_(scale_factor) assert utils.check_equal_torch( pinhole_scale.intrinsics[..., 0, 0], pinhole.intrinsics[..., 0, 0] * scale_val) # fx assert utils.check_equal_torch( pinhole_scale.intrinsics[..., 1, 1], pinhole.intrinsics[..., 1, 1] * scale_val) # fy assert utils.check_equal_torch( pinhole_scale.intrinsics[..., 0, 2], pinhole.intrinsics[..., 0, 2] * scale_val) # cx assert utils.check_equal_torch( pinhole_scale.intrinsics[..., 1, 2], pinhole.intrinsics[..., 1, 2] * scale_val) # cy assert utils.check_equal_torch( pinhole_scale.height, pinhole.height * scale_val) assert utils.check_equal_torch( pinhole_scale.width, pinhole.width * scale_val)
def test_pinhole_camera_list_get_pinhole(self): batch_size = 1 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole_1 = tgm.PinholeCamera(intrinsics, extrinsics, height, width) pinhole_2 = pinhole_1.clone() pinholes_list = [pinhole_1, pinhole_2] pinholes = tgm.PinholeCamerasList(pinholes_list) pinhole_11 = pinholes.get_pinhole(0) assert pinhole_1.batch_size == pinhole_11.batch_size assert torch.allclose(pinhole_1.fx, pinhole_11.fx) assert torch.allclose(pinhole_1.fy, pinhole_11.fy) assert torch.allclose(pinhole_1.cx, pinhole_11.cx) assert torch.allclose(pinhole_1.cy, pinhole_11.cy) assert torch.allclose(pinhole_1.tx, pinhole_11.tx) assert torch.allclose(pinhole_1.ty, pinhole_11.ty) assert torch.allclose(pinhole_1.tz, pinhole_11.tz) assert torch.allclose(pinhole_1.rt_matrix, pinhole_11.rt_matrix) assert torch.allclose( pinhole_1.camera_matrix, pinhole_11.camera_matrix) assert torch.allclose( pinhole_1.rotation_matrix, pinhole_11.rotation_matrix) assert torch.allclose( pinhole_1.translation_vector, pinhole_11.translation_vector)
def test_pinhole_camera_list_attributes(self): batch_size = 1 height, width = 4, 6 fx, fy, cx, cy = 1, 2, width / 2, height / 2 tx, ty, tz = 1, 2, 3 intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy) extrinsics = self._create_extrinsics(batch_size, tx, ty, tz) height = torch.ones(batch_size) * height width = torch.ones(batch_size) * width pinhole_1 = tgm.PinholeCamera(intrinsics, extrinsics, height, width) pinhole_2 = pinhole_1.clone() pinholes_list = [pinhole_1, pinhole_2] pinholes = tgm.PinholeCamerasList(pinholes_list) assert pinholes.batch_size == batch_size assert pinholes.num_cameras == 2 assert torch.allclose(pinholes.fx, torch.ones(1, 2) * fx) assert torch.allclose(pinholes.fy, torch.ones(1, 2) * fy) assert torch.allclose(pinholes.cx, torch.ones(1, 2) * cx) assert torch.allclose(pinholes.cy, torch.ones(1, 2) * cy) assert torch.allclose(pinholes.tx, torch.ones(1, 2) * tx) assert torch.allclose(pinholes.ty, torch.ones(1, 2) * ty) assert torch.allclose(pinholes.tz, torch.ones(1, 2) * tz) assert pinholes.rt_matrix.shape == (batch_size, 2, 3, 4) assert pinholes.camera_matrix.shape == (batch_size, 2, 3, 3) assert pinholes.rotation_matrix.shape == (batch_size, 2, 3, 3) assert pinholes.translation_vector.shape == (batch_size, 2, 3, 1)