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 GazeDPTR(nn.Module): """ GazeDPTR: Dual-stream Gaze Pyramid Transformer CVPR 2024论文核心方法实现 双流处理: 原始图像 + 归一化图像 金字塔特征: 多层级面部特征 Args: feat_dim: 特征维度 num_heads: 注意力头数 num_layers: Transformer层数 """ def __init__(self, feat_dim=512, num_heads=8, num_layers=4): super().__init__() self.raw_stream = PyramidBackbone( in_channels=3, feat_dim=feat_dim, levels=3 ) self.normalized_stream = PyramidBackbone( in_channels=3, feat_dim=feat_dim, levels=3 ) self.camera_pose_embed = nn.Sequential( nn.Linear(6, feat_dim // 4), nn.GELU(), nn.Linear(feat_dim // 4, feat_dim) ) self.fusion_transformer = DualStreamFusion( d_model=feat_dim, num_heads=num_heads, num_layers=num_layers ) self.gaze_head = nn.Sequential( nn.Linear(feat_dim, feat_dim // 2), nn.GELU(), nn.Linear(feat_dim // 2, 3) ) def forward(self, raw_img, normalized_img, camera_pose): """ Args: raw_img: (B, 3, 224, 224) 原始驾驶员面部图像 normalized_img: (B, 3, 224, 224) 视角归一化图像 camera_pose: (B, 6) 6DoF摄像头姿态 Returns: gaze_vector: (B, 3) 3D注视方向向量 """ raw_feat = self.raw_stream(raw_img) norm_feat = self.normalized_stream(normalized_img) pose_embed = self.camera_pose_embed(camera_pose) fused = self.fusion_transformer( raw_feat, norm_feat, pose_embed ) gaze = self.gaze_head(fused) gaze = F.normalize(gaze, p=2, dim=-1) return gaze
class PyramidBackbone(nn.Module): """金字塔特征提取器:多层级面部特征""" def __init__(self, in_channels=3, feat_dim=512, levels=3): super().__init__() self.levels = levels self.convs = nn.ModuleList([ nn.Sequential( nn.Conv2d(in_channels if i == 0 else 64*(2**i), 64*(2**(i+1)), 3, 2, 1), nn.BatchNorm2d(64*(2**(i+1))), nn.ReLU() ) for i in range(levels) ]) self.projections = nn.ModuleList([ nn.Linear(64 * (2**(i+1)), feat_dim // levels) for i in range(levels) ]) self.fusion = nn.Linear(feat_dim, feat_dim) def forward(self, x): features = [] feat = x for i in range(self.levels): feat = self.convs[i](feat) pooled = F.adaptive_avg_pool2d(feat, 1).flatten(1) projected = self.projections[i](pooled) features.append(projected) combined = torch.cat(features, dim=-1) return self.fusion(combined)
class DualStreamFusion(nn.Module): """双流Transformer融合""" def __init__(self, d_model=512, num_heads=8, num_layers=4): super().__init__() cross_attn_layer = nn.TransformerDecoderLayer( d_model=d_model, nhead=num_heads, dim_feedforward=d_model * 4, dropout=0.1, activation='gelu', batch_first=True ) self.cross_attn = nn.TransformerDecoder(cross_attn_layer, num_layers) self.pose_gate = nn.Sequential( nn.Linear(d_model, d_model), nn.Sigmoid() ) def forward(self, raw_feat, norm_feat, pose_embed): """ 双流融合 + 摄像头姿态引导 """ raw_query = raw_feat.unsqueeze(1) norm_kv = norm_feat.unsqueeze(1) fused = self.cross_attn(raw_query, norm_kv, norm_kv) fused = fused.squeeze(1) gate = self.pose_gate(pose_embed) fused = fused * gate + fused return fused
if __name__ == "__main__": model = GazeDPTR(feat_dim=512, num_heads=8, num_layers=4) raw = torch.randn(4, 3, 224, 224) norm = torch.randn(4, 3, 224, 224) pose = torch.randn(4, 6) gaze = model(raw, norm, pose) print(f"Raw image: {raw.shape}") print(f"Normalized image: {norm.shape}") print(f"Camera pose: {pose.shape}") print(f"Gaze vector: {gaze.shape}") print(f"Parameters: {sum(p.numel() for p in model.parameters()):,}") norms = torch.norm(gaze, dim=-1) print(f"Gaze vector norms: {norms}") print("✅ 应接近1.0")
|