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
| class DualResolutionNetwork(nn.Module): """ 双分辨率网络 全局分支: - 低分辨率(如 256x256) - 理解整体场景 - 人眼位置检测 局部分支: - 高分辨率(如 128x128 per eye) - 精细眼睛特征 - 视线方向估计 """ def __init__(self): super().__init__() self.global_encoder = nn.Sequential( nn.Conv2d(3, 64, 7, stride=2, padding=3), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.ReLU() ) self.local_encoder = nn.Sequential( nn.Conv2d(3, 64, 3, padding=1), nn.ReLU(), nn.Conv2d(64, 128, 3, padding=1), nn.ReLU(), nn.Conv2d(128, 256, 3, padding=1), nn.ReLU() ) self.fusion = nn.Sequential( nn.Conv2d(512, 256, 1), nn.ReLU(), nn.Conv2d(256, 128, 1) ) self.gaze_regressor = nn.Sequential( nn.Linear(128 * 8 * 8, 512), nn.ReLU(), nn.Linear(512, 3) ) def forward(self, fisheye_image, eye_locations): """ 前向传播 Args: fisheye_image: 鱼眼图像 (B, 3, H, W) eye_locations: 眼睛位置列表 [(x, y, w, h), ...] """ global_feat = self.global_encoder(fisheye_image) local_features = [] for eyes in eye_locations: left_eye_crop = self._crop_eye(fisheye_image, eyes['left']) right_eye_crop = self._crop_eye(fisheye_image, eyes['right']) left_feat = self.local_encoder(left_eye_crop) right_feat = self.local_encoder(right_eye_crop) eye_feat = left_feat + right_feat local_features.append(eye_feat) gaze_vectors = [] for i, local_feat in enumerate(local_features): local_upsampled = F.interpolate( local_feat, size=global_feat.shape[2:] ) fused = torch.cat([global_feat, local_upsampled], dim=1) fused = self.fusion(fused) flattened = fused.view(fused.size(0), -1) gaze = self.gaze_regressor(flattened) gaze = F.normalize(gaze, dim=1) gaze_vectors.append(gaze) return gaze_vectors def _crop_eye(self, image, eye_box): """裁剪眼睛区域""" x, y, w, h = eye_box return image[:, :, y:y+h, x:x+w]
|