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
| import torch import torch.nn as nn
class CabinRGBTFusion(nn.Module): """ 座舱RGB-热红外融合DMS框架 基于BFA-HARF: bidirectional feature adapter + hybrid attention """ def __init__(self, hidden_dim: int = 256): super().__init__() self.rgb_encoder = self._build_encoder(3, hidden_dim) self.thermal_encoder = self._build_encoder(1, hidden_dim) self.bfa_rgb_to_t = BidirectionalFeatureAdapter(hidden_dim) self.bfa_t_to_rgb = BidirectionalFeatureAdapter(hidden_dim) self.harf_fusion = HybridAttentionRF(hidden_dim) self.fatigue_head = nn.Linear(hidden_dim, 2) self.distraction_head = nn.Linear(hidden_dim, 5) self.gaze_head = nn.Linear(hidden_dim, 9) def _build_encoder(self, in_channels, dim): return nn.Sequential( nn.Conv2d(in_channels, 64, 7, 2, 3), nn.BatchNorm2d(64), nn.SiLU(), self._res_block(64, 128, 2), self._res_block(128, 256, 2), self._res_block(256, dim, 1), ) def _res_block(self, in_c, out_c, stride): return nn.Sequential( nn.Conv2d(in_c, out_c, 3, stride, 1), nn.BatchNorm2d(out_c), nn.SiLU(), nn.Conv2d(out_c, out_c, 3, 1, 1), nn.BatchNorm2d(out_c), nn.SiLU(), ) def forward(self, rgb, thermal): """ Args: rgb: [B, 3, H, W] 可见光图像 thermal: [B, 1, H, W] 热红外图像 Returns: fatigue, distraction, gaze 预测 """ rgb_feat = self.rgb_encoder(rgb) t_feat = self.thermal_encoder(thermal) rgb_aligned = self.bfa_rgb_to_t(rgb_feat, t_feat) t_aligned = self.bfa_t_to_rgb(t_feat, rgb_feat) fused = self.harf_fusion(rgb_aligned, t_aligned) pooled = torch.mean(fused, dim=[2, 3]) fatigue = self.fatigue_head(pooled) distraction = self.distraction_head(pooled) gaze = self.gaze_head(pooled) return { 'fatigue': fatigue, 'distraction': distraction, 'gaze': gaze }
class BidirectionalFeatureAdapter(nn.Module): """双向特征适配器""" def __init__(self, dim): super().__init__() self.adapter = nn.Sequential( nn.Conv2d(dim * 2, dim, 1), nn.BatchNorm2d(dim), nn.SiLU(), nn.Conv2d(dim, dim, 3, 1, 1), nn.BatchNorm2d(dim), nn.Sigmoid(), ) def forward(self, src_feat, ref_feat): weight = self.adapter(torch.cat([src_feat, ref_feat], dim=1)) return src_feat * weight + ref_feat * (1 - weight)
class HybridAttentionRF(nn.Module): """混合注意力+感受野""" def __init__(self, dim): super().__init__() self.branches = nn.ModuleList([ nn.Conv2d(dim, dim//3, 3, 1, d, dilation=d) for d in [1, 2, 4] ]) self.channel_att = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(dim, dim//8), nn.SiLU(), nn.Linear(dim//8, dim), nn.Sigmoid(), ) self.spatial_att = nn.Sequential( nn.Conv2d(2, 1, 7, 1, 3), nn.Sigmoid(), ) def forward(self, rgb_feat, t_feat): x = rgb_feat + t_feat branches = [branch(x) for branch in self.branches] x = torch.cat(branches, dim=1) ca = self.channel_att(x).unsqueeze(-1).unsqueeze(-1) x = x * ca sa_input = torch.cat([ torch.mean(x, dim=1, keepdim=True), torch.max(x, dim=1, keepdim=True)[0] ], dim=1) sa = self.spatial_att(sa_input) x = x * sa return x
if __name__ == "__main__": model = CabinRGBTFusion(hidden_dim=256) rgb = torch.randn(1, 3, 224, 224) thermal = torch.randn(1, 1, 224, 224) out = model(rgb, thermal) print("=== 座舱RGB-T融合DMS ===") print(f"疲劳检测: {out['fatigue'].shape}") print(f"分心检测: {out['distraction'].shape}") print(f"视线估计: {out['gaze'].shape}") print(f"\n模型参数: {sum(p.numel() for p in model.parameters())/1e6:.2f}M")
|