NAVSIM 主要模块和调用关系图
一、系统架构总览
┌─────────────────────────────────────────────────────────────────┐
│ NAVSIM System │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Agents │ │ Planning │ │ Evaluation │ │
│ │ Module │───▶│ Module │───▶│ Module │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Common │ │ Simulation │ │ Traffic │ │
│ │ DataTypes │◀───│ Engine │◀───│ Agents │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
二、核心模块详解
1. Agents 模块 (navsim/agents/)
AbstractAgent
│
┌────────────────┼────────────────────┐
│ │ │
ConstantVelocity EgoStatusMLP TransfuserAgent
Agent Agent │
│ │ │
简单基线 盲驾驶基线 ┌──────┴──────┐
│ │
TransfuserModel Features
│ │
Backbone Callbacks
2. Planning 模块 (navsim/planning/)
Planning Module
│
├── scenario_builder/
│ ├── NavsimScenario ────────▶ 构建场景数据
│ └── ScenarioUtils
│
├── metric_caching/
│ ├── MetricCache ───────────▶ 缓存评估指标
│ └── CacheProcessor
│
├── simulation/
│ └── planner/
│ └── pdm_planner/ ──────▶ PDM规划器核心
│ ├── PDMClosedPlanner
│ ├── observation/
│ ├── proposal/
│ ├── scoring/
│ └── simulation/
│
└── training/
├── Dataset ────────────────▶ 训练数据管理
├── FeatureBuilder
└── LightningModule
3. PDM Planner 详细架构
PDMPlanner
│
├── Observation (观测)
│ ├── PDMObservation
│ ├── PDMObjectManager ──────▶ 管理周围物体
│ └── PDMOccupancyMap ───────▶ 占用栅格地图
│
├── Proposal (提议生成)
│ ├── PDMGenerator ──────────▶ 生成候选轨迹
│ ├── BatchIDMPolicy ────────▶ IDM策略批处理
│ └── PDMProposal
│
├── Scoring (评分)
│ ├── PDMScorer ─────────────▶ 轨迹评分
│ ├── PDMComfortMetrics ────▶ 舒适度指标
│ └── SceneAggregator ──────▶ 场景聚合
│
└── Simulation (仿真)
├── PDMSimulator ──────────▶ 主仿真器
├── BatchKinematicBicycle ─▶ 车辆动力学
└── BatchLQR ──────────────▶ LQR控制器
三、主要调用流程
1. 评估流程 (run_pdm_score.py)
run_pdm_score()
│
├─▶ 1. 初始化组件
│ ├── instantiate(Agent)
│ ├── instantiate(Simulator)
│ ├── instantiate(Scorer)
│ └── SceneLoader & MetricCacheLoader
│
├─▶ 2. 第一阶段评估
│ ├── scene_loader.get_agent_input()
│ ├── agent.compute_trajectory()
│ ├── pdm_score() ──────────────────▶ 计算EPDMS
│ └── traffic_simulation()
│
└─▶ 3. 第二阶段评估
├── load_follow_up_scenes()
├── weighted_aggregation() ───────▶ 高斯加权
└── final_score_multiplication()
2. 训练流程 (run_training.py)
run_training()
│
├─▶ 1. 构建训练组件
│ ├── build_agent()
│ ├── build_dataset()
│ └── build_lightning_module()
│
├─▶ 2. 训练循环
│ ├── feature_builders.compute()
│ ├── agent.forward() ─────────────▶ 前向传播
│ ├── compute_loss()
│ └── optimizer.step()
│
└─▶ 3. 验证和回调
├── validation_step()
└── training_callbacks()
3. Agent计算轨迹流程
agent.compute_trajectory(agent_input)
│
├─▶ 1. 特征提取
│ └── for builder in feature_builders:
│ features.update(builder.compute())
│
├─▶ 2. 前向传播
│ └── predictions = self.forward(features)
│
└─▶ 3. 轨迹生成
└── Trajectory(poses, sampling)
四、数据流向图
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Raw Sensor │ │ Scene │ │ Cached │
│ Data │─────▶│ Loader │─────▶│ Metrics │
└──────────────┘ └──────────────┘ └──────────────┘
│ │ │
│ ▼ │
│ ┌──────────────┐ │
└─────────────▶│ AgentInput │◀─────────────┘
└──────────────┘
│
▼
┌──────────────┐
│ Agent │
└──────────────┘
│
▼
┌──────────────┐
│ Trajectory │
└──────────────┘
│
┌────────────┼────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│Simulator │ │ Scorer │ │ Traffic │
└──────────┘ └──────────┘ └──────────┘
│ │ │
└────────────┼────────────┘
▼
┌──────────────┐
│ EPDMS Score │
└──────────────┘
五、关键接口调用关系
1. Agent接口实现
AbstractAgent (接口定义)
│
├── name() ─────────────────────▶ 返回Agent名称
├── initialize() ───────────────▶ 初始化模型
├── get_sensor_config() ────────▶ 定义传感器需求
├── compute_trajectory() ───────▶ 计算轨迹输出
│
└── (可选-学习型Agent)
├── forward() ──────────────▶ 神经网络前向
├── compute_loss() ─────────▶ 计算训练损失
├── get_feature_builders() ─▶ 特征构建器
└── get_target_builders() ──▶ 目标构建器
2. PDM组件交互
PDMClosedPlanner.compute_planner_trajectory()
│
├─▶ PDMDrivableMap.from_simulation()
│ └─▶ 构建可行驶区域地图
│
├─▶ _get_closed_loop_trajectory()
│ ├─▶ PDMObservation.from_simulation()
│ ├─▶ PDMGenerator.get_proposals()
│ └─▶ PDMScorer.score_proposals()
│
└─▶ PDMSimulator.simulate()
├─▶ BatchKinematicBicycle.simulate()
└─▶ BatchLQR.track_trajectory()
六、模块间依赖关系
┌─────────────────────────────────────────┐
│ Application Layer │
│ (run_pdm_score, run_training, etc.) │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Agent Layer │
│ (AbstractAgent implementations) │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Planning Layer │
│ (PDMPlanner, Simulator, Scorer) │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Common Layer │
│ (DataClasses, DataLoader, Enums) │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ External Dependencies │
│ (nuPlan, PyTorch, NumPy, etc.) │
└─────────────────────────────────────────┘
七、并行处理架构
Main Process
│
├─▶ Ray Distributed Worker Pool
│ ├── Worker 1 ──▶ Process Scenes[0:n]
│ ├── Worker 2 ──▶ Process Scenes[n:2n]
│ ├── Worker 3 ──▶ Process Scenes[2n:3n]
│ └── Worker N ──▶ Process Scenes[...]
│
└─▶ Results Aggregation
└── Merge & Output Final Scores