构建多模态推理系统,涵盖视觉定位(Grounding)、多模态思维链(Visual CoT)、多模态 Agent 与 RAG。
Grounding(视觉定位)是将语言概念"锚定"到图像的特定区域。对于自动驾驶,这意味着不仅要"知道画面中有行人",还要"知道行人在哪里"。
LLM 天然处理离散 token,不能直接输出连续坐标。解决方案是将坐标离散化:
x ∈ [0, 1] → bin = round(x * 1000) → token "<loc500>"
y ∈ [0, 1] → bin = round(y * 1000) → token "<loc300>"
bbox → "<loc250><sep><loc500><sep><loc750><sep><loc900>"
从 LLM 隐藏状态直接预测目标类别和位置:
class GroundingHead(nn.Module):
def forward(self, hidden_states):
class_logits = self.class_head(hidden_states) # (B, N, C+1)
bbox_preds = self.bbox_head(hidden_states) # (B, N, 4)
return {"class_logits": class_logits, "bbox_preds": bbox_preds}
- 检测并定位:车辆、行人、交通灯、交通标志、车道线
- 输出格式:类别 + bbox + 置信度
- 应用:碰撞预警、行人意图预测、交通信号响应
仅仅检测目标不够,还需要理解目标之间的关系:
- "行人在斑马线上" vs "行人在人行道上" → 不同的危险等级
- "前方车辆在我的车道上" vs "前方车辆在相邻车道" → 不同的行为响应
| 类别 | 关系 | 驾驶含义 |
|---|---|---|
| 方向 | left_of, right_of | 目标在左右方向 |
| 深度 | in_front_of, behind | 目标在前后方向 |
| 垂直 | above, below | 交通灯通常 above |
| 拓扑 | inside, near | 行人 inside 斑马线 |
基于位置距离的注意力偏置:
dist = cdist(query_positions, key_positions)
spatial_bias = -dist * 5.0 # 距离越近,注意力越高
传统的"输入→输出"决策缺乏可解释性。CoT 提供逐步推理:
Q: <image> 在这个路口我应该怎么做?
A (无 CoT): 停车等待。
A (有 CoT):
Let me think step by step:
1. 观察到交通灯状态 → 红色
2. 检测到斑马线上有行人 → 1人, bbox=[0.3,0.5,0.4,0.7]
3. 行人位置分析 → 正在横穿ego车道
4. 规则匹配 → 红灯+行人横穿 = 必须停车
5. 安全距离 → 行人距离5m, 当前车速40km/h
答案: 立即减速至完全停止,等待行人穿过且绿灯亮起后再通行。
多次采样取最一致答案,提高可靠性并估计不确定性。
Step 1: 场景感知(检测目标、交通灯)
Step 2: 关系分析(目标位置、交互关系)
Step 3: 风险评估(碰撞风险、违规风险)
Step 4: 规则匹配(交通规则约束)
Step 5: 决策输出(具体行动)
Agent 不是被动回答问题,而是主动调用工具:
Thought → Action → Observation → Thought → ... → Answer
| 工具 | 功能 | 返回 |
|---|---|---|
| object_detector | 检测目标 | bboxes, classes, confidences |
| lane_detector | 检测车道线 | lane geometry |
| traffic_light_detector | 检测交通灯 | state, position |
| depth_estimator | 深度估计 | depth_map |
| speed_reader | 读取车速 | speed_kmh |
| navigation_api | 导航查询 | route info |
<tool_call>object_detector(image_id='front', classes=['vehicle','pedestrian'])</tool_call>
→ <tool_response>{"bboxes": [...], "classes": [...], "confidences": [...]}</tool_response>
- 危险场景提醒:从历史上类似危险场景中检索经验
- 驾驶知识库:从交通规则手册中检索相关知识
- Corner case 参考:检索类似边缘场景的专家决策
支持文本↔图像、图像↔图像的跨模态检索:
text_results = retriever.retrieve(text_embed, modality="text", top_k=3)
image_results = retriever.retrieve(image_embed, modality="image", top_k=3)
检索到的知识会作为上下文拼入 prompt,帮助模型做出更准确的决策。
本章建立的核心能力:
1. ✅ Grounding:坐标离散化 + 定位头
2. ✅ 空间推理:8种空间关系预测
3. ✅ Multimodal CoT:多步场景推理链
4. ✅ Agent:Tool Registry + ReAct 循环
5. ✅ RAG:跨模态检索 + 增强生成
6. ✅ 驾驶场景综合推理:Grounding + CoT + Agent 集成
下一章:视频理解模型 — 驾驶视频的时序建模