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
| class RGBThermalFusion(nn.Module): """ 可见光-热成像融合模块 对DMS RGB-IR融合的启示: - 白天RGB主导,IR辅助 - 夜间IR主导,RGB辅助 - 自动判断哪个模态更可靠 """ def __init__(self, rgb_channels=3, thermal_channels=1): super().__init__() self.rgb_backbone = self._build_backbone(rgb_channels) self.thermal_backbone = self._build_backbone(thermal_channels) self.modal_weight = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(256 * 2, 64, 1), nn.ReLU(), nn.Conv2d(64, 2, 1), nn.Softmax(dim=1) ) def _build_backbone(self, in_ch): return nn.Sequential( nn.Conv2d(in_ch, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, 3, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), ) def forward(self, rgb, thermal): rgb_feat = self.rgb_backbone(rgb) thermal_feat = self.thermal_backbone(thermal) combined = torch.cat([rgb_feat, thermal_feat], dim=1) weights = self.modal_weight(combined) rgb_weight = weights[:, 0:1] thermal_weight = weights[:, 1:2] fused = rgb_feat * rgb_weight + thermal_feat * thermal_weight return fused, weights
|