Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚗 DSD: Deep Self Drive

一个开源项目,致力于解决端到端模型在自动驾驶和机器人控制中的核心挑战
An open-source project dedicated to solving core challenges of end-to-end models in autonomous driving and robot control

License: MIT Python 3.8+ PyTorch

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模型灵感,更在探索技术可能性的道路上不断突破边界。


📖 术语表 (Glossary)

中文 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 不同类别的样本数量差异很大

🎯 项目愿景 (Project Vision)

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 Bodyjim (About Comma.ai Bodyjim)

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

为什么需要DSD?(Why DSD?)

虽然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)

🏗️ 项目架构 (Project Architecture)

核心组件 (Core Components)

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等)

模型设计 (Model Design)

输入 (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 through plan_head)

🚧 当前状态:我们遇到了挑战 (Current Status: Challenges)

: 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.

三种尝试,三种挑战 (Three Attempts, Three Challenges)

方案1️⃣:从gpt2.onnx复现网络结构 (Approach 1: Reproduce Network Structure from gpt2.onnx)

目标 (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)

方案2️⃣:GPT-2 + Plan Head 实现MPC (Approach 2: GPT-2 + Plan Head for MPC)

目标 (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)

方案3️⃣:知识蒸馏gpt2.onnx (Approach 3: Knowledge Distillation from gpt2.onnx)

目标 (Goal):通过蒸馏预训练模型来训练我们自己的模型 (Train our own model by distilling from pretrained model)

结果 (Result):❌ 数据同步问题 (Data Synchronization Issues)

  • 数据流同步存在技术障碍 (Technical obstacles in data stream synchronization)
  • 尚未完全解决 (Not fully resolved)

🔍 从训练日志中发现的问题 (Issues Found in Training Logs)

基于最新的训练日志(Epoch 17-18 / Based on latest training logs),我们发现了以下关键问题 (Key Issues)

1. 预测准确率极低 (Very Low Prediction Accuracy) 🔴

预测准确率: 0-25% (远低于预期 / Far below expectation)
  • 模型几乎没有学到正确的动作映射 (Model has barely learned correct action mapping)
  • 即使有高置信度(>0.9),预测仍然是错误的 (Even with high confidence (>0.9), predictions are still wrong)

2. 动作多样性严重不足 (Severe Lack of Action Diversity) 🔴

唯一动作数: 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)

3. 动作分布严重偏斜 (Severely Skewed Action Distribution) 🟠

真实分布: 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)

4. 置信度与准确性不匹配 (Confidence-Accuracy Mismatch) 🟠

情况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)

5. 损失函数可能存在问题 (Potential Issues with Loss Function) 🟡

总损失: 0.5-0.6 (看起来正常 / Looks normal)
但预测准确率: 0-25% (极低 / Very low)
  • tracking_loss可能权重不够 (Tracking loss weight may be insufficient)
  • consistency_losssmoothness_loss可能干扰了学习 (Consistency and smoothness losses may interfere with learning)
  • 类别不平衡问题(某些动作被过度预测 / Class imbalance issue, some actions over-predicted)

💡 可能的解决方案方向 (Potential Solution Directions)

基于问题分析,我们提出以下研究方向 (Research Directions)

1. 损失函数重新设计 (Loss Function Redesign) 🎯

  • 调整损失权重(增加tracking_loss权重 / Adjust loss weights, increase tracking_loss weight)
  • 重新设计consistency_loss(可能过于严格 / Redesign consistency_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)

2. 模型架构优化 (Model Architecture Optimization) 🏗️

  • 增加模型容量(更大的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)

3. 训练策略改进 (Training Strategy Improvement) 📈

  • 课程学习(Curriculum Learning):从简单到复杂 (From simple to complex)
  • 数据增强:增加动作多样性 (Data augmentation: increase action diversity)
  • 更好的学习率调度 (Better learning rate scheduling)
  • 混合精度训练优化 (Mixed precision training optimization)

4. 数据质量检查 (Data Quality Check) 📊

  • 检查数据标注质量 (Check data annotation quality)
  • 分析动作分布(是否存在标注偏差 / Analyze action distribution, check for annotation bias)
  • 数据预处理优化 (Data preprocessing optimization)

5. 知识蒸馏优化 (Knowledge Distillation Optimization) 🧠

  • 解决数据同步问题 (Resolve data synchronization issues)
  • 改进蒸馏损失函数 (Improve distillation loss function)
  • 温度调度优化 (Temperature scheduling optimization)

🤝 如何参与 (How to Contribute)

我们需要的帮助 (Help We Need)

  1. 损失函数专家 (Loss Function Expert) 🎯

    • 帮助重新设计MPC损失函数 (Help redesign MPC loss function)
    • 解决类别不平衡问题 (Solve class imbalance issues)
    • 优化多目标损失权重 (Optimize multi-objective loss weights)
  2. 模型架构师 (Model Architect) 🏗️

    • 改进Plan Head设计 (Improve Plan Head design)
    • 优化Transformer架构 (Optimize Transformer architecture)
    • 探索新的注意力机制 (Explore new attention mechanisms)
  3. 训练策略专家 (Training Strategy Expert) 📈

    • 设计课程学习策略 (Design curriculum learning strategies)
    • 优化学习率调度 (Optimize learning rate scheduling)
    • 改进数据增强方法 (Improve data augmentation methods)
  4. MPC控制理论专家 (MPC Control Theory Expert) 🧮

    • 验证MPC数学设计的正确性 (Verify correctness of MPC mathematical design)
    • 提供控制理论指导 (Provide control theory guidance)
    • 优化约束条件 (Optimize constraint conditions)
  5. 数据科学家 (Data Scientist) 📊

    • 分析训练数据和标注质量 (Analyze training data and annotation quality)
    • 诊断数据分布问题 (Diagnose data distribution issues)
    • 设计数据增强策略 (Design data augmentation strategies)

参与方式 (How to Participate)

  1. Fork & Clone

    git clone https://github.com/bccw2021/DSD.git
    cd DSD
  2. 查看详细文档 (View Detailed Documentation)

    cd training/docs/优化MPC文档
    # 查看 08_MPC算法设计.md 了解技术细节
    # View 08_MPC算法设计.md for technical details
  3. 运行训练脚本 (Run Training Script)

    cd training
    bash training_optimized_mpc.sh
  4. 提交Issue (Submit Issues)

    • 报告bug (Report bugs)
    • 提出改进建议 (Suggest improvements)
    • 分享实验结果 (Share experimental results)
  5. 提交Pull Request (Submit Pull Requests)

    • 修复问题 (Fix issues)
    • 实现新功能 (Implement new features)
    • 改进文档 (Improve documentation)

📚 技术文档 (Technical Documentation)

详细的技术文档位于 training/docs/优化MPC文档/ (Detailed technical documentation is located at):

核心文档 (Core Documents)

快速参考 (Quick References)

  • 27_快速参考卡片索引.md - 快速参考导航 (Quick reference navigation)
  • 快速参考/ - 所有快速参考卡片目录 (All quick reference cards)

🛠️ 环境要求 (Environment Requirements)

# 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)

📊 当前实验结果 (Current Experimental Results)

训练配置 (Training Configuration)

  • 模型 (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×)

当前性能 (Current Performance)

  • 验证准确率 (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)

🎓 理论基础 (Theoretical Foundation)

MPC (Model Predictive Control / 模型预测控制)

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


📝 贡献指南 (Contribution Guidelines)

  1. 代码规范 (Code Standards)

    • 遵循PEP 8 (Follow PEP 8)
    • 添加类型注解 (Add type annotations)
    • 编写文档字符串 (Write docstrings)
  2. 提交规范 (Commit Standards)

    • 清晰的commit message (Clear commit messages)
    • 关联Issue编号 (Link Issue numbers)
    • 添加测试(如适用 / Add tests if applicable)
  3. 实验报告 (Experimental Reports)

    • 记录实验配置 (Record experimental configuration)
    • 保存训练日志 (Save training logs)
    • 分享实验结果 (Share experimental results)

📄 许可证 (License)

本项目采用 MIT License (This project uses MIT License)


🙏 致谢 (Acknowledgments)

  • Comma.ai - 提供了bodyjim环境、gpt2.onnx模型和实现思路 (Provided bodyjim environment, gpt2.onnx model, and implementation insights)
    • 他们的成功证明了GPT模型在机器人控制中的潜力
    • DSD项目基于他们的工作,尝试进一步探索MPC能力
  • 所有贡献者 (All Contributors) - 感谢每一位参与开源协作的朋友 (Thanks to everyone who participates in open-source collaboration)

📮 联系方式 (Contact)


🌟 Star History

如果这个项目对你有帮助,请给我们一个⭐️!
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! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages