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
| import torch import torch.nn as nn import numpy as np from dataclasses import dataclass
@dataclass class RadarConfig: """mmWave雷达配置""" freq: float = 60e9 bandwidth: float = 4e9 num_tx: int = 2 num_rx: int = 4 frame_rate: float = 10 range_res: float = 0.0375 angle_res: float = 15
class PointCloudEncoder(nn.Module): """ mmWave点云编码器 输入:原始点云 (N, 5) [x, y, z, doppler, intensity] 输出:特征向量 """ def __init__(self, n_points: int = 200): super().__init__() self.n_points = n_points self.mlp1 = nn.Sequential( nn.Linear(5, 32), nn.ReLU(), nn.Linear(32, 64), nn.ReLU(), ) self.mlp2 = nn.Sequential( nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 256), ) def forward(self, points): """points: (B, N, 5)""" B, N, _ = points.shape x = self.mlp1(points) global_feat = x.max(dim=1)[0] global_feat = self.mlp2(global_feat) return global_feat
class CPDDetector(nn.Module): """ CPD检测器:分类 [无人, 成人, 儿童, 儿童座椅, 宠物] 架构: 1. 点云编码 2. 时序融合(多帧) 3. 分类+回归(位置+尺寸) """ def __init__(self, n_classes: int = 5, n_frames: int = 3): super().__init__() self.encoder = PointCloudEncoder() self.n_frames = n_frames self.temporal = nn.LSTM( input_size=256, hidden_size=128, num_layers=2, batch_first=True, ) self.classifier = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Dropout(0.1), nn.Linear(64, n_classes) ) self.regressor = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 3) ) self.vital_sign = nn.Sequential( nn.Linear(128, 32), nn.ReLU(), nn.Linear(32, 1), nn.Sigmoid() ) def forward(self, point_cloud_sequence): """ Args: point_cloud_sequence: (B, T, N, 5) T帧时序点云 Returns: classification, position, vital """ B, T, N, F = point_cloud_sequence.shape frames_feat = [] for t in range(T): feat = self.encoder(point_cloud_sequence[:, t]) frames_feat.append(feat) sequence = torch.stack(frames_feat, dim=1) lstm_out, _ = self.temporal(sequence) final_feat = lstm_out[:, -1] cls = self.classifier(final_feat) pos = self.regressor(final_feat) vital = self.vital_sign(final_feat) return cls, pos, vital
class CPDSystem: """完整CPD系统""" def __init__(self): self.model = CPDDetector() self.config = RadarConfig() self.detection_history = [] self.alarm_threshold = 3 def process_frame(self, point_cloud): """处理一帧""" self.detection_history.append(point_cloud) if len(self.detection_history) > self.n_frames: self.detection_history.pop(0) if len(self.detection_history) < self.n_frames: return None sequence = torch.stack(self.detection_history, dim=1) cls, pos, vital = self.model(sequence) pred = cls.argmax(dim=-1) labels = ['无人', '成人', '儿童', '儿童座椅', '宠物'] result = { 'label': labels[pred.item()], 'confidence': cls.max(dim=-1)[0].item(), 'position': pos[0].tolist(), 'vital_sign': vital[0].item(), 'is_child': pred.item() == 2, } if result['is_child'] and result['confidence'] > 0.7: result['alarm'] = 'CHILD_DETECTED' elif result['vital_sign'] > 0.5 and result['label'] == '无人': result['alarm'] = 'VITAL_BUT_INVISIBLE' return result
if __name__ == "__main__": system = CPDSystem() system.n_frames = 3 print("=== mmWave雷达CPD儿童检测 ===") for i in range(3): points = torch.randn(1, 200, 5) points[:, :, :3] *= 2 points[:, :, 3] = np.random.rand() * 0.5 points[:, :, 4] = np.random.rand() * 10 result = system.process_frame(points) if result: print(f"检测结果: {result['label']}") print(f"置信度: {result['confidence']:.2f}") print(f"位置: {result['position']}") print(f"生命体征: {result['vital_sign']:.2f}") print(f"告警: {result.get('alarm', '无')}") params = sum(p.numel() for p in system.model.parameters()) print(f"\n模型参数: {params:,}") print(f"模型大小: {params * 4 / 1024:.1f} KB (FP32)")
|