Open3D--core模块函数详解

以下是 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: strbool检查设备是否可用(如 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: DeviceTensor从数据创建张量
Tensor.zeros(shape, dtype, device)shape: tuple, dtype: 数据类型, device: DeviceTensor全零张量
Tensor.ones(shape, dtype, device)zerosTensor全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.float3232位浮点np.float32
o3d.core.float6464位浮点np.float64
o3d.core.int88位有符号整数np.int8
o3d.core.int3232位有符号整数np.int32
o3d.core.int6464位有符号整数np.int64
o3d.core.uint88位无符号整数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*2count 为迭代次数)

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

# 1. 创建设备和张量
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")

# 2. 数学运算
c = (a + b.to(device)).sin()  # GPU计算 + 三角函数

# 3. 索引与统计
print(c[0].item())           # 输出第0个元素的值
print(c.mean())              # 输出均值

# 4. 并行计算
result = o3c.parallel_for(lambda i: i * 2, count=1000, device=device)

注意事项

  1. GPU张量转NumPy:需先通过 to("CPU") 转到CPU。
  2. 性能优化:优先使用 contiguous()pin_memory()
  3. 错误处理:跨设备操作需确保设备兼容性(如避免CPU张量与GPU张量直接运算)。

如需更高级功能(如自定义算子或与PyTorch交互),可参考 Open3D 的 扩展模块 或官方文档的 C++ API 部分。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-Vision

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值