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
| import torch import torch.nn as nn import torch.nn.functional as F
class MultiModalFusion(nn.Module): """ 多模态融合网络 RGB + 热成像 + 近红外 """ def __init__( self, rgb_channels: int = 3, thermal_channels: int = 1, nir_channels: int = 1, hidden_dim: int = 256 ): super().__init__() self.rgb_encoder = nn.Sequential( nn.Conv2d(rgb_channels, 64, 7, stride=2, padding=3), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(128, hidden_dim, 3, stride=2, padding=1) ) self.thermal_encoder = nn.Sequential( nn.Conv2d(thermal_channels, 64, 7, stride=2, padding=3), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(128, hidden_dim, 3, stride=2, padding=1) ) self.nir_encoder = nn.Sequential( nn.Conv2d(nir_channels, 64, 7, stride=2, padding=3), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(128, hidden_dim, 3, stride=2, padding=1) ) self.fusion_gate = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(hidden_dim * 3, 3, 1), nn.Softmax(dim=1) ) self.fusion_conv = nn.Conv2d(hidden_dim * 3, hidden_dim, 1) def forward( self, rgb: torch.Tensor, thermal: torch.Tensor, nir: torch.Tensor ) -> torch.Tensor: """ Args: rgb: RGB图像, shape=(B, 3, H, W) thermal: 热成像, shape=(B, 1, H, W) nir: 近红外, shape=(B, 1, H, W) Returns: fused: 融合特征, shape=(B, C, H', W') """ rgb_feat = self.rgb_encoder(rgb) thermal_feat = self.thermal_encoder(thermal) nir_feat = self.nir_encoder(nir) concat = torch.cat([rgb_feat, thermal_feat, nir_feat], dim=1) weights = self.fusion_gate(concat) w_rgb = weights[:, 0:1, :, :] w_thermal = weights[:, 1:2, :, :] w_nir = weights[:, 2:3, :, :] weighted = torch.cat([ rgb_feat * w_rgb, thermal_feat * w_thermal, nir_feat * w_nir ], dim=1) fused = self.fusion_conv(weighted) return fused
class PedestrianDetector(nn.Module): """ 行人检测器 基于融合特征 """ def __init__(self, hidden_dim: int = 256): super().__init__() self.fusion = MultiModalFusion(hidden_dim=hidden_dim) self.detector = nn.Sequential( nn.Conv2d(hidden_dim, 128, 3, padding=1), nn.ReLU(), nn.Conv2d(128, 64, 3, padding=1), nn.ReLU() ) self.classifier = nn.Conv2d(64, 2, 1) self.regressor = nn.Conv2d(64, 4, 1) def forward( self, rgb: torch.Tensor, thermal: torch.Tensor, nir: torch.Tensor ) -> dict: """ Args: rgb: RGB图像 thermal: 热成像 nir: 近红外 Returns: { 'cls': 分类logits, 'bbox': 边界框回归 } """ fused = self.fusion(rgb, thermal, nir) feat = self.detector(fused) cls = self.classifier(feat) bbox = self.regressor(feat) return { 'cls': cls, 'bbox': bbox }
if __name__ == "__main__": model = PedestrianDetector() B = 2 H, W = 480, 640 rgb = torch.randn(B, 3, H, W) thermal = torch.randn(B, 1, H, W) nir = torch.randn(B, 1, H, W) result = model(rgb, thermal, nir) print(f"输入RGB: {rgb.shape}") print(f"输入热成像: {thermal.shape}") print(f"输入近红外: {nir.shape}") print(f"分类输出: {result['cls'].shape}") print(f"边界框输出: {result['bbox'].shape}")
|