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
| """ MultiNet 多分支视线估计框架 """
import torch import torch.nn as nn import torch.nn.functional as F
class MultiBranchGazeNet(nn.Module): """ MultiNet: 多分支视线估计网络 分支1: 眼部分支(高分辨率眼部裁剪) 分支2: 面部分支(完整人脸上下文) 分支3: 距离分支(人脸到摄像头度量距离) 分支4: 头部姿态分支(头部角度先验) """ def __init__(self, config: dict): super().__init__() self.eye_branch = self._build_eye_branch( config.get('eye_input_size', (64, 128)) ) self.face_branch = self._build_face_branch( config.get('face_input_size', (224, 224)) ) self.distance_branch = nn.Sequential( nn.Linear(1, 32), nn.ReLU(), nn.Linear(32, 64), nn.ReLU() ) self.head_pose_branch = nn.Sequential( nn.Linear(3, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU() ) eye_dim = config.get('eye_feat_dim', 128) face_dim = config.get('face_feat_dim', 256) dist_dim = 64 pose_dim = 128 fusion_input_dim = eye_dim + face_dim + dist_dim + pose_dim self.fusion = nn.Sequential( nn.Linear(fusion_input_dim, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, 2) ) self.distance_aware_gate = nn.Sequential( nn.Linear(1, 32), nn.ReLU(), nn.Linear(32, 4), nn.Softmax(dim=-1) ) def _build_eye_branch(self, input_size): """眼部特征提取分支""" return nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 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, 128) ) def _build_face_branch(self, input_size): """全脸特征提取分支""" return nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 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.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(256, 256) ) def forward(self, eye_img, face_img, distance, head_pose): """ Args: eye_img: (B, 3, H, W) 眼部裁剪 face_img: (B, 3, 224, 224) 全脸 distance: (B, 1) 人脸到摄像头距离 (米) head_pose: (B, 3) 头部姿态 (pitch, yaw, roll) """ eye_feat = self.eye_branch(eye_img) face_feat = self.face_branch(face_img) dist_feat = self.distance_branch(distance) pose_feat = self.head_pose_branch(head_pose) weights = self.distance_aware_gate(distance) feats = torch.stack([ eye_feat, face_feat, dist_feat, pose_feat ], dim=1) fused = torch.cat([ eye_feat, face_feat, dist_feat, pose_feat ], dim=-1) gaze = self.fusion(fused) return gaze, weights def loss(self, pred_gaze, target_gaze, pred_weights, distance): """复合损失函数""" mse = F.mse_loss(pred_gaze, target_gaze) ideal_weights = self._compute_ideal_weights(distance) reg = F.mse_loss(pred_weights, ideal_weights) return mse + 0.1 * reg def _compute_ideal_weights(self, distance): """根据距离计算理想权重(软目标)""" d = distance.squeeze(-1) eye_w = torch.exp(-d / 0.3) face_w = 1.0 - eye_w * 0.5 dist_w = torch.ones_like(d) * 0.1 pose_w = 1.0 - eye_w - face_w - dist_w pose_w = torch.clamp(pose_w, min=0.01) total = eye_w + face_w + dist_w + pose_w return torch.stack([ eye_w/total, face_w/total, dist_w/total, pose_w/total ], dim=-1)
if __name__ == "__main__": model = MultiBranchGazeNet({ 'eye_input_size': (64, 128), 'face_input_size': (224, 224), 'eye_feat_dim': 128, 'face_feat_dim': 256 }) batch = 4 eye = torch.randn(batch, 3, 64, 128) face = torch.randn(batch, 3, 224, 224) dist = torch.tensor([[0.4], [0.6], [0.8], [1.0]]) pose = torch.randn(batch, 3) gaze, weights = model(eye, face, dist, pose) print(f"视线预测: {gaze.shape}") print(f"分支权重 (近距离0.4m): {weights[0].detach()}") print(f"分支权重 (远距离1.0m): {weights[3].detach()}") target = torch.randn(batch, 2) loss = model.loss(gaze, target, weights, dist) print(f"损失: {loss.item():.4f}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}")
|