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
| """ SGAP-Gaze 场景网格注意力机制
核心创新:使用 Transformer 注意力融合视线意图和场景网格 """
import torch import torch.nn as nn import torch.nn.functional as F
class SceneGridAttention(nn.Module): """ 场景网格注意力模块 将场景图像划分为网格,使用 Transformer 注意力 计算每个网格位置的视线相关性 """ def __init__( self, gaze_intent_dim: int = 256, scene_feature_dim: int = 512, num_heads: int = 8, grid_size: int = 7 ): super().__init__() self.grid_size = grid_size self.num_heads = num_heads self.gaze_proj = nn.Linear(gaze_intent_dim, scene_feature_dim) self.scene_proj = nn.Linear(scene_feature_dim, scene_feature_dim) self.attention = nn.MultiheadAttention( embed_dim=scene_feature_dim, num_heads=num_heads, batch_first=True ) self.output_proj = nn.Linear(scene_feature_dim, 2) def forward( self, gaze_intent: torch.Tensor, scene_features: torch.Tensor ) -> torch.Tensor: """ Args: gaze_intent: 视线意图向量 scene_features: 场景特征图 Returns: pog: 视线落点坐标 (B, 2) """ B, C, H, W = scene_features.shape cell_H = H // self.grid_size cell_W = W // self.grid_size scene_grid = F.adaptive_avg_pool2d( scene_features, (self.grid_size, self.grid_size) ) scene_grid = scene_grid.flatten(2) scene_grid = scene_grid.transpose(1, 2) gaze_query = self.gaze_proj(gaze_intent) gaze_query = gaze_query.unsqueeze(1) scene_keys = self.scene_proj(scene_grid) scene_values = scene_grid attn_output, attn_weights = self.attention( query=gaze_query, key=scene_keys, value=scene_values ) attn_weights = attn_weights.squeeze(1) grid_coords = torch.stack(torch.meshgrid( torch.linspace(0, 1, self.grid_size), torch.linspace(0, 1, self.grid_size) ), dim=-1).flatten(0, 1).to(gaze_intent.device) pog = torch.matmul(attn_weights, grid_coords) return pog, attn_weights
class SGAPGaze(nn.Module): """ SGAP-Gaze 完整模型 融合面部、眼部、虹膜特征,使用场景网格注意力预测视线落点 """ def __init__(self): super().__init__() self.face_encoder = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.ReLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.eye_encoder = nn.Sequential( nn.Conv2d(3, 64, 3, 2, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.iris_encoder = nn.Sequential( nn.Conv2d(3, 32, 3, 1, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.fusion = nn.Sequential( nn.Linear(256 + 128 + 32, 256), nn.ReLU(), nn.Linear(256, 256) ) self.scene_encoder = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.ReLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(), nn.Conv2d(256, 512, 3, 2, 1), nn.ReLU() ) self.scene_attention = SceneGridAttention( gaze_intent_dim=256, scene_feature_dim=512 ) def forward( self, face_image: torch.Tensor, eye_image: torch.Tensor, iris_image: torch.Tensor, scene_image: torch.Tensor ) -> torch.Tensor: """ Args: face_image: (B, 3, 224, 224) eye_image: (B, 3, 64, 64) iris_image: (B, 3, 32, 32) scene_image: (B, 3, 224, 224) Returns: pog: (B, 2) 视线落点坐标 [0, 1] """ face_feat = self.face_encoder(face_image).flatten(1) eye_feat = self.eye_encoder(eye_image).flatten(1) iris_feat = self.iris_encoder(iris_image).flatten(1) fused_feat = torch.cat([face_feat, eye_feat, iris_feat], dim=1) gaze_intent = self.fusion(fused_feat) scene_feat = self.scene_encoder(scene_image) pog, attn_weights = self.scene_attention(gaze_intent, scene_feat) return pog, attn_weights
if __name__ == "__main__": model = SGAPGaze() face = torch.randn(1, 3, 224, 224) eye = torch.randn(1, 3, 64, 64) iris = torch.randn(1, 3, 32, 32) scene = torch.randn(1, 3, 224, 224) pog, attn = model(face, eye, iris, scene) print(f"视线落点: ({pog[0, 0]:.3f}, {pog[0, 1]:.3f})") print(f"注意力权重形状: {attn.shape}")
|