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
| class SceneGridAttention(nn.Module): """ 场景网格注意力模块 论文Section 4.3 核心: - Query: 面部多模态特征融合后的注视意图向量 - Key/Value: 场景网格特征 - 输出: 注意力权重分布 → 加权场景网格中心 → PoG PoG计算: PoG = Σ_i (attn_weight_i × grid_center_i) 性能: - UD-FSG数据集: 平均像素误差 96.16 (6.55%对角线) - LBW数据集: 平均像素误差 63.48 (5.98%对角线) - 相比SOTA降低 29.78% """ def __init__(self, gaze_intent_dim: int = 256, scene_feat_dim: int = 256, d_model: int = 256, nhead: int = 8, num_layers: int = 3, grid_size: int = 8): super().__init__() self.grid_size = grid_size self.gaze_proj = nn.Linear(gaze_intent_dim, d_model) self.scene_proj = nn.Linear(scene_feat_dim, d_model) self.pos_embed = nn.Linear(2, d_model) decoder_layer = nn.TransformerDecoderLayer( d_model=d_model, nhead=nhead, dim_feedforward=1024, dropout=0.1, batch_first=True ) self.transformer = nn.TransformerDecoder( decoder_layer, num_layers=num_layers ) self.attn_score = nn.Linear(d_model, 1) def forward(self, gaze_intent: torch.Tensor, scene_grid_feat: torch.Tensor) -> dict: """ Args: gaze_intent: (B, gaze_intent_dim) 面部注视意图向量 scene_grid_feat: (B, G*G, scene_feat_dim) 场景网格特征 Returns: { 'pog': (B, 2) 注视点 [x_norm, y_norm] 'attn_weights': (B, G*G) 注意力权重分布 'attn_map': (B, G, G) 2D注意力热图 } """ B, N, _ = scene_grid_feat.shape G = self.grid_size query = self.gaze_proj(gaze_intent).unsqueeze(1) kv = self.scene_proj(scene_grid_feat) grid_pos = self._get_grid_positions(G, device=query.device) pos = self.pos_embed(grid_pos).unsqueeze(0) kv = kv + pos output = self.transformer( tgt=query, memory=kv ) attn_scores = torch.matmul( query.squeeze(1), kv.transpose(-1, -2) ) / (query.size(-1) ** 0.5) attn_weights = torch.softmax(attn_scores, dim=-1) grid_centers = self._get_grid_centers(G, device=query.device) pog = torch.matmul(attn_weights, grid_centers) attn_map = attn_weights.view(B, G, G) return { 'pog': pog, 'attn_weights': attn_weights, 'attn_map': attn_map } def _get_grid_positions(self, G: int, device) -> torch.Tensor: """生成网格位置坐标 (G*G, 2)""" rows, cols = torch.meshgrid( torch.linspace(0, 1, G, device=device), torch.linspace(0, 1, G, device=device), indexing='ij' ) return torch.stack([rows.flatten(), cols.flatten()], dim=-1) def _get_grid_centers(self, G: int, device) -> torch.Tensor: """生成网格中心坐标 (G*G, 2)""" rows, cols = torch.meshgrid( torch.arange(G, device=device, dtype=torch.float32), torch.arange(G, device=device, dtype=torch.float32), indexing='ij' ) centers = torch.stack([ (cols + 0.5) / G, (rows + 0.5) / G ], dim=-1) return centers.view(G * G, 2)
class SGAPGaze(nn.Module): """ SGAP-Gaze 完整模型 三阶段流程: 1. 面部多模态特征提取 (Face + Eye + Iris → Gaze Intent Vector) 2. 场景网格特征提取 (Scene Image → Grid Features) 3. Transformer注意力融合 (Gaze Intent + Grid Features → PoG) 论文结果: - UD-FSG: 96.16 像素误差 (6.55% 对角线) - LBW: 63.48 像素误差 (5.98% 对角线) - 相比SOTA: 降低29.78%误差 """ def __init__(self, grid_size: int = 8): super().__init__() from torchvision import models resnet = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) self.face_encoder = nn.Sequential(*list(resnet.children())[:-1]) self.face_proj = nn.Linear(512, 256) resnet2 = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) self.eye_encoder = nn.Sequential(*list(resnet2.children())[:-1]) self.eye_proj = nn.Linear(512, 256) resnet3 = models.resnet18(weights=models.ResNet18_Weights.DEFAULT) self.iris_encoder = nn.Sequential(*list(resnet3.children())[:-1]) self.iris_proj = nn.Linear(512, 256) self.intent_fusion = nn.Sequential( nn.Linear(768, 512), nn.ReLU(), nn.Dropout(0.1), nn.Linear(512, 256), nn.ReLU(), ) self.scene_extractor = SceneGridFeatureExtractor( grid_size=grid_size, feat_dim=256 ) self.attention = SceneGridAttention( gaze_intent_dim=256, scene_feat_dim=256, d_model=256, nhead=8, num_layers=3, grid_size=grid_size ) def forward(self, face_img: torch.Tensor, eye_img: torch.Tensor, iris_img: torch.Tensor, scene_img: torch.Tensor) -> dict: """ Args: face_img: (B, 3, 224, 224) 面部ROI eye_img: (B, 3, 224, 224) 虹膜加权眼部ROI iris_img: (B, 3, 224, 224) 虹膜ROI scene_img: (B, 3, H, W) 场景图像 Returns: { 'pog': (B, 2) 注视点 'attn_map': (B, G, G) 注意力热图 } """ face_feat = self.face_encoder(face_img).flatten(1) face_feat = self.face_proj(face_feat) eye_feat = self.eye_encoder(eye_img).flatten(1) eye_feat = self.eye_proj(eye_feat) iris_feat = self.iris_encoder(iris_img).flatten(1) iris_feat = self.iris_proj(iris_feat) multi_feat = torch.cat([face_feat, eye_feat, iris_feat], dim=-1) gaze_intent = self.intent_fusion(multi_feat) grid_seq, grid_2d = self.scene_extractor(scene_img) result = self.attention(gaze_intent, grid_seq) return result
if __name__ == "__main__": model = SGAPGaze(grid_size=8) B = 2 face = torch.randn(B, 3, 224, 224) eye = torch.randn(B, 3, 224, 224) iris = torch.randn(B, 3, 224, 224) scene = torch.randn(B, 3, 720, 1280) output = model(face, eye, iris, scene) print(f"注视点 (归一化): {output['pog']}") print(f"注意力热图: {output['attn_map'].shape}") print(f"注意力分布: {output['attn_weights'][0]}")
|