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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
| """ PART: Physics-Aware Radar Transformer 论文: arXiv 2609.02289, submitted to ICRA 2027 代码: https://github.com/sunyinghao-uestc/PART
核心: 完全稀疏的雷达-only检测器,预测存在置信度、表面点、2D速度 依赖: torch>=2.0, einops, scipy """
import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange from scipy.spatial.distance import cdist import numpy as np from typing import List, Tuple
class RadarPointEncoder(nn.Module): """ 雷达点编码器 每个雷达点16维特征: - 2D位置 (x, y) - RCS (雷达散射截面) - 补偿后速度 (vx, vy) - 6个传感器报告的不确定度 - sweep时间戳 - 径向速度和方向 - k-NN密度估计 (k=5) """ def __init__(self, in_dim: int = 16, embed_dim: int = 128): super().__init__() self.mlp = nn.Sequential( nn.Linear(in_dim, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, embed_dim) ) def forward(self, radar_points: torch.Tensor) -> torch.Tensor: """radar_points: (N, 16) → (N, embed_dim)""" return self.mlp(radar_points)
class SEChannelAttention(nn.Module): """SE-inspired channel attention for point features""" def __init__(self, dim: int): super().__init__() self.gating = nn.Sequential( nn.Linear(dim, dim // 4), nn.ReLU(), nn.Linear(dim // 4, dim), nn.Sigmoid() ) def forward(self, x: torch.Tensor) -> torch.Tensor: return x * self.gating(x.mean(dim=0, keepdim=True))
class RoPEMultiHeadSelfAttention(nn.Module): """ 2D Rotary Position Embedding MHSA 用旋转矩阵编码相对位置 """ def __init__(self, dim: int, num_heads: int = 4): super().__init__() self.num_heads = num_heads self.head_dim = dim // num_heads self.scale = self.head_dim ** -0.5 self.qkv = nn.Linear(dim, dim * 3) self.proj = nn.Linear(dim, dim) def apply_rope_2d(self, x: torch.Tensor, pos: torch.Tensor) -> torch.Tensor: """ 2D RoPE: 对x,y分别应用旋转 x: (N, heads, head_dim) pos: (N, 2) - 2D坐标 """ half = self.head_dim // 2 angles_x = pos[:, 0:1] / (10000 ** (torch.arange(0, half).float() / half)) cos_x, sin_x = torch.cos(angles_x), torch.sin(angles_x) angles_y = pos[:, 1:2] / (10000 ** (torch.arange(0, half).float() / half)) cos_y, sin_y = torch.cos(angles_y), torch.sin(angles_y) x_x, x_y = x[..., :half], x[..., half:] rot_x = torch.stack([x_x * cos_x.unsqueeze(1) - x_x.roll(1, -1) * sin_x.unsqueeze(1), x_x * sin_x.unsqueeze(1) + x_x.roll(1, -1) * cos_x.unsqueeze(1)], dim=-1).flatten(-2) return x def forward(self, x: torch.Tensor, pos: torch.Tensor) -> torch.Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] q = self.apply_rope_2d(q, pos) k = self.apply_rope_2d(k, pos) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) x = (attn @ v).transpose(1, 2).reshape(B, N, C) return self.proj(x)
class TransformerBlock(nn.Module): def __init__(self, dim: int, num_heads: int = 4): super().__init__() self.seca = SEChannelAttention(dim) self.attn = RoPEMultiHeadSelfAttention(dim, num_heads) self.ffn = nn.Sequential( nn.Linear(dim, dim * 2), nn.GELU(), nn.Linear(dim * 2, dim) ) self.norm1 = nn.LayerNorm(dim) self.norm2 = nn.LayerNorm(dim) def forward(self, x: torch.Tensor, pos: torch.Tensor) -> torch.Tensor: x = x + self.attn(self.norm1(self.seca(x)), pos) x = x + self.ffn(self.norm2(x)) return x
class DopplerAwareQueryInit: """ DAQI: 用DBSCAN在(x, y, vx, vy)空间聚类雷达点 替代标准DETR的学习式查询,用输入依赖的物理聚类初始化 """ def __init__(self, eps: float = 1.0, min_samples: int = 3, max_queries: int = 100): self.eps = eps self.min_samples = min_samples self.max_queries = max_queries def cluster(self, points: np.ndarray, velocities: np.ndarray) -> List[np.ndarray]: """ 在位置-速度空间聚类 points: (N, 2) 位置 velocities: (N, 2) 速度 """ from sklearn.cluster import DBSCAN features = np.hstack([points, velocities]) clustering = DBSCAN(eps=self.eps, min_samples=self.min_samples).fit(features) clusters = [] for label in set(clustering.labels_): if label == -1: continue mask = clustering.labels_ == label if mask.sum() >= self.min_samples: clusters.append(np.where(mask)[0]) return clusters[:self.max_queries]
class PhysicsGuidedCrossAttention(nn.Module): """ PGCA: 融入径向Doppler一致性和RCS的交叉注意力 物理约束: 1. 预测速度的径向分量应与Doppler测量一致 2. RCS提供目标大小/材质信息 """ def __init__(self, dim: int, num_heads: int = 4): super().__init__() self.num_heads = num_heads self.head_dim = dim // num_heads self.scale = self.head_dim ** -0.5 self.q_proj = nn.Linear(dim, dim) self.k_proj = nn.Linear(dim + 2, dim) self.v_proj = nn.Linear(dim, dim) self.out_proj = nn.Linear(dim, dim) def forward(self, queries: torch.Tensor, query_pos: torch.Tensor, key_points: torch.Tensor, key_features: torch.Tensor, radial_doppler: torch.Tensor, rcs: torch.Tensor) -> torch.Tensor: """ queries: (K, dim) - DAQI初始化的查询 query_pos: (K, 2) - 查询位置 key_points: (N, dim) - 编码后的雷达点 key_features: (N, 2) - 雷达点位置 radial_doppler: (N,) - 径向Doppler速度 rcs: (N,) - 雷达散射截面 """ q = self.q_proj(queries) k_extra = torch.stack([radial_doppler, rcs], dim=-1) k = self.k_proj(torch.cat([key_features, k_extra], dim=-1)) v = self.v_proj(key_features) q = rearrange(q, 'k (h d) -> h k d', h=self.num_heads) k = rearrange(k, 'n (h d) -> h n d', h=self.num_heads) v = rearrange(v, 'n (h d) -> h n d', h=self.num_heads) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) out = attn @ v out = rearrange(out, 'h k d -> k (h d)') return self.out_proj(out)
class PARTDetector(nn.Module): """ PART: Physics-Aware Radar Transformer 完整的雷达-only运动目标检测器 输出: 存在置信度 + 表面点 + 2D速度 """ def __init__(self, in_dim: int = 16, embed_dim: int = 128, num_heads: int = 4, num_layers: int = 3, max_queries: int = 100): super().__init__() self.encoder = RadarPointEncoder(in_dim, embed_dim) self.transformer_blocks = nn.ModuleList([ TransformerBlock(embed_dim, num_heads) for _ in range(num_layers) ]) self.daqi = DopplerAwareQueryInit(max_queries=max_queries) self.pgca = PhysicsGuidedCrossAttention(embed_dim, num_heads) self.exist_head = nn.Linear(embed_dim, 1) self.surface_head = nn.Linear(embed_dim, 2) self.velocity_head = nn.Linear(embed_dim, 2) nn.init.zeros_(self.surface_head.weight) nn.init.zeros_(self.surface_head.bias) nn.init.zeros_(self.velocity_head.weight) nn.init.zeros_(self.velocity_head.bias) def forward(self, radar_points: torch.Tensor, positions: torch.Tensor, velocities: torch.Tensor, radial_doppler: torch.Tensor, rcs: torch.Tensor) -> dict: """ Args: radar_points: (N, 16) 雷达点特征 positions: (N, 2) 2D位置 velocities: (N, 2) 补偿后速度 radial_doppler: (N,) 径向Doppler rcs: (N,) RCS值 Returns: dict with 'existence', 'surface_point', 'velocity' """ encoded = self.encoder(radar_points) for block in self.transformer_blocks: encoded = block(encoded, positions) clusters = self.daqi.cluster(positions.cpu().numpy(), velocities.cpu().numpy()) if len(clusters) == 0: return {'existence': torch.zeros(0), 'surface_point': torch.zeros(0, 2), 'velocity': torch.zeros(0, 2)} query_features = [] query_positions = [] for cluster_idx in clusters: cluster_feat = encoded[cluster_idx].mean(dim=0) cluster_pos = positions[cluster_idx].mean(dim=0) query_features.append(cluster_feat) query_positions.append(cluster_pos) queries = torch.stack(query_features) query_pos = torch.stack(query_positions) refined = self.pgca(queries, query_pos, encoded, encoded, radial_doppler, rcs) existence = torch.sigmoid(self.exist_head(refined)).squeeze(-1) surface_offset = self.surface_head(refined) surface_point = query_pos + surface_offset velocity = self.velocity_head(refined) return { 'existence': existence, 'surface_point': surface_point, 'velocity': velocity }
if __name__ == "__main__": N = 250 in_dim = 16 radar_points = torch.randn(N, in_dim) positions = torch.randn(N, 2) * 30 velocities = torch.randn(N, 2) * 2 radial_doppler = torch.randn(N) * 2 rcs = torch.randn(N) * 10 + 0 model = PARTDetector(in_dim=in_dim) outputs = model(radar_points, positions, velocities, radial_doppler, rcs) print(f"输入雷达点数: {N}") print(f"检测到目标数: {outputs['existence'].shape[0]}") print(f"存在置信度: {outputs['existence'][:5]}") print(f"表面点: {outputs['surface_point'][:3]}") print(f"速度: {outputs['velocity'][:3]}") total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params:,} ({total_params/1e6:.2f}M)")
|