MMDetection3D 自定义数据集使用指南

MMDetection3D 自定义数据集使用指南

mmdetection3d OpenMMLab's next-generation platform for general 3D object detection. mmdetection3d 项目地址: https://gitcode.com/gh_mirrors/mm/mmdetection3d

前言

在3D目标检测领域,使用自定义数据集进行模型训练和评估是一个常见需求。本文将详细介绍如何在MMDetection3D框架中使用自定义数据集,包括数据准备、格式转换、配置文件修改等关键步骤。

数据准备流程

1. 点云数据格式处理

MMDetection3D目前主要支持.bin格式的点云数据。如果您的原始数据是其他格式,需要进行转换:

常见转换场景
  1. PCD转BIN

    • 使用pypcd工具进行转换
    • 转换示例代码:
      import numpy as np
      from pypcd import pypcd
      
      pcd_data = pypcd.PointCloud.from_path('input.pcd')
      points = np.zeros([pcd_data.width, 4], dtype=np.float32)
      points[:, 0] = pcd_data.pc_data['x']
      points[:, 1] = pcd_data.pc_data['y']
      points[:, 2] = pcd_data.pc_data['z']
      points[:, 3] = pcd_data.pc_data['intensity'].astype(np.float32)
      points.tofile('output.bin')
      
  2. LAS转BIN

    • 通常需要先转换为PCD格式,再转为BIN
    • 可使用专业点云处理工具如CloudCompare进行中间转换

2. 标注文件格式

标注文件应为文本格式,每行代表一个3D边界框,包含以下信息:

x y z dx dy dz yaw category_name

其中:

  • (x,y,z):边界框中心坐标
  • (dx,dy,dz):边界框尺寸(长宽高)
  • yaw:绕Z轴的旋转角度(弧度)
  • category_name:目标类别名称

3. 校准文件格式

对于多传感器数据,需要提供校准文件,包含:

  • 每个相机的内参矩阵(P0-Pn)
  • 激光雷达到每个相机的外参变换矩阵(lidar2cam0-lidar2camn)

数据组织结构

根据任务类型不同,数据组织方式有所差异:

1. 纯激光雷达3D检测

data/custom/
├── ImageSets/
│   ├── train.txt
│   ├── val.txt
├── points/
│   ├── 000000.bin
│   ├── 000001.bin
├── labels/
│   ├── 000000.txt
│   ├── 000001.txt

2. 纯视觉3D检测

data/custom/
├── ImageSets/
├── calibs/
├── images/
│   ├── images_0/  # 相机0图像
│   ├── images_1/  # 相机1图像
├── labels/

3. 多模态3D检测

结合了激光雷达和视觉数据:

data/custom/
├── ImageSets/
├── calibs/
├── points/  # 激光雷达数据
├── images/  # 多相机图像
├── labels/

4. 3D语义分割

data/custom/
├── ImageSets/
├── points/  # 点云数据
├── semantic_mask/  # 逐点标签

数据转换工具

准备好原始数据后,使用以下命令生成训练所需的中间文件:

python tools/create_data.py custom \
    --root-path ./data/custom \
    --out-dir ./data/custom \
    --extra-tag custom

该脚本会生成:

  • .pkl信息文件(包含数据路径和标注信息)
  • 其他必要的中间文件

自定义数据集类实现

需要在mmdet3d/datasets/下创建自定义数据集类:

@DATASETS.register_module()
class MyDataset(Det3DDataset):
    METAINFO = {
        'classes': ('Pedestrian', 'Cyclist', 'Car')  # 替换为您的类别
    }
    
    def parse_ann_info(self, info):
        """处理标注信息"""
        ann_info = super().parse_ann_info(info)
        if ann_info is None:
            ann_info = dict()
            ann_info['gt_bboxes_3d'] = np.zeros((0, 7), dtype=np.float32)
            ann_info['gt_labels_3d'] = np.zeros(0, dtype=np.int64)
        
        # 过滤不需要的类别
        ann_info = self._remove_dontcare(ann_info)
        gt_bboxes_3d = LiDARInstance3DBoxes(ann_info['gt_bboxes_3d'])
        ann_info['gt_bboxes_3d'] = gt_bboxes_3d
        return ann_info

配置文件调整

1. 数据集配置

configs/_base_/datasets/custom.py中配置:

dataset_type = 'MyDataset'
data_root = 'data/custom/'
class_names = ['Pedestrian', 'Cyclist', 'Car']  # 您的类别
point_cloud_range = [0, -40, -3, 70.4, 40, 1]  # 点云范围

train_pipeline = [
    dict(type='LoadPointsFromFile', coord_type='LIDAR', load_dim=4, use_dim=4),
    dict(type='LoadAnnotations3D', with_bbox_3d=True, with_label_3d=True),
    # 其他数据增强操作...
]

train_dataloader = dict(
    batch_size=6,
    dataset=dict(
        type=dataset_type,
        data_root=data_root,
        ann_file='custom_infos_train.pkl',
        pipeline=train_pipeline,
        metainfo=dict(classes=class_names)))

2. 模型配置调整

对于基于体素的检测器(如PointPillars),需要特别注意:

  1. 点云范围体素大小需要匹配
  2. 中间特征图尺寸需要根据体素大小计算
  3. Anchor设置需要适配您的数据分布

示例配置(configs/_base_/models/pointpillars_hv_secfpn_custom.py):

voxel_size = [0.16, 0.16, 4]
point_cloud_range = [0, -39.68, -3, 69.12, 39.68, 1]

model = dict(
    voxel_layer=dict(
        point_cloud_range=point_cloud_range,
        voxel_size=voxel_size),
    middle_encoder=dict(
        output_shape=[496, 432]),  # 根据实际计算调整
    anchor_generator=dict(
        ranges=[
            [0, -39.68, -0.6, 69.12, 39.68, -0.6],
            # 其他类别...
        ],
        sizes=[[0.8, 0.6, 1.73], [1.76, 0.6, 1.73]]  # 根据实际目标尺寸调整
    )
)

训练与评估

1. 训练命令

python tools/train.py configs/pointpillars/pointpillars_hv_secfpn_8xb6_custom.py

2. 评估配置

使用KITTI评估标准:

val_evaluator = dict(
    type='KittiMetric',
    ann_file='data/custom/custom_infos_val.pkl',
    metric='bbox')

可视化验证

训练前建议使用可视化工具检查数据:

python tools/misc/browse_dataset.py configs/pointpillars/pointpillars_hv_secfpn_8xb6_custom.py

该工具可以显示点云和3D框的叠加效果,帮助验证标注是否正确。

总结

本文详细介绍了在MMDetection3D中使用自定义数据集的完整流程,包括:

  1. 数据格式转换与组织
  2. 自定义数据集类实现
  3. 配置文件调整要点
  4. 训练与评估流程
  5. 可视化验证方法

通过遵循这些步骤,您可以顺利地将自己的3D数据集应用于MMDetection3D框架中的各种先进算法。

mmdetection3d OpenMMLab's next-generation platform for general 3D object detection. mmdetection3d 项目地址: https://gitcode.com/gh_mirrors/mm/mmdetection3d

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卓艾滢Kingsley

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

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

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

打赏作者

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

抵扣说明:

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

余额充值