以下是 Open3D 核心模块 open3d.core
的 完整函数及类 的详细说明,涵盖所有公开接口,按功能分类整理。内容基于 Open3D 最新版本(v0.17+),并标注关键用途和参数。
1. 设备管理 (Device
)
管理计算设备(CPU/GPU/CUDA)的配置与状态检查。
函数/类 | 参数 | 返回值 | 说明 |
---|
Device(device_type, device_id=0) | device_type: str (e.g., "CPU" , "CUDA" ) | Device 对象 | 创建设备实例,如 Device("CUDA", 0) |
Device.is_available(device_type) | device_type: str | bool | 检查设备是否可用(如 Device.is_available("CUDA") ) |
Device.default_device() | - | Device | 返回当前默认设备(通常优先用GPU) |
device.__str__() | - | str | 设备描述字符串(如 "CUDA:0" ) |
2. 张量操作 (Tensor
)
创建与初始化
函数 | 参数 | 返回值 | 说明 |
---|
Tensor(array, dtype=None, device=None) | array : np.ndarray/list, dtype : float32/int64 等, device : Device | Tensor | 从数据创建张量 |
Tensor.zeros(shape, dtype, device) | shape : tuple, dtype : 数据类型, device : Device | Tensor | 全零张量 |
Tensor.ones(shape, dtype, device) | 同 zeros | Tensor | 全1张量 |
Tensor.full(shape, value, dtype, device) | value : 标量填充值 | Tensor | 填充固定值 |
Tensor.arange(start, end, step, dtype, device) | start , end , step : 数值范围 | Tensor | 生成序列张量 |
Tensor.empty(shape, dtype, device) | - | Tensor | 未初始化张量(内容随机) |
属性与转换
属性/方法 | 说明 |
---|
tensor.shape | 张量形状(tuple) |
tensor.dtype | 数据类型(Dtype 类型) |
tensor.device | 设备信息 |
tensor.numpy() | 转为NumPy数组(仅支持CPU张量) |
tensor.to(device) | 跨设备复制(如 tensor.to("CUDA:0") ) |
tensor.clone() | 深拷贝 |
tensor.copy_(src) | 从源张量复制数据 |
3. 数据类型 (Dtype
)
支持的数据类型常量(完整列表):
类型 | 说明 | 对应NumPy类型 |
---|
o3d.core.float32 | 32位浮点 | np.float32 |
o3d.core.float64 | 64位浮点 | np.float64 |
o3d.core.int8 | 8位有符号整数 | np.int8 |
o3d.core.int32 | 32位有符号整数 | np.int32 |
o3d.core.int64 | 64位有符号整数 | np.int64 |
o3d.core.uint8 | 8位无符号整数 | np.uint8 |
o3d.core.bool | 布尔类型 | np.bool_ |
4. 数学运算
逐元素运算
函数 | 说明 | 函数 | 说明 |
---|
tensor + other | 加法 | tensor.sin() | 正弦函数 |
tensor - other | 减法 | tensor.cos() | 余弦函数 |
tensor * other | 乘法 | tensor.sqrt() | 平方根 |
tensor / other | 除法 | tensor.exp() | 指数函数 |
tensor.abs() | 绝对值 | tensor.log() | 自然对数 |
矩阵运算
函数 | 说明 |
---|
tensor.matmul(other) | 矩阵乘法 |
tensor.T() | 转置 |
tensor.inv() | 矩阵求逆(仅方阵) |
o3d.core.einsum(equation, *tensors) | 爱因斯坦求和(如 "ij,jk->ik" ) |
统计与归约
函数 | 参数 | 说明 |
---|
tensor.sum(dim) | dim : 可选维度 | 求和(可指定轴) |
tensor.mean(dim) | - | 均值 |
tensor.max(dim) | - | 最大值(返回值和索引) |
tensor.min(dim) | - | 最小值(返回值和索引) |
5. 索引与切片
操作 | 示例 | 说明 |
---|
tensor[0] | 第0个元素 | 单维索引 |
tensor[:, 1:3] | 所有行,第1到2列 | 多维切片 |
tensor.gather(indices, dim) | indices : 索引张量, dim : 维度 | 按索引收集数据 |
tensor.reshape(new_shape) | new_shape : tuple | 调整形状(不改变数据) |
6. 内存与优化
函数 | 说明 |
---|
tensor.contiguous() | 确保内存连续存储(提升运算效率) |
tensor.pin_memory() | 固定内存(加速CPU→GPU传输) |
o3d.core.empty_cache() | 清空CUDA缓存(避免GPU内存泄漏) |
7. 并行计算
函数 | 说明 |
---|
o3d.core.parallel_for(func, count, device) | 并行执行函数(func 需接受索引参数,如 lambda i: i*2 ,count 为迭代次数) |
8. 其他工具函数
函数 | 说明 |
---|
o3d.core.hash(tensor) | 计算张量的哈希值(用于快速比较) |
o3d.core.cat(tensors, dim=0) | 沿维度拼接张量(类似 torch.cat ) |
o3d.core.stack(tensors, dim=0) | 在新维度上堆叠张量 |
完整示例
import open3d.core as o3c
import numpy as np
device = o3c.Device("CUDA:0")
a = o3c.Tensor(np.array([1, 2, 3]), dtype=o3c.float32, device=device)
b = o3c.Tensor.ones((3,), dtype=o3c.int32, device="CPU")
c = (a + b.to(device)).sin()
print(c[0].item())
print(c.mean())
result = o3c.parallel_for(lambda i: i * 2, count=1000, device=device)
注意事项
- GPU张量转NumPy:需先通过
to("CPU")
转到CPU。 - 性能优化:优先使用
contiguous()
和 pin_memory()
。 - 错误处理:跨设备操作需确保设备兼容性(如避免CPU张量与GPU张量直接运算)。
如需更高级功能(如自定义算子或与PyTorch交互),可参考 Open3D 的 扩展模块 或官方文档的 C++ API 部分。