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
| """ 论文:EyeTAG: Eye Trajectory-Aware Gaze Estimation 作者:Jungmin Lee et al. 会议:BMVC 2026 链接:https://arxiv.org/abs/2610.00922
核心方法:差分运动令牌反馈的因果多帧视线估计 """
import torch import torch.nn as nn import torch.nn.functional as F from typing import Tuple, Optional
class KinematicToken(nn.Module): """ 差分运动令牌模块 将历史视线预测的差分编码为运动先验 关键:差分操作在视线空间中平移不变 """ def __init__(self, gaze_dim: int = 2, token_dim: int = 256): super().__init__() self.projection = nn.Linear(gaze_dim, token_dim) self.norm = nn.LayerNorm(token_dim) def forward(self, gaze_history: torch.Tensor, current_gaze: Optional[torch.Tensor] = None) -> torch.Tensor: """ Args: gaze_history: 历史视线预测 (B, T, gaze_dim) current_gaze: 当前预测 (B, gaze_dim), 用于计算最新差分 Returns: kinematic_token: 运动令牌 (B, token_dim) """ if current_gaze is not None and gaze_history.size(1) > 0: last_gaze = gaze_history[:, -1, :] delta = current_gaze - last_gaze else: delta = torch.zeros(gaze_history.size(0), gaze_history.size(2), device=gaze_history.device) token = self.projection(delta) token = self.norm(token) return token
class EyeTAGModel(nn.Module): """ EyeTAG: 眼动轨迹感知的视线估计模型 架构: 1. Face Stream: 面部图像编码器 2. Eye Stream: 眼部图像编码器 3. Cross-Attention: 融合视觉证据 4. Causal Transformer: 因果解码器 + 运动令牌 """ def __init__(self, face_encoder: nn.Module, eye_encoder: nn.Module, token_dim: int = 256, num_heads: int = 8, num_layers: int = 6, gaze_dim: int = 2): super().__init__() self.face_encoder = face_encoder self.eye_encoder = eye_encoder self.gaze_dim = gaze_dim self.face_proj = nn.Linear(face_encoder.feature_dim, token_dim) self.eye_proj = nn.Linear(eye_encoder.feature_dim, token_dim) self.kinematic_token = KinematicToken(gaze_dim, token_dim) self.cross_attn = nn.MultiheadAttention( token_dim, num_heads, batch_first=True ) self.fusion_norm = nn.LayerNorm(token_dim) decoder_layer = nn.TransformerDecoderLayer( d_model=token_dim, nhead=num_heads, dim_feedforward=token_dim * 4, dropout=0.1, batch_first=True ) self.decoder = nn.TransformerDecoder( decoder_layer, num_layers=num_layers ) self.gaze_head = nn.Sequential( nn.Linear(token_dim, token_dim // 2), nn.GELU(), nn.Linear(token_dim // 2, gaze_dim) ) def forward(self, face_img: torch.Tensor, eye_img: torch.Tensor, gaze_history: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]: """ Args: face_img: 面部图像 (B, 3, 224, 224) eye_img: 眼部图像 (B, 3, 64, 64) gaze_history: 历史预测 (B, T, 2), None表示首帧 Returns: gaze_pred: 视线预测 (B, 2) - pitch, yaw (弧度) token: 融合特征 (B, token_dim) """ B = face_img.size(0) device = face_img.device face_feat = self.face_encoder(face_img) eye_feat = self.eye_encoder(eye_img) face_feat = self.face_proj(face_feat) eye_feat = self.eye_proj(eye_feat) if gaze_history is not None and gaze_history.size(1) > 0: current_gaze = gaze_history[:, -1, :] else: gaze_history = torch.zeros(B, 1, self.gaze_dim, device=device) current_gaze = torch.zeros(B, self.gaze_dim, device=device) kin_token = self.kinematic_token(gaze_history, current_gaze) visual_tokens = torch.stack([face_feat, eye_feat], dim=1) fused, _ = self.cross_attn( query=visual_tokens, key=visual_tokens, value=visual_tokens ) fused = self.fusion_norm(fused + visual_tokens) kin_tokens = kin_token.unsqueeze(1) sequence = torch.cat([fused, kin_tokens], dim=1) causal_mask = torch.triu( torch.ones(3, 3, device=device) * float('-inf'), diagonal=1 ) decoded = self.decoder( tgt=sequence, memory=sequence, tgt_mask=causal_mask ) output_token = decoded[:, -1, :] gaze_pred = self.gaze_head(output_token) return gaze_pred, output_token
class SimpleFaceEncoder(nn.Module): """基于ResNet的面部特征编码器""" def __init__(self, feature_dim: int = 512): super().__init__() self.feature_dim = feature_dim import torchvision.models as models backbone = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) self.backbone = nn.Sequential(*list(backbone.children())[:-1]) self.proj = nn.Linear(512, feature_dim) def forward(self, x): feat = self.backbone(x).flatten(1) return self.proj(feat)
class SimpleEyeEncoder(nn.Module): """基于轻量CNN的眼部特征编码器""" def __init__(self, feature_dim: int = 256): super().__init__() self.feature_dim = feature_dim self.conv = nn.Sequential( nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1), ) self.fc = nn.Linear(128, feature_dim) def forward(self, x): feat = self.conv(x).flatten(1) return self.fc(feat)
if __name__ == "__main__": torch.manual_seed(42) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') face_enc = SimpleFaceEncoder(512) eye_enc = SimpleEyeEncoder(256) model = EyeTAGModel(face_enc, eye_enc, token_dim=256).to(device) face_img = torch.randn(4, 3, 224, 224).to(device) eye_img = torch.randn(4, 3, 64, 64).to(device) gaze_pred, _ = model(face_img, eye_img, gaze_history=None) print(f"首帧视线预测 (pitch, yaw): {gaze_pred[0].cpu().detach()}") gaze_history = gaze_pred.unsqueeze(1).detach() gaze_pred2, _ = model(face_img, eye_img, gaze_history=gaze_history) print(f"第二帧视线预测: {gaze_pred2[0].cpu().detach()}") delta = gaze_pred2 - gaze_pred print(f"帧间差分(运动令牌来源): {delta[0].cpu().detach()}") total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params/1e6:.2f}M") import time model.eval() with torch.no_grad(): for _ in range(10): _ = model(face_img, eye_img) start = time.time() for _ in range(100): _ = model(face_img, eye_img) elapsed = time.time() - start fps = 100 / elapsed print(f"推理速度: {fps:.1f} FPS") print(f"单帧延迟: {1000/fps:.1f} ms")
|