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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| import torch import torch.nn as nn import torch.nn.functional as F
class RotationConstrainedGazeFusion(nn.Module): """ 旋转约束跨视角视线融合 WACV 2024 论文核心: 多摄像头视角不同,需旋转约束对齐 关键: 1. 旋转不变特征提取 2. 跨视角特征融合 3. 约束优化视线估计 """ def __init__(self, num_views: int = 2, feature_dim: int = 256, gaze_dim: int = 2): super().__init__() self.view_encoder = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.ReLU(inplace=True), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(inplace=True), nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d((1, 1)) ) self.rotation_constraint = RotationConstraintModule(feature_dim) self.fusion_module = CrossViewFusion(num_views, feature_dim) self.gaze_head = nn.Linear(feature_dim, gaze_dim) self.num_views = num_views def forward(self, multi_view_images: torch.Tensor, view_rotations: torch.Tensor) -> dict: """ 前向传播 Args: multi_view_images: 多视角图像, shape=(B, num_views, C, H, W) view_rotations: 各视角相对旋转角度, shape=(B, num_views, 3) (pitch, yaw, roll) Returns: dict: { "fused_gaze": 融合视线估计, shape=(B, 2) "view_gazes": 各视角视线估计, shape=(B, num_views, 2) "fusion_weights": 融合权重, shape=(B, num_views) "rotation_error": 旋转约束误差, shape=(B, 1) } """ B = multi_view_images.size(0) view_features = [] for i in range(self.num_views): view_img = multi_view_images[:, i] features = self.view_encoder(view_img).view(B, -1) view_features.append(features) view_features_stack = torch.stack(view_features, dim=1) aligned_features, rotation_error = self.rotation_constraint( view_features_stack, view_rotations ) fused_features, fusion_weights = self.fusion_module(aligned_features) fused_gaze = self.gaze_head(fused_features) view_gazes = [] for i in range(self.num_views): view_gaze = self.gaze_head(aligned_features[:, i]) view_gazes.append(view_gaze) view_gazes = torch.stack(view_gazes, dim=1) return { "fused_gaze": fused_gaze, "view_gazes": view_gazes, "fusion_weights": fusion_weights, "rotation_error": rotation_error }
class RotationConstraintModule(nn.Module): """ 旋转约束模块 论文方法: 不同视角需旋转对齐才能融合 约束: 旋转后的视线应满足几何一致性 """ def __init__(self, feature_dim: int): super().__init__() self.rotation_encoder = nn.Sequential( nn.Linear(3, 64), nn.ReLU(inplace=True), nn.Linear(64, feature_dim) ) self.alignment_head = nn.Linear(feature_dim * 2, feature_dim) def forward(self, view_features: torch.Tensor, rotations: torch.Tensor) -> tuple: """ 旋转对齐 Args: view_features: 各视角特征, shape=(B, num_views, feature_dim) rotations: 旋转角度, shape=(B, num_views, 3) Returns: aligned_features: 对齐后的特征 rotation_error: 旋转一致性误差 """ B, num_views, feature_dim = view_features.shape rotation_features = self.rotation_encoder(rotations) aligned_features = [] for i in range(num_views): combined = torch.cat([ view_features[:, i], rotation_features[:, i] ], dim=1) aligned = self.alignment_head(combined) aligned_features.append(aligned) aligned_features = torch.stack(aligned_features, dim=1) rotation_error = self._compute_rotation_consistency( aligned_features, rotations ) return aligned_features, rotation_error def _compute_rotation_consistency(self, features: torch.Tensor, rotations: torch.Tensor) -> torch.Tensor: """ 旋转一致性计算 不同视角估计的视线,旋转到同一坐标系后应一致 """ similarity_matrix = F.cosine_similarity( features[:, 0].unsqueeze(1), features[:, 1].unsqueeze(1), dim=2 ) consistency_error = 1 - similarity_matrix.mean() return consistency_error.unsqueeze(1)
class CrossViewFusion(nn.Module): """ 跨视角融合模块 论文方法: 加权融合多视角视线估计 权重基于: - 视角质量(遮挡、光照) - 旋转角度(正面权重高) """ def __init__(self, num_views: int, feature_dim: int): super().__init__() self.weight_predictor = nn.Sequential( nn.Linear(feature_dim * num_views, 128), nn.ReLU(inplace=True), nn.Linear(128, num_views) ) def forward(self, aligned_features: torch.Tensor) -> tuple: """ 融合 Args: aligned_features: 对齐后的特征, shape=(B, num_views, feature_dim) Returns: fused_features: 融合特征 fusion_weights: 融合权重 """ B, num_views, feature_dim = aligned_features.shape features_flat = aligned_features.view(B, -1) weights_logits = self.weight_predictor(features_flat) fusion_weights = F.softmax(weights_logits, dim=1) fused_features = torch.sum( aligned_features * fusion_weights.unsqueeze(2), dim=1 ) return fused_features, fusion_weights
if __name__ == "__main__": model = RotationConstrainedGazeFusion(num_views=2) multi_view_images = torch.randn(2, 2, 3, 224, 224) view_rotations = torch.tensor([ [[0, 0, 0], [0, 30, 0]], [[0, 15, 0], [0, 45, 0]], ], dtype=torch.float32) output = model(multi_view_images, view_rotations) print("=== 多视角视线融合结果 ===") print(f"融合视线: {output['fused_gaze']}") print(f"各视角视线: {output['view_gazes']}") print(f"融合权重: {output['fusion_weights']}") print(f"旋转一致性误差: {output['rotation_error']}")
|