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
| import torch import torch.nn as nn import numpy as np
class ECRAR(nn.Module): """ Environmental Context-Aware Radar Action Recognition 双流架构: 1. 人体运动流: 点云序列 → 动作特征 2. 环境上下文流: 静态点云 → 场景特征 """ def __init__(self, n_classes=10, n_points=256): super().__init__() self.human_encoder = PointNetPlusPlus( n_points=n_points, output_dim=256 ) self.env_encoder = PointNetPlusPlus( n_points=n_points, output_dim=128 ) self.fusion = nn.Sequential( nn.Linear(256 + 128, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128) ) self.temporal = nn.GRU( input_size=128, hidden_size=128, num_layers=2, batch_first=True, bidirectional=True, dropout=0.3 ) self.classifier = nn.Linear(128 * 2, n_classes) def forward(self, human_pc_seq, env_pc): """ Args: human_pc_seq: (B, T, N, 4) 人体点云序列 [x, y, z, doppler] env_pc: (B, N, 3) 环境静态点云 [x, y, z] Returns: logits: (B, n_classes) """ B, T, N, _ = human_pc_seq.shape env_feat = self.env_encoder(env_pc) human_feats = [] for t in range(T): feat = self.human_encoder(human_pc_seq[:, t]) human_feats.append(feat) human_seq = torch.stack(human_feats, dim=1) env_expanded = env_feat.unsqueeze(1).expand(-1, T, -1) fused_input = torch.cat([human_seq, env_expanded], dim=-1) fused = self.fusion(fused_input) temporal_out, _ = self.temporal(fused) output = self.classifier(temporal_out[:, -1, :]) return output
class PointNetPlusPlus(nn.Module): """简化版 PointNet++ 编码器""" def __init__(self, n_points=256, output_dim=256): super().__init__() self.n_points = n_points self.mlp1 = nn.Sequential( nn.Linear(4, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 256) ) self.mlp2 = nn.Sequential( nn.Linear(256, output_dim) ) def forward(self, x): """ Args: x: (B, N, C) 点云 Returns: feat: (B, output_dim) """ point_feat = self.mlp1(x) global_feat = point_feat.max(dim=1)[0] return self.mlp2(global_feat)
CABIN_ENVIRONMENT = { 'static_objects': [ 'steering_wheel', 'dashboard', 'center_console', 'seats', 'rearview_mirror', 'windows', ], 'dynamic_zone': { 'driver_seat': {'center': [0, 0, 0], 'radius': 0.5}, 'passenger_seat': {'center': [0.5, 0, 0], 'radius': 0.5}, 'rear_seats': {'center': [0, 1, 0], 'radius': 0.6}, } }
CABIN_ACTIONS = { 0: 'sitting_normal', 1: 'leaning_forward', 2: 'leaning_back', 3: 'turning_left', 4: 'turning_right', 5: 'reaching_back', 6: 'adjusting_seatbelt', 7: 'drinking', 8: 'phone_to_ear', 9: 'texting', }
if __name__ == "__main__": model = ECRAR(n_classes=10, n_points=128) human_pc = torch.randn(4, 30, 128, 4) env_pc = torch.randn(4, 128, 3) logits = model(human_pc, env_pc) print(f"人体点云: {human_pc.shape}") print(f"环境点云: {env_pc.shape}") print(f"输出: {logits.shape} (10类动作)") print(f"参数: {sum(p.numel() for p in model.parameters()):,}")
|