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
| import torch import torch.nn as nn import torch.nn.functional as F
class ProjectionBasedFusion(nn.Module): """ 基于投影的融合表示 将雷达点云投影到图像平面,与视觉特征融合 优点: - 实现简单 - 计算效率高 缺点: - 丢失雷达的高度信息 - 稀疏点云投影后信息有限 """ def __init__(self, image_channels: int = 3, radar_channels: int = 5, feature_dim: int = 64): super().__init__() self.image_encoder = nn.Sequential( nn.Conv2d(image_channels, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), nn.Conv2d(64, feature_dim, 3, padding=1) ) self.radar_embedding = nn.Linear(radar_channels, feature_dim) self.fusion_conv = nn.Conv2d(feature_dim * 2, feature_dim, 1) def forward(self, image: torch.Tensor, radar_points: torch.Tensor, radar_pixels: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: image: 图像 (B, C, H, W) radar_points: 雷达点云 (B, N, 5) [x, y, z, v_r, rcs] radar_pixels: 雷达投影像素坐标 (B, N, 2) Returns: fused_features: 融合特征 (B, D, H, W) """ B, C, H, W = image.shape N = radar_points.shape[1] img_features = self.image_encoder(image) radar_features = self.radar_embedding(radar_points) radar_feature_map = torch.zeros(B, radar_features.shape[-1], H, W, device=image.device) for b in range(B): for n in range(N): u, v = radar_pixels[b, n].long() if 0 <= u < W and 0 <= v < H: radar_feature_map[b, :, v, u] = radar_features[b, n] combined = torch.cat([img_features, radar_feature_map], dim=1) fused = self.fusion_conv(combined) return fused
class BEVBasedFusion(nn.Module): """ 基于鸟瞰图(BEV)的融合表示 将图像和雷达数据都转换到BEV空间 优点: - 保留空间关系 - 适合多目标跟踪 缺点: - 深度估计不准确 - 计算量大 """ def __init__(self, image_size: tuple = (720, 1280), bev_size: tuple = (200, 200), feature_dim: int = 64): super().__init__() self.image_size = image_size self.bev_size = bev_size self.image_to_bev = nn.Sequential( nn.Conv2d(3, 32, 3, padding=1), nn.ReLU(), nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(), ) self.depth_net = nn.Sequential( nn.Conv2d(64, 64, 3, padding=1), nn.ReLU(), nn.Conv2d(64, 1, 1) ) self.radar_to_bev = nn.Linear(5, feature_dim) self.bev_fusion = nn.Sequential( nn.Conv2d(64 + feature_dim, 128, 3, padding=1), nn.ReLU(), nn.Conv2d(128, feature_dim, 3, padding=1) ) def forward(self, image: torch.Tensor, radar_points: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: image: 图像 (B, C, H, W) radar_points: 雷达点云 (B, N, 5) Returns: bev_features: BEV特征 (B, D, BH, BW) """ B = image.shape[0] BH, BW = self.bev_size img_features = self.image_to_bev(image) depth = self.depth_net(img_features) bev_from_image = self._lift_splat(img_features, depth) radar_features = self.radar_to_bev(radar_points) bev_from_radar = self._radar_to_bev_map(radar_features, radar_points) combined = torch.cat([bev_from_image, bev_from_radar], dim=1) bev_features = self.bev_fusion(combined) return bev_features def _lift_splat(self, img_features, depth): """图像特征提升到BEV空间""" B, D, H, W = img_features.shape BH, BW = self.bev_size bev = torch.zeros(B, D, BH, BW, device=img_features.device) bev = F.interpolate(bev, size=(BH, BW), mode='bilinear') return bev def _radar_to_bev_map(self, features, points): """雷达点云转换为BEV特征图""" B, N, D = features.shape BH, BW = self.bev_size bev = torch.zeros(B, D, BH, BW, device=features.device) range_m = 50.0 for b in range(B): for n in range(N): x, y = points[b, n, 0], points[b, n, 1] bev_x = int((x + range_m) / (2 * range_m) * BW) bev_y = int((y + range_m) / (2 * range_m) * BH) if 0 <= bev_x < BW and 0 <= bev_y < BH: bev[b, :, bev_y, bev_x] = features[b, n] return bev
if __name__ == "__main__": proj_fusion = ProjectionBasedFusion() image = torch.randn(2, 3, 720, 1280) radar = torch.randn(2, 100, 5) pixels = torch.rand(2, 100, 2) * 720 features = proj_fusion(image, radar, pixels) print(f"投影融合输出: {features.shape}") bev_fusion = BEVBasedFusion() bev_features = bev_fusion(image, radar) print(f"BEV融合输出: {bev_features.shape}")
|