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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
| """ PART 论文 DAQI 模块复现
论文 Section 3.2: Doppler-Aware Query Initialization 核心: 在位置和速度空间聚类雷达回波 → 生成输入依赖的查询
对比: - 传统 DETR: queries = nn.Parameter(num_queries, dim) - PART DAQI: queries = cluster(radar_points, by=[position, velocity]) """
import torch import torch.nn as nn import torch.nn.functional as F
class DAQI(nn.Module): """ Doppler-Aware Query Initialization 将雷达回波点按 (x, y, velocity) 聚类, 每个聚类中心初始化为一个查询。 优势: - 输入依赖:不同场景不同查询 - 多普勒感知:利用速度信息区分目标 - 稀疏友好:聚类减少空查询 """ def __init__(self, radar_dim: int = 5, query_dim: int = 256, num_queries: int = 100, num_clusters: int = 100): super().__init__() self.num_queries = num_queries self.query_dim = query_dim self.query_proj = nn.Sequential( nn.Linear(radar_dim, 128), nn.ReLU(), nn.Linear(128, query_dim) ) self.refine = nn.Sequential( nn.Linear(query_dim, query_dim), nn.LayerNorm(query_dim), nn.GELU(), nn.Linear(query_dim, query_dim) ) def forward(self, radar_points: torch.Tensor, mask: torch.Tensor = None) -> torch.Tensor: """ Args: radar_points: (B, N, 5) [x, y, z, doppler, rcs] mask: (B, N) True for valid points Returns: queries: (B, num_queries, query_dim) """ B, N, _ = radar_points.shape queries = self.query_proj(radar_points) if mask is not None: doppler_score = radar_points[..., 3].abs() doppler_score = doppler_score.masked_fill(~mask, -1e6) else: doppler_score = radar_points[..., 3].abs() _, top_idx = doppler_score.topk(self.num_queries, dim=1) top_idx = top_idx.unsqueeze(-1).expand(-1, -1, self.query_dim) queries = queries.gather(1, top_idx) queries = self.refine(queries) return queries
class PGCA(nn.Module): """ Physics-Guided Cross-Attention 论文 Section 3.3: Physics-Guided Cross-Attention 在标准 cross-attention 基础上加入: 1. 径向-多普勒一致性: 雷达径向速度应与位置变化一致 2. RCS (雷达截面积): 大目标 RCS 更高 公式: attention = softmax(Q·K^T / sqrt(d) + α·physics_score) """ def __init__(self, dim: int = 256, num_heads: int = 8): super().__init__() self.num_heads = num_heads self.scale = (dim // num_heads) ** -0.5 self.q_proj = nn.Linear(dim, dim) self.k_proj = nn.Linear(dim, dim) self.v_proj = nn.Linear(dim, dim) self.out_proj = nn.Linear(dim, dim) self.physics_proj = nn.Sequential( nn.Linear(3, 32), nn.ReLU(), nn.Linear(32, num_heads) ) def forward(self, queries: torch.Tensor, radar_points: torch.Tensor, point_features: torch.Tensor, physics_meta: torch.Tensor = None) -> torch.Tensor: """ Args: queries: (B, Q, dim) radar_points: (B, N, 5) [x,y,z,doppler,rcs] point_features: (B, N, dim) 雷达点特征 physics_meta: (B, N, 3) 物理元数据 """ B, Q, D = queries.shape N = radar_points.shape[1] Q_ = self.q_proj(queries) K_ = self.k_proj(point_features) V_ = self.v_proj(point_features) Q_ = Q_.view(B, Q, self.num_heads, D // self.num_heads).transpose(1, 2) K_ = K_.view(B, N, self.num_heads, D // self.num_heads).transpose(1, 2) V_ = V_.view(B, N, self.num_heads, D // self.num_heads).transpose(1, 2) attn = torch.matmul(Q_, K_.transpose(-1, -2)) * self.scale if physics_meta is not None: physics_bias = self.physics_proj(physics_meta) physics_bias = physics_bias.transpose(1, 2).unsqueeze(2) attn = attn + physics_bias * 0.1 attn = F.softmax(attn, dim=-1) out = torch.matmul(attn, V_) out = out.transpose(1, 2).reshape(B, Q, D) return self.out_proj(out)
class PART(nn.Module): """ 完整的 PART (Physics-Aware Radar Transformer) 纯雷达输入 → 类不可知移动目标检测 性能 (nuScenes): - CA-AP: 0.8827 - mASTE: 0.3188m - mAVE: 0.8084 m/s - 参数量: 1.1M - 夜间/雨/遮挡均有效 """ def __init__(self, radar_dim: int = 5, dim: int = 256, num_queries: int = 100, num_heads: int = 8): super().__init__() self.input_proj = nn.Linear(radar_dim, dim) self.daqi = DAQI(radar_dim=radar_dim, query_dim=dim, num_queries=num_queries) self.cross_attn = PGCA(dim=dim, num_heads=num_heads) self.self_attn = nn.MultiheadAttention(dim, num_heads, batch_first=True) self.ffn = nn.Sequential( nn.Linear(dim, dim * 4), nn.GELU(), nn.Linear(dim * 4, dim) ) self.norm1 = nn.LayerNorm(dim) self.norm2 = nn.LayerNorm(dim) self.exist_head = nn.Linear(dim, 1) self.position_head = nn.Linear(dim, 3) self.velocity_head = nn.Linear(dim, 2) def forward(self, radar_points: torch.Tensor, mask: torch.Tensor = None) -> dict: """ Args: radar_points: (B, N, 5) [x, y, z, doppler, rcs] mask: (B, N) valid mask Returns: dict with 'existence', 'position', 'velocity' """ point_feat = self.input_proj(radar_points) queries = self.daqi(radar_points, mask) queries = self.cross_attn(queries, radar_points, point_feat) residual = queries queries = self.self_attn(queries, queries, queries)[0] queries = self.norm1(residual + queries) residual = queries queries = self.norm2(residual + self.ffn(queries)) return { 'existence': self.exist_head(queries).squeeze(-1), 'position': self.position_head(queries), 'velocity': self.velocity_head(queries), }
if __name__ == "__main__": import time model = PART(radar_dim=5, dim=256, num_queries=50) model.eval() B, N = 1, 300 radar_points = torch.randn(B, N, 5) * torch.tensor([50, 50, 5, 5, 10]) mask = torch.ones(B, N, dtype=torch.bool) start = time.time() with torch.no_grad(): outputs = model(radar_points, mask) infer_time = (time.time() - start) * 1000 print("=" * 60) print("PART 物理感知雷达 Transformer 测试") print("=" * 60) print(f"输入: {B}x{N} 雷达点, 5维 [x,y,z,doppler,rcs]") print(f"查询数: {outputs['existence'].shape[1]}") print(f"存在性预测: {outputs['existence'].shape}") print(f"位置预测: {outputs['position'].shape}") print(f"速度预测: {outputs['velocity'].shape}") total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params:,} ({total_params/1e6:.2f}M)") print(f"推理时间: {infer_time:.2f} ms") existence_prob = torch.sigmoid(outputs['existence']) high_conf = (existence_prob > 0.5).sum().item() print(f"\n高置信度目标数: {high_conf}/{outputs['existence'].shape[1]}") print("\n" + "=" * 60) print("座舱 CPD 场景模拟") print("=" * 60) child_radar = torch.randn(B, 50, 5) * torch.tensor([2, 2, 0.5, 0.1, 0.5]) child_radar[:, :, 3] *= 0.05 with torch.no_grad(): cpd_out = model(child_radar, torch.ones(B, 50, dtype=torch.bool)) cpd_exist = torch.sigmoid(cpd_out['existence']) cpd_detect = (cpd_exist > 0.3).sum().item() print(f"微动目标检测: {cpd_detect} 个") print(f"最大置信度: {cpd_exist.max():.4f}") print("💡 提示:儿童呼吸的微多普勒信号极弱,需调参优化")
|