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 TransGazeObject(nn.Module): """ TransGaze-Object: 端到端注视目标预测 架构: 1. 面部分支: 面部特征 + 虹膜加权眼特征 2. 场景分支: YOLOv8 检测对象 + 空间特征 3. Transformer 交叉注意力: 计算面部-对象相似度 4. 注视目标分类 """ def __init__(self, face_dim=512, eye_dim=256, object_dim=512, hidden_dim=256, num_heads=8): super().__init__() self.face_encoder = FaceEncoder(face_dim, hidden_dim) self.eye_encoder = IrisWeightedEyeEncoder(eye_dim, hidden_dim) self.object_encoder = ObjectEncoder(object_dim, hidden_dim) self.cross_attn = nn.MultiheadAttention( hidden_dim, num_heads, batch_first=True ) self.norm = nn.LayerNorm(hidden_dim) self.gaze_classifier = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.GELU(), nn.Dropout(0.1), nn.Linear(hidden_dim, 1) ) def forward(self, face_img, eye_img, scene_objects): """ Args: face_img: (B, 3, 224, 224) 驾驶员面部 eye_img: (B, 3, 112, 112) 眼部裁剪 scene_objects: (B, N, 5) [x1,y1,x2,y2,class] 对象框 Returns: gaze_scores: (B, N) 每个对象的注视概率 """ face_feat = self.face_encoder(face_img) eye_feat = self.eye_encoder(eye_img) driver_feat = face_feat + eye_feat obj_feat = self.object_encoder(scene_objects) driver_query = driver_feat.unsqueeze(1) normed_q = self.norm(driver_query) normed_kv = self.norm(obj_feat) attn_out, attn_weights = self.cross_attn( normed_q, normed_kv, normed_kv ) driver_enhanced = attn_out.squeeze(1) scores = [] for i in range(scene_objects.shape[1]): pair = torch.cat([ driver_enhanced, obj_feat[:, i, :] ], dim=-1) score = self.gaze_classifier(pair) scores.append(score) gaze_scores = torch.cat(scores, dim=-1) gaze_scores = F.softmax(gaze_scores, dim=-1) return gaze_scores, attn_weights
class FaceEncoder(nn.Module): """面部特征编码器 (简化版 ResNet)""" def __init__(self, in_dim=3, out_dim=256): super().__init__() self.encoder = nn.Sequential( nn.Conv2d(in_dim, 64, 7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(256, out_dim) ) def forward(self, x): return self.encoder(x)
class IrisWeightedEyeEncoder(nn.Module): """虹膜加权眼部特征编码器""" def __init__(self, in_dim=3, out_dim=256): super().__init__() self.iris_detector = nn.Sequential( nn.Conv2d(in_dim, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 1, 1), nn.Sigmoid() ) self.eye_encoder = nn.Sequential( nn.Conv2d(in_dim, 64, 3, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(128, out_dim) ) def forward(self, x): iris_weight = self.iris_detector(x) weighted_x = x * iris_weight.expand_as(x) feat = self.eye_encoder(weighted_x) return feat
class ObjectEncoder(nn.Module): """场景对象编码器""" def __init__(self, in_dim=5, out_dim=256): super().__init__() self.encoder = nn.Sequential( nn.Linear(in_dim, 128), nn.GELU(), nn.Linear(128, out_dim) ) def forward(self, objects): """ Args: objects: (B, N, 5) [x1, y1, x2, y2, class] """ return self.encoder(objects)
if __name__ == "__main__": model = TransGazeObject() B, N = 2, 10 face = torch.randn(B, 3, 224, 224) eye = torch.randn(B, 3, 112, 112) objects = torch.rand(B, N, 5) scores, attn = model(face, eye, objects) print(f"Face: {face.shape}") print(f"Objects: {objects.shape}") print(f"Gaze scores: {scores.shape}") print(f"Attention: {attn.shape}") print(f"Top object: {scores.argmax(dim=-1)}") print(f"Params: {sum(p.numel() for p in model.parameters()):,}")
|