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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| """ Transformer vs CNN 驾驶员疲劳检测对比实验 基于: Scientific Reports (2025) 论文复现
核心发现: - ViT 99.15% > VGG19 98.7% > ResNet50 97.3% - Transformer 在长距离面部特征关联上优势明显 - Swin Transformer 更适合实时部署 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple, Dict import time
class PatchEmbedding(nn.Module): """图像分块嵌入""" def __init__(self, img_size=224, patch_size=16, in_ch=3, embed_dim=768): super().__init__() self.num_patches = (img_size // patch_size) ** 2 self.proj = nn.Conv2d( in_ch, embed_dim, kernel_size=patch_size, stride=patch_size ) self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim)) self.pos_embed = nn.Parameter( torch.randn(1, self.num_patches + 1, embed_dim) ) def forward(self, x): B = x.shape[0] x = self.proj(x) x = x.flatten(2).transpose(1, 2) cls = self.cls_token.expand(B, -1, -1) x = torch.cat([cls, x], dim=1) x = x + self.pos_embed return x
class MultiHeadSelfAttention(nn.Module): """多头自注意力""" def __init__(self, embed_dim=768, num_heads=12, dropout=0.1): super().__init__() self.num_heads = num_heads self.head_dim = embed_dim // num_heads self.scale = self.head_dim ** -0.5 self.qkv = nn.Linear(embed_dim, embed_dim * 3) self.proj = nn.Linear(embed_dim, embed_dim) self.dropout = nn.Dropout(dropout) def forward(self, x): B, N, D = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim) qkv = qkv.permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) attn = self.dropout(attn) out = (attn @ v).transpose(1, 2).reshape(B, N, D) out = self.proj(out) return out
class ViTBlock(nn.Module): """ViT 编码器块""" def __init__(self, embed_dim=768, num_heads=12, mlp_ratio=4, dropout=0.1): super().__init__() self.norm1 = nn.LayerNorm(embed_dim) self.attn = MultiHeadSelfAttention(embed_dim, num_heads, dropout) self.norm2 = nn.LayerNorm(embed_dim) self.mlp = nn.Sequential( nn.Linear(embed_dim, embed_dim * mlp_ratio), nn.GELU(), nn.Dropout(dropout), nn.Linear(embed_dim * mlp_ratio, embed_dim), nn.Dropout(dropout) ) def forward(self, x): x = x + self.attn(self.norm1(x)) x = x + self.mlp(self.norm2(x)) return x
class DrowsinessViT(nn.Module): """ViT 疲劳检测模型""" def __init__( self, img_size=224, patch_size=16, in_ch=3, embed_dim=768, depth=12, num_heads=12, num_classes=2 ): super().__init__() self.patch_embed = PatchEmbedding( img_size, patch_size, in_ch, embed_dim ) self.blocks = nn.ModuleList([ ViTBlock(embed_dim, num_heads) for _ in range(depth) ]) self.norm = nn.LayerNorm(embed_dim) self.head = nn.Linear(embed_dim, num_classes) def forward(self, x): x = self.patch_embed(x) for block in self.blocks: x = block(x) x = self.norm(x) cls = x[:, 0] return self.head(cls)
class DrowsinessCNN(nn.Module): """CNN 基线 (VGG19-style)""" def __init__(self, num_classes=2): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 64, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(64, 64, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(128, 128, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(256, 512, 3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(512, 512, 3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(2), ) self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(512, 256), nn.ReLU(inplace=True), nn.Dropout(0.5), nn.Linear(256, num_classes) ) def forward(self, x): return self.classifier(self.features(x))
class DrowsinessDetectionSystem: """完整疲劳检测系统""" DROWSINESS_THRESHOLD = 0.7 CLOSED_EYE_DURATION = 2.0 def __init__(self, model, device='cpu'): self.model = model.to(device).eval() self.device = device self.eye_closed_start = None def process_frame(self, frame, timestamp): """ 处理单帧 Returns: drowsy: bool confidence: float """ tensor = self._preprocess(frame) with torch.no_grad(): logits = self.model(tensor.to(self.device)) probs = F.softmax(logits, dim=-1) closed_prob = probs[0, 1].item() drowsy = False if closed_prob > self.DROWSINESS_THRESHOLD: if self.eye_closed_start is None: self.eye_closed_start = timestamp elif timestamp - self.eye_closed_start > self.CLOSED_EYE_DURATION: drowsy = True else: self.eye_closed_start = None return drowsy, closed_prob def _preprocess(self, frame): """预处理""" import cv2 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) resized = cv2.resize(rgb, (224, 224)) normalized = resized.astype(np.float32) / 255.0 mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) normalized = (normalized - mean) / std return torch.from_numpy(normalized).permute(2, 0, 1).unsqueeze(0)
if __name__ == "__main__": print("=" * 60) print("Transformer vs CNN 疲劳检测对比") print("=" * 60) models = { 'ViT (12层)': DrowsinessViT(depth=12, embed_dim=768, num_heads=12), 'ViT-Small (6层)': DrowsinessViT(depth=6, embed_dim=384, num_heads=6), 'CNN (VGG19-style)': DrowsinessCNN(), } dummy = torch.randn(1, 3, 224, 224) print(f"\n{'模型':<20} {'参数量':<15} {'推理(ms)':<12} {'准确率*':<10}") print("-" * 60) for name, model in models.items(): model.eval() params = sum(p.numel() for p in model.parameters()) / 1e6 times = [] with torch.no_grad(): for _ in range(50): t0 = time.time() _ = model(dummy) times.append((time.time() - t0) * 1000) avg_ms = np.mean(times[5:]) acc = {'ViT (12层)': 99.15, 'ViT-Small (6层)': 97.8, 'CNN (VGG19-style)': 98.7}[name] print(f"{name:<20} {params:.1f}M{'':<10} {avg_ms:.1f}{'':<8} {acc}%") print("\n* 准确率为论文报告值 (MRL数据集)") print("\n" + "=" * 60) print("实时疲劳检测系统演示") print("=" * 60) system = DrowsinessDetectionSystem( DrowsinessViT(depth=6, embed_dim=384, num_heads=6) ) print("\n模拟30秒驾驶视频:") alert_count = 0 for i in range(30 * 15): frame = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) ts = i / 15.0 drowsy, conf = system.process_frame(frame, ts) if drowsy: alert_count += 1 if alert_count <= 3: print(f" [{ts:.1f}s] ⚠️ 疲劳警报! 闭眼概率: {conf:.2%}") print(f"\n总警报数: {alert_count}")
|