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
| """ DoRF++ 核心实现
论文核心思路: 1. 从 CSI 提取多普勒速度投影 2. 将投影视为虚拟相机对 3D 运动的观测 3. 用 NeRF 思想从多视图重建潜在 3D 运动 4. 将重建运动投影到球面等角网格 5. 球面 Transformer 进行动作分类 """
import torch import torch.nn as nn import numpy as np from typing import Tuple, Optional
class DopplerProjectionExtractor(nn.Module): """ 从 WiFi CSI 提取多普勒速度投影 CSI → 时频分析 → 多普勒频移 → 速度投影 每个收发天线对提供一个投影方向 """ def __init__(self, num_tx: int = 1, num_rx: int = 3, num_subcarriers: int = 56): super().__init__() self.num_tx = num_tx self.num_rx = num_rx self.num_subcarriers = num_subcarriers self.num_views = num_tx * num_rx self.doppler_fft_size = 64 def forward(self, csi: torch.Tensor) -> torch.Tensor: """ Args: csi: (B, T, num_tx, num_rx, num_subcarriers) Returns: doppler_projections: (B, num_views, T_doppler) """ B, T, tx, rx, S = csi.shape doppler = torch.fft.fft(csi, n=self.doppler_fft_size, dim=1) doppler = torch.abs(doppler) doppler = doppler.view(B, tx * rx, -1, S) doppler = doppler.mean(dim=-1) return doppler
class DopplerRadianceField(nn.Module): """ 多普勒辐射场 (DoRF) 借鉴 NeRF 思想: - 输入:多个多普勒投影(虚拟视图) - 输出:潜在 3D 运动序列 DoRF(M) = {(d_i, v_i)} where d_i 是方向, v_i 是多普勒观测 """ def __init__( self, num_views: int = 3, latent_dim: int = 256, motion_dim: int = 3, num_harmonics: int = 16 ): super().__init__() self.num_views = num_views self.latent_dim = latent_dim self.motion_dim = motion_dim self.num_harmonics = num_harmonics self.direction_encoder = nn.Sequential( nn.Linear(2 * num_harmonics, latent_dim), nn.GELU(), nn.Linear(latent_dim, latent_dim) ) self.doppler_encoder = nn.Sequential( nn.Linear(1, latent_dim // 2), nn.GELU(), nn.Linear(latent_dim // 2, latent_dim) ) self.motion_mlp = nn.Sequential( nn.Linear(latent_dim * 2, latent_dim), nn.GELU(), nn.Linear(latent_dim, latent_dim), nn.GELU(), nn.Linear(latent_dim, motion_dim + 1) ) def forward( self, doppler_projections: torch.Tensor, directions: torch.Tensor ) -> torch.Tensor: """ Returns: motion_3d: (B, motion_dim, T_d) 潜在3D运动 """ B, V, T_d = doppler_projections.shape dir_emb = self.direction_encoder(directions) dop_emb = self.doppler_encoder( doppler_projections.unsqueeze(-1) ) dir_emb = dir_emb.unsqueeze(0).unsqueeze(2) fused = torch.cat([dop_emb, dir_emb.expand_as(dop_emb)], dim=-1) output = self.motion_mlp(fused) motion = output[..., :self.motion_dim] density = output[..., self.motion_dim:] weights = torch.softmax(density, dim=1) motion_3d = (motion * weights).sum(dim=1) return motion_3d
class SphericalProjection(nn.Module): """ 将 3D 运动序列投影到球面等角网格 球面坐标: (θ, φ) → 筃角网格 (H_grid × W_grid) """ def __init__(self, grid_height: int = 32, grid_width: int = 64): super().__init__() self.H = grid_height self.W = grid_width theta = torch.linspace(0, np.pi, grid_height) phi = torch.linspace(0, 2 * np.pi, grid_width) theta, phi = torch.meshgrid(theta, phi, indexing='ij') x = torch.sin(theta) * torch.cos(phi) y = torch.sin(theta) * torch.sin(phi) z = torch.cos(theta) self.register_buffer('grid_3d', torch.stack([x, y, z], dim=-1)) def forward(self, motion_3d: torch.Tensor) -> torch.Tensor: """ Args: motion_3d: (B, T, 3) Returns: spherical_map: (B, T, H, W) """ B, T, _ = motion_3d.shape motion_norm = motion_3d / (torch.norm(motion_3d, dim=-1, keepdim=True) + 1e-8) spherical_map = torch.zeros(B, T, self.H, self.W) for b in range(B): for t in range(T): d = motion_norm[b, t] dist = torch.norm(self.grid_3d - d, dim=-1) idx = torch.argmin(dist.view(-1)) i, j = idx // self.W, idx % self.W spherical_map[b, t, i, j] = torch.norm(motion_3d[b, t]) return spherical_map
class SphericalTransformer(nn.Module): """ 球面 Transformer 用于动作分类 在球面上做注意力计算,保持旋转等变性 """ def __init__( self, grid_height: int = 32, grid_width: int = 64, embed_dim: int = 256, num_heads: int = 8, num_layers: int = 4, num_classes: int = 10 ): super().__init__() self.H = grid_height self.W = grid_width self.input_proj = nn.Conv2d(1, embed_dim, kernel_size=3, padding=1) self.pos_embed = nn.Parameter( torch.randn(1, embed_dim, grid_height, grid_width) * 0.02 ) encoder_layer = nn.TransformerEncoderLayer( d_model=embed_dim * grid_width, nhead=num_heads, dim_feedforward=embed_dim * 4, batch_first=True ) self.transformer = nn.TransformerEncoder(encoder_layer, num_layers) self.cls_head = nn.Sequential( nn.Linear(embed_dim * grid_width, embed_dim), nn.GELU(), nn.Linear(embed_dim, num_classes) ) def forward(self, spherical_map: torch.Tensor) -> torch.Tensor: """ Args: spherical_map: (B, T, H, W) Returns: logits: (B, num_classes) """ B, T, H, W = spherical_map.shape x = spherical_map.view(B * T, 1, H, W) x = self.input_proj(x) x = x + self.pos_embed x = x.flatten(2).transpose(1, 2) x = x.view(B, T, -1) x = self.transformer(x) x = x.mean(dim=1) logits = self.cls_head(x) return logits
class DoRFPlusPlus(nn.Module): """ 完整 DoRF++ 管道 CSI → 多普勒投影 → DoRF 3D运动重建 → 球面投影 → 球面Transformer → 分类 """ def __init__(self, num_classes: int = 10): super().__init__() self.doppler_extractor = DopplerProjectionExtractor() self.dorf = DopplerRadianceField(num_views=3) self.sph_proj = SphericalProjection() self.sph_transformer = SphericalTransformer(num_classes=num_classes) def forward(self, csi: torch.Tensor) -> torch.Tensor: projections = self.doppler_extractor(csi) directions = torch.randn(3, 2) motion_3d = self.dorf(projections, directions) sph_map = self.sph_proj(motion_3d) logits = self.sph_transformer(sph_map) return logits
if __name__ == "__main__": torch.manual_seed(42) B, T = 2, 100 csi = torch.randn(B, T, 1, 3, 56, dtype=torch.cfloat) model = DoRFPlusPlus(num_classes=8) logits = model(csi) print("=" * 60) print("DoRF++ 测试结果") print("=" * 60) print(f"CSI 输入: {csi.shape} (complex)") print(f"分类输出: {logits.shape}") print(f"预测类别: {logits.argmax(dim=-1)}") params = sum(p.numel() for p in model.parameters()) print(f"参数: {params:,} ({params/1e6:.2f}M)")
|