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
| """ CNN-BiLSTM多模态情感识别 用于座舱内驾驶员情绪监控
架构: 1. 面部分支:CNN提取空间特征 2. 语音分支:CNN+BiLSTM提取时序特征 3. HRV分支:BiLSTM提取时序特征 4. 融合层:三模态特征拼接→全连接→情感分类 """
import torch import torch.nn as nn import torch.nn.functional as F
class FacialBranch(nn.Module): """面部表情分支:ResNet18 + Attention""" def __init__(self, num_classes=7): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 64, 7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(3, stride=2, padding=1), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.Conv2d(256, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d((4, 4)) ) self.attention = nn.Sequential( nn.Conv2d(256, 1, 1), nn.Sigmoid() ) self.fc = nn.Linear(256 * 4 * 4, 128) def forward(self, x): feat = self.features(x) att = self.attention(feat) feat = feat * att feat = feat.view(feat.size(0), -1) return self.fc(feat)
class VoiceBranch(nn.Module): """语音分支:CNN + BiLSTM""" def __init__(self, input_dim=80, hidden=128): super().__init__() self.cnn = nn.Sequential( nn.Conv2d(1, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((1, None)) ) self.bilstm = nn.LSTM(128, hidden, batch_first=True, bidirectional=True, num_layers=2) def forward(self, x): feat = self.cnn(x) feat = feat.squeeze(2) feat = feat.permute(0, 2, 1) feat, _ = self.bilstm(feat) feat = feat.mean(dim=1) return feat[:, :128]
class HRVBranch(nn.Module): """心率变异性分支:BiLSTM""" def __init__(self, input_dim=12, hidden=64): super().__init__() self.fc1 = nn.Linear(input_dim, 32) self.bilstm = nn.LSTM(32, hidden, batch_first=True, bidirectional=True, num_layers=1) self.fc2 = nn.Linear(hidden * 2, 128) def forward(self, x): feat = F.relu(self.fc1(x)) feat, _ = self.bilstm(feat) feat = feat[:, -1, :] return self.fc2(feat)
class MultimodalFusion(nn.Module): """多模态融合分类器""" def __init__(self, num_emotions=7): super().__init__() self.facial = FacialBranch() self.voice = VoiceBranch() self.hrv = HRVBranch() self.modality_weight = nn.Parameter(torch.tensor([1.0, 1.0, 1.0])) self.classifier = nn.Sequential( nn.Linear(128 * 3, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.2), nn.Linear(128, num_emotions) ) def forward(self, face, voice, hrv): f_feat = self.facial(face) v_feat = self.voice(voice) h_feat = self.hrv(hrv) w = F.softmax(self.modality_weight, dim=0) fused = torch.cat([ f_feat * w[0], v_feat * w[1], h_feat * w[2] ], dim=1) return self.classifier(fused)
if __name__ == "__main__": model = MultimodalFusion(num_emotions=7) batch_size = 4 face = torch.randn(batch_size, 3, 224, 224) voice = torch.randn(batch_size, 1, 80, 100) hrv = torch.randn(batch_size, 30, 12) output = model(face, voice, hrv) emotions = ['愤怒', '厌恶', '恐惧', '快乐', '悲伤', '惊讶', '中性'] print("=" * 50) print("CNN-BiLSTM多模态情感识别") print("=" * 50) print(f"模型参数量: {sum(p.numel() for p in model.parameters()):,}") print(f"模型大小: {sum(p.numel() for p in model.parameters())*4/1024/1024:.2f} MB") print("\n输出示例:") for i in range(batch_size): probs = F.softmax(output[i], dim=0) pred = emotions[probs.argmax()] print(f" 样本{i+1}: {pred} (置信度: {probs.max():.1%})") print("\n模态权重:") w = F.softmax(model.modality_weight, dim=0) print(f" 面部: {w[0]:.2f}") print(f" 语音: {w[1]:.2f}") print(f" HRV: {w[2]:.2f}")
|