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
| import torch import torch.nn as nn import torch.nn.functional as F
class EyeTAGModel(nn.Module): """ EyeTAG: 眼动轨迹感知视线估计 输入: 面部视频帧序列 输出: 每帧视线方向(俯仰角,偏航角) + 轨迹类型 + 置信度 """ def __init__(self, feat_dim=512, hidden_dim=256, num_heads=4): super().__init__() self.eye_encoder = EyeRegionEncoder(feat_dim=feat_dim) self.trajectory_classifier = TrajectoryClassifier( feat_dim=feat_dim, hidden_dim=hidden_dim, num_classes=4 ) self.gaze_decoder = TrajectoryAwareGazeDecoder( feat_dim=feat_dim, hidden_dim=hidden_dim, num_heads=num_heads ) self.uncertainty_head = nn.Sequential( nn.Linear(feat_dim + hidden_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 2) ) def forward(self, eye_sequence): """ Args: eye_sequence: (B, T, C, H, W) 眼部区域视频 T: 时序窗口(如30帧=1秒@30fps) Returns: gaze: (B, T, 2) 视线方向 (pitch, yaw) in degrees traj_type: (B, T, 4) 轨迹类型概率 uncertainty: (B, T, 2) 每帧不确定性 """ B, T, C, H, W = eye_sequence.shape eyes_flat = eye_sequence.reshape(B*T, C, H, W) feats = self.eye_encoder(eyes_flat) feats = feats.reshape(B, T, -1) traj_logits = self.trajectory_classifier(feats) gaze = self.gaze_decoder(feats, traj_logits) unc_input = torch.cat([feats, gaze], dim=-1) uncertainty = F.softplus(self.uncertainty_head(unc_input)) return { 'gaze': gaze, 'trajectory': traj_logits, 'uncertainty': uncertainty }
class EyeRegionEncoder(nn.Module): """眼部区域特征提取器""" def __init__(self, feat_dim=512): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU6(), self._conv_dw(32, 64, stride=1), self._conv_dw(64, 128, stride=2), self._conv_dw(128, 256, stride=2), self._conv_dw(256, feat_dim, stride=2), nn.AdaptiveAvgPool2d(1), ) self.fc = nn.Linear(feat_dim, feat_dim) def _conv_dw(self, in_ch, out_ch, stride=1): return nn.Sequential( nn.Conv2d(in_ch, in_ch, 3, stride=stride, padding=1, groups=in_ch, bias=False), nn.BatchNorm2d(in_ch), nn.ReLU6(), nn.Conv2d(in_ch, out_ch, 1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU6(), ) def forward(self, x): return self.fc(self.features(x).flatten(1))
class TrajectoryClassifier(nn.Module): """眼动轨迹分类器""" def __init__(self, feat_dim, hidden_dim, num_classes=4): super().__init__() self.lstm = nn.LSTM( feat_dim, hidden_dim, num_layers=2, batch_first=True, bidirectional=True ) self.classifier = nn.Linear(hidden_dim * 2, num_classes) def forward(self, feats): """ Args: feats: (B, T, D) 帧特征序列 Returns: traj_logits: (B, T, num_classes) """ lstm_out, _ = self.lstm(feats) return self.classifier(lstm_out)
class TrajectoryAwareGazeDecoder(nn.Module): """轨迹感知视线解码器""" def __init__(self, feat_dim, hidden_dim, num_heads=4): super().__init__() self.self_attn = nn.MultiheadAttention( feat_dim, num_heads, batch_first=True ) self.traj_proj = nn.Linear(4, feat_dim) self.gaze_head = nn.Sequential( nn.Linear(feat_dim * 2, hidden_dim), nn.ReLU(), nn.Dropout(0.1), nn.Linear(hidden_dim, 2) ) def forward(self, feats, traj_logits): """ Args: feats: (B, T, D) traj_logits: (B, T, 4) Returns: gaze: (B, T, 2) degrees """ attn_out, _ = self.self_attn(feats, feats, feats) traj_embed = self.traj_proj( F.softmax(traj_logits, dim=-1) ) fused = torch.cat([attn_out, traj_embed], dim=-1) gaze = self.gaze_head(fused) return gaze
|