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
| """ Guided Dual-Attention Network: 13种注意力机制对比 论文: Technologies (MDPI), 2026, 14(9), 544
核心: 双注意力机制 - Spatial Attention (空间注意力): 聚焦眼睛关键区域 - Channel Attention (通道注意力): 聚焦重要特征通道 - Guided: 空间和通道注意力互相引导
13种变体: 1. SE (Squeeze-and-Excitation) 2. CBAM (Convolutional Block Attention Module) 3. ECA (Efficient Channel Attention) 4. CA (Coordinate Attention) 5. GA (Gated Attention) 6. TA (Triplet Attention) 7. PA (Polarized Attention) 8. SA (Spatial Attention) 9. CA-only (Channel Only) 10. SA-only (Spatial Only) 11. SGE (Spatial Group-wise Enhancement) 12. CGA (Convolutional Guided Attention) ← 论文提出 13. Baseline (无注意力) """
import torch import torch.nn as nn import torch.nn.functional as F
class SpatialAttention(nn.Module): """空间注意力: 聚焦眼睛关键区域""" def __init__(self, kernel_size=7): super().__init__() self.conv = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2) def forward(self, x): avg_out = torch.mean(x, dim=1, keepdim=True) max_out, _ = torch.max(x, dim=1, keepdim=True) cat = torch.cat([avg_out, max_out], dim=1) return x * torch.sigmoid(self.conv(cat))
class ChannelAttention(nn.Module): """通道注意力: 聚焦重要特征通道""" def __init__(self, channels, reduction=16): super().__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.mlp = nn.Sequential( nn.Linear(channels, channels // reduction), nn.ReLU(), nn.Linear(channels // reduction, channels) ) def forward(self, x): b, c, _, _ = x.shape avg_out = self.mlp(self.avg_pool(x).view(b, c)) max_out = self.mlp(self.max_pool(x).view(b, c)) return x * torch.sigmoid(avg_out + max_out).view(b, c, 1, 1)
class GuidedDualAttention(nn.Module): """ 论文提出的CGA (Convolutional Guided Attention) 核心: 空间和通道注意力互相引导 - 通道注意力输出 → 引导空间注意力 - 空间注意力输出 → 引导通道注意力 - 双向引导形成闭环 """ def __init__(self, channels, reduction=16, kernel_size=7): super().__init__() self.ca = ChannelAttention(channels, reduction) self.sa = SpatialAttention(kernel_size) self.gate_c = nn.Sequential( nn.Conv2d(channels, 1, 1), nn.Sigmoid() ) self.gate_s = nn.Sequential( nn.Conv2d(1, channels, 1), nn.Sigmoid() ) def forward(self, x): ca_out = self.ca(x) sa_out = self.sa(ca_out) c_guide = self.gate_c(ca_out) s_guide = self.gate_s(sa_out.mean(dim=1, keepdim=True)) out = x * ca_out * s_guide + x * sa_out * c_guide return out
class DrowsinessDetector(nn.Module): """ 完整疲劳检测模型 输入: 眼部ROI图像 (48x48x3) 输出: 疲劳概率 (0-1) 部署: FPGA INT8量化 """ def __init__(self, attention_type='cga'): super().__init__() self.features = nn.Sequential( nn.Conv2d(3, 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), ) if attention_type == 'cga': self.attention = GuidedDualAttention(64) elif attention_type == 'se': self.attention = ChannelAttention(64) elif attention_type == 'sa': self.attention = SpatialAttention() else: self.attention = nn.Identity() self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Dropout(0.3), nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 2) ) def forward(self, x): x = self.features(x) x = self.attention(x) return self.classifier(x)
if __name__ == "__main__": model = DrowsinessDetector('cga') x = torch.randn(1, 3, 48, 48) out = model(x) params = sum(p.numel() for p in model.parameters()) print(f"模型输出: {out.shape}") print(f"参数量: {params:,} ({params/1e3:.1f}K)") print(f"FP32大小: {params*4/1024:.1f}KB") print(f"INT8大小: {params/1024:.1f}KB")
|