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
| import torch import torch.nn as nn import torch.nn.functional as F
class EyeCueModel(nn.Module): """ EyeCue: 眼动+自我中心视频认知分心检测 论文:IJCAI 2026, arXiv:2605.07859 核心:跨模态交互建模眼动-场景关系 输入: - video: (B, T, C, H, W) 自我中心视频 - gaze: (B, T, 4) 眼动数据 (x, y, pupil_l, pupil_r) 输出: - logits: (B, 2) 认知分心/正常 """ def __init__(self, hidden_dim=256, n_heads=4): super().__init__() self.visual_encoder = nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(32, 64, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d((4, 4)), ) self.visual_proj = nn.Linear(128 * 16, hidden_dim) self.gaze_encoder = nn.Sequential( nn.Linear(4, 64), nn.ReLU(), nn.Linear(64, hidden_dim), nn.ReLU(), ) self.cross_attention = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=n_heads, batch_first=True, ) self.temporal_lstm = nn.LSTM( input_size=hidden_dim * 2, hidden_size=hidden_dim, num_layers=2, batch_first=True, dropout=0.1, ) self.classifier = nn.Sequential( nn.Linear(hidden_dim, 64), nn.ReLU(), nn.Dropout(0.3), nn.Linear(64, 2) ) def forward(self, video, gaze): """ Args: video: (B, T, C, H, W) 自我中心视频序列 gaze: (B, T, 4) 眼动序列 (x, y, pupil_l, pupil_r) Returns: logits: (B, 2) 认知分心/正常 attention_weights: 跨模态注意力权重(可解释性) """ B, T, C, H, W = video.shape video_flat = video.view(B * T, C, H, W) vis_feat = self.visual_encoder(video_flat) vis_feat = self.visual_proj(vis_feat) vis_feat = vis_feat.view(B, T, -1) gaze_feat = self.gaze_encoder(gaze) attn_out, attn_weights = self.cross_attention( query=gaze_feat, key=vis_feat, value=vis_feat, ) combined = torch.cat([attn_out, vis_feat], dim=-1) lstm_out, _ = self.temporal_lstm(combined) feat = lstm_out[:, -1, :] logits = self.classifier(feat) return { 'logits': logits, 'attention_weights': attn_weights, }
if __name__ == "__main__": model = EyeCueModel() video = torch.randn(2, 60, 3, 64, 64) gaze = torch.randn(2, 60, 4) output = model(video, gaze) print(f"输入视频: {video.shape}") print(f"输入眼动: {gaze.shape}") print(f"分类输出: {output['logits'].shape}") print(f"注意力权重: {output['attention_weights'].shape}") print(f"参数总数: {sum(p.numel() for p in model.parameters()):,}") pred = F.softmax(output['logits'], dim=-1) print(f"\n预测概率: 正常={pred[0,0]:.2%}, 认知分心={pred[0,1]:.2%}")
|