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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| import torch import torch.nn as nn import torch.nn.functional as F
class AttentiveGazeModel(nn.Module): """ AttentiveGaze: 不确定性感知的多模态视线估计模型 三大创新: 1. 注意力增强眼部特征提取(空间+通道注意力) 2. 跨模态注意力融合 + 可学习门控 3. 不确定性量化(异方差回归) """ def __init__(self, eye_backbone='resnet18', face_backbone='resnet18', feature_dim=256, num_heads=4): super().__init__() self.eye_encoder = AttentionEyeEncoder( backbone=eye_backbone, feature_dim=feature_dim ) self.face_encoder = FaceContextEncoder( backbone=face_backbone, feature_dim=feature_dim ) self.head_pose_encoder = HeadPoseEncoder( feature_dim=64 ) self.cross_modal_fusion = CrossModalAttentionFusion( eye_dim=feature_dim, face_dim=feature_dim, head_dim=64, hidden_dim=256, num_heads=num_heads ) self.multi_head_embedding = MultiHeadEmbedding( input_dim=256, num_heads=num_heads, head_dim=64 ) self.gaze_regressor = UncertaintyGazeRegressor( input_dim=num_heads * 64, output_dim=2 ) def forward(self, eye_img: torch.Tensor, face_img: torch.Tensor, head_pose: torch.Tensor) -> dict: """ Args: eye_img: (B, 3, 64, 64) 眼部裁剪 face_img: (B, 3, 224, 224) 全脸图像 head_pose: (B, 3) 头部姿态 (pitch, yaw, roll) Returns: {'gaze': (B, 2), 'uncertainty': (B, 1)} """ eye_feat = self.eye_encoder(eye_img) face_feat = self.face_encoder(face_img) head_feat = self.head_pose_encoder(head_pose) fused = self.cross_modal_fusion( eye_feat, face_feat, head_feat ) embedded = self.multi_head_embedding(fused) result = self.gaze_regressor(embedded) return result
class AttentionEyeEncoder(nn.Module): """ 注意力增强眼部特征提取器 空间注意力:学习眼部图像中哪些位置最重要 通道注意力:学习哪些特征通道最相关 关键:在遮挡/光照差时,注意力可降权受损区域 """ def __init__(self, backbone='resnet18', feature_dim=256): super().__init__() from torchvision.models import resnet18 self.backbone = resnet18(pretrained=True) self.backbone = nn.Sequential(*list(self.backbone.children())[:-2]) self.spatial_attn = nn.Sequential( nn.Conv2d(512, 1, kernel_size=7, padding=3), nn.Sigmoid() ) self.channel_attn = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(512, 128), nn.ReLU(), nn.Linear(128, 512), nn.Sigmoid() ) self.proj = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(512, feature_dim), nn.LayerNorm(feature_dim) ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: (B, 3, 64, 64) 眼部图像 Returns: feat: (B, feature_dim) 注意力增强的眼部特征 """ feat = self.backbone(x) spatial_weights = self.spatial_attn(feat) feat_spatial = feat * spatial_weights channel_weights = self.channel_attn(feat) feat_channel = feat_spatial * channel_weights.unsqueeze(-1).unsqueeze(-1) return self.proj(feat_channel)
class CrossModalAttentionFusion(nn.Module): """ 跨模态注意力融合 + 可学习门控 三个模态互相查询: - 眼部特征查询面部上下文 - 眼部特征查询头部姿态 - 面部查询眼部细节 门控决定最终各模态权重(非固定) """ def __init__(self, eye_dim, face_dim, head_dim, hidden_dim=256, num_heads=4): super().__init__() self.eye_proj = nn.Linear(eye_dim, hidden_dim) self.face_proj = nn.Linear(face_dim, hidden_dim) self.head_proj = nn.Linear(head_dim, hidden_dim) self.cross_attn = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=num_heads, batch_first=True ) self.gate = nn.Sequential( nn.Linear(hidden_dim * 3, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 3), nn.Softmax(dim=-1) ) def forward(self, eye_feat, face_feat, head_feat): """ Returns: fused: (B, hidden_dim) 融合特征 """ eye = self.eye_proj(eye_feat) face = self.face_proj(face_feat) head = self.head_proj(head_feat) seq = torch.stack([eye, face, head], dim=1) attended, _ = self.cross_attn(seq, seq, seq) concat = torch.cat([eye, face, head], dim=-1) weights = self.gate(concat) fused = (attended * weights.unsqueeze(-1)).sum(dim=1) return fused
class UncertaintyGazeRegressor(nn.Module): """ 不确定性感知视线回归器 使用异方差回归(heteroscedastic regression): - 预测 gaze (θ, φ) 和 variance σ² - σ² 大 → 不确定性高 → 预测不可信 - σ² 小 → 不确定性低 → 预测可信 损失函数:NLL损失 L = (1/2) * exp(-s) * ||y - ŷ||² + (1/2) * s 其中 s = log(σ²) 这让模型在难以预测的样本上自动增大σ² """ def __init__(self, input_dim, output_dim=2): super().__init__() self.shared = nn.Sequential( nn.Linear(input_dim, 128), nn.ReLU(), nn.Dropout(0.2) ) self.gaze_head = nn.Linear(128, output_dim) self.uncertainty_head = nn.Linear(128, 1) def forward(self, x): feat = self.shared(x) gaze = self.gaze_head(feat) log_var = self.uncertainty_head(feat) uncertainty = torch.exp(log_var) return { 'gaze': gaze, 'uncertainty': uncertainty, 'confidence': 1.0 / (1.0 + uncertainty) } def loss_function(self, pred_gaze, target_gaze, uncertainty): """异方差NLL损失""" nll = 0.5 * torch.exp(-torch.log(uncertainty + 1e-8)) * \ (pred_gaze - target_gaze).pow(2) + \ 0.5 * torch.log(uncertainty + 1e-8) return nll.mean()
if __name__ == "__main__": model = AttentiveGazeModel() B = 4 eye = torch.randn(B, 3, 64, 64) face = torch.randn(B, 3, 224, 224) head = torch.randn(B, 3) output = model(eye, face, head) print("=== AttentiveGaze 输出 ===") print(f"视线方向: {output['gaze'].shape}") print(f" pitch: {output['gaze'][0, 0]:.4f} rad = {torch.degrees(output['gaze'][0, 0]):.1f}°") print(f" yaw: {output['gaze'][0, 1]:.4f} rad = {torch.degrees(output['gaze'][0, 1]):.1f}°") print(f"不确定性(σ²): {output['uncertainty'][0]:.4f}") print(f"置信度: {output['confidence'][0]:.4f}")
|