一个开源项目,致力于解决端到端模型在自动驾驶和机器人控制中的核心挑战
An open-source project dedicated to solving core challenges of end-to-end models in autonomous driving and robot control
An open-source initiative tackling the core challenges of end-to-end models in autonomous driving and robotics control. DSD (Deep Self Drive) draws inspiration from comma.ai's groundbreaking bodyjim model while pushing the boundaries of what's possible.
一个致力于攻克自动驾驶与机器人控制领域端到端模型核心挑战的开源项目。该项目不仅汲取了comma.ai突破性的bodyjim模型灵感,更在探索技术可能性的道路上不断突破边界。
| 中文 | English | 说明 |
|---|---|---|
| 端到端模型 | End-to-End Model | 直接从输入到输出的完整模型 |
| 模型预测控制 | Model Predictive Control (MPC) | 使用系统模型预测未来并优化控制输入 |
| 模仿学习 | Imitation Learning | 通过模仿专家行为来学习策略 |
| 滚动优化 | Receding Horizon Control | 每步只执行最优控制序列的第一个动作 |
| 知识蒸馏 | Knowledge Distillation | 用大模型(教师)指导小模型(学生)训练 |
| 轨迹 | Trajectory | 一系列状态-动作序列 |
| 动作空间 | Action Space | 模型可选的动作集合(9个离散动作) |
| 预测时域 | Prediction Horizon | 一次预测的未来步数(16步) |
| 损失函数 | Loss Function | 衡量预测与目标差距的指标 |
| 类别不平衡 | Class Imbalance | 不同类别的样本数量差异很大 |
DSD (Deep Self Drive) 的灵感来自comma.ai的bodyjim,DSD是一个开源协作项目 (Open-Source Collaborative Project),目标是:
让端到端(End-to-End)模型真正学会"思考未来",实现具备MPC(模型预测控制 / Model Predictive Control)能力的自主决策系统
Enable end-to-end models to truly "think about the future" and achieve autonomous decision-making systems with MPC capabilities
Comma.ai成功实现了一个~90M参数的vanilla GPT模型,通过以下方式实现了端到端控制:
- 数据收集: 使用bodyjim在办公室内驾驶,收集了约3小时的(image, action)对
- 模型训练: 自回归预测下一个VQGAN tokenized视频帧、轮速和动作
- Plan Head: 添加MLP head预测未来动作计划(受Aloha架构启发)
- 学习成果: 模型学会了墙壁、物体的存在,基本动力学,以及细微行为(如停止时的前倾)
三种使用模式:
- Simulator Mode: 预测下一个观测
(o_{t-k}, a_{t-k}), ..., (o_t, a_t) → o_{t+1} - Policy Mode: 输出当前动作
(o_{t-k}, a_{t-k}), ..., (o_t, ) → a_t - Rollout Mode: 想象完整序列
(o_{t-k}, a_{t-k}), ..., (o_t, a_t) → (o_{t+1}, a_{t+1})
详细对比: 查看 29_Comma.ai实现与DSD对比.md
虽然Comma.ai的bodyjim已经成功实现了端到端控制,但DSD的目标是进一步探索和突破:
Comma.ai的成功 ✅:
- 通过自回归学习隐式世界模型
- 简单有效的MLP Plan Head
- 大量高质量数据驱动
DSD的探索方向 🚀:
- 显式MPC设计,提供更强的控制能力
- 物理约束明确,可解释性更强
- 开源协作,共同解决挑战
当前挑战
- 模型仍然倾向于预测简单动作
- 动作多样性极低(经常16步都是同一个动作)
- 预测准确率:0-25%(远低于预期)
DSD的目标是 (DSD's Goal):让GPT-2这样的Transformer模型,通过精心设计的plan_head和损失函数 (loss function),真正具备**MPC(Model Predictive Control / 模型预测控制)**能力,能够:
- ✅ 预测未来16步的动作序列 (Predict 16-step future action sequences)
- ✅ 考虑动作的物理约束和一致性 (Consider physical constraints and consistency of actions)
- ✅ 实现滚动优化(Receding Horizon Control / 滚动时域控制)(Implement receding horizon control)
DSD/
├── bodyjim/ # Comma.ai bodyjim环境接口
├── collected_data/ # 训练数据目录
│ ├── train/ # 训练集轨迹文件
│ └── val/ # 验证集轨迹文件
├── training/ # 训练代码和模型
│ ├── models/ # GPT-2 + Plan Head模型
│ │ ├── gpt2_commaai_style.py # 方案1: 从ONNX复现
│ │ ├── gpt2_optimized_mpc.py # 方案2&3: MPC优化版本
│ │ └── onnx_distillation.py # 方案3: 知识蒸馏
│ ├── train_optimized_mpc.py # 主训练脚本
│ └── docs/优化MPC文档/ # 详细技术文档
└── models/ # 预训练模型(gpt2.onnx等)
输入 (Input):
- 过去16帧的视觉历史(每帧160个图像tokens / Visual history of past 16 frames, 160 image tokens per frame)
- 轮速信息(左右轮速度,每帧2个tokens / Wheel speed information, 2 tokens per frame)
- 历史动作序列(动作ID 0-8 / Historical action sequence, action IDs 0-8)
输出 (Output):
- 未来16步的动作分布(每步9个动作类别的logits / Action distribution for 16 future steps, logits for 9 action classes per step)
- 通过
plan_head实现MPC功能 (MPC functionality implemented throughplan_head)
注: Comma.ai使用简单的MLP Plan Head就成功了,但我们尝试了更复杂的MPC设计,目前仍在探索中。
Note: Comma.ai succeeded with a simple MLP Plan Head, but we're exploring more complex MPC designs and still facing challenges.
目标 (Goal):直接复现Comma.ai的模型架构 (Directly reproduce Comma.ai's model architecture)
结果 (Result):❌ 失败 (Failed)
- 模型只能学习到简单动作 (Model can only learn simple actions)
- 无法学习复杂的动作序列 (Cannot learn complex action sequences)
- 预测准确率极低 (Very low prediction accuracy)
目标 (Goal):设计可微分的MPC Plan Head,让GPT-2具备预测控制能力 (Design differentiable MPC Plan Head to enable predictive control in GPT-2)
架构 (Architecture):
GPT2ImplicitDynamicsPlanHead:利用GPT-2的隐式动态学习 (Leverage GPT-2's implicit dynamics learning)ImplicitMPCLoss:多组件损失(tracking + consistency + smoothness / Multi-component loss)
结果 (Result):❌ 部分失败 (Partially Failed)
- 模型仍然倾向于预测简单动作 (Model still tends to predict simple actions)
- 动作多样性极低(经常16步都是同一个动作 / Very low action diversity, often all 16 steps are the same action)
- 预测准确率:0-25%(远低于预期 / Prediction accuracy: 0-25%, far below expectation)
目标 (Goal):通过蒸馏预训练模型来训练我们自己的模型 (Train our own model by distilling from pretrained model)
结果 (Result):❌ 数据同步问题 (Data Synchronization Issues)
- 数据流同步存在技术障碍 (Technical obstacles in data stream synchronization)
- 尚未完全解决 (Not fully resolved)
基于最新的训练日志(Epoch 17-18 / Based on latest training logs),我们发现了以下关键问题 (Key Issues):
预测准确率: 0-25% (远低于预期 / Far below expectation)
- 模型几乎没有学到正确的动作映射 (Model has barely learned correct action mapping)
- 即使有高置信度(>0.9),预测仍然是错误的 (Even with high confidence (>0.9), predictions are still wrong)
唯一动作数: 1-3 / 16 (理想应该是8-9 / Ideally 8-9)
预测序列: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
- 模型经常预测所有16步都是同一个动作(如动作4 "-" / Model often predicts all 16 steps as the same action, e.g., action 4 "-")
- 缺乏时序多样性 (Lack of temporal diversity)
真实分布: W(68.75%), W+D(25%), W+A(6.25%)
预测分布: W(56.25%), D(43.75%), 其他(0% / Others 0%)
- 模型倾向于预测少数几个动作(W, D, 动作4 / Model tends to predict only a few actions)
- 无法学习到真实数据中的动作分布 (Cannot learn the action distribution in real data)
情况1: 置信度很低 (<0.3) → 模型不确定 (Case 1: Low confidence → Model uncertain)
情况2: 置信度很高 (>0.9) → 但预测完全错误 (Case 2: High confidence → But prediction completely wrong)
- 模型要么过于保守,要么过于自信但错误 (Model is either too conservative or overconfident but wrong)
总损失: 0.5-0.6 (看起来正常 / Looks normal)
但预测准确率: 0-25% (极低 / Very low)
tracking_loss可能权重不够 (Tracking loss weight may be insufficient)consistency_loss和smoothness_loss可能干扰了学习 (Consistency and smoothness losses may interfere with learning)- 类别不平衡问题(某些动作被过度预测 / Class imbalance issue, some actions over-predicted)
基于问题分析,我们提出以下研究方向 (Research Directions):
- 调整损失权重(增加
tracking_loss权重 / Adjust loss weights, increasetracking_lossweight) - 重新设计
consistency_loss(可能过于严格 / Redesignconsistency_loss, may be too strict) - 改进类别不平衡处理(Focal Loss参数调优 / Improve class imbalance handling, Focal Loss parameter tuning)
- 尝试新的损失函数(如Huber Loss、Label Smoothing增强 / Try new loss functions, e.g., Huber Loss, enhanced Label Smoothing)
- 增加模型容量(更大的Plan Head / Increase model capacity, larger Plan Head)
- 改进动作约束网络(
action_constraint可能过于限制 / Improve action constraint network, may be too restrictive) - 尝试不同的注意力机制 (Try different attention mechanisms)
- 添加残差连接和层归一化 (Add residual connections and layer normalization)
- 课程学习(Curriculum Learning):从简单到复杂 (From simple to complex)
- 数据增强:增加动作多样性 (Data augmentation: increase action diversity)
- 更好的学习率调度 (Better learning rate scheduling)
- 混合精度训练优化 (Mixed precision training optimization)
- 检查数据标注质量 (Check data annotation quality)
- 分析动作分布(是否存在标注偏差 / Analyze action distribution, check for annotation bias)
- 数据预处理优化 (Data preprocessing optimization)
- 解决数据同步问题 (Resolve data synchronization issues)
- 改进蒸馏损失函数 (Improve distillation loss function)
- 温度调度优化 (Temperature scheduling optimization)
-
损失函数专家 (Loss Function Expert) 🎯
- 帮助重新设计MPC损失函数 (Help redesign MPC loss function)
- 解决类别不平衡问题 (Solve class imbalance issues)
- 优化多目标损失权重 (Optimize multi-objective loss weights)
-
模型架构师 (Model Architect) 🏗️
- 改进Plan Head设计 (Improve Plan Head design)
- 优化Transformer架构 (Optimize Transformer architecture)
- 探索新的注意力机制 (Explore new attention mechanisms)
-
训练策略专家 (Training Strategy Expert) 📈
- 设计课程学习策略 (Design curriculum learning strategies)
- 优化学习率调度 (Optimize learning rate scheduling)
- 改进数据增强方法 (Improve data augmentation methods)
-
MPC控制理论专家 (MPC Control Theory Expert) 🧮
- 验证MPC数学设计的正确性 (Verify correctness of MPC mathematical design)
- 提供控制理论指导 (Provide control theory guidance)
- 优化约束条件 (Optimize constraint conditions)
-
数据科学家 (Data Scientist) 📊
- 分析训练数据和标注质量 (Analyze training data and annotation quality)
- 诊断数据分布问题 (Diagnose data distribution issues)
- 设计数据增强策略 (Design data augmentation strategies)
-
Fork & Clone
git clone https://github.com/bccw2021/DSD.git cd DSD -
查看详细文档 (View Detailed Documentation)
cd training/docs/优化MPC文档 # 查看 08_MPC算法设计.md 了解技术细节 # View 08_MPC算法设计.md for technical details
-
运行训练脚本 (Run Training Script)
cd training bash training_optimized_mpc.sh -
提交Issue (Submit Issues)
- 报告bug (Report bugs)
- 提出改进建议 (Suggest improvements)
- 分享实验结果 (Share experimental results)
-
提交Pull Request (Submit Pull Requests)
- 修复问题 (Fix issues)
- 实现新功能 (Implement new features)
- 改进文档 (Improve documentation)
详细的技术文档位于 training/docs/优化MPC文档/ (Detailed technical documentation is located at):
- 08_MPC算法设计.md - MPC算法完整设计文档 (Complete MPC algorithm design document)
- 24_方案3增强隐式MPC迁移总结.md - 方案3技术细节 (Approach 3 technical details)
- 29_Comma.ai实现与DSD对比.md - Comma.ai实现分析与对比 ⭐ NEW
- 26_训练日志问题分析.md - 当前问题详细分析
- 00_目录索引.md - 完整文档索引 (Complete documentation index)
- 27_快速参考卡片索引.md - 快速参考导航 (Quick reference navigation)
- 快速参考/ - 所有快速参考卡片目录 (All quick reference cards)
# Python 3.8+
pip install torch torchvision torchaudio
pip install transformers
pip install numpy scipy
pip install tqdm tensorboard
pip install bodyjim # Comma.ai bodyjim环境 (Comma.ai bodyjim environment)- 模型 (Model): GPT-2 (12层, 768维 / 12 layers, 768 dim) + ImplicitMPC Plan Head
- 数据 (Data): 69条训练轨迹 (17,309样本 / 69 training trajectories, 17,309 samples), 24条验证轨迹 (5,968样本 / 24 validation trajectories, 5,968 samples)
- Batch Size: 16 (每GPU / per GPU) × 2 GPU × 32梯度累积 (gradient accumulation) = 1024有效batch (effective batch)
- 学习率 (Learning Rate): 1e-4 (带warmup和cosine decay / with warmup and cosine decay)
- 损失函数 (Loss Function): Tracking Loss + Consistency Loss (0.3×) + Smoothness Loss (0.2×)
- 验证准确率 (Validation Accuracy): 0-25% (目标: >80% / Target: >80%)
- 动作多样性 (Action Diversity): 1-3/16 (目标: 8-9/16 / Target: 8-9/16)
- 损失值 (Loss Value): 0.5-0.6 (下降缓慢 / Slow decrease)
MPC的核心思想是 (The core idea of MPC is):
"在每个时刻t,使用系统模型预测未来一段时间(预测时域H)内的状态变化和控制输入对系统的影响,然后通过优化代价函数来求得最优控制序列。"
"At each time t, use the system model to predict the impact of state changes and control inputs on the system over a future period (prediction horizon H), then obtain the optimal control sequence by optimizing the cost function."
数学表达 (Mathematical Expression):
J = Σ_{k=0}^{H-1} ℓ(x_{t+k}, u_{t+k}) + ℓ_f(x_{t+H})
我们的实现 (Our Implementation):
- 状态转移 (State Transition):GPT-2隐式学习
x_{t+1} = f(x_t, u_t)(GPT-2 implicit learning) - 代价函数 (Cost Function):
tracking_loss + consistency_loss + smoothness_loss - 预测时域 (Prediction Horizon):H = 16步 (16 steps)
详细理论见 (Detailed theory see) 08_MPC算法设计.md
-
代码规范 (Code Standards)
- 遵循PEP 8 (Follow PEP 8)
- 添加类型注解 (Add type annotations)
- 编写文档字符串 (Write docstrings)
-
提交规范 (Commit Standards)
- 清晰的commit message (Clear commit messages)
- 关联Issue编号 (Link Issue numbers)
- 添加测试(如适用 / Add tests if applicable)
-
实验报告 (Experimental Reports)
- 记录实验配置 (Record experimental configuration)
- 保存训练日志 (Save training logs)
- 分享实验结果 (Share experimental results)
本项目采用 MIT License (This project uses MIT License)
- Comma.ai - 提供了
bodyjim环境、gpt2.onnx模型和实现思路 (Providedbodyjimenvironment,gpt2.onnxmodel, and implementation insights)- 他们的成功证明了GPT模型在机器人控制中的潜力
- DSD项目基于他们的工作,尝试进一步探索MPC能力
- 所有贡献者 (All Contributors) - 感谢每一位参与开源协作的朋友 (Thanks to everyone who participates in open-source collaboration)
- GitHub Issues: 提交问题 (Submit Issues)
- Discussions: 参与讨论 (Join Discussions)
如果这个项目对你有帮助,请给我们一个⭐️!
If this project helps you, please give us a ⭐️!
让我们一起解决端到端模型在自动驾驶和机器人控制中的核心挑战! 🚀
Let's solve the core challenges of end-to-end models in autonomous driving and robot control together! 🚀