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
|
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include "open3d/ml/contrib/GridSubsampling.h"
#include "pybind/core/tensor_converter.h"
#include "pybind/docstring.h"
#include "pybind/ml/contrib/contrib.h"
#include "pybind/open3d_pybind.h"
#include "pybind/pybind_utils.h"
namespace open3d {
namespace ml {
namespace contrib {
const py::tuple SubsampleBatch(py::array points,
py::array batches,
utility::optional<py::array> features,
utility::optional<py::array> classes,
float sampleDl,
const std::string& method,
int max_p,
int verbose) {
std::vector<PointXYZ> original_points;
std::vector<PointXYZ> subsampled_points;
std::vector<int> original_batches;
std::vector<int> subsampled_batches;
std::vector<float> original_features;
std::vector<float> subsampled_features;
std::vector<int> original_classes;
std::vector<int> subsampled_classes;
// Fill original_points.
core::Tensor points_t = core::PyArrayToTensor(points, true).Contiguous();
if (points_t.GetDtype() != core::Float32) {
utility::LogError("points must be np.float32.");
}
if (points_t.NumDims() != 2 || points_t.GetShape()[1] != 3) {
utility::LogError("points must have shape (N, 3), but got {}.",
points_t.GetShape().ToString());
}
int64_t num_points = points_t.NumElements() / 3;
original_points = std::vector<PointXYZ>(
reinterpret_cast<PointXYZ*>(points_t.GetDataPtr()),
reinterpret_cast<PointXYZ*>(points_t.GetDataPtr()) + num_points);
// Fill original batches.
core::Tensor batches_t = core::PyArrayToTensor(batches, true).Contiguous();
if (batches_t.GetDtype() != core::Int32) {
utility::LogError("batches must be np.int32.");
}
if (batches_t.NumDims() != 1) {
utility::LogError("batches must have shape (NB,), but got {}.",
batches_t.GetShape().ToString());
}
int64_t num_batches = batches_t.GetShape()[0];
if (static_cast<int64_t>(batches_t.Sum({0}).Item<int32_t>()) !=
num_points) {
utility::LogError("batches got {} points, but points got {} points.",
batches_t.Sum({0}).Item<int32_t>(), num_points);
}
original_batches = batches_t.ToFlatVector<int32_t>();
if (verbose) {
utility::LogInfo("Got {} batches with a total of {} points as inputs.",
num_batches, num_points);
}
// Fill original_features.
int64_t feature_dim = -1;
if (features.has_value()) {
core::Tensor features_t =
core::PyArrayToTensor(features.value(), true).Contiguous();
if (features_t.GetDtype() != core::Float32) {
utility::LogError("features must be np.float32.");
}
if (features_t.NumDims() != 2) {
utility::LogError("features must have shape (N, d), but got {}.",
features_t.GetShape().ToString());
}
if (features_t.GetShape()[0] != num_points) {
utility::LogError(
"features's shape {} is not compatible with "
"points's shape {}, their first dimension must "
"be equal.",
features_t.GetShape().ToString(),
points_t.GetShape().ToString());
}
feature_dim = features_t.GetShape()[1];
original_features = features_t.ToFlatVector<float>();
}
// Fill original_classes.
if (classes.has_value()) {
core::Tensor classes_t =
core::PyArrayToTensor(classes.value(), true).Contiguous();
if (classes_t.GetDtype() != core::Int32) {
utility::LogError("classes must be np.int32.");
}
if (classes_t.NumDims() != 1) {
utility::LogError("classes must have shape (N,), but got {}.",
classes_t.GetShape().ToString());
}
if (classes_t.GetShape()[0] != num_points) {
utility::LogError(
"classes's shape {} is not compatible with "
"points's shape {}, their first dimension must "
"be equal.",
classes_t.GetShape().ToString(),
points_t.GetShape().ToString());
}
original_classes = classes_t.ToFlatVector<int32_t>();
}
// Call function.
batch_grid_subsampling(
original_points, subsampled_points, original_features,
subsampled_features, original_classes, subsampled_classes,
original_batches, subsampled_batches, sampleDl, max_p);
// Wrap result subsampled_points. Data will be copied.
int64_t num_subsampled_points =
static_cast<int64_t>(subsampled_points.size());
core::Tensor subsampled_points_t(
reinterpret_cast<float*>(subsampled_points.data()),
{num_subsampled_points, 3}, core::Float32);
// Wrap result subsampled_batches. Data will be copied.
int64_t num_subsampled_batches =
static_cast<int64_t>(subsampled_batches.size());
core::Tensor subsampled_batches_t = core::Tensor(
subsampled_batches, {num_subsampled_batches}, core::Int32);
if (static_cast<int64_t>(subsampled_batches_t.Sum({0}).Item<int32_t>()) !=
num_subsampled_points) {
utility::LogError(
"subsampled_batches got {} points, but subsampled_points got "
"{} points.",
subsampled_batches_t.Sum({0}).Item<int32_t>(),
num_subsampled_points);
}
if (verbose) {
utility::LogInfo("Subsampled to {} batches with a total of {} points.",
num_subsampled_batches, num_subsampled_points);
}
// Wrap result subsampled_features. Data will be copied.
core::Tensor subsampled_features_t;
if (features.has_value()) {
if (subsampled_features.size() % num_subsampled_points != 0) {
utility::LogError(
"Error: subsampled_points.size() {} is not a "
"multiple of num_subsampled_points {}.",
subsampled_points.size(), num_subsampled_points);
}
int64_t subsampled_feature_dim =
static_cast<int64_t>(subsampled_features.size()) /
num_subsampled_points;
if (feature_dim != subsampled_feature_dim) {
utility::LogError(
"Error: input feature dim {} does not match "
"the subsampled feature dim {}.",
feature_dim, subsampled_feature_dim);
}
subsampled_features_t = core::Tensor(
subsampled_features, {num_subsampled_points, feature_dim},
core::Float32);
}
// Wrap result subsampled_classes. Data will be copied.
core::Tensor subsampled_classes_t;
if (classes.has_value()) {
if (static_cast<int64_t>(subsampled_classes.size()) !=
num_subsampled_points) {
utility::LogError(
"Error: subsampled_classes.size() {} != "
"num_subsampled_points {}.",
subsampled_classes.size(), num_subsampled_points);
}
subsampled_classes_t = core::Tensor(
subsampled_classes, {num_subsampled_points}, core::Int32);
}
if (features.has_value() && classes.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_batches_t),
core::TensorToPyArray(subsampled_features_t),
core::TensorToPyArray(subsampled_classes_t));
} else if (features.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_batches_t),
core::TensorToPyArray(subsampled_features_t));
} else if (classes.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_batches_t),
core::TensorToPyArray(subsampled_classes_t));
} else {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_batches_t));
}
}
const py::object Subsample(py::array points,
utility::optional<py::array> features,
utility::optional<py::array> classes,
float sampleDl,
int verbose) {
std::vector<PointXYZ> original_points;
std::vector<PointXYZ> subsampled_points;
std::vector<float> original_features;
std::vector<float> subsampled_features;
std::vector<int> original_classes;
std::vector<int> subsampled_classes;
// Fill original_points.
core::Tensor points_t = core::PyArrayToTensor(points, true).Contiguous();
if (points_t.GetDtype() != core::Float32) {
utility::LogError("points must be np.float32.");
}
if (points_t.NumDims() != 2 || points_t.GetShape()[1] != 3) {
utility::LogError("points must have shape (N, 3), but got {}.",
points_t.GetShape().ToString());
}
int64_t num_points = points_t.NumElements() / 3;
original_points = std::vector<PointXYZ>(
reinterpret_cast<PointXYZ*>(points_t.GetDataPtr()),
reinterpret_cast<PointXYZ*>(points_t.GetDataPtr()) + num_points);
if (verbose) {
utility::LogInfo("Got {} points as inputs.", num_points);
}
// Fill original_features.
int64_t feature_dim = -1;
if (features.has_value()) {
core::Tensor features_t =
core::PyArrayToTensor(features.value(), true).Contiguous();
if (features_t.GetDtype() != core::Float32) {
utility::LogError("features must be np.float32.");
}
if (features_t.NumDims() != 2) {
utility::LogError("features must have shape (N, d), but got {}.",
features_t.GetShape().ToString());
}
if (features_t.GetShape()[0] != num_points) {
utility::LogError(
"features's shape {} is not compatible with "
"points's shape {}, their first dimension must "
"be equal.",
features_t.GetShape().ToString(),
points_t.GetShape().ToString());
}
feature_dim = features_t.GetShape()[1];
original_features = features_t.ToFlatVector<float>();
}
// Fill original_classes.
if (classes.has_value()) {
core::Tensor classes_t =
core::PyArrayToTensor(classes.value(), true).Contiguous();
if (classes_t.GetDtype() != core::Int32) {
utility::LogError("classes must be np.int32.");
}
if (classes_t.NumDims() != 1) {
utility::LogError("classes must have shape (N,), but got {}.",
classes_t.GetShape().ToString());
}
if (classes_t.GetShape()[0] != num_points) {
utility::LogError(
"classes's shape {} is not compatible with "
"points's shape {}, their first dimension must "
"be equal.",
classes_t.GetShape().ToString(),
points_t.GetShape().ToString());
}
original_classes = classes_t.ToFlatVector<int32_t>();
}
// Call function.
grid_subsampling(original_points, subsampled_points, original_features,
subsampled_features, original_classes, subsampled_classes,
sampleDl, verbose);
// Wrap result subsampled_points. Data will be copied.
int64_t num_subsampled_points =
static_cast<int64_t>(subsampled_points.size());
core::Tensor subsampled_points_t(
reinterpret_cast<float*>(subsampled_points.data()),
{num_subsampled_points, 3}, core::Float32);
if (verbose) {
utility::LogInfo("Subsampled to {} points.", num_subsampled_points);
}
// Wrap result subsampled_features. Data will be copied.
core::Tensor subsampled_features_t;
if (features.has_value()) {
if (subsampled_features.size() % num_subsampled_points != 0) {
utility::LogError(
"Error: subsampled_points.size() {} is not a "
"multiple of num_subsampled_points {}.",
subsampled_points.size(), num_subsampled_points);
}
int64_t subsampled_feature_dim =
static_cast<int64_t>(subsampled_features.size()) /
num_subsampled_points;
if (feature_dim != subsampled_feature_dim) {
utility::LogError(
"Error: input feature dim {} does not match "
"the subsampled feature dim {}.",
feature_dim, subsampled_feature_dim);
}
subsampled_features_t = core::Tensor(
subsampled_features, {num_subsampled_points, feature_dim},
core::Float32);
}
// Wrap result subsampled_classes. Data will be copied.
core::Tensor subsampled_classes_t;
if (classes.has_value()) {
if (static_cast<int64_t>(subsampled_classes.size()) !=
num_subsampled_points) {
utility::LogError(
"Error: subsampled_classes.size() {} != "
"num_subsampled_points {}.",
subsampled_classes.size(), num_subsampled_points);
}
subsampled_classes_t = core::Tensor(
subsampled_classes, {num_subsampled_points}, core::Int32);
}
if (features.has_value() && classes.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_features_t),
core::TensorToPyArray(subsampled_classes_t));
} else if (features.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_features_t));
} else if (classes.has_value()) {
return py::make_tuple(core::TensorToPyArray(subsampled_points_t),
core::TensorToPyArray(subsampled_classes_t));
} else {
return core::TensorToPyArray(subsampled_points_t);
}
}
void pybind_contrib_subsample_definitions(py::module& m_contrib) {
m_contrib.def("subsample", &Subsample, "points"_a,
"features"_a = py::none(), "classes"_a = py::none(),
"sampleDl"_a = 0.1, "verbose"_a = 0);
m_contrib.def("subsample_batch", &SubsampleBatch, "points"_a, "batches"_a,
"features"_a = py::none(), "classes"_a = py::none(),
"sampleDl"_a = 0.1, "method"_a = "barycenters", "max_p"_a = 0,
"verbose"_a = 0);
}
} // namespace contrib
} // namespace ml
} // namespace open3d
|