1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| import torch import torch.nn as nn from typing import Tuple
class CausalContextGatedForecaster(nn.Module): """ CCGF: 因果上下文门控预测器 论文: arXiv:2609.12374 核心功能: 在追踪器丢失期间,仅用过去数据预测驾驶员视线 架构: 1. 历史LSTM: 编码60帧预丢失视线+头姿 2. 场景LSTM: 编码DINOv3场景特征 3. 可靠性门控: 动态融合历史和场景 4. 空间概率解码: 24×32分布 → 质心 """ def __init__( self, gaze_dim: int = 2, head_dim: int = 6, scene_dim: int = 768, hidden_dim: int = 256, grid_h: int = 24, grid_w: int = 32, history_len: int = 60 ): super().__init__() self.grid_h = grid_h self.grid_w = grid_w self.history_len = history_len self.history_lstm = nn.LSTM( input_size=gaze_dim + head_dim, hidden_size=hidden_dim, num_layers=2, batch_first=True, dropout=0.1 ) self.scene_lstm = nn.LSTM( input_size=scene_dim, hidden_size=hidden_dim, num_layers=1, batch_first=True ) self.gate = nn.Sequential( nn.Linear(hidden_dim * 2 + 1, hidden_dim), nn.GELU(), nn.Linear(hidden_dim, 1), nn.Sigmoid() ) self.decoder = nn.Sequential( nn.Linear(hidden_dim, hidden_dim * 2), nn.GELU(), nn.Linear(hidden_dim * 2, grid_h * grid_w) ) self.register_buffer( 'grid_coords', self._create_grid_coords(grid_h, grid_w) ) def _create_grid_coords(self, h: int, w: int) -> torch.Tensor: """创建空间网格坐标""" ys = torch.linspace(0, 1, h) xs = torch.linspace(0, 1, w) grid = torch.stack(torch.meshgrid(ys, xs, indexing='ij')) return grid.reshape(2, -1).T def forward( self, gaze_history: torch.Tensor, head_history: torch.Tensor, scene_features: torch.Tensor, dropout_progress: float = 0.0 ) -> Tuple[torch.Tensor, torch.Tensor]: """ 前向传播 Args: gaze_history: 过去T帧视线坐标 (B, T, 2) head_history: 过去T帧头姿 (B, T, 6) scene_features: 场景DINOv3特征 (B, T, 768) dropout_progress: 当前在丢失期间的进度 [0, 1] Returns: predicted_gaze: (B, 2) 预测视线位置 prob_map: (B, 24, 32) 空间概率分布 """ B = gaze_history.shape[0] hist_input = torch.cat([gaze_history, head_history], dim=-1) _, (h_n, _) = self.history_lstm(hist_input) hist_hidden = h_n[-1] _, (s_n, _) = self.scene_lstm(scene_features) scene_hidden = s_n[-1] progress = torch.tensor(dropout_progress, device=gaze_history.device).expand(B, 1) gate_input = torch.cat([hist_hidden, scene_hidden, progress], dim=-1) alpha = self.gate(gate_input) fused = alpha * hist_hidden + (1 - alpha) * scene_hidden logits = self.decoder(fused) prob_map = torch.softmax(logits, dim=-1) prob_map = prob_map.reshape(B, self.grid_h, self.grid_w) grid = self.grid_coords.to(gaze_history.device) prob_flat = prob_map.reshape(B, -1) predicted_gaze = (prob_flat.unsqueeze(-1) * grid).sum(dim=1) return predicted_gaze, prob_map
class DropoutRecoverySystem: """ CCGF在线丢失恢复系统 集成到DMS中的完整pipeline: 1. 检测追踪器丢失 2. 启动CCGF预测 3. 追踪器恢复后平滑切换 """ def __init__(self, history_len=60, max_dropout_len=120): self.forecaster = CausalContextGatedForecaster(history_len=history_len) self.history_len = history_len self.max_dropout_len = max_dropout_len self.gaze_buffer = [] self.head_buffer = [] self.scene_buffer = [] self.is_dropout = False self.dropout_frame = 0 def update(self, gaze, head_pose, scene_feat, tracker_status): """ 每帧调用 Args: gaze: (2,) 视线坐标, None if tracker lost head_pose: (6,) 头姿 scene_feat: (768,) 场景特征 tracker_status: 'tracking' or 'lost' Returns: predicted_gaze: (2,) 视线坐标(真实或预测) is_predicted: bool 是否为预测值 """ if tracker_status == 'tracking': self.gaze_buffer.append(gaze.copy()) self.head_buffer.append(head_pose.copy()) self.scene_buffer.append(scene_feat.copy()) if len(self.gaze_buffer) > self.history_len: self.gaze_buffer.pop(0) self.head_buffer.pop(0) self.scene_buffer.pop(0) self.is_dropout = False self.dropout_frame = 0 return gaze, False else: if not self.is_dropout: self.is_dropout = True self.dropout_frame = 0 self.dropout_frame += 1 if len(self.gaze_buffer) < self.history_len: return None, False gaze_hist = torch.tensor( np.array(self.gaze_buffer) ).unsqueeze(0).float() head_hist = torch.tensor( np.array(self.head_buffer) ).unsqueeze(0).float() scene_hist = torch.tensor( np.array(self.scene_buffer) ).unsqueeze(0).float() progress = min(self.dropout_frame / self.max_dropout_len, 1.0) with torch.no_grad(): pred_gaze, prob_map = self.forecaster( gaze_hist, head_hist, scene_hist, dropout_progress=progress ) return pred_gaze[0].numpy(), True
import numpy as np
if __name__ == "__main__": print("=" * 60) print("CCGF 因果视线恢复 - 测试") print("=" * 60) model = CausalContextGatedForecaster() total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params:,}") B, T = 1, 60 gaze_hist = torch.randn(B, T, 2) head_hist = torch.randn(B, T, 6) scene_feat = torch.randn(B, T, 768) pred_gaze, prob_map = model(gaze_hist, head_hist, scene_feat, 0.3) print(f"\n输入:") print(f" 视线历史: {gaze_hist.shape}") print(f" 头姿历史: {head_hist.shape}") print(f" 场景特征: {scene_feat.shape}") print(f"\n输出:") print(f" 预测视线: {pred_gaze.shape} → {pred_gaze[0].detach().numpy()}") print(f" 概率分布: {prob_map.shape}") print(f" 概率和: {prob_map[0].sum().item():.4f}") print(f"\n{'='*60}") print("在线丢失恢复系统测试") print(f"{'='*60}") system = DropoutRecoverySystem(history_len=60) for i in range(60): gaze = np.array([0.5 + 0.01*i, 0.5]) head = np.random.randn(6) scene = np.random.randn(768) system.update(gaze, head, scene, 'tracking') print(f"历史缓冲区已填充: {len(system.gaze_buffer)}帧") print(f"\n模拟追踪丢失(5帧):") print(f"{'帧':>4} | {'x':>8} | {'y':>8} | {'预测?':>6}") print("-" * 36) for i in range(5): gaze = system.update( None, np.random.randn(6), np.random.randn(768), 'lost' ) if gaze[0] is not None: x, y = gaze[0] print(f"{i:4d} | {x:8.4f} | {y:8.4f} | {'是' if gaze[1] else '否':>6}")
|