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
| import torch import torch.nn as nn import torch.nn.functional as F
class TransGazeObject(nn.Module): """ TransGaze-Object: 基于Transformer的驾驶员注视物体预测框架 论文: arXiv:2609.10139 输入: 驾驶员面部图像 + 交通场景图像 输出: 注视物体类别 + 边界框 核心创新: 1. 面部特征: 人脸特征 + 虹膜加权眼部特征 2. 场景特征: 物体边界框 + 空间位置编码 3. Transformer交叉注意力: 计算面部-物体相似度 """ def __init__(self, num_objects=10, face_feat_dim=512, scene_feat_dim=512, num_heads=8, num_layers=4): super().__init__() self.face_encoder = FaceEncoder(feat_dim=face_feat_dim) self.scene_encoder = SceneObjectEncoder(feat_dim=scene_feat_dim) self.spatial_pos_enc = SpatialPositionEncoding(d_model=scene_feat_dim) cross_attn_layer = nn.TransformerDecoderLayer( d_model=face_feat_dim, nhead=num_heads, dim_feedforward=face_feat_dim * 4, dropout=0.1, activation='gelu', batch_first=True ) self.cross_attn = nn.TransformerDecoder(cross_attn_layer, num_layers=num_layers) self.gaze_object_head = nn.Sequential( nn.Linear(face_feat_dim, face_feat_dim // 2), nn.GELU(), nn.Linear(face_feat_dim // 2, num_objects + 1) ) self.attn_weights = None def forward(self, face_img, scene_img, object_boxes=None): """ Args: face_img: (B, 3, 224, 224) 驾驶员面部图像 scene_img: (B, 3, H, W) 交通场景图像 object_boxes: (B, N, 4) 检测到的物体边界框 Returns: gaze_object_logits: (B, N+1) 注视物体分类logits attention_weights: (B, N) 注意力权重 """ face_feat = self.face_encoder(face_img) face_feat = face_feat.unsqueeze(1) scene_feat = self.scene_encoder(scene_img, object_boxes) scene_feat = self.spatial_pos_enc(scene_feat, object_boxes) gaze_feat, self.attn_weights = self.cross_attn( face_feat, scene_feat, scene_feat, need_weights=True ) gaze_feat = gaze_feat.squeeze(1) logits = self.gaze_object_head(gaze_feat) return logits, self.attn_weights
class FaceEncoder(nn.Module): """面部特征提取器:人脸特征 + 虹膜加权眼部特征""" def __init__(self, feat_dim=512): super().__init__() self.face_backbone = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(3, 2, 1), nn.Conv2d(64, 128, 3, 1, 1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, feat_dim) ) self.iris_encoder = nn.Sequential( nn.Conv2d(3, 32, 3, 1, 1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, 1, 1), nn.BatchNorm2d(64), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(64, feat_dim // 4) ) self.fusion = nn.Linear(feat_dim + feat_dim // 4, feat_dim) def forward(self, face_img): face_feat = self.face_backbone(face_img) eye_region = face_img[:, :, 60:120, 80:160] iris_feat = self.iris_encoder(eye_region) combined = torch.cat([face_feat, iris_feat], dim=-1) return self.fusion(combined)
class SceneObjectEncoder(nn.Module): """场景物体特征提取器""" def __init__(self, feat_dim=512): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, 1, 1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, feat_dim) ) self.proj = nn.Linear(feat_dim, feat_dim) def forward(self, scene_img, object_boxes=None): """ 提取场景全局特征,如果有物体框则提取每个物体特征 """ B = scene_img.shape[0] global_feat = self.backbone(scene_img) if object_boxes is None: return global_feat.unsqueeze(1) N = object_boxes.shape[1] obj_feats = [] for i in range(N): box_feat = self.proj(global_feat) obj_feats.append(box_feat) return torch.stack(obj_feats, dim=1)
class SpatialPositionEncoding(nn.Module): """空间位置编码:物体边界框位置信息""" def __init__(self, d_model=512): super().__init__() self.box_proj = nn.Linear(4, d_model) def forward(self, feat, boxes): if boxes is None: return feat pos_enc = self.box_proj(boxes) return feat + pos_enc
if __name__ == "__main__": model = TransGazeObject(num_objects=10, face_feat_dim=512) face = torch.randn(4, 3, 224, 224) scene = torch.randn(4, 3, 256, 256) boxes = torch.rand(4, 5, 4) logits, attn = model(face, scene, boxes) print(f"Face input: {face.shape}") print(f"Scene input: {scene.shape}") print(f"Gaze object logits: {logits.shape}") print(f"Attention weights: {attn.shape}") print(f"Parameters: {sum(p.numel() for p in model.parameters()):,}")
|