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
| import torch import torch.nn as nn import torch.nn.functional as F
class GazeMovementPredictor(nn.Module): """ 视线-运动预测模型 ICCV 2025 论文核心: 基于当前视线 + 车辆运动 → 预测未来视觉注意力 应用场景: 1. 驾驶员注意力预测 2. ADAS 警告时机优化 3. 分心趋势检测 """ def __init__(self, gaze_dim: int = 2, vehicle_dim: int = 6, hidden_dim: int = 128, future_steps: int = 5): super().__init__() self.gaze_encoder = nn.Sequential( nn.Linear(gaze_dim, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, hidden_dim) ) self.vehicle_encoder = nn.Sequential( nn.Linear(vehicle_dim, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, hidden_dim) ) self.fusion = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.ReLU(inplace=True), nn.Linear(hidden_dim, hidden_dim) ) self.future_gaze_head = nn.Linear(hidden_dim, gaze_dim * future_steps) self.attention_head = nn.Linear(hidden_dim, 3) self.future_steps = future_steps self.gaze_dim = gaze_dim def forward(self, current_gaze: torch.Tensor, vehicle_state: torch.Tensor) -> dict: """ 前向传播 Args: current_gaze: 当前视线 (pitch, yaw), shape=(B, 2) vehicle_state: 车辆状态 [速度, 方向盘, 车道偏移等], shape=(B, 6) Returns: dict: { "future_gaze": 未来视线轨迹, shape=(B, future_steps, 2) "attention_probs": 注意力区域概率, shape=(B, 3) "predicted_deviation": 预测视线偏离, shape=(B, 1) } """ gaze_features = self.gaze_encoder(current_gaze) vehicle_features = self.vehicle_encoder(vehicle_state) combined = torch.cat([gaze_features, vehicle_features], dim=1) fused_features = self.fusion(combined) future_gaze_flat = self.future_gaze_head(fused_features) future_gaze = future_gaze_flat.view(-1, self.future_steps, self.gaze_dim) attention_probs = F.softmax(self.attention_head(fused_features), dim=1) predicted_deviation = self._compute_deviation(future_gaze) return { "future_gaze": future_gaze, "attention_probs": attention_probs, "predicted_deviation": predicted_deviation } def _compute_deviation(self, future_gaze: torch.Tensor) -> torch.Tensor: """ 计算预测偏离程度 如果未来视线持续偏离道路中心,预测偏离高 """ road_center = torch.zeros(self.gaze_dim) deviations = torch.norm(future_gaze - road_center, dim=2) mean_deviation = deviations.mean(dim=1) return mean_deviation
if __name__ == "__main__": model = GazeMovementPredictor() current_gaze = torch.tensor([ [0.1, 0.05], [0.5, -0.3], ]) vehicle_state = torch.tensor([ [60, 0.1, 0.02, 0, 0, 0], [60, 0.5, 0.2, 0, 0, 0], ]) output = model(current_gaze, vehicle_state) print("=== 正常驾驶 ===") print(f"未来视线轨迹: {output['future_gaze'][0]}") print(f"注意力区域概率: {output['attention_probs'][0]}") print(f"预测偏离: {output['predicted_deviation'][0]:.3f}") print("\n=== 分心驾驶 ===") print(f"未来视线轨迹: {output['future_gaze'][1]}") print(f"注意力区域概率: {output['attention_probs'][1]}") print(f"预测偏离: {output['predicted_deviation'][1]:.3f}")
|