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
| """ 多模态疲劳检测架构
驾驶员姿态 + 面部状态联合分析 """
import torch import torch.nn as nn import torch.nn.functional as F
class FacialStateEncoder(nn.Module): """ 面部状态编码器 提取 PERCLOS、打哈欠、视线特征 """ def __init__(self): super().__init__() self.face_cnn = nn.Sequential( nn.Conv2d(3, 64, 7, 2, 3), nn.ReLU(), nn.MaxPool2d(3, 2), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.perclos_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 1) ) self.yawn_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 1) ) self.gaze_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 2) ) def forward(self, face_image): """ Args: face_image: (B, 3, 224, 224) Returns: facial_features: (B, 256+1+1+2) """ features = self.face_cnn(face_image).flatten(1) perclos = self.perclos_head(features) yawn = torch.sigmoid(self.yawn_head(features)) gaze = self.gaze_head(features) return torch.cat([features, perclos, yawn, gaze], dim=1)
class DrivingPostureEncoder(nn.Module): """ 驾驶员姿态编码器 提取握方向盘、身体晃动、头部姿态特征 """ def __init__(self, seq_len: int = 300): """ Args: seq_len: 时间序列长度(10秒 @ 30fps) """ super().__init__() self.skeleton_encoder = nn.Sequential( nn.Conv1d(3, 64, 3, 1, 1), nn.ReLU(), nn.Conv1d(64, 128, 3, 1, 1), nn.ReLU(), nn.Conv1d(128, 256, 3, 1, 1), nn.AdaptiveAvgPool1d(1) ) self.temporal_encoder = nn.LSTM( input_size=256, hidden_size=128, num_layers=2, batch_first=True, bidirectional=True ) self.head_pose_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 3) ) self.body_motion_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 1) ) def forward(self, skeleton_sequence): """ Args: skeleton_sequence: (B, seq_len, 17, 3) Returns: posture_features: (B, 256) """ B, T, J, C = skeleton_sequence.shape skeleton_flat = skeleton_sequence.view(B, T, J * C) temporal_features, _ = self.temporal_encoder(skeleton_flat) last_features = temporal_features[:, -1, :] return last_features
class MultiModalFatigueDetector(nn.Module): """ 多模态疲劳检测器 融合面部状态和驾驶员姿态 """ def __init__(self): super().__init__() self.facial_encoder = FacialStateEncoder() self.posture_encoder = DrivingPostureEncoder() self.fusion = nn.Sequential( nn.Linear(256 + 260, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU(), ) self.classifier = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 3) ) self.attention = nn.Sequential( nn.Linear(256 + 260, 64), nn.ReLU(), nn.Linear(64, 2), nn.Softmax(dim=1) ) def forward(self, face_image, skeleton_sequence): """ Args: face_image: (B, 3, 224, 224) skeleton_sequence: (B, seq_len, 17, 3) Returns: fatigue_level: (B, 3) attention_weights: (B, 2) """ facial_features = self.facial_encoder(face_image) posture_features = self.posture_encoder(skeleton_sequence) concat_features = torch.cat([posture_features, facial_features], dim=1) attention_weights = self.attention(concat_features) weighted_facial = facial_features * attention_weights[:, 1:2] weighted_posture = posture_features * attention_weights[:, 0:1] fused = torch.cat([weighted_posture, weighted_facial], dim=1) features = self.fusion(fused) fatigue_logits = self.classifier(features) return fatigue_logits, attention_weights
if __name__ == "__main__": model = MultiModalFatigueDetector() face = torch.randn(2, 3, 224, 224) skeleton = torch.randn(2, 300, 17, 3) logits, attention = model(face, skeleton) print(f"疲劳等级: {logits.argmax(dim=1)}") print(f"注意力权重 [姿态, 面部]: {attention}")
|