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
| class CDATWModel(nn.Module): """ CDATW疲劳检测模型 架构: 1. MobileNetV3 + CBAM 空间特征提取 2. BiLSTM 时序建模 3. Monte Carlo Dropout 不确定性估计 4. 自适应窗口控制器 """ def __init__(self, config: dict): super().__init__() self.backbone = MobileNetV3Encoder( width_mult=0.75, output_dim=128 ) self.cbam = CBAM( channels=128, reduction_ratio=16, kernel_size=7 ) self.temporal_encoder = nn.LSTM( input_size=128, hidden_size=64, num_layers=2, batch_first=True, bidirectional=True, dropout=0.2 ) self.fatigue_head = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Dropout(0.3), nn.Linear(64, 1), nn.Sigmoid() ) self.mc_dropout = nn.Dropout(0.2) self.window_controller = AdaptiveTimeWindow() def forward(self, x: torch.Tensor, return_uncertainty: bool = False): """ 前向传播 Args: x: 输入视频片段,shape=(B, T, C, H, W) return_uncertainty: 是否返回不确定性 Returns: output: { 'fatigue_prob': 疲劳概率, 'confidence': 置信度, 'window_sec': 建议窗口长度 } """ B, T, C, H, W = x.shape x_flat = x.view(B * T, C, H, W) spatial_features = self.backbone(x_flat) spatial_features = self.cbam(spatial_features) spatial_features = spatial_features.view(B, T, -1) temporal_features, _ = self.temporal_encoder(spatial_features) last_hidden = temporal_features[:, -1, :] fatigue_prob = self.fatigue_head(last_hidden) if return_uncertainty: last_hidden_drop = self.mc_dropout(last_hidden) fatigue_prob_drop = self.fatigue_head(last_hidden_drop) confidence = 1.0 - 2.0 * torch.abs(fatigue_prob_drop - 0.5) confidence = confidence.squeeze(-1) else: confidence = torch.ones(B, device=x.device) window_sec = self.window_controller.adjust_window( confidence.mean().item() ) return { 'fatigue_prob': fatigue_prob.squeeze(-1), 'confidence': confidence, 'window_sec': window_sec }
class CBAM(nn.Module): """ Convolutional Block Attention Module 包含: - 通道注意力(Channel Attention) - 空间注意力(Spatial Attention) """ def __init__(self, channels: int, reduction_ratio: int = 16, kernel_size: int = 7): super().__init__() self.channel_attention = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(channels, channels // reduction_ratio, 1), nn.ReLU(), nn.Conv2d(channels // reduction_ratio, channels, 1), nn.Sigmoid() ) self.spatial_attention = nn.Sequential( nn.Conv2d(2, 1, kernel_size, padding=kernel_size // 2), nn.Sigmoid() ) def forward(self, x: torch.Tensor) -> torch.Tensor: ca = self.channel_attention(x) x = x * ca avg_out = torch.mean(x, dim=1, keepdim=True) max_out = torch.max(x, dim=1, keepdim=True)[0] sa_input = torch.cat([avg_out, max_out], dim=1) sa = self.spatial_attention(sa_input) x = x * sa return x
|