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
| """ 论文:MFT: Multimodal Fusion Transformer for Phone Distraction arXiv: 2609.23507 会议:IEEE ICEICT 2026
核心方法:跨模态注意力融合姿态+视觉特征 """
import torch import torch.nn as nn import torch.nn.functional as F
class MFT(nn.Module): """ Multimodal Fusion Transformer (MFT) 论文核心模型: 1. 姿态分支: 从关键点提取骨骼动力学特征 2. 视觉分支: 从图像提取外观特征 3. 跨模态注意力: 多头交叉注意力融合 4. 时序注意力: Transformer编码器捕获时序依赖 5. 分类: 分心/正常二分类 """ def __init__(self, num_keypoints: int = 17, keypoint_dim: int = 2, visual_feat_dim: int = 512, hidden_dim: int = 256, num_heads: int = 8, num_layers: int = 4, num_classes: int = 2): super().__init__() self.pose_encoder = nn.Sequential( nn.Linear(num_keypoints * keypoint_dim, hidden_dim), nn.GELU(), nn.Linear(hidden_dim, hidden_dim), nn.GELU(), nn.Linear(hidden_dim, hidden_dim) ) self.visual_encoder = nn.Sequential( nn.Conv2d(3, 64, 3, stride=2, padding=1), nn.BatchNorm2d(64), nn.GELU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.GELU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.GELU(), nn.Conv2d(256, 512, 3, stride=2, padding=1), nn.BatchNorm2d(512), nn.GELU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(512, hidden_dim) ) self.cross_attn_p2v = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=num_heads, batch_first=True ) self.cross_attn_v2p = nn.MultiheadAttention( embed_dim=hidden_dim, num_heads=num_heads, batch_first=True ) self.norm_p = nn.LayerNorm(hidden_dim) self.norm_v = nn.LayerNorm(hidden_dim) encoder_layer = nn.TransformerEncoderLayer( d_model=hidden_dim * 2, nhead=num_heads, dim_feedforward=hidden_dim * 4, dropout=0.1, activation='gelu', batch_first=True ) self.temporal_encoder = nn.TransformerEncoder( encoder_layer, num_layers=num_layers ) self.classifier = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.GELU(), nn.Dropout(0.3), nn.Linear(hidden_dim, hidden_dim // 2), nn.GELU(), nn.Dropout(0.3), nn.Linear(hidden_dim // 2, num_classes) ) def forward(self, images, keypoints, seq_len=1): """ Args: images: (B, T, C, H, W) 图像序列 keypoints: (B, T, N, D) 关键点序列 seq_len: 时序长度 Returns: logits: (B, num_classes) """ B, T = images.shape[:2] images_flat = images.view(B * T, *images.shape[2:]) kp_flat = keypoints.view(B * T, -1) pose_feat = self.pose_encoder(kp_flat) pose_feat = pose_feat.view(B, T, -1) visual_feat = self.visual_encoder(images_flat) visual_feat = visual_feat.view(B, T, -1) p2v_attn, _ = self.cross_attn_p2v( visual_feat, pose_feat, pose_feat ) visual_enhanced = self.norm_v(visual_feat + p2v_attn) v2p_attn, _ = self.cross_attn_v2p( pose_feat, visual_feat, visual_feat ) pose_enhanced = self.norm_p(pose_feat + v2p_attn) fused = torch.cat([pose_enhanced, visual_enhanced], dim=-1) temporal_out = self.temporal_encoder(fused) last_output = temporal_out[:, -1, :] logits = self.classifier(last_output) return logits
if __name__ == "__main__": B, T = 4, 10 images = torch.randn(B, T, 3, 224, 224) keypoints = torch.randn(B, T, 17, 2) model = MFT(num_keypoints=17, keypoint_dim=2, hidden_dim=256) logits = model(images, keypoints) print(f"图像输入: {images.shape}") print(f"关键点输入: {keypoints.shape}") print(f"输出logits: {logits.shape}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}") print("\n=== 论文实验结果 ===") print(f"总体准确率: 95%") print(f"基线最佳: 89%") print(f"提升: +6%") print(f"数据集: 287行人, 20,741图像") print(f"模态: 姿态关键点 + 视觉外观") print(f"跨模态注意力: 多头交叉注意力") print(f"时序建模: Transformer编码器")
|