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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include "open3d/t/pipelines/registration/Registration.h"
#include "core/CoreTest.h"
#include "open3d/core/Dispatch.h"
#include "open3d/core/EigenConverter.h"
#include "open3d/core/Tensor.h"
#include "open3d/data/Dataset.h"
#include "open3d/pipelines/registration/ColoredICP.h"
#include "open3d/pipelines/registration/Registration.h"
#include "open3d/pipelines/registration/RobustKernel.h"
#include "open3d/t/io/PointCloudIO.h"
#include "open3d/t/pipelines/registration/RobustKernel.h"
#include "open3d/t/pipelines/registration/RobustKernelImpl.h"
#include "tests/Tests.h"
namespace t_reg = open3d::t::pipelines::registration;
namespace l_reg = open3d::pipelines::registration;
namespace open3d {
namespace tests {
class RegistrationPermuteDevices : public PermuteDevices {};
INSTANTIATE_TEST_SUITE_P(Registration,
RegistrationPermuteDevices,
testing::ValuesIn(PermuteDevices::TestCases()));
TEST_P(RegistrationPermuteDevices, ICPConvergenceCriteriaConstructor) {
// Constructor.
t_reg::ICPConvergenceCriteria convergence_criteria;
// Default values.
EXPECT_EQ(convergence_criteria.max_iteration_, 30);
EXPECT_DOUBLE_EQ(convergence_criteria.relative_fitness_, 1e-6);
EXPECT_DOUBLE_EQ(convergence_criteria.relative_rmse_, 1e-6);
}
TEST_P(RegistrationPermuteDevices, RegistrationResultConstructor) {
core::Device device = GetParam();
core::Dtype dtype = core::Float64;
// Initial transformation input for tensor implementation.
core::Tensor init_trans_t = core::Tensor::Eye(4, dtype, device);
t_reg::RegistrationResult reg_result(init_trans_t);
EXPECT_DOUBLE_EQ(reg_result.inlier_rmse_, 0.0);
EXPECT_DOUBLE_EQ(reg_result.fitness_, 0.0);
EXPECT_TRUE(reg_result.transformation_.AllClose(init_trans_t));
}
static std::tuple<t::geometry::PointCloud,
t::geometry::PointCloud,
core::Tensor,
double>
GetRegistrationTestData(core::Dtype& dtype, core::Device& device) {
t::geometry::PointCloud source_tpcd, target_tpcd;
data::DemoICPPointClouds pcd_fragments;
t::io::ReadPointCloud(pcd_fragments.GetPaths()[0], source_tpcd);
t::io::ReadPointCloud(pcd_fragments.GetPaths()[1], target_tpcd);
source_tpcd = source_tpcd.To(device).VoxelDownSample(0.05);
target_tpcd = target_tpcd.To(device).VoxelDownSample(0.05);
// Convert color to float values.
for (auto& kv : source_tpcd.GetPointAttr()) {
if (kv.first == "colors" && kv.second.GetDtype() == core::UInt8) {
source_tpcd.SetPointAttr(kv.first,
kv.second.To(device, dtype).Div(255.0));
} else {
source_tpcd.SetPointAttr(kv.first, kv.second.To(device, dtype));
}
}
for (auto& kv : target_tpcd.GetPointAttr()) {
if (kv.first == "colors" && kv.second.GetDtype() == core::UInt8) {
target_tpcd.SetPointAttr(kv.first,
kv.second.To(device, dtype).Div(255.0));
} else {
target_tpcd.SetPointAttr(kv.first, kv.second.To(device, dtype));
}
}
// Initial transformation input.
const core::Tensor initial_transform_t =
core::Tensor::Init<double>({{0.862, 0.011, -0.507, 0.5},
{-0.139, 0.967, -0.215, 0.7},
{0.487, 0.255, 0.835, -1.4},
{0.0, 0.0, 0.0, 1.0}},
core::Device("CPU:0"));
const double max_correspondence_dist = 0.7;
return std::make_tuple(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist);
}
TEST_P(RegistrationPermuteDevices, EvaluateRegistration) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// 1. Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Initial transformation input for tensor implementation.
core::Tensor initial_transform_t;
// Search radius.
double max_correspondence_dist;
std::tie(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist) =
GetRegistrationTestData(dtype, device);
open3d::geometry::PointCloud source_lpcd = source_tpcd.ToLegacy();
open3d::geometry::PointCloud target_lpcd = target_tpcd.ToLegacy();
// Initial transformation input for legacy implementation.
const Eigen::Matrix4d initial_transform_l =
core::eigen_converter::TensorToEigenMatrixXd(
initial_transform_t);
// Tensor evaluation.
t_reg::RegistrationResult evaluation_t = t_reg::EvaluateRegistration(
source_tpcd, target_tpcd, max_correspondence_dist,
initial_transform_t);
// Legacy evaluation.
l_reg::RegistrationResult evaluation_l = l_reg::EvaluateRegistration(
source_lpcd, target_lpcd, max_correspondence_dist,
initial_transform_l);
Eigen::Matrix4d::Identity();
EXPECT_NEAR(evaluation_t.fitness_, evaluation_l.fitness_, 0.005);
EXPECT_NEAR(evaluation_t.inlier_rmse_, evaluation_l.inlier_rmse_,
0.005);
}
}
TEST_P(RegistrationPermuteDevices, ICPPointToPoint) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// 1. Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Initial transformation input for tensor implementation.
core::Tensor initial_transform_t;
// Search radius.
double max_correspondence_dist;
std::tie(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist) =
GetRegistrationTestData(dtype, device);
open3d::geometry::PointCloud source_lpcd = source_tpcd.ToLegacy();
open3d::geometry::PointCloud target_lpcd = target_tpcd.ToLegacy();
// Initial transformation input for legacy implementation.
const Eigen::Matrix4d initial_transform_l =
core::eigen_converter::TensorToEigenMatrixXd(
initial_transform_t);
double relative_fitness = 1e-6;
double relative_rmse = 1e-6;
int max_iterations = 2;
// PointToPoint - Tensor.
t_reg::RegistrationResult reg_p2p_t = t_reg::ICP(
source_tpcd, target_tpcd, max_correspondence_dist,
initial_transform_t,
t_reg::TransformationEstimationPointToPoint(),
t_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations),
-1.0);
// PointToPoint - Legacy.
l_reg::RegistrationResult reg_p2p_l = l_reg::RegistrationICP(
source_lpcd, target_lpcd, max_correspondence_dist,
initial_transform_l,
l_reg::TransformationEstimationPointToPoint(),
l_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations));
EXPECT_NEAR(reg_p2p_t.fitness_, reg_p2p_l.fitness_, 0.005);
EXPECT_NEAR(reg_p2p_t.inlier_rmse_, reg_p2p_l.inlier_rmse_, 0.005);
}
}
TEST_P(RegistrationPermuteDevices, ICPPointToPlane) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// 1. Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Initial transformation input for tensor implementation.
core::Tensor initial_transform_t;
// Search radius.
double max_correspondence_dist;
std::tie(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist) =
GetRegistrationTestData(dtype, device);
open3d::geometry::PointCloud source_lpcd = source_tpcd.ToLegacy();
open3d::geometry::PointCloud target_lpcd = target_tpcd.ToLegacy();
// Initial transformation input for legacy implementation.
const Eigen::Matrix4d initial_transform_l =
core::eigen_converter::TensorToEigenMatrixXd(
initial_transform_t);
double relative_fitness = 1e-6;
double relative_rmse = 1e-6;
int max_iterations = 2;
// L1Loss Method:
// PointToPlane - Tensor.
t_reg::RegistrationResult reg_p2plane_t = t_reg::ICP(
source_tpcd, target_tpcd, max_correspondence_dist,
initial_transform_t,
t_reg::TransformationEstimationPointToPlane(
t_reg::RobustKernel(t_reg::RobustKernelMethod::L1Loss,
/*scale parameter =*/1.0,
/*shape parameter =*/1.0)),
t_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations),
-1.0);
// PointToPlane - Legacy.
l_reg::RegistrationResult reg_p2plane_l = l_reg::RegistrationICP(
source_lpcd, target_lpcd, max_correspondence_dist,
initial_transform_l,
l_reg::TransformationEstimationPointToPlane(
std::make_shared<l_reg::L1Loss>()),
l_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations));
EXPECT_NEAR(reg_p2plane_t.fitness_, reg_p2plane_l.fitness_, 0.005);
EXPECT_NEAR(reg_p2plane_t.inlier_rmse_, reg_p2plane_l.inlier_rmse_,
0.005);
}
}
TEST_P(RegistrationPermuteDevices, ICPColored) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// 1. Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Initial transformation input for tensor implementation.
core::Tensor initial_transform_t;
// Search radius.
double max_correspondence_dist;
std::tie(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist) =
GetRegistrationTestData(dtype, device);
open3d::geometry::PointCloud source_lpcd = source_tpcd.ToLegacy();
open3d::geometry::PointCloud target_lpcd = target_tpcd.ToLegacy();
// Initial transformation input for legacy implementation.
const Eigen::Matrix4d initial_transform_l =
core::eigen_converter::TensorToEigenMatrixXd(
initial_transform_t);
double relative_fitness = 1e-6;
double relative_rmse = 1e-6;
int max_iterations = 2;
// ColoredICP - Tensor.
t_reg::RegistrationResult reg_colored_t = t_reg::ICP(
source_tpcd, target_tpcd, max_correspondence_dist,
initial_transform_t,
t_reg::TransformationEstimationForColoredICP(),
t_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations),
-1.0);
// ColoredICP - Legacy.
l_reg::RegistrationResult reg_colored_l = l_reg::RegistrationColoredICP(
source_lpcd, target_lpcd, max_correspondence_dist,
initial_transform_l,
l_reg::TransformationEstimationForColoredICP(),
l_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations));
EXPECT_NEAR(reg_colored_t.fitness_, reg_colored_l.fitness_, 0.05);
EXPECT_NEAR(reg_colored_t.inlier_rmse_, reg_colored_l.inlier_rmse_,
0.02);
}
}
core::Tensor ComputeDirectionVectors(const core::Tensor& positions) {
core::Tensor directions = core::Tensor::Empty(
positions.GetShape(), positions.GetDtype(), positions.GetDevice());
for (int64_t i = 0; i < positions.GetLength(); ++i) {
// Compute the norm of the position vector.
core::Tensor norm = (positions[i][0] * positions[i][0] +
positions[i][1] * positions[i][1] +
positions[i][2] * positions[i][2])
.Sqrt();
// If the norm is zero, set the direction vector to zero.
if (norm.Item<float>() == 0.0) {
directions[i].Fill(0.0);
} else {
// Otherwise, compute the direction vector by dividing the position
// vector by its norm.
directions[i] = positions[i] / norm;
}
}
return directions;
}
static std::tuple<t::geometry::PointCloud,
t::geometry::PointCloud,
core::Tensor,
core::Tensor,
double,
double>
GetDopplerICPRegistrationTestData(core::Dtype& dtype, core::Device& device) {
t::geometry::PointCloud source_tpcd, target_tpcd;
data::DemoDopplerICPSequence demo_sequence;
t::io::ReadPointCloud(demo_sequence.GetPath(0), source_tpcd);
t::io::ReadPointCloud(demo_sequence.GetPath(1), target_tpcd);
source_tpcd.SetPointAttr(
"directions",
ComputeDirectionVectors(source_tpcd.GetPointPositions()));
source_tpcd = source_tpcd.To(device).UniformDownSample(5);
target_tpcd = target_tpcd.To(device).UniformDownSample(5);
Eigen::Matrix4d calibration{Eigen::Matrix4d::Identity()};
double period{0.0};
demo_sequence.GetCalibration(calibration, period);
// Calibration transformation input.
const core::Tensor calibration_t =
core::eigen_converter::EigenMatrixToTensor(calibration)
.To(device, dtype);
// Get the ground truth pose for the pair<0, 1> (on CPU:0).
auto trajectory = demo_sequence.GetTrajectory();
const core::Tensor pose_t =
core::eigen_converter::EigenMatrixToTensor(trajectory[1].second);
const double max_correspondence_dist = 0.3;
const double normals_search_radius = 10.0;
const int normals_max_neighbors = 30;
target_tpcd.EstimateNormals(normals_search_radius, normals_max_neighbors);
return std::make_tuple(source_tpcd, target_tpcd, calibration_t, pose_t,
period, max_correspondence_dist);
}
TEST_P(RegistrationPermuteDevices, ICPDoppler) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Calibration transformation input.
core::Tensor calibration_t;
// Ground truth pose.
core::Tensor pose_t;
// Time period between each point cloud scan.
double period{0.0};
// Search radius.
double max_correspondence_dist{0.0};
std::tie(source_tpcd, target_tpcd, calibration_t, pose_t, period,
max_correspondence_dist) =
GetDopplerICPRegistrationTestData(dtype, device);
const double relative_fitness = 1e-6;
const double relative_rmse = 1e-6;
const int max_iterations = 20;
t_reg::TransformationEstimationForDopplerICP estimation_dicp;
estimation_dicp.period_ = period;
estimation_dicp.transform_vehicle_to_sensor_ = calibration_t;
// DopplerICP - Tensor.
t_reg::RegistrationResult reg_doppler_t = t_reg::ICP(
source_tpcd, target_tpcd, max_correspondence_dist,
core::Tensor::Eye(4, dtype, device), estimation_dicp,
t_reg::ICPConvergenceCriteria(relative_fitness, relative_rmse,
max_iterations),
-1.0);
core::Tensor estimated_pose =
t::pipelines::kernel::TransformationToPose(
reg_doppler_t.transformation_.Inverse());
core::Tensor expected_pose =
t::pipelines::kernel::TransformationToPose(pose_t);
const double pose_diff =
(expected_pose - estimated_pose).Abs().Sum({0}).Item<double>();
EXPECT_NEAR(reg_doppler_t.fitness_ - 0.9, 0.0, 0.05);
EXPECT_NEAR(pose_diff - 0.017, 0.0, 0.005);
}
}
TEST_P(RegistrationPermuteDevices, RobustKernel) {
double scaling_parameter = 1.0;
double shape_parameter = 1.0;
std::unordered_map<int, double> expected_output = {
{0, 1.0}, // L2Loss [1.0]
{1, 1.0204}, // L1Loss [1.0 / abs(residual)]
{2, 1.0}, // HuberLoss [scale / max(abs(residual), scale)]
{3, 0.5101}, // CauchyLoss [1 / (1 + sq(residual / scale))]
{4, 0.260202}, // GMLoss [scale / sq(scale + sq(residual))]
{5, 0.00156816}, // TukeyLoss [sq(1 - sq(min(1, abs(r) / scale)))]
{6, 0.714213} // GeneralizedLoss
};
for (auto dtype : {core::Float32, core::Float64}) {
for (auto loss_method : {t_reg::RobustKernelMethod::L2Loss,
t_reg::RobustKernelMethod::L1Loss,
t_reg::RobustKernelMethod::HuberLoss,
t_reg::RobustKernelMethod::CauchyLoss,
t_reg::RobustKernelMethod::GMLoss,
t_reg::RobustKernelMethod::TukeyLoss,
t_reg::RobustKernelMethod::GeneralizedLoss}) {
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
DISPATCH_ROBUST_KERNEL_FUNCTION(
loss_method, scalar_t, scaling_parameter,
shape_parameter, [&]() {
auto weight = GetWeightFromRobustKernel(0.98);
EXPECT_NEAR(weight,
expected_output[(int)loss_method],
1e-3);
});
});
}
// GeneralizedLoss can behave as other loss methods by changing the
// shape_parameter (and adjusting the scaling_parameter).
// For shape_parameter = 2 : L2Loss.
// For shape_parameter = 0 : Cauchy or Lorentzian Loss.
// For shape_parameter = -2 : German-McClure or GM Loss.
// For shape_parameter = 1 : Charbonnier Loss or Pseudo-Huber loss or
// smoothened form of L1 Loss.
//
// Refer:
// @article{BarronCVPR2019,
// Author = {Jonathan T. Barron},
// Title = {A General and Adaptive Robust Loss Function},
// Journal = {CVPR},
// Year = {2019}
// }
DISPATCH_FLOAT_DTYPE_TO_TEMPLATE(dtype, [&]() {
DISPATCH_ROBUST_KERNEL_FUNCTION(
t_reg::RobustKernelMethod::GeneralizedLoss, scalar_t,
scaling_parameter, 2.0, [&]() {
auto weight = GetWeightFromRobustKernel(0.98);
EXPECT_NEAR(weight, 1.0, 1e-3);
});
DISPATCH_ROBUST_KERNEL_FUNCTION(
t_reg::RobustKernelMethod::GeneralizedLoss, scalar_t,
scaling_parameter, 0.0, [&]() {
auto weight = GetWeightFromRobustKernel(0.98);
EXPECT_NEAR(weight, 0.675584, 1e-3);
});
DISPATCH_ROBUST_KERNEL_FUNCTION(
t_reg::RobustKernelMethod::GeneralizedLoss, scalar_t,
scaling_parameter, -2.0, [&]() {
auto weight = GetWeightFromRobustKernel(0.98);
EXPECT_NEAR(weight, 0.650259, 1e-3);
});
DISPATCH_ROBUST_KERNEL_FUNCTION(
t_reg::RobustKernelMethod::GeneralizedLoss, scalar_t,
scaling_parameter, 1.0, [&]() {
auto weight = GetWeightFromRobustKernel(0.98);
EXPECT_NEAR(weight, 0.714213, 1e-3);
});
});
}
}
TEST_P(RegistrationPermuteDevices, GetInformationMatrixFromPointCloud) {
core::Device device = GetParam();
for (auto dtype : {core::Float32, core::Float64}) {
// 1. Get data and parameters.
t::geometry::PointCloud source_tpcd, target_tpcd;
// Initial transformation input for tensor implementation.
core::Tensor initial_transform_t;
// Search radius.
double max_correspondence_dist;
std::tie(source_tpcd, target_tpcd, initial_transform_t,
max_correspondence_dist) =
GetRegistrationTestData(dtype, device);
open3d::geometry::PointCloud source_lpcd = source_tpcd.ToLegacy();
open3d::geometry::PointCloud target_lpcd = target_tpcd.ToLegacy();
// Initial transformation input for legacy implementation.
const Eigen::Matrix4d initial_transform_l =
core::eigen_converter::TensorToEigenMatrixXd(
initial_transform_t);
// Tensor information matrix.
core::Tensor information_matrix_t = t_reg::GetInformationMatrix(
source_tpcd, target_tpcd, max_correspondence_dist,
initial_transform_t);
// Legacy evaluation.
Eigen::Matrix6d information_matrix_l =
l_reg::GetInformationMatrixFromPointClouds(
source_lpcd, target_lpcd, max_correspondence_dist,
initial_transform_l);
core::Tensor information_matrix_from_legacy =
core::eigen_converter::EigenMatrixToTensor(
information_matrix_l);
EXPECT_TRUE(information_matrix_t.AllClose(
information_matrix_from_legacy, 1e-1, 1e-1));
}
}
} // namespace tests
} // namespace open3d
|