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
| """ CNN-LSTM睡眠检测模型 参考:arXiv 2024 Survey ResearchGate CNN-LSTM + PERCLOS + Yawning
架构: 1. CNN提取面部特征 2. LSTM建模时序依赖 3. 多特征融合(PERCLOS + 哈欠频率) """
import torch import torch.nn as nn import numpy as np
class CNNLSTM_Drowsiness(nn.Module): """ CNN-LSTM睡眠检测模型 结构: - CNN特征提取:ResNet/MobileNet - LSTM时序建模:捕捉眨眼/哈欠序列 - 多特征融合:PERCLOS + Yawning + Head Pose """ def __init__( self, cnn_feature_dim: int = 512, lstm_hidden_dim: int = 256, lstm_layers: int = 2, additional_features: int = 10, num_classes: int = 3, dropout: float = 0.3 ): super().__init__() self.cnn = 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.MaxPool2d(2), nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(256, cnn_feature_dim, 3, padding=1), nn.BatchNorm2d(cnn_feature_dim), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.lstm = nn.LSTM( input_size=cnn_feature_dim + additional_features, hidden_size=lstm_hidden_dim, num_layers=lstm_layers, batch_first=True, dropout=dropout, bidirectional=True ) self.classifier = nn.Sequential( nn.Linear(lstm_hidden_dim * 2, lstm_hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(lstm_hidden_dim, num_classes) ) def forward( self, frame_sequence: torch.Tensor, additional_features: torch.Tensor ) -> torch.Tensor: """ 前向传播 Args: frame_sequence: 视频帧序列, shape=(B, T, C, H, W) additional_features: PERCLOS/Yawning等特征, shape=(B, T, F) Returns: logits: 分类输出, shape=(B, num_classes) """ B, T, C, H, W = frame_sequence.shape cnn_features = [] for t in range(T): frame = frame_sequence[:, t] feat = self.cnn(frame) feat = feat.view(B, -1) cnn_features.append(feat) cnn_features = torch.stack(cnn_features, dim=1) combined_features = torch.cat([ cnn_features, additional_features ], dim=-1) lstm_out, (h_n, c_n) = self.lstm(combined_features) final_hidden = lstm_out[:, -1, :] logits = self.classifier(final_hidden) return logits
if __name__ == "__main__": model = CNNLSTM_Drowsiness( cnn_feature_dim=512, lstm_hidden_dim=256, additional_features=10, num_classes=3 ) frames = torch.randn(4, 30, 3, 112, 112) features = torch.randn(4, 30, 10) logits = model(frames, features) probs = torch.softmax(logits, dim=-1) print(f"输出shape: {logits.shape}") print(f"预测类别分布:") classes = ['正常', '疲劳', '睡眠'] for i in range(4): for j, cls in enumerate(classes): print(f" 样本{i+1} {cls}: {probs[i, j].item():.2%}")
|