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
| """ 双注意力机制疲劳检测
InceptionV3 + 空间注意力 + 通道注意力 """
import torch import torch.nn as nn import torch.nn.functional as F
class SpatialAttention(nn.Module): """ 空间注意力模块 聚焦面部关键区域(眼部、嘴部) """ def __init__(self, kernel_size: int = 7): super().__init__() self.conv = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2) self.sigmoid = nn.Sigmoid() def forward(self, x): """ Args: x: (B, C, H, W) Returns: attention: (B, 1, H, W) """ max_pool = torch.max(x, dim=1, keepdim=True)[0] avg_pool = torch.mean(x, dim=1, keepdim=True) concat = torch.cat([max_pool, avg_pool], dim=1) attention = self.sigmoid(self.conv(concat)) return x * attention
class ChannelAttention(nn.Module): """ 通道注意力模块 强调疲劳相关特征通道 """ def __init__(self, channels: int, reduction: int = 16): super().__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.fc = nn.Sequential( nn.Linear(channels, channels // reduction, bias=False), nn.ReLU(), nn.Linear(channels // reduction, channels, bias=False), ) self.sigmoid = nn.Sigmoid() def forward(self, x): B, C, H, W = x.shape avg_out = self.fc(self.avg_pool(x).view(B, C)) max_out = self.fc(self.max_pool(x).view(B, C)) attention = self.sigmoid(avg_out + max_out).view(B, C, 1, 1) return x * attention
class DualAttentionFatigueNet(nn.Module): """ 双注意力疲劳检测网络 InceptionV3 骨干 + 空间注意力 + 通道注意力 """ def __init__(self, num_classes: int = 3): """ Args: num_classes: 0=清醒, 1=轻度疲劳, 2=重度疲劳 """ super().__init__() from torchvision.models import inception_v3 inception = inception_v3(pretrained=True) self.features = nn.Sequential( inception.Conv2d_1a_3x3, inception.Conv2d_2a_3x3, inception.Conv2d_2b_3x3, nn.MaxPool2d(3, 2), inception.Conv2d_3b_1x1, inception.Conv2d_4a_3x3, nn.MaxPool2d(3, 2), ) self.spatial_attention = SpatialAttention() self.channel_attention = ChannelAttention(192) self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Dropout(0.5), nn.Linear(192, 128), nn.ReLU(), nn.Linear(128, num_classes) ) def forward(self, x): """ Args: x: (B, 3, 299, 299) 低光增强后的图像 Returns: logits: (B, num_classes) """ features = self.features(x) features = self.channel_attention(features) features = self.spatial_attention(features) logits = self.classifier(features) return logits
class LowLightEnhancer(nn.Module): """ 低光图像增强 使用 Zero-DCE 轻量级方法 """ def __init__(self, num_iterations: int = 8): super().__init__() self.num_iterations = num_iterations self.curve_net = nn.Sequential( nn.Conv2d(3, 32, 3, 1, 1), nn.ReLU(), nn.Conv2d(32, 32, 3, 1, 1), nn.ReLU(), nn.Conv2d(32, 3 * num_iterations, 3, 1, 1), nn.Tanh() ) def forward(self, x): """ Args: x: (B, 3, H, W) 低光图像 Returns: enhanced: (B, 3, H, W) 增强图像 """ curves = self.curve_net(x) enhanced = x for i in range(self.num_iterations): curve = curves[:, 3*i:3*(i+1), :, :] enhanced = enhanced + curve * enhanced * (1 - enhanced) return enhanced
class LowLightFatigueDetector(nn.Module): """ 低光疲劳检测完整流水线 低光增强 + 疲劳分类 """ def __init__(self): super().__init__() self.enhancer = LowLightEnhancer() self.detector = DualAttentionFatigueNet() def forward(self, low_light_image): """ Args: low_light_image: (B, 3, H, W) Returns: fatigue_level: (B,) 疲劳等级 enhanced_image: (B, 3, H, W) 增强图像(用于可解释性) """ enhanced = self.enhancer(low_light_image) logits = self.detector(enhanced) fatigue_level = torch.argmax(logits, dim=1) return fatigue_level, enhanced
if __name__ == "__main__": model = LowLightFatigueDetector() low_light = torch.randn(1, 3, 299, 299) * 0.3 + 0.2 fatigue_level, enhanced = model(low_light) print(f"疲劳等级: {fatigue_level.item()}") print(f"增强图像亮度: {enhanced.mean().item():.3f}")
|