Open3D面向机器学习的扩展库

2020-11-19 15:31:14 浏览数 (1)

Open3D-ML是Open3D的一个扩展,用于3D机器学习任务。它建立在Open3D核心库之上,并通过机器学习工具对其进行扩展,以进行3D数据处理。此repo集中于语义点云分割等应用程序,并提供可应用于常见任务的预训练模型以及用于训练的流程。

Open3D-ML与TensorFlow和PyTorch一起工作,可以轻松地集成到现有项目中,还可以提供独立于ML框架的通用功能,如数据可视化。

安装

Open3D-ML集成在Open3D v0.11 python发行版中,并与以下版本的ML框架兼容

* PyTorch 1.6

* TensorFlow 2.3

* CUDA 10.1 (On GNU/Linux x86_64, optional)

安装Open3D

代码语言:javascript复制
# make sure you have the latest pip version
pip install --upgrade pip
# install open3d
pip install open3d

要安装Pythorch或TensorFlow的兼容版本,需要使用相应的需求文件:

代码语言:javascript复制
# To install a compatible version of TensorFlow
pip install -r requirements-tensorflow.txt
# To install a compatible version of PyTorch with CUDA
pip install -r requirements-torch-cuda.txt

测试安装

代码语言:javascript复制
# with PyTorch
$ python -c "import open3d.ml.torch as ml3d"# or with TensorFlow
$ python -c "import open3d.ml.tf as ml3d"

如果需要使用不同版本的ML框架或CUDA,可以从源代码重新构建Open3D。

使用教程

读取数据集

dataset命名空间包含用于读取公共数据集的类。这里我们读取SemanticKITTI数据集并将其可视化。

代码语言:javascript复制
import open3d.ml.torch as ml3d  # or open3d.ml.tf as ml3d

# construct a dataset by specifying dataset_path
dataset = ml3d.datasets.SemanticKITTI(dataset_path='/path/to/SemanticKITTI/')

# get the 'all' split that combines training, validation and test set
all_split = dataset.get_split('all')

# print the attributes of the first datum
print(all_split.get_attr(0))

# print the shape of the first point cloud
print(all_split.get_data(0)['point'].shape)

# show the first 100 frames using the visualizer
vis = ml3d.vis.Visualizer()
vis.visualize_dataset(dataset, 'all', indices=range(100))import open3d.ml.torch as ml3d # or open3d.ml.tf as ml3d
# construct a dataset by specifying dataset_pathdataset = ml3d.datasets.SemanticKITTI(dataset_path='/path/to/SemanticKITTI/')
# get the 'all' split that combines training, validation and test setall_split = dataset.get_split('all')
# print the attributes of the first datumprint(all_split.get_attr(0))
# print the shape of the first point cloudprint(all_split.get_data(0)['point'].shape)
# show the first 100 frames using the visualizervis = ml3d.vis.Visualizer()
vis.visualize_dataset(dataset, 'all', indices=range(100))

加载配置文件

模型、数据集和流程的配置存储在ml3d/Configs中。用户还可以构建自己的yaml文件来记录他们的定制配置。下面是一个读取配置文件并从中构造模块的示例。

代码语言:javascript复制
import open3d.ml as _ml3d
import open3d.ml.torch as ml3d # or open3d.ml.tf as ml3d  

framework = "torch" # or tf
cfg_file = "ml3d/configs/randlanet_semantickitti.yml"
cfg = _ml3d.utils.Config.load_from_file(cfg_file)

# fetch the classes by the name
Pipeline = _ml3d.utils.get_module("pipeline", cfg.pipeline.name, framework)
Model = _ml3d.utils.get_module("model", cfg.model.name, framework)
Dataset = _ml3d.utils.get_module("dataset", cfg.dataset.name)

# use the arguments in the config file to construct the instances 
cfg.dataset['dataset_path'] = "/path/to/your/dataset"
dataset = Dataset(cfg.dataset.pop('dataset_path', None), **cfg.dataset)
model = Model(**cfg.model)
pipeline = Pipeline(model, dataset, **cfg.pipeline)

运行一个预先训练过的模型

在上一个例子的基础上,我们可以用一个预先训练的语义分割模型实例化一个算法,并在数据集的点云上运行它。查看模型集合以获取预训练模型的权重。

代码语言:javascript复制
import os
import open3d.ml as _ml3d
import open3d.ml.torch as ml3d

cfg_file = "ml3d/configs/randlanet_semantickitti.yml"
cfg = _ml3d.utils.Config.load_from_file(cfg_file)

model = ml3d.models.RandLANet(**cfg.model)
cfg.dataset['dataset_path'] = "/path/to/your/dataset"
dataset = ml3d.datasets.SemanticKITTI(cfg.dataset.pop('dataset_path', None), **cfg.dataset)
pipeline = ml3d.pipelines.SemanticSegmentation(model, dataset=dataset, device="gpu", **cfg.pipeline)

# download the weights.
ckpt_folder = "./logs/"
os.makedirs(ckpt_folder, exist_ok=True)
ckpt_path = ckpt_folder   "randlanet_semantickitti_202009090354utc.pth"
randlanet_url = "https://storage.googleapis.com/open3d-releases/model-zoo/randlanet_semantickitti_202009090354utc.pth"
if not os.path.exists(ckpt_path):
    cmd = "wget {} -O {}".format(randlanet_url, ckpt_path)
    os.system(cmd)
    
# load the parameters.
pipeline.load_ckpt(ckpt_path=ckpt_path)

test_split = dataset.get_split("test")
data = test_split.get_data(0)

# run inference on a single example.
# returns dict with 'predict_labels' and 'predict_scores'.
result = pipeline.run_inference(data)

# evaluate performance on the test set; this will write logs to './logs'.
pipeline.run_test()

用户还可以使用预定义的脚本来加载预先训练的权重并运行测试。

训练模型

与推理类似,流程中提供了一个在数据集上训练模型的接口。

代码语言:javascript复制
# use a cache for storing the results of the preprocessing (default path is './logs/cache')
dataset = ml3d.datasets.SemanticKITTI(dataset_path='/path/to/SemanticKITTI/', use_cache=True)

# create the model with random initialization.
model = RandLANet()

pipeline = SemanticSegmentation(model=model, dataset=dataset, max_epoch=100)

# prints training progress in the console.
pipeline.run_train()

有关更多示例,请参见examples/和scripts/目录。

使用预定义脚本

scripts/semseg.py 提供了一个简单的数据集评估接口。准确地定义模型,避免了定义具体模型的麻烦。

python scripts/semseg.py {tf/torch} -c <path-to-config> --<extra args>

注意, extra args 将优先于配置文件中的相同参数。因此,在启动脚本时,可以将其作为命令行参数传递,而不是更改配置文件中的param。

例如:

代码语言:javascript复制
# Launch training for RandLANet on SemanticKITTI with torch.
python scripts/semseg.py torch -c ml3d/configs/randlanet_semantickitti.yml --dataset.dataset_path <path-to-dataset> --dataset.use_cache True

# Launch testing for KPConv on Toronto3D with tensorflow.
python scripts/semseg.py tf -c ml3d/configs/kpconv_toronto3d.yml --split test --dataset.dataset_path <path-to-dataset> --model.ckpt_path <path-to-checkpoint>

要获得进一步的帮助,可运行python脚本 python scripts/semseg.py --help

ML库结构

Open3D-ML的核心部分位于ml3d子文件夹中,该子文件夹被集成到ML命名空间中的Open3D中。除了核心部分之外,目录示例和脚本还提供了支持脚本,用于开始在数据集上设置训练流程或运行网络。

代码语言:javascript复制
├─ docs                   # Markdown and rst files for documentation
├─ examples               # Place for example scripts and notebooks
├─ ml3d                   # Package root dir that is integrated in open3d
     ├─ configs           # Model configuration files
     ├─ datasets          # Generic dataset code; will be integratede as open3d.ml.{tf,torch}.datasets
     ├─ utils             # Framework independent utilities; available as open3d.ml.{tf,torch}.utils
     ├─ vis               # ML specific visualization functions
     ├─ tf                # Directory for TensorFlow specific code. same structure as ml3d/torch.
     │                    # This will be available as open3d.ml.tf
     ├─ torch             # Directory for PyTorch specific code; available as open3d.ml.torch
          ├─ dataloaders  # Framework specific dataset code, e.g. wrappers that can make use of the
          │               # generic dataset code.
          ├─ models       # Code for models
          ├─ modules      # Smaller modules, e.g., metrics and losses
          ├─ pipelines    # Pipelines for tasks like semantic segmentation
├─ scripts                # Demo scripts for training and dataset download scripts

任务和算法

分割

对于语义分割的任务,我们使用mIoU(mean intersection-over-union)来衡量不同方法在所有类上的性能。下表显示了分段任务的可用模型和数据集以及各自的分数。每个分数链接到各自的权重文件。

模型集合

有关所有权重文件的完整列表,请参见模型文件 model_weights.txt 以及MD5校验model_weights.md5.

数据集集合

下面是我们为其提供数据集读取器类的数据集列表。

SemanticKITTI

Toronto 3D

Semantic 3D

S3DIS

Paris-Lille 3D

要下载这些数据集,请访问相应的网页,可查看scripts/download_datasets中的脚本。

本文仅做学术分享,如有侵权,请联系删文。

0 人点赞