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
| import torch import torch.nn as nn
class CausalGazeForecaster(nn.Module): """ Context-Aware Causal Gaze Forecaster 在追踪丢失期间: 1. 分析丢失前的视线轨迹 2. 结合驾驶上下文(车辆状态、场景) 3. 因果推断丢失期间的视线方向 关键假设: 驾驶员的视线转移由驾驶意图因果驱动 """ def __init__(self, feat_dim: int = 128, n_context: int = 30): super().__init__() self.gaze_encoder = nn.LSTM( input_size=2, hidden_size=feat_dim, num_layers=2, batch_first=True, bidirectional=True ) self.context_encoder = nn.Sequential( nn.Linear(10, 32), nn.ReLU(), nn.Linear(32, feat_dim) ) self.causal_net = nn.TransformerDecoder( nn.TransformerDecoderLayer( d_model=feat_dim * 2, nhead=8, dim_feedforward=256, batch_first=True ), num_layers=3 ) self.forecast_head = nn.Sequential( nn.Linear(feat_dim * 2, 64), nn.ReLU(), nn.Linear(64, 2) ) self.confidence_head = nn.Sequential( nn.Linear(feat_dim * 2, 32), nn.ReLU(), nn.Linear(32, 1), nn.Sigmoid() ) def forward( self, gaze_history: torch.Tensor, context: torch.Tensor, n_forecast: int = 30 ) -> dict: """ 预测追踪丢失期间的视线方向 Returns: forecast_gaze: (B, n_forecast, 2) 预测视线 confidence: (B, n_forecast) 每帧置信度 """ _, (h_n, _) = self.gaze_encoder(gaze_history) gaze_feat = torch.cat([h_n[-2], h_n[-1]], dim=1) ctx_feat = self.context_encoder(context) combined = gaze_feat + torch.cat([ctx_feat, ctx_feat], dim=1) queries = combined.unsqueeze(1).expand(-1, n_forecast, -1) decoded = self.causal_net( queries, combined.unsqueeze(1) ) forecast_gaze = self.forecast_head(decoded) confidence = self.confidence_head(decoded).squeeze(-1) return { 'forecast_gaze': forecast_gaze, 'confidence': confidence }
if __name__ == "__main__": model = CausalGazeForecaster(feat_dim=128, n_context=30) gaze_hist = torch.randn(4, 30, 2) ctx = torch.randn(4, 10) out = model(gaze_hist, ctx, n_forecast=45) print(f"视线历史: {gaze_hist.shape}") print(f"预测视线: {out['forecast_gaze'].shape}") print(f"置信度: {out['confidence'].shape}") print(f"平均置信度: {out['confidence'].mean():.3f}")
|