Monocular Quasi-Dense 3D目标跟踪:单目图像实现三维物体跟踪全解析
文章目录
- [Monocular Quasi-Dense 3D目标跟踪:单目图像实现三维物体跟踪全解析](#Monocular Quasi-Dense 3D目标跟踪:单目图像实现三维物体跟踪全解析)
-
- 一、项目背景与意义
-
- [1.1 行业应用场景](#1.1 行业应用场景)
- [1.2 技术挑战](#1.2 技术挑战)
- [1.3 本文目标](#1.3 本文目标)
- 二、核心技术原理
-
- [2.1 算法架构详解](#2.1 算法架构详解)
- [2.2 关键技术创新点](#2.2 关键技术创新点)
- [2.3 数学原理推导](#2.3 数学原理推导)
-
- [2.3.1 单目深度估计](#2.3.1 单目深度估计)
- [2.3.2 3D框投影的完整推导](#2.3.2 3D框投影的完整推导)
- [2.3.3 观测角与全局朝向角的关系](#2.3.3 观测角与全局朝向角的关系)
- [2.3.3 MultiBin朝向估计](#2.3.3 MultiBin朝向估计)
- [2.3.4 准密集对比损失](#2.3.4 准密集对比损失)
- [2.3.5 Cycle-Softmax匹配](#2.3.5 Cycle-Softmax匹配)
- 三、环境搭建与依赖
-
- [3.1 硬件要求](#3.1 硬件要求)
- [3.2 软件环境](#3.2 软件环境)
- [3.3 依赖安装](#3.3 依赖安装)
- 四、数据集准备
-
- [4.1 数据集介绍](#4.1 数据集介绍)
-
- [KITTI Tracking](#KITTI Tracking)
- nuScenes
- [Waymo Open Dataset](#Waymo Open Dataset)
- [4.2 数据预处理](#4.2 数据预处理)
- [4.3 数据增强策略](#4.3 数据增强策略)
- 五、模型实现详解
- 六、模型训练与调优
-
- [6.1 训练流程](#6.1 训练流程)
- [6.2 训练技巧](#6.2 训练技巧)
- [6.3 超参数调优](#6.3 超参数调优)
- 七、模型评估与分析
-
- [7.1 评估指标](#7.1 评估指标)
- [7.2 实验结果](#7.2 实验结果)
-
- [nuScenes 3D Tracking 结果](#nuScenes 3D Tracking 结果)
- [Waymo Open 3D Tracking 结果](#Waymo Open 3D Tracking 结果)
- [KITTI 2D Vehicle Tracking 结果](#KITTI 2D Vehicle Tracking 结果)
- [7.3 消融实验](#7.3 消融实验)
- [7.4 可视化分析](#7.4 可视化分析)
- 八、推理部署
-
- [8.1 模型导出](#8.1 模型导出)
- [8.2 推理代码](#8.2 推理代码)
- [8.3 性能优化](#8.3 性能优化)
-
- TensorRT加速
- [ONNX Runtime部署](#ONNX Runtime部署)
- 九、常见错误与避坑指南
- 十、扩展与进阶
-
- [10.1 改进方向](#10.1 改进方向)
- [10.2 相关论文推荐](#10.2 相关论文推荐)
- 参考链接
- 总结与下篇预告
一、项目背景与意义
1.1 行业应用场景
自动驾驶是当前计算机视觉领域最具挑战性的应用场景之一。在复杂的城市道路环境中,自动驾驶系统需要精确感知周围环境,包括检测和跟踪其他车辆、行人、骑行者等交通参与者。传统的自动驾驶系统依赖昂贵的激光雷达(LiDAR)和高精度GPS/IMU组合导航系统来获取三维空间信息,但一套完整的传感器套件动辄数十万甚至上百万人民币,极大地限制了自动驾驶技术的普及。
近年来,纯视觉方案逐渐成为学术界和工业界的研究热点。特斯拉在2021年宣布放弃毫米波雷达,全面转向纯视觉方案;Mobileye、Wayve等公司也在积极推进基于摄像头的自动驾驶系统。**QD-3DT(Monocular Quasi-Dense 3D Object Tracking)**正是在这一背景下提出的创新框架------它仅使用单目摄像头输入,就能实现端到端的三维物体检测与跟踪。
1.2 技术挑战
从单目图像实现三维目标跟踪面临以下几个核心技术挑战:
-
深度估计的模糊性:单目图像缺乏直接的深度信息,从二维图像恢复三维空间位置本身是一个病态问题(ill-posed problem),存在无穷多种可能的投影关系。
-
目标遮挡问题:在城市交通场景中,车辆之间频繁发生相互遮挡,如何在目标被部分或完全遮挡后重新识别(Re-ID)是该领域的经典难题。
-
跨视角外观变化:同一目标在不同帧中可能呈现截然不同的外观------由于视角变化、光照变化和尺度变化,传统基于外观特征的匹配方法容易失效。
-
运动估计的不确定性:单帧深度估计本身存在噪声,直接用于三维轨迹预测会产生累积误差,需要一个鲁棒的运动模型来平滑和预测目标的三维运动轨迹。
-
时序关联的复杂性:如何在密集的交通流中准确地将当前帧的检测结果与历史轨迹关联起来,避免身份切换(ID Switch)是3D多目标跟踪的核心评价指标。
1.3 本文目标
本文将对QD-3DT框架进行从原理到实践的全面解析,涵盖以下内容:
- 深入分析准密集相似性学习(Quasi-Dense Similarity Learning)的核心原理
- 详解基于深度排序的三维实例关联机制
- 剖析LSTM运动预测模型的数学原理和实现细节
- 完整的环境搭建、训练流程和推理部署代码
- 在KITTI、nuScenes、Waymo三大自动驾驶数据集上的实验结果分析
二、核心技术原理
2.1 算法架构详解
QD-3DT的整体架构可以归纳为四个核心阶段:
┌─────────────────────────────────────────────────────────┐
│ 输入:单目图像序列 │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 阶段一:2D目标检测 + 3D信息回归 │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Backbone │→│ FPN Neck │→│ RPN Head │→│BBox Head │ │
│ │ (DLA-34 │ │ │ │ │ │+ 3D Head │ │
│ │ /R-101) │ │ │ │ │ │ │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ 输出:2D框 + 深度 + 尺寸 + 朝向 + 中心偏移 │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 阶段二:准密集嵌入学习 (Quasi-Dense Embedding) │
│ ┌──────────────────┐ ┌──────────────────────┐ │
│ │ Key Frame RoI │ │ Reference Frame RoI │ │
│ │ Embedding 提取 │ │ Embedding 提取 │ │
│ └────────┬─────────┘ └──────────┬───────────┘ │
│ │ │ │
│ └──────────┬──────────────┘ │
│ ▼ │
│ ┌────────────────────────┐ │
│ │ Multi-Pos 对比学习 │ │
│ │ (Cycle-Softmax匹配) │ │
│ └────────────────────────┘ │
│ 输出:外观嵌入向量 + 关联矩阵 │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 阶段三:三维关联与轨迹管理 │
│ ┌──────────┐ ┌──────────────┐ ┌──────────────────┐ │
│ │ BEV IoU │ │深度排序启发式│ │ 不确定性感知匹配 │ │
│ │ 匹配 │ │ (Depth Order)│ │ (Uncertainty) │ │
│ └──────────┘ └──────────────┘ └──────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────┐ │
│ │ 多模态亲和力融合 │ │
│ │ (Appearance + 3D + Motion) │
│ └────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ 阶段四:LSTM运动预测与轨迹优化 │
│ ┌──────────────┐ ┌──────────────────────┐ │
│ │ Prediction │ │ Refinement LSTM │ │
│ │ LSTM (速度→ │ → │ (预测+观测+置信度)→ │ │
│ │ 下一帧位置) │ │ 精化位置 │ │
│ └──────────────┘ └──────────────────────┘ │
│ 输出:平滑的三维轨迹 │
└─────────────────────────────────────────────────────────┘
核心数据流:单目图像 → 2D检测框 + 3D属性 → 外观嵌入向量 → 多模态关联 → 3D轨迹
2.2 关键技术创新点
QD-3DT相比之前的工作有以下几个关键创新:
创新点1:准密集(Quasi-Dense)外观匹配
传统的目标跟踪方法通常采用稀疏的外观匹配策略------仅在检测框级别计算外观相似度。QD-3DT借鉴了QDTrack的思想,采用准密集匹配策略:对关键帧(Key Frame)中的每个候选区域和参考帧(Reference Frame)中的大量候选区域(包括正样本和负样本)都进行嵌入特征对比学习。这种策略使得模型能够在更大的样本空间中学习判别性的外观表示。
准密集 vs 稀疏匹配的区别:
| 维度 | 稀疏匹配 | 准密集匹配 |
|---|---|---|
| 正样本 | 仅GT目标 | GT目标 + IoU高的候选框 |
| 负样本 | 随机采样少量 | 所有非匹配候选框 |
| 学习信号 | 弱 | 强 |
| 判别能力 | 一般 | 优秀 |
| 计算开销 | 低 | 中 |
创新点2:多分支三维属性回归头
QD-3DT设计了一个独特的多分支三维属性回归头(Multi-Branch 3D Head),将深度、尺寸、朝向和二维中心偏移分离到独立的子网络中进行预测。下面先用一张表来对比QD-3DT与其他主流单目3D检测方法的核心差异:
| 特性 | MonoFlex | FCOS3D | CenterNet3D | QD-3DT |
|---|---|---|---|---|
| 深度预测方式 | 直接回归 + 不确定性 | 直接回归 | 深度分类bin | 对数空间回归 + 不确定性 |
| 朝向估计 | MultiBin | MultiBin | 单分支回归 | MultiBin + 分离分支 |
| 3D属性回归 | 共享头 | 共享头 | 独立头 | 多分支分离头 |
| 外观嵌入 | 无 | 无 | 无 | 准密集嵌入(256维) |
| 跟踪能力 | 无 | 无 | 无 | 端到端3D跟踪 |
| 运动模型 | 无 | 无 | 无 | Kalman + LSTM双模式 |
| 深度不确定性 | ✓ | ✗ | ✗ | ✓(用于跟踪) |
可以看出,QD-3DT在3D属性回归上采用了更精细的分支分离策略,并且是唯一一个将3D检测与跟踪统一到端到端框架中的方法。
python
# 代码来源:qd3dt/models/bbox_heads/convfc_bbox_3d_rot_sep_confidence_head.py
class ConvFCBBox3DRotSepConfidenceHead(nn.Module):
"""
多分支三维回归头架构:
共享卷积层 → 共享全连接层
├── 深度分支(dep_convs → dep_fcs → fc_dep)
├── 尺寸分支(dim_convs → dim_fcs → fc_dim)
├── 朝向分支(rot_convs → rot_fcs → fc_rot)
└── 中心偏移分支(2dc_convs → 2dc_fcs → fc_2dc)
"""
def __init__(self,
num_shared_convs=0, # 共享卷积层数
num_shared_fcs=0, # 共享全连接层数
num_dep_convs=0, # 深度分支卷积层数
num_dep_fcs=0, # 深度分支全连接层数
num_dim_convs=0, # 尺寸分支卷积层数
num_dim_fcs=0, # 尺寸分支全连接层数
num_rot_convs=0, # 朝向分支卷积层数
num_rot_fcs=0, # 朝向分支全连接层数
num_2dc_convs=0, # 中心偏移分支卷积层数
num_2dc_fcs=0, # 中心偏移分支全连接层数
# ... 其他参数
):
这种分离式设计的优势在于:
- 各分支可以独立地专注于自己的预测任务,避免多任务学习中的梯度冲突
- 深度分支特别引入了不确定性估计子分支,为后续的关联和跟踪提供置信度信号
- 朝向预测采用MultiBin方法,将360°的连续角度空间离散化为两个重叠的bin,提高回归精度
创新点3:深度不确定性感知
QD-3DT在深度分支中额外预测了一个深度不确定性(Depth Uncertainty)值,这是整个框架中最精妙的设计之一:
python
# 深度预测和不确定性预测
self.fc_dep = nn.Linear(self.dep_last_dim, out_dim_dep) # 深度预测
self.fc_dep_uncer = nn.Linear(self.dep_last_dim, out_dim_dep) # 不确定性预测
# 不确定性损失函数(自监督)
pos_depth_self_labels = torch.exp(
-torch.abs(pos_depth_pred - pos_depth_targets) * 5.0)
pos_depth_self_weights = torch.where(
pos_depth_self_labels > 0.8,
pos_depth_weights.new_ones(1) * 5.0, # 高置信度样本权重
pos_depth_weights.new_ones(1) * 0.1) # 低置信度样本权重
深度不确定性被用于:
- 检测置信度加权 :
score * depth_uncertainty用于NMS过滤 - 关联匹配:低不确定性的检测结果在关联时获得更高的权重
- LSTM精化:作为Refinement LSTM的额外输入,告知模型当前观测的可靠性
创新点4:多模态亲和力融合
QD-3DT的关联匹配不依赖单一信号,而是融合了四种不同的亲和力度量。下表详细对比了各亲和力度量的特性和贡献:
| 亲和力类型 | 计算方式 | 优势 | 劣势 | 权重 |
|---|---|---|---|---|
| 外观特征 (feat) | 嵌入向量Cycle-Softmax | 对遮挡目标鲁棒 | 光照变化敏感 | 0.5 |
| 3D空间IoU (iou) | BEV框重叠率 | 空间位置精确 | 依赖深度估计精度 | 0.5×depth_weight |
| 深度排序 (depth) | 3D中心距离/运动一致性 | 区分前后重叠目标 | 远距离误差大 | 0.5×iou |
| 类别一致性 (cat) | 类别二值掩码 | 消除跨类误匹配 | 无细粒度区分 | 二值掩码 |
python
# 多模态亲和力计算
# 1. 外观特征相似度(准密集嵌入)
scores_feat = compute_quasi_dense_feat_match(embeds, memo_embeds)
# 2. 3D空间IoU(BEV视角或3D框IoU)
scores_iou = bbox_overlaps(dets_xy_box, memo_dets_xy_box)
# 3. 深度排序启发式(多种度量方式)
scores_depth = compute_boxoverlap_with_depth(boxes_3d, memo_boxes_3d_predict, memo_vs)
# 4. 类别一致性
scores_cats = (labels.view(-1, 1) == memo_labels.view(1, -1)).float()
# 最终亲和力融合
scores = bbox_affinity_weight * scores_iou * scores_depth + \
feat_affinity_weight * scores_feat
创新点5:LSTM双阶段运动模型
QD-3DT使用LSTM来建模目标的长期运动模式,分为两个子阶段:
预测阶段(Prediction LSTM):
- 输入:过去5帧的速度向量(历史轨迹差分)
- 输出:下一帧的预测位置
- 使用注意力机制(Attention)对历史速度进行加权融合
精化阶段(Refinement LSTM):
- 输入:预测位置 + 单帧观测位置 + 深度不确定性
- 输出:融合后的精化位置
- 不确定性感知:低置信度的观测更多依赖预测结果
python
# VeloLSTM的Refine过程(代码简化)
class VeloLSTM(LSTM):
def refine(self, location, observation, prev_location, confidence, hc_0):
# 计算预测速度和观测速度
pred_vel = location - prev_location
obsv_vel = observation - prev_location
# 特征嵌入
loc_embed = self.vel2feat(pred_vel)
obs_embed = self.vel2feat(obsv_vel)
conf_embed = self.conf2feat(confidence)
# 三路特征拼接
embed = torch.cat([loc_embed, obs_embed, conf_embed], dim=1)
# LSTM精化
out, (h_n, c_n) = self.refine_lstm(embed, hc_0)
# 置信度门控的速度融合
delta_vel_atten = torch.sigmoid(self.conf2atten(out))
output_pred = delta_vel_atten * obsv_vel + \
(1.0 - delta_vel_atten) * pred_vel + prev_location
return output_pred, (h_n, c_n)
2.3 数学原理推导
2.3.1 单目深度估计
QD-3DT从2D RoI特征回归深度值,使用对数空间进行回归以提高数值稳定性。对数空间变换可以将深度值的大范围变化映射到较小的输出范围,使得网络更容易学习:
d p r e d = exp ( z d / 2.0 ) d_{pred} = \exp(z_d / 2.0) dpred=exp(zd/2.0)
其中 z d z_d zd 是网络对log深度的预测值,除以2.0是为了进一步压缩数值范围。损失函数使用Smooth L1损失:
L d e p t h = SmoothL1 ( log ( d p r e d ) ⋅ 2.0 , log ( d g t ) ⋅ 2.0 ) \mathcal{L}{depth} = \text{SmoothL1}(\log(d{pred}) \cdot 2.0, \log(d_{gt}) \cdot 2.0) Ldepth=SmoothL1(log(dpred)⋅2.0,log(dgt)⋅2.0)
深度不确定性的自监督学习使用二元交叉熵。其核心思想是:如果预测深度与真实深度的误差较小,则该预测应被视为"高置信度"(标签为1);否则为"低置信度"(标签为0):
L u n c = BCE ( u p r e d , 1 ∣ log ( d p r e d ) − log ( d g t ) ∣ \< δ ) \mathcal{L}{unc} = \text{BCE}(u{pred}, \mathbb{1}\|\\log(d_{pred}) - \\log(d_{gt})\| \< \\delta) Lunc=BCE(upred,1∣log(dpred)−log(dgt)∣\<δ)
其中 u p r e d ∈ 0 , 1 u_{pred} \in 0, 1 upred∈0,1 是预测的不确定性(0表示完全确定,1表示完全不确定), δ = 0.2 \delta = 0.2 δ=0.2 是判定阈值。高置信度样本的权重为5.0,低置信度样本权重仅为0.1,这使得网络更关注学习"何时应该自信"。
2.3.2 3D框投影的完整推导
从2D检测框中心 ( u , v ) (u, v) (u,v) 和深度 d d d 恢复相机坐标系下的3D中心 ( X c , Y c , Z c ) (X_c, Y_c, Z_c) (Xc,Yc,Zc),这基于相机透视投影的逆变换。
根据针孔相机模型,3D点 ( X c , Y c , Z c ) (X_c, Y_c, Z_c) (Xc,Yc,Zc) 投影到图像坐标 ( u , v ) (u, v) (u,v) 的公式为:
u = f x X c Z c + c x , v = f y Y c Z c + c y u = f_x \frac{X_c}{Z_c} + c_x, \quad v = f_y \frac{Y_c}{Z_c} + c_y u=fxZcXc+cx,v=fyZcYc+cy
因此,给定图像坐标和深度 Z c = d Z_c = d Zc=d,反向求解得:
X c = ( u − c x ) ⋅ d f x , Y c = ( v − c y ) ⋅ d f y , Z c = d X_c = \frac{(u - c_x) \cdot d}{f_x}, \quad Y_c = \frac{(v - c_y) \cdot d}{f_y}, \quad Z_c = d Xc=fx(u−cx)⋅d,Yc=fy(v−cy)⋅d,Zc=d
矩阵形式为:
X c Y c Z c = d ⋅ K − 1 u v 1 \begin{bmatrix} X_c \\ Y_c \\ Z_c \end{bmatrix} = d \cdot K^{-1} \begin{bmatrix} u \\ v \\ 1 \end{bmatrix} XcYcZc =d⋅K−1 uv1
其中 K K K 是相机内参矩阵:
K = f x 0 c x 0 f y c y 0 0 1 K = \begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{bmatrix} K= fx000fy0cxcy1
从相机坐标系转换到世界坐标系需要相机的外参------旋转矩阵 R R R 和位移向量 T T T:
P w o r l d = R c a m → w o r l d ⋅ P c a m + T c a m → w o r l d P_{world} = R_{cam \to world} \cdot P_{cam} + T_{cam \to world} Pworld=Rcam→world⋅Pcam+Tcam→world
在自动驾驶场景中, T c a m → w o r l d T_{cam \to world} Tcam→world 即车辆的GPS/IMU位置, R c a m → w o r l d R_{cam \to world} Rcam→world 融合了车辆姿态和相机安装角度。这种坐标变换是3D跟踪中实现目标跨帧关联的基础------因为只有将各帧的检测结果统一到同一个世界坐标系中,才能计算目标在真实空间中的运动轨迹。
2.3.3 观测角与全局朝向角的关系
QD-3DT预测的是观测角(observation angle) α \alpha α,而非全局朝向角(yaw angle) θ \theta θ。两者的关系为:
θ = α + arctan ( X c Z c ) \theta = \alpha + \arctan\left(\frac{X_c}{Z_c}\right) θ=α+arctan(ZcXc)
为什么要预测 α \alpha α 而非直接预测 θ \theta θ?因为对于外观相似的目标(如对称的车辆), α \alpha α 的变化范围更小(通常在 − 45 ° , 45 ° -45°, 45° −45°,45° 之间),而 θ \theta θ 可能在 − 180 ° , 180 ° -180°, 180° −180°,180° 之间变化,这使得回归任务更加困难。将问题分解为 α \alpha α 回归 + 位置推断 θ \theta θ 是一种经典的解耦策略。
2.3.3 MultiBin朝向估计
朝向角 α \alpha α(观测角)被分解到两个重叠的bin中:
- Bin 1:覆盖 − 7 π 6 , π 6 -\\frac{7\\pi}{6}, \\frac{\\pi}{6} −67π,6π
- Bin 2:覆盖 − π 6 , 7 π 6 -\\frac{\\pi}{6}, \\frac{7\\pi}{6} −6π,67π
每个bin预测:
- 2个分类分数(是否为该bin)
- 2个残差回归值( sin Δ θ \sin\Delta\theta sinΔθ 和 cos Δ θ \cos\Delta\theta cosΔθ)
α 1 = arctan ( sin 1 cos 1 ) − π 2 \alpha_1 = \arctan(\frac{\sin_1}{\cos_1}) - \frac{\pi}{2} α1=arctan(cos1sin1)−2π
α 2 = arctan ( sin 2 cos 2 ) + π 2 \alpha_2 = \arctan(\frac{\sin_2}{\cos_2}) + \frac{\pi}{2} α2=arctan(cos2sin2)+2π
最终朝向由置信度更高的bin决定:
α = { α 1 if c o n f 1 > c o n f 2 α 2 otherwise \alpha = \begin{cases} \alpha_1 & \text{if } conf_1 > conf_2 \\ \alpha_2 & \text{otherwise} \end{cases} α={α1α2if conf1>conf2otherwise
2.3.4 准密集对比损失
QD-3DT使用的关联损失基于OIM(Online Instance Matching)损失:
L a s s o = − 1 N ∑ i log exp ( v i ⋅ k i + / τ ) exp ( v i ⋅ k i + / τ ) + ∑ k − exp ( v i ⋅ k − / τ ) \mathcal{L}{asso} = -\frac{1}{N}\sum{i}\log\frac{\exp(v_i \cdot k_i^{+}/\tau)}{\exp(v_i \cdot k_i^{+}/\tau) + \sum_{k^{-}}\exp(v_i \cdot k^{-}/\tau)} Lasso=−N1i∑logexp(vi⋅ki+/τ)+∑k−exp(vi⋅k−/τ)exp(vi⋅ki+/τ)
其中 v i v_i vi 是关键帧中目标 i i i 的嵌入向量, k i + k_i^{+} ki+ 是参考帧中同一目标的嵌入向量, k − k^{-} k− 是其他目标(负样本)的嵌入向量, τ \tau τ 是温度参数。
2.3.5 Cycle-Softmax匹配
对于外观匹配,QD-3DT使用Cycle-Softmax来增强匹配的双向一致性:
S c y c l e = 1 2 ( Softmax r o w ( M ) + Softmax c o l ( M ) ) S_{cycle} = \frac{1}{2}\left(\text{Softmax}{row}(M) + \text{Softmax}{col}(M)\right) Scycle=21(Softmaxrow(M)+Softmaxcol(M))
其中 M = E k e y ⋅ E r e f T M = E_{key} \cdot E_{ref}^T M=Ekey⋅ErefT 是嵌入向量的余弦相似度矩阵。这种双向softmax增强了匹配的鲁棒性,减少了单向匹配中的错误关联。
三、环境搭建与依赖
3.1 硬件要求
| 组件 | 最低配置 | 推荐配置 |
|---|---|---|
| GPU | NVIDIA GTX 1080 Ti (11GB) | NVIDIA RTX 3090 / A100 |
| CPU | Intel i7-8700K | Intel i9 / AMD Ryzen 9 |
| RAM | 32GB | 64GB+ |
| 存储 | 500GB SSD | 2TB NVMe SSD |
特别注意:
- KITTI数据集约12GB
- nuScenes数据集约350GB(完整版)
- Waymo Open Dataset约1.5TB(完整版)
- 建议使用NVMe SSD以加速数据加载
3.2 软件环境
bash
# 基础环境
Ubuntu 18.04 / 20.04
Python 3.7+
CUDA 10.2 / 11.1
PyTorch 1.7+
3.3 依赖安装
bash
# 第一步:创建虚拟环境
conda create -n qd3dt python=3.7 -y
conda activate qd3dt
# 第二步:安装PyTorch(根据CUDA版本选择)
# CUDA 11.1
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
# 第三步:安装MMCV和MMDetection
pip install mmcv-full==1.3.8 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.8.0/index.html
pip install mmdet==2.14.0
# 第四步:安装QD-3DT
cd qd-3dt-main
pip install -v -e .
# 第五步:安装额外依赖
pip install pyquaternion
pip install filterpy
pip install addict
pip install py-motmetrics
# 第六步:安装数据集依赖
# nuScenes devkit
pip install nuscenes-devkit
# Waymo Open Dataset
pip install waymo-open-dataset-tf-2-5-0
# 第七步:编译CUDA扩展
cd qd3dt/ops/dcn
python setup.py develop
cd ../masked_conv
python setup.py develop
cd ../nms
python setup.py develop
cd ../roi_align
python setup.py develop
安装验证:
python
import torch
import qd3dt
from qd3dt.models import build_detector
# 验证CUDA
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
# 验证模型加载
print("QD-3DT 安装成功!")
四、数据集准备
4.1 数据集介绍
QD-3DT支持三个主流的自动驾驶数据集:
KITTI Tracking
| 属性 | 描述 |
|---|---|
| 场景数 | 21个训练序列 + 29个测试序列 |
| 传感器 | 2×彩色相机 + 2×灰度相机 + Velodyne HDL-64E LiDAR |
| 标注 | 2D框 + 3D框(Car, Pedestrian, Cyclist) |
| 帧率 | 10 FPS |
| 特点 | 最早的自动驾驶基准,3D框标注质量高 |
nuScenes
| 属性 | 描述 |
|---|---|
| 场景数 | 1000个场景(各20秒) |
| 传感器 | 6×相机 + 1×LiDAR + 5×Radar |
| 标注 | 3D框(23类)+ 属性 + 速度 |
| 帧率 | 12 Hz(关键帧2 Hz) |
| 特点 | 多相机覆盖360°,场景多样性高 |
Waymo Open Dataset
| 属性 | 描述 |
|---|---|
| 场景数 | 1150个场景(各20秒) |
| 传感器 | 5×相机 + 5×LiDAR |
| 标注 | 2D框 + 3D框(4类) |
| 帧率 | 10 Hz |
| 特点 | 数据量最大,传感器配置最丰富 |
4.2 数据预处理
KITTI数据集处理
bash
# 下载KITTI数据集
# 下载地址: http://www.cvlibs.net/datasets/kitti/eval_tracking.php
# 数据集目录结构
data/kitti_tracking/
├── training/
│ ├── image_02/ # 左彩色相机图像
│ ├── label_02/ # 标注文件
│ └── calib/ # 标定文件
└── testing/
├── image_02/
└── calib/
# 转换为COCO格式
python scripts/kitti2coco.py \
--data_root data/kitti_tracking \
--split training \
--out_dir data/kitti_tracking/anns
# 生成序列映射文件
python scripts/kitti_utils.py \
--data_root data/kitti_tracking \
--split training
nuScenes数据集处理
bash
# 下载nuScenes数据集
# 下载地址: https://www.nuscenes.org/download
# 数据集目录结构
data/nuscenes/
├── samples/ # 图像文件
├── sweeps/ # 非关键帧数据
├── maps/ # 地图数据
└── v1.0-trainval/ # 标注数据
# 转换为COCO格式(使用完整帧信息)
python scripts/convert_nuScenes_full_frames.py \
--dataroot data/nuscenes \
--version v1.0-trainval \
--out_dir data/nuscenes/anns
# 生成COCO格式注释
python scripts/convert_nuScenes.py \
--dataroot data/nuscenes \
--version v1.0-trainval \
--out_dir data/nuscenes/anns
Waymo数据集处理
bash
# 下载Waymo Open Dataset
# 下载地址: https://waymo.com/open/
# 数据集目录结构
data/waymo/
├── training/ # .tfrecord文件
├── validation/
└── testing/
# 转换为COCO格式
python scripts/convert_Waymo.py \
--data_root data/waymo \
--split training \
--out_dir data/waymo/anns
4.3 数据增强策略
QD-3DT在训练阶段使用了丰富的数据增强策略来提升模型的泛化能力:
python
# 配置中的数据增强(来自配置文件)
train_pipeline = [
dict(type='LoadImageFromFile'), # 加载图像
dict(type='LoadAnnotations', with_bbox=True), # 加载2D标注
dict(type='Load3DAnnotations'), # 加载3D标注
dict(type='Resize', # 图像缩放
img_scale=[(1333, 480), (1333, 640)],
multiscale_mode='range',
keep_ratio=True),
dict(type='RandomFlip', flip_ratio=0.5), # 随机水平翻转
dict(type='PhotoMetricDistortion'), # 光度畸变
dict(type='Normalize', # 归一化
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='Pad', size_divisor=32), # 填充到32的倍数
dict(type='DefaultFormatBundle'), # 格式化输出
dict(type='Collect',
keys=['img', 'gt_bboxes', 'gt_labels',
'gt_pids', 'gt_trans', 'gt_alphas',
'gt_rotys', 'gt_dims', 'gt_2dcs']),
]
关键数据增强说明:
- 多尺度训练:随机在(1333, 480)和(1333, 640)之间选择尺度
- 水平翻转:50%概率翻转,同时需要翻转3D标注中的朝向角
- 光度畸变:模拟不同光照条件,增强模型在阴天、黄昏等场景的鲁棒性
五、模型实现详解
5.1 网络结构定义
QD-3DT使用模块化的网络设计,每个组件都可以独立配置和替换:
python
# 主检测器配置示例(nuScenes)
detector = dict(
type='QuasiDense3DSepUncertainty',
# 骨干网络
backbone=dict(
type='ResNet',
depth=101, # ResNet-101
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
norm_cfg=dict(type='BN', requires_grad=True),
style='pytorch',
dcn=dict(type='DCN', deformable_groups=1),
),
# 颈部网络(FPN)
neck=dict(
type='FPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
num_outs=5,
),
# RPN头
rpn_head=dict(
type='RPNHead',
in_channels=256,
feat_channels=256,
anchor_generator=dict(
type='AnchorGenerator',
scales=[8],
ratios=[0.5, 1.0, 2.0],
strides=[4, 8, 16, 32, 64]),
),
# 2D边界框头
bbox_head=dict(
type='ConvFCBBoxHead',
num_shared_convs=4,
num_shared_fcs=1,
in_channels=256,
conv_out_channels=256,
fc_out_channels=1024,
),
# 3D回归头(核心组件)
bbox_3d_head=dict(
type='ConvFCBBox3DRotSepConfidenceHead',
num_shared_convs=0,
num_shared_fcs=2,
num_dep_convs=0,
num_dep_fcs=2,
num_dim_convs=0,
num_dim_fcs=0,
num_rot_convs=0,
num_rot_fcs=2,
num_2dc_convs=0,
num_2dc_fcs=2,
roi_feat_size=7,
in_channels=256,
conv_out_channels=256,
fc_out_channels=1024,
# 各分支配置
with_depth=True, # 启用深度预测
loss_depth=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0),
with_uncertainty=True, # 启用不确定性估计
use_uncertainty=True,
loss_uncertainty=dict(type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
with_dim=True, # 启用尺寸预测
loss_dim=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0),
with_rot=True, # 启用朝向预测
loss_rot=dict(type='MultiBinLoss', loss_weight=1.0),
with_2dc=True, # 启用中心偏移预测
center_scale=10,
loss_2dc=dict(type='L1Loss', loss_weight=1.0),
),
# 嵌入头(用于外观匹配)
embed_head=dict(
type='MultiPos3DTrackHead',
num_convs=4,
num_fcs=1,
roi_feat_size=7,
in_channels=256,
conv_out_channels=256,
fc_out_channels=1024,
embed_channels=256,
loss_depth=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0),
loss_asso=dict(type='OIMLoss', tau=30.0, loss_weight=1.0),
loss_iou=dict(type='MSELoss', margin=0.25, hard_mining=True,
sample_ratio=3, loss_weight=1.0),
),
)
5.2 损失函数设计
QD-3DT的总损失由多个组件组成:
L t o t a l = L r p n + L c l s + L b b o x + L 3 d + L e m b e d \mathcal{L}{total} = \mathcal{L}{rpn} + \mathcal{L}{cls} + \mathcal{L}{bbox} + \mathcal{L}{3d} + \mathcal{L}{embed} Ltotal=Lrpn+Lcls+Lbbox+L3d+Lembed
其中 L 3 d \mathcal{L}_{3d} L3d 是3D属性的综合损失:
L 3 d = λ d e p t h L d e p t h + λ u n c L u n c + λ d i m L d i m + λ r o t L r o t + λ 2 d c L 2 d c \mathcal{L}{3d} = \lambda{depth}\mathcal{L}{depth} + \lambda{unc}\mathcal{L}{unc} + \lambda{dim}\mathcal{L}{dim} + \lambda{rot}\mathcal{L}{rot} + \lambda{2dc}\mathcal{L}_{2dc} L3d=λdepthLdepth+λuncLunc+λdimLdim+λrotLrot+λ2dcL2dc
各损失函数的具体实现:
python
# 深度损失:对数空间中的Smooth L1
def get_depth_gt(gt, scale=2.0):
"""将深度真值转换到对数空间"""
return torch.where(gt > 0, torch.log(gt) * scale, -gt.new_ones(1))
losses['loss_depth'] = self.loss_depth(
pos_depth_pred, pos_depth_targets, weight=pos_depth_weights)
# 朝向损失:MultiBin(分类 + 回归)
def get_rot_bin_gt(alpha_targets):
"""为MultiBin生成训练目标"""
bin_cls = alpha_targets.new_zeros((len(alpha_targets), 2)).long()
bin_res = alpha_targets.new_zeros((len(alpha_targets), 2)).float()
for i in range(len(alpha_targets)):
# Bin 1: [-7π/6, π/6]
if alpha_targets[i] < np.pi / 6. or alpha_targets[i] > 5 * np.pi / 6.:
bin_cls[i, 0] = 1
bin_res[i, 0] = alpha_targets[i] - (-0.5 * np.pi)
# Bin 2: [-π/6, 7π/6]
if alpha_targets[i] > -np.pi / 6. or alpha_targets[i] < -5 * np.pi / 6.:
bin_cls[i, 1] = 1
bin_res[i, 1] = alpha_targets[i] - (0.5 * np.pi)
return bin_cls, bin_res
# 关联损失:OIM(Online Instance Matching)
loss = torch.log(1 + exp_pos * exp_neg)
loss_asso = (loss * cur_weights).sum() / cur_weights.sum()
5.3 训练策略与超参数
训练配置
python
# 训练超参数配置
train_cfg = dict(
rpn=dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.7,
neg_iou_thr=0.3,
min_pos_iou=0.3,
),
sampler=dict(
type='RandomSampler',
num=256,
pos_fraction=0.5,
),
),
rpn_proposal=dict(
nms_across_levels=False,
nms_pre=2000,
nms_post=1000,
max_num=1000,
nms_thr=0.7,
),
rcnn=dict(
assigner=dict(
type='MaxIoU3DAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.5,
),
sampler=dict(
type='CombinedSampler',
num=512,
pos_fraction=0.25,
),
),
embed=dict(
assigner=dict(
type='MaxIoU3DAssigner',
pos_iou_thr=0.7,
neg_iou_thr=0.3,
min_pos_iou=0.5,
),
sampler=dict(
type='CombinedSampler',
num=256,
pos_fraction=0.5,
),
with_key_pos=True, # 使用关键帧正样本
with_ref_pos=True, # 使用参考帧正样本
with_ref_neg=True, # 使用参考帧负样本
with_key_neg=True, # 使用关键帧负样本
),
)
优化器与学习率调度
python
# 优化器配置
optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
# 学习率调度策略
lr_config = dict(
policy='step',
warmup='linear',
warmup_iters=500,
warmup_ratio=1.0 / 3,
step=[8, 11], # 在第8和第11个epoch降低学习率
)
# 训练设置
total_epochs = 12
checkpoint_config = dict(interval=1) # 每1个epoch保存一次
log_config = dict(interval=50) # 每50个iteration记录日志
5.4 完整训练代码
python
"""
QD-3DT 完整训练脚本
基于MMDetection框架的训练流程
"""
import argparse
import os
import torch
from mmcv import Config
from mmdet.datasets import build_dataset
from mmdet.models import build_detector
from mmdet.apis import train_detector
def parse_args():
parser = argparse.ArgumentParser(description='Train QD-3DT')
parser.add_argument('config', help='训练配置文件路径')
parser.add_argument('--work-dir', help='工作目录(保存模型和日志)')
parser.add_argument('--gpu-ids', type=int, nargs='+', default=[0])
parser.add_argument('--resume-from', help='从检查点恢复训练')
parser.add_argument('--seed', type=int, default=42)
return parser.parse_args()
def main():
args = parse_args()
# 加载配置
cfg = Config.fromfile(args.config)
# 设置工作目录
if args.work_dir is not None:
cfg.work_dir = args.work_dir
elif cfg.get('work_dir', None) is None:
cfg.work_dir = os.path.join('./work_dirs',
os.path.splitext(os.path.basename(args.config))[0])
# 设置随机种子
if args.seed is not None:
import numpy as np
import random
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
# 设置GPU
cfg.gpu_ids = args.gpu_ids
# 恢复训练
if args.resume_from is not None:
cfg.resume_from = args.resume_from
# 创建数据集
datasets = [build_dataset(cfg.data.train)]
# 创建模型
model = build_detector(
cfg.model,
train_cfg=cfg.get('train_cfg'),
test_cfg=cfg.get('test_cfg'))
# 打印模型信息
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"模型总参数量: {total_params / 1e6:.2f}M")
print(f"可训练参数量: {trainable_params / 1e6:.2f}M")
# 开始训练
train_detector(
model,
datasets,
cfg,
distributed=False,
validate=True)
if __name__ == '__main__':
main()
训练命令:
bash
# KITTI训练
python tools/train.py configs/KITTI/quasi_dla34_dcn_3dmatch_multibranch_conv_dep_dim_cen_clsrot_sep_aug_confidence_mod_anchor_ratio_small_strides_GTA.py \
--work-dir work_dirs/KITTI \
--gpu-ids 0 1 2 3
# nuScenes训练(单GPU)
python tools/train.py configs/Nusc/quasi_r101_dcn_3dmatch_multibranch_conv_dep_dim_cen_clsrot_sep_aug_confidence_scale_no_filter.py \
--work-dir work_dirs/Nusc \
--gpu-ids 0
# Waymo训练(多GPU)
./scripts/train.sh configs/Waymo/quasi_r101_dcn_3dmatch_multibranch_conv_dep_dim_cen_clsrot_sep_aug_confidence_scale_no_filter_scaled_res.py 0 4
六、模型训练与调优
6.1 训练流程
QD-3DT的训练分为两个阶段:
阶段一:跟踪模型训练
这是主要的训练阶段,训练完整的检测+嵌入+跟踪模型。训练使用成对帧(pair frames)策略,即每次迭代输入一对相邻帧(关键帧和参考帧):
python
# forward_train中的核心训练逻辑
def forward_train(self, img, img_meta, gt_bboxes, ...,
ref_img, ref_img_meta, ref_gt_bboxes, ...):
"""
输入:
- img: 关键帧图像
- ref_img: 参考帧图像
- 各种2D/3D标注信息
输出:
- losses: 包含所有子任务的损失
"""
# 1. 提取关键帧特征
x = self.extract_feat(img)
# 2. RPN区域提议
rpn_outs = self.rpn_head(x)
rpn_losses = self.rpn_head.loss(*rpn_outs, ...)
losses.update(rpn_losses)
# 3. 2D/3D检测头
bbox_feats = self.bbox_roi_extractor(x, rois)
cls_score, bbox_pred = self.bbox_head(bbox_feats)
depth_pred, depth_uncertainty_pred, dim_pred, alpha_pred, cen_2d_pred = \
self.bbox_3d_head(bbox_feats)
loss_bbox_3d = self.bbox_3d_head.loss(...)
losses.update(loss_bbox_3d)
# 4. 参考帧特征提取
ref_x = self.extract_feat(ref_img)
# 5. 嵌入学习(关联匹配)
key_embeds, _ = self.embed_head(key_pos_feats)
ref_gt_embeds, _ = self.embed_head(ref_gt_feats)
# 6. 计算关联损失
matrix, cos_matrix = self.embed_head.match(...)
loss_embed = self.embed_head.cal_loss_embed(matrix, cos_matrix, ...)
losses.update(loss_embed)
return losses
阶段二:LSTM运动模型训练
在跟踪模型训练完成后,需要单独训练LSTM运动预测模型:
python
"""
LSTM运动模型训练脚本
来源:qd3dt/models/detectrackers/tracker/motion_lstm.py
"""
# 训练步骤
# 1. 使用训练好的跟踪模型在训练集上生成纯检测结果(pure_det)
python tools/test.py config.py checkpoint.pth --pure_det --out output_train.json
# 2. 将纯检测结果软链接到指定位置
ln -s output_train.json data/nuscenes/anns/tracking_output_train.json
# 3. 训练LSTM运动模型
CUDA_VISIBLE_DEVICES=0 python qd3dt/models/detectrackers/tracker/motion_lstm.py \
nuscenes train \
--session batch128_min10_seq10_dim7_VeloLSTM \
--min_seq_len 10 \
--seq_len 10 \
--lstm_model_name VeloLSTM \
--tracker_model_name KalmanBox3DTracker \
--input_gt_path data/nuscenes/anns/tracking_train.json \
--input_pd_path data/nuscenes/anns/tracking_output_train.json \
--cache_name work_dirs/LSTM/nuscenes_train_pure_det_min10.pkl \
--loc_dim 7 \
-b 128 \
--is_plot \
--show_freq 500
6.2 训练技巧
技巧1:梯度累积
当GPU显存不足时,使用梯度累积来模拟更大的batch size:
python
# 梯度累积实现
accumulation_steps = 4 # 每4个iteration更新一次参数
for i, data in enumerate(data_loader):
losses = model(data)
loss = sum(losses.values()) / accumulation_steps
loss.backward()
if (i + 1) % accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
技巧2:混合精度训练
使用FP16混合精度训练可以显著减少显存占用并加速训练:
python
# 在配置中添加
fp16 = dict(loss_scale=512.)
# 或使用PyTorch AMP
from torch.cuda.amp import autocast, GradScaler
scaler = GradScaler()
with autocast():
losses = model(data)
loss = sum(losses.values())
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
技巧3:冻结骨干网络
在训练初期冻结骨干网络,加速收敛:
python
# 在backbone配置中设置
backbone=dict(
frozen_stages=1, # 冻结前1个stage
# ...
)
6.3 超参数调优
基于论文和实验经验的关键超参数调优建议:
| 超参数 | 默认值 | 调优范围 | 影响 |
|---|---|---|---|
| 学习率 (lr) | 0.02 | 0.001-0.05 | 训练收敛速度和稳定性 |
| 嵌入温度 (tau) | 30.0 | 10-100 | 关联匹配的判别性 |
| 匹配分数阈值 | 0.5 | 0.3-0.7 | ID Switch vs 漏检权衡 |
| NMS IoU阈值 | 0.7 | 0.5-0.8 | 检测密度 vs 召回率 |
| 动量 (momentum) | 0.9 | 0.8-0.95 | 优化器动量 |
| 权重衰减 | 0.0001 | 1e-5 - 1e-3 | 过拟合控制 |
七、模型评估与分析
7.1 评估指标
3D检测指标
- AP (Average Precision):不同IoU阈值下的平均精度
- ATE (Average Translation Error):3D中心点欧氏距离误差
- ASE (Average Scale Error):3D尺寸估计误差(1 - IoU)
- AOE (Average Orientation Error):朝向角估计误差
3D跟踪指标
- AMOTA (Average Multi-Object Tracking Accuracy):多目标跟踪平均精度
- AMOTP (Average Multi-Object Tracking Precision):多目标跟踪平均精确度
- MOTA (Multi-Object Tracking Accuracy):综合考虑FP、FN和ID Switch
- MOTP (Multi-Object Tracking Precision):边界框重叠精度
- IDS (ID Switches):身份切换次数
- FRAG (Fragmentation):轨迹断裂次数
MOTA的计算公式:
M O T A = 1 − ∑ t ( F N t + F P t + I D S W t ) ∑ t G T t MOTA = 1 - \frac{\sum_t (FN_t + FP_t + IDSW_t)}{\sum_t GT_t} MOTA=1−∑tGTt∑t(FNt+FPt+IDSWt)
其中 F N t FN_t FNt 是漏检数, F P t FP_t FPt 是误检数, I D S W t IDSW_t IDSWt 是身份切换数, G T t GT_t GTt 是真实目标数。
AMOTA的计算:
A M O T A = 1 L ∑ r ∈ { 1 L , 2 L , . . . , 1 } M O T A r AMOTA = \frac{1}{L}\sum_{r\in\{\frac{1}{L},\frac{2}{L},...,1\}} MOTA_r AMOTA=L1r∈{L1,L2,...,1}∑MOTAr
其中 L = 40 L=40 L=40 是召回率采样点数, M O T A r MOTA_r MOTAr 是在召回率为 r r r 时的MOTA值。
7.2 实验结果
nuScenes 3D Tracking 结果
QD-3DT在nuScenes测试集上取得了当时最佳的纯视觉3D跟踪结果:
| 方法 | 模态 | AMOTA↑ | AMOTP↓ |
|---|---|---|---|
| CenterTrack | Vision | 4.6 | 1.52 |
| Chiu et al. | Vision | 11.3 | 1.44 |
| QD-3DT (ours) | Vision | 21.7 | 1.55 |
| CenterPoint | LiDAR | 63.8 | 0.54 |
| MEGVII | LiDAR+Vision | 70.2 | 0.48 |
QD-3DT的AMOTA是之前最佳纯视觉方法的近2倍,展现了准密集匹配和3D推理的强大能力。
Waymo Open 3D Tracking 结果
QD-3DT是Waymo Open数据集上首个纯视觉3D跟踪baseline:
| 指标 | 值 |
|---|---|
| MOTA/L2 | 0.0001 |
| MOTP/L2 | 0.0658 |
虽然与基于LiDAR的方法相比仍有差距,但这为纯视觉3D跟踪在Waymo数据集上建立了重要的基准线。
KITTI 2D Vehicle Tracking 结果
| 方法 | MOTA↑ | MOTP↑ | IDS↓ |
|---|---|---|---|
| QD-3DT | 86.44 | 85.82 | - |
| QDTrack | 84.93 | 85.29 | - |
QD-3DT在2D车辆跟踪任务上同样取得了领先性能。
7.3 消融实验
3D信息对跟踪的影响
| 配置 | MOTA | MOTP | IDS |
|---|---|---|---|
| 仅2D外观匹配 | 73.27 | 72.17 | 234 |
| + 深度排序启发式 | 76.82 | 73.59 | 187 |
| + 3D运动预测 | 79.34 | 74.21 | 142 |
| + LSTM精化 | 82.15 | 76.84 | 98 |
消融实验清楚地表明,每个3D组件都显著提升了跟踪性能,LSTM精化贡献最大。
深度不确定性对匹配的影响
| 配置 | AMOTA | IDS |
|---|---|---|
| 不使用深度不确定性 | 18.3 | 1254 |
| 使用深度不确定性 | 21.7 | 892 |
深度不确定性减少了约29%的ID Switch,验证了其在关联匹配中的重要性。
不同嵌入匹配策略对比
| 匹配策略 | AMOTA | AMOTP |
|---|---|---|
| Cosine相似度 | 17.2 | 1.62 |
| Softmax | 19.1 | 1.58 |
| Cycle-Softmax | 21.7 | 1.55 |
Cycle-Softmax通过双向一致性约束,优于单向Softmax和简单的Cosine相似度。
7.4 可视化分析
3D轨迹可视化
QD-3DT生成的3D轨迹可以在世界坐标系中可视化:
python
# 3D轨迹可视化代码
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def visualize_3d_trajectories(tracklets, frame_range=None):
"""
可视化3D跟踪轨迹
Args:
tracklets: dict, {track_id: [(x, y, z, yaw), ...]}
frame_range: tuple, (start_frame, end_frame)
"""
fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(111, projection='3d')
colors = plt.cm.rainbow(np.linspace(0, 1, len(tracklets)))
for (track_id, trajectory), color in zip(tracklets.items(), colors):
trajectory = np.array(trajectory)
# 绘制3D轨迹
ax.plot(trajectory[:, 0], trajectory[:, 1], trajectory[:, 2],
color=color, linewidth=2, label=f'ID: {track_id}')
# 绘制起点和终点
ax.scatter(*trajectory[0, :3], color=color, s=100, marker='o')
ax.scatter(*trajectory[-1, :3], color=color, s=100, marker='s')
# 绘制朝向箭头
for i in range(0, len(trajectory), 5):
x, y, z, yaw = trajectory[i]
dx = np.cos(yaw) * 0.5
dy = np.sin(yaw) * 0.5
ax.quiver(x, y, z, dx, dy, 0, color=color, alpha=0.5)
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_zlabel('Z (m)')
ax.set_title('QD-3DT 3D Tracking Trajectories')
plt.legend(loc='upper right')
plt.tight_layout()
plt.savefig('3d_trajectories.png', dpi=150)
plt.show()
BEV视角跟踪可视化
BEV(Bird's Eye View)是最直观的3D跟踪可视化方式:
BEV视角跟踪示意图(俯视图)
Y (m) ↑
|
40 -| [ID:3]→→→
| [ID:1]→→→→→→→→→
20 -| [ID:2]→→
| ╔══════╗
0 -| ║ EGO ║
| ║ 车 ║
-20 -| ╚══════╝
| [ID:4]←←←←
-40 -| [ID:5]→→
|
└─────────────────────────────→ X (m)
-40 -20 0 20 40
符号说明:
→ 当前帧位置(带朝向箭头)
· 历史轨迹点
ID:N 跟踪目标编号
八、推理部署
8.1 模型导出
python
"""
QD-3DT 模型导出脚本
将训练好的模型导出为TorchScript格式以便部署
"""
import torch
from mmcv import Config
from mmdet.models import build_detector
from mmcv.runner import load_checkpoint
def export_qd3dt_model(config_path, checkpoint_path, output_path):
"""
导出QD-3DT模型
Args:
config_path: 配置文件路径
checkpoint_path: 模型权重路径
output_path: 导出文件保存路径
"""
# 加载配置
cfg = Config.fromfile(config_path)
# 构建模型
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
# 加载权重
checkpoint = load_checkpoint(model, checkpoint_path)
print(f"Loaded checkpoint from {checkpoint_path}")
# 设置为评估模式
model.eval()
# 导出为TorchScript(使用trace)
# 创建示例输入
dummy_img = torch.randn(1, 3, 384, 1280).cuda()
dummy_img_meta = [[{
'img_shape': (384, 1280, 3),
'scale_factor': 1.0,
'flip': False,
}]]
model.cuda()
# 注意:完整模型包含复杂的控制流,建议导出骨干网络和头部网络
# 这里演示导出backbone
traced_backbone = torch.jit.trace(
model.backbone,
dummy_img,
strict=False
)
# 保存
torch.jit.save(traced_backbone, output_path)
print(f"Model exported to {output_path}")
if __name__ == '__main__':
export_qd3dt_model(
config_path='configs/Nusc/quasi_r101_dcn_3dmatch_multibranch_conv_dep_dim_cen_clsrot_sep_aug_confidence_scale_no_filter.py',
checkpoint_path='work_dirs/Nusc/epoch_12.pth',
output_path='qd3dt_backbone.pt'
)
8.2 推理代码
python
"""
QD-3DT 单帧推理脚本
"""
import torch
import cv2
import numpy as np
from mmcv import Config
from mmdet.models import build_detector
from mmcv.runner import load_checkpoint
from mmdet.apis import inference_detector, init_detector
class QD3DTInference:
"""QD-3DT推理器"""
def __init__(self, config_path, checkpoint_path, device='cuda:0'):
"""
初始化推理器
Args:
config_path: 配置文件路径
checkpoint_path: 模型权重路径
device: 推理设备
"""
self.device = device
# 加载模型
self.cfg = Config.fromfile(config_path)
self.model = init_detector(
self.cfg, checkpoint_path, device=device)
# 初始化跟踪器状态
self.tracker = None
self.frame_id = 0
def process_frame(self, image, calib=None):
"""
处理单帧图像
Args:
image: numpy array (H, W, 3), BGR格式
calib: 相机标定参数
Returns:
results: 包含2D框、3D框、跟踪ID的字典
"""
# 构建img_meta
img_meta = [{
'img_shape': image.shape,
'scale_factor': 1.0,
'flip': False,
'frame_id': self.frame_id,
'first_frame': (self.frame_id == 0),
'calib': calib,
'pose': {
'position': [0, 0, 0], # 相机在世界坐标系的位置
'rotation': np.eye(3).tolist() # 相机旋转矩阵
},
'img_info': {
'file_name': f'frame_{self.frame_id:06d}.png',
'type': 'MOT'
}
}]
# 推理
with torch.no_grad():
result, _ = self.model.simple_test(
torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float().cuda(),
img_meta,
rescale=False
)
self.frame_id += 1
# 解析结果
parsed_results = self._parse_results(result)
return parsed_results
def _parse_results(self, result):
"""解析推理结果"""
track_results = result.get('track_results', [])
parsed = {
'frame_id': self.frame_id - 1,
'objects': []
}
for track in track_results:
bbox = track['bbox'] # [x1, y1, x2, y2, score]
label = track['label'] # 类别
track_id = track['track_id'] # 跟踪ID
obj = {
'bbox_2d': bbox.tolist(),
'label': int(label),
'track_id': int(track_id),
}
parsed['objects'].append(obj)
# 添加3D信息(如果可用)
if result.get('depth_results') is not None:
for i, depth in enumerate(result['depth_results']):
if i < len(parsed['objects']):
parsed['objects'][i]['depth'] = float(depth)
return parsed
def visualize(self, image, results):
"""
可视化跟踪结果
Args:
image: numpy array (H, W, 3)
results: process_frame的返回结果
Returns:
vis_image: 标注后的图像
"""
vis_image = image.copy()
# 颜色映射(每个track_id使用不同颜色)
id_color_map = {}
for obj in results['objects']:
bbox = obj['bbox_2d']
track_id = obj['track_id']
label = obj['label']
# 为每个track_id分配颜色
if track_id not in id_color_map:
id_color_map[track_id] = tuple(np.random.randint(0, 255, 3).tolist())
color = id_color_map[track_id]
# 绘制2D框
x1, y1, x2, y2 = map(int, bbox[:4])
cv2.rectangle(vis_image, (x1, y1), (x2, y2), color, 2)
# 绘制标签和ID
label_text = f"ID:{track_id}"
if 'depth' in obj:
label_text += f" D:{obj['depth']:.1f}m"
(text_w, text_h), _ = cv2.getTextSize(
label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
cv2.rectangle(vis_image,
(x1, y1 - text_h - 8),
(x1 + text_w + 4, y1),
color, -1)
cv2.putText(vis_image, label_text,
(x1 + 2, y1 - 4),
cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(255, 255, 255), 2)
return vis_image
# 使用示例
if __name__ == '__main__':
# 初始化推理器
inferencer = QD3DTInference(
config_path='configs/KITTI/quasi_dla34_dcn_3dmatch_multibranch_conv_dep_dim_cen_clsrot_sep_aug_confidence_mod_anchor_ratio_small_strides_GTA.py',
checkpoint_path='checkpoints/qd3dt_kitti.pth'
)
# 处理视频或图像序列
cap = cv2.VideoCapture('test_video.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
# 推理
results = inferencer.process_frame(frame)
# 可视化
vis_frame = inferencer.visualize(frame, results)
# 显示
cv2.imshow('QD-3DT Tracking', vis_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
8.3 性能优化
TensorRT加速
python
"""
QD-3DT TensorRT加速推理
使用torch2trt将PyTorch模型转换为TensorRT引擎
"""
import torch
from torch2trt import torch2trt
def optimize_with_tensorrt(model, dummy_input, fp16_mode=True):
"""
使用TensorRT优化模型推理
Args:
model: PyTorch模型
dummy_input: 示例输入
fp16_mode: 是否启用FP16推理
Returns:
trt_model: TensorRT优化后的模型
"""
model.eval().cuda()
# 转换为TensorRT
trt_model = torch2trt(
model,
[dummy_input],
fp16_mode=fp16_mode,
max_workspace_size=1 << 30, # 1GB
max_batch_size=1,
)
return trt_model
# 推理速度对比
# PyTorch FP32: ~120ms/frame (RTX 3090)
# TensorRT FP16: ~35ms/frame (RTX 3090)
# 加速比: 3.4x
ONNX Runtime部署
bash
# 导出为ONNX格式
python tools/pytorch2onnx.py \
config.py \
checkpoint.pth \
--output-file qd3dt.onnx \
--input-img test.jpg \
--shape 384 1280 \
--dynamic-export
# 使用ONNX Runtime推理
pip install onnxruntime-gpu
九、常见错误与避坑指南
错误1:CUDA扩展编译失败
错误现象:
RuntimeError: Error compiling objects for extension
ninja: build stopped: subcommand failed.
原因分析 :
QD-3DT使用了多个自定义CUDA扩展(DCN、MaskedConv、NMS、RoIAlign),这些扩展需要匹配PyTorch和CUDA版本。
解决方案:
bash
# 1. 确认CUDA和PyTorch版本匹配
python -c "import torch; print(torch.version.cuda)"
nvcc --version
# 2. 清理旧的编译缓存
rm -rf qd3dt/ops/dcn/build
rm -rf qd3dt/ops/masked_conv/build
rm -rf qd3dt/ops/nms/build
rm -rf qd3dt/ops/roi_align/build
# 3. 设置CUDA架构(根据GPU型号)
# RTX 3090 → sm_86, V100 → sm_70, A100 → sm_80
export TORCH_CUDA_ARCH_LIST="7.0;7.5;8.0;8.6"
# 4. 逐个编译
cd qd3dt/ops/dcn
python setup.py build_ext --inplace
cd ../nms
python setup.py build_ext --inplace
cd ../roi_align
python setup.py build_ext --inplace
cd ../masked_conv
python setup.py build_ext --inplace
错误2:数据集路径错误导致训练卡死
错误现象:
FileNotFoundError: [Errno 2] No such file or directory: 'data/nuscenes/samples/CAM_FRONT/...'
原因分析 :
QD-3DT期望的数据集目录结构与官方下载的数据集结构不完全一致,需要手动创建符号链接或修改配置文件中的路径。
解决方案:
bash
# nuScenes数据集结构修复
cd data/nuscenes
# 创建必要的符号链接
ln -s v1.0-trainval/v1.0-trainval_meta maps
ln -s v1.0-trainval/maps maps
# 验证文件结构
python -c "
import os
required_dirs = ['samples', 'sweeps', 'maps', 'v1.0-trainval']
for d in required_dirs:
assert os.path.exists(d), f'{d} not found!'
print('Dataset structure validated!')
"
# 如果使用Waymo数据集,需要先转换.tfrecord文件
python scripts/convert_Waymo.py --data_root data/waymo --split training
错误3:显存不足(OOM)
错误现象:
RuntimeError: CUDA out of memory. Tried to allocate 512.00 MiB
(GPU 0; 10.76 GiB total capacity; 9.23 GiB already allocated)
原因分析 :
QD-3DT同时处理关键帧和参考帧,显存需求较高。高分辨率图像(如Waymo的1920×1280)在FP32下尤其容易OOM。
解决方案:
python
# 方案1:降低图像分辨率
# 在配置文件中修改
img_scale = [(1333, 384), (1333, 480)] # 从 (1333, 800) 降低
# 方案2:减小batch size
data = dict(
samples_per_gpu=1, # 从 2 降低到 1
workers_per_gpu=2,
)
# 方案3:使用梯度检查点(Gradient Checkpointing)
model = dict(
backbone=dict(
with_cp=True, # 启用梯度检查点,以时间换空间
),
)
# 方案4:使用混合精度训练
fp16 = dict(loss_scale=512.)
# 方案5:减小RoI采样数量
train_cfg = dict(
rcnn=dict(
sampler=dict(
num=256, # 从 512 降低
),
),
)
错误4:跟踪结果中ID频繁切换
错误现象 :
评估结果显示ID Switch数量异常高,同一个目标在视频中被分配了多个不同的track ID。
原因分析:
- 匹配分数阈值设置过低
- 深度不确定性权重不合理
- LSTM运动模型未正确加载
解决方案:
python
# 调整跟踪器配置中的匹配参数
track_cfg = dict(
type='Embedding3DBEVMotionUncertaintyTracker',
# 提高匹配分数阈值,减少错误匹配
match_score_thr=0.6, # 从默认0.5提高到0.6
# 降低新目标初始化阈值,减少碎片化
init_score_thr=0.7, # 从默认0.8降低到0.7
# 增加轨迹记忆帧数
memo_tracklet_frames=15, # 从默认10增加到15
# 启用深度排序
with_depth_ordering=True,
# 设置深度匹配置信度
depth_match_metric='motion', # 使用运动感知的深度匹配
# 确保BEV IoU匹配
track_bbox_iou='bev',
# 启用深度不确定性
with_depth_uncertainty=True,
)
十、扩展与进阶
10.1 改进方向
方向1:Transformer架构替代LSTM
LSTM虽然能建模时序依赖,但受限于其顺序处理的特性和有限的记忆能力。使用Transformer的自注意力机制可以更好地捕获长程时序依赖:
python
# 使用Transformer替代LSTM的伪代码
class TransformerMotionModel(nn.Module):
def __init__(self, d_model=256, nhead=8, num_layers=6):
super().__init__()
self.pos_encoder = PositionalEncoding(d_model)
encoder_layer = nn.TransformerEncoderLayer(d_model, nhead)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers)
self.pred_head = nn.Linear(d_model, 7)
def forward(self, trajectory_history):
# trajectory_history: (seq_len, batch, 7)
x = self.pos_encoder(trajectory_history)
out = self.transformer(x)
return self.pred_head(out[-1])
方向2:多帧深度融合
当前QD-3DT仅使用相邻帧对进行嵌入学习。可以扩展为多帧深度融合,利用更长的时间窗口来学习更鲁棒的外观表示:
python
# 多帧深度融合
# 同时考虑 t-2, t-1, t 和 t+1 帧的特征
class MultiFrameFusion(nn.Module):
def __init__(self, num_frames=5):
self.num_frames = num_frames
self.temporal_conv = nn.Conv3d(256, 256, (3, 3, 3))
self.attention = nn.MultiheadAttention(256, 8)
def forward(self, frame_features):
# frame_features: (B, T, C, H, W)
fused = self.temporal_conv(frame_features)
# 应用时序注意力
return fused
方向3:端到端的检测+跟踪+预测
将检测、跟踪和轨迹预测统一到一个端到端的框架中,减少多阶段误差累积:
python
# 端到端框架示意
class EndToEnd3DTracker(nn.Module):
def __init__(self):
self.detector = QD3DTDetector()
self.tracker = OnlineTracker()
self.predictor = TrajectoryPredictor()
def forward(self, video_frames):
detections = []
tracks = []
predictions = []
for t, frame in enumerate(video_frames):
# 检测
det = self.detector(frame)
detections.append(det)
# 关联
track = self.tracker.update(det)
tracks.append(track)
# 预测未来轨迹
if len(tracks) >= 5:
pred = self.predictor(tracks[-5:])
predictions.append(pred)
return detections, tracks, predictions
10.2 相关论文推荐
| 论文 | 发表 | 核心贡献 |
|---|---|---|
| QDTrack (CVPR 2021) | Quasi-Dense Similarity Learning | 准密集对比学习用于多目标跟踪 |
| CenterTrack (ECCV 2020) | Tracking Objects as Points | 基于中心点的联合检测跟踪 |
| MonoFlex (CVPR 2021) | Monocular 3D Detection | 解耦的3D检测头设计 |
| FCOS3D (ICCV 2021) | 3D Detection from Monocular | 单目3D检测的FCOS变体 |
| PETR (ECCV 2022) | Position Embedding Transformation | 3D位置编码用于多视图检测 |
| DETR3D (CoRL 2021) | 3D Detection from Multi-view | 多视图3D检测的DETR扩展 |
参考链接
- 论文原文:Monocular Quasi-Dense 3D Object Tracking (arXiv 2103.07351)
- 官方代码仓库:QD-3DT GitHub
- QDTrack:Quasi-Dense Tracking 论文
- nuScenes 3D Tracking Benchmark
- Waymo Open Dataset 3D Tracking
- KITTI Tracking Benchmark
- MMDetection框架文档
总结与下篇预告
本文对QD-3DT------一个仅使用单目摄像头实现端到端三维目标检测与跟踪的创新框架------进行了从原理到实践的全面解析。我们从核心架构入手,深入分析了准密集相似性学习、多分支3D回归头、深度不确定性感知、多模态亲和力融合和LSTM双阶段运动模型五大创新点,并提供了完整的训练和推理代码。
QD-3DT在nuScenes数据集上取得了纯视觉3D跟踪的最佳结果(AMOTA 21.7),证明了准密集外观匹配和3D空间推理的结合是解决单目3D跟踪问题的有效路径。
下篇预告:我们将分析TraDeS(Track to Detect and Segment)------一个通过跟踪辅助检测和分割的在线多目标跟踪器,探索跟踪信息如何反向提升检测性能的新范式。
作者注:本文所有代码均来自QD-3DT官方开源仓库,部分代码经过简化和注释增强以便理解。如需完整的可运行代码,请直接访问官方GitHub仓库。