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
| """ EyeCue: 眼动增强第一人称视频认知分心检测 论文:https://arxiv.org/abs/2605.07859 代码:https://github.com/langzhang2000/EyeCue
核心方法:眼动-场景交互建模 + GDSQ跨模态注意力
依赖: - torch>=2.0 - transformers>=4.30 - opencv-python>=4.8 - numpy>=1.24 """
import torch import torch.nn as nn import torch.nn.functional as F from transformers import VideoMAEModel import numpy as np from typing import Tuple, Dict
class EyeCue(nn.Module): """ EyeCue: 眼动增强第一人称视频认知分心检测框架 架构: 1. 视频编码器:VideoMAE提取时空特征 2. 眼动编码器:BiLSTM建模眼动模式 3. GDSQ模块:眼动驱动的语义查询 4. 分类器:分心/专注二元判断 输入: - 第一人称视频:(B, T, C, H, W) - 眼动序列:(B, T_g, 6) [x, y, pupil, fixation_dur, saccade_vel, blink_rate] 输出: - 分心概率:(B,) 范围[0, 1] """ def __init__(self, config: Dict): super().__init__() self.video_encoder = VideoEncoder( model_name=config.get('video_model', 'videomae_base'), num_frames=config.get('num_frames', 16) ) video_dim = 768 self.gaze_encoder = GazeEncoder( input_dim=config.get('gaze_dim', 6), hidden_dim=config.get('gaze_hidden', 256), num_layers=config.get('gaze_layers', 2) ) gaze_dim = 512 self.gdsq = GazeDrivenSemanticQuery( video_dim=video_dim, gaze_dim=gaze_dim, num_heads=config.get('num_heads', 8) ) self.classifier = nn.Sequential( nn.Linear(video_dim, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 1) ) def forward(self, video: torch.Tensor, gaze_sequence: torch.Tensor, gaze_positions: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ 前向传播 Args: video: (B, T_v, C, H, W) 第一人称视频 gaze_sequence: (B, T_g, 6) 眼动特征序列 gaze_positions: (B, T_g, 2) 注视点坐标 Returns: predictions: (B,) 分心概率 attention_weights: (B, T_g, T_v) 注意力权重 """ video_features = self.video_encoder(video) gaze_features = self.gaze_encoder(gaze_sequence) attended_features, attention_weights = self.gdsq( video_features, gaze_features, gaze_positions ) pooled_features = attended_features.mean(dim=1) predictions = self.classifier(pooled_features).squeeze(-1) return predictions, attention_weights
class VideoEncoder(nn.Module): """视频编码器(VideoMAE)""" def __init__(self, model_name='MCG-NJU/videomae-base', num_frames=16): super().__init__() self.model = VideoMAEModel.from_pretrained(model_name) self.num_frames = num_frames def forward(self, video_clip: torch.Tensor) -> torch.Tensor: """ Args: video_clip: (B, T, C, H, W) RGB视频片段 Returns: features: (B, T, 768) 时空特征 """ video_clip = video_clip.float() / 255.0 video_clip = (video_clip - 0.5) / 0.5 outputs = self.model(pixel_values=video_clip) return outputs.last_hidden_state
class GazeEncoder(nn.Module): """眼动编码器(双向LSTM)""" def __init__(self, input_dim=6, hidden_dim=256, num_layers=2): super().__init__() self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers=num_layers, batch_first=True, bidirectional=True, dropout=0.2) self.fc = nn.Linear(hidden_dim * 2, 512) def forward(self, gaze_sequence: torch.Tensor) -> torch.Tensor: """ Args: gaze_sequence: (B, T, 6) 眼动特征 Returns: features: (B, T, 512) """ lstm_out, _ = self.lstm(gaze_sequence) return self.fc(lstm_out)
class GazeDrivenSemanticQuery(nn.Module): """眼动驱动的语义查询模块""" def __init__(self, video_dim=768, gaze_dim=512, num_heads=8): super().__init__() self.cross_attention = nn.MultiheadAttention( embed_dim=video_dim, num_heads=num_heads, batch_first=True, dropout=0.1 ) self.gaze_proj = nn.Linear(gaze_dim, video_dim) self.norm = nn.LayerNorm(video_dim) def forward(self, video_features: torch.Tensor, gaze_features: torch.Tensor, gaze_positions: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: video_features: (B, T_v, D) gaze_features: (B, T_g, D) gaze_positions: (B, T_g, 2) 归一化坐标 Returns: attended_features: (B, T_g, D) attention_weights: (B, T_g, T_v) """ query = self.gaze_proj(gaze_features) key = value = video_features attended, attention_weights = self.cross_attention( query, key, value, need_weights=True ) output = self.norm(query + attended) return output, attention_weights
if __name__ == "__main__": """ 实际测试:模拟驾驶数据 """ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') config = { 'video_model': 'MCG-NJU/videomae-base', 'num_frames': 16, 'gaze_dim': 6, 'gaze_hidden': 256, 'gaze_layers': 2, 'num_heads': 8 } model = EyeCue(config).to(device) print(f"模型参数量: {sum(p.numel() for p in model.parameters()) / 1e6:.2f}M") batch_size = 4 video = torch.randn(batch_size, 16, 3, 224, 224).to(device) gaze_sequence = torch.randn(batch_size, 30, 6).to(device) gaze_positions = torch.rand(batch_size, 30, 2).to(device) model.eval() with torch.no_grad(): predictions, attention = model(video, gaze_sequence, gaze_positions) print(f"\n输入形状:") print(f" 视频: {video.shape}") print(f" 眼动: {gaze_sequence.shape}") print(f" 注视点: {gaze_positions.shape}") print(f"\n输出:") print(f" 分心概率: {predictions.shape}, 值范围: [{predictions.min():.3f}, {predictions.max():.3f}]") print(f" 注意力权重: {attention.shape}, 总和: {attention.sum(dim=-1).mean():.3f}") for i in range(batch_size): prob = torch.sigmoid(predictions[i]).item() status = "分心" if prob > 0.5 else "专注" print(f"样本{i+1}: 分心概率={prob:.2%}, 判断={status}")
|