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
| import torch import torch.nn as nn import torch.nn.functional as F from typing import Dict, List from enum import Enum
class OccupantType(Enum): """乘员类型枚举""" EMPTY = 0 ADULT = 1 CHILD = 2 CHILD_SEAT = 3 UNKNOWN = 4
class OccupantClassifier(nn.Module): """乘员分类网络 Aptiv AOC核心模型 """ def __init__(self, num_classes: int = 5): super().__init__() self.backbone = self._build_backbone() self.classifier = nn.Sequential( nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU(), nn.Linear(128, num_classes) ) self.size_regressor = nn.Sequential( nn.Linear(512, 128), nn.ReLU(), nn.Linear(128, 1) ) self.position_regressor = nn.Sequential( nn.Linear(512, 64), nn.ReLU(), nn.Linear(64, 3) ) def _build_backbone(self) -> nn.Module: """构建骨干网络""" import torchvision.models as models model = models.mobilenet_v3_small(pretrained=True) model = nn.Sequential(*list(model.children())[:-1]) return model def forward(self, x: torch.Tensor) -> Dict: """ 前向传播 Args: x: (B, 3, H, W) 座舱图像 Returns: output: 分类结果 """ feat = self.backbone(x).squeeze(-1).squeeze(-1) if feat.size(1) < 512: feat = F.pad(feat, (0, 512 - feat.size(1))) logits = self.classifier(feat) probs = F.softmax(logits, dim=-1) size = self.size_regressor(feat) position = self.position_regressor(feat) return { 'logits': logits, 'probs': probs, 'predicted_class': torch.argmax(probs, dim=-1), 'estimated_size': size, 'estimated_position': position }
class AptivAOC: """Aptiv AOC完整系统 AI Occupant Classification """ def __init__(self, model_path: str = None): self.classifier = OccupantClassifier() if model_path: self.classifier.load_state_dict(torch.load(model_path)) self.classifier.eval() self.airbag_policy = { OccupantType.EMPTY: {'enabled': False, 'power': 0}, OccupantType.ADULT: {'enabled': True, 'power': 100}, OccupantType.CHILD: {'enabled': True, 'power': 50}, OccupantType.CHILD_SEAT: {'enabled': False, 'power': 0}, OccupantType.UNKNOWN: {'enabled': True, 'power': 75} } def classify(self, image: torch.Tensor) -> Dict: """ 分类乘员 Args: image: (B, 3, H, W) 座舱图像 Returns: result: 分类结果与气囊策略 """ with torch.no_grad(): output = self.classifier(image) pred_class = output['predicted_class'][0].item() occupant_type = OccupantType(pred_class) policy = self.airbag_policy[occupant_type] return { 'occupant_type': occupant_type.name, 'confidence': output['probs'][0, pred_class].item(), 'estimated_size': output['estimated_size'][0].item(), 'estimated_position': output['estimated_position'][0].tolist(), 'airbag_policy': policy }
class ComparisonBenchmark: """对比基准测试""" def __init__(self): self.aoc = AptivAOC() self.weight_sensor = WeightSensorSystem() def run_test(self, test_cases: List[Dict]) -> Dict: """ 运行对比测试 Args: test_cases: 测试用例列表 - image: 座舱图像 - ground_truth: 真实类别 - weight: 真实体重 Returns: comparison: 对比结果 """ aoc_results = [] sensor_results = [] for case in test_cases: aoc_pred = self.aoc.classify(case['image']) aoc_correct = aoc_pred['occupant_type'] == case['ground_truth'] aoc_results.append({ 'correct': aoc_correct, 'predicted': aoc_pred['occupant_type'], 'actual': case['ground_truth'] }) sensor_pred = self.weight_sensor.classify(case['weight']) sensor_correct = sensor_pred == case['ground_truth'] sensor_results.append({ 'correct': sensor_correct, 'predicted': sensor_pred, 'actual': case['ground_truth'] }) aoc_accuracy = sum(r['correct'] for r in aoc_results) / len(aoc_results) sensor_accuracy = sum(r['correct'] for r in sensor_results) / len(sensor_results) return { 'aoc_accuracy': aoc_accuracy, 'sensor_accuracy': sensor_accuracy, 'improvement': aoc_accuracy - sensor_accuracy, 'aoc_details': aoc_results, 'sensor_details': sensor_results }
class WeightSensorSystem: """传统重量传感器系统(对比基线)""" def __init__(self): self.thresholds = { 'empty': 5, 'child': 30, 'adult': 30 } def classify(self, weight: float) -> str: """基于重量分类""" if weight < self.thresholds['empty']: return 'EMPTY' elif weight < self.thresholds['child']: return 'CHILD' else: return 'ADULT'
if __name__ == "__main__": aoc = AptivAOC() image = torch.randn(1, 3, 224, 224) result = aoc.classify(image) print(f"乘员类型: {result['occupant_type']}") print(f"置信度: {result['confidence']:.2f}") print(f"估计体重: {result['estimated_size']:.1f} kg") print(f"气囊策略: {result['airbag_policy']}")
|