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
| """ Aptiv AOC乘员分类模型 """ import torch import torch.nn as nn import numpy as np from typing import Tuple
class OccupantClassifier(nn.Module): """乘员分类网络""" def __init__(self, num_classes: int = 5): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, 3, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)) ) self.classifier = nn.Sequential( nn.Flatten(), nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.2), nn.Linear(128, num_classes) ) self.classes = ['empty', 'adult', 'child', 'infant_seat', 'object'] def forward(self, x: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: x: 输入图像 (B, 3, H, W) Returns: logits: 分类logits (B, num_classes) """ features = self.backbone(x) logits = self.classifier(features) return logits def predict(self, image: np.ndarray) -> dict: """ 预测乘员类型 Args: image: 输入图像 (H, W, C) Returns: result: 预测结果 """ x = self._preprocess(image) with torch.no_grad(): logits = self.forward(x) probs = torch.softmax(logits, dim=1) pred_idx = torch.argmax(probs, dim=1).item() confidence = probs[0, pred_idx].item() return { 'class': self.classes[pred_idx], 'confidence': confidence, 'probabilities': { cls: probs[0, i].item() for i, cls in enumerate(self.classes) } } def _preprocess(self, image: np.ndarray) -> torch.Tensor: """图像预处理""" from PIL import Image img = Image.fromarray(image) img = img.resize((224, 224)) x = np.array(img).astype(np.float32) / 255.0 x = (x - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225] x = torch.from_numpy(x).permute(2, 0, 1).unsqueeze(0) return x
class AirbagController: """气囊控制器""" def __init__(self): self.classifier = OccupantClassifier() self.classifier.eval() def get_airbag_mode(self, occupant_class: str) -> dict: """ 获取气囊展开模式 Args: occupant_class: 乘员类型 Returns: mode: 气囊模式配置 """ modes = { 'empty': { 'enabled': False, 'power': 0, 'reason': '无乘员' }, 'adult': { 'enabled': True, 'power': 100, 'reason': '成人乘员' }, 'child': { 'enabled': True, 'power': 50, 'reason': '儿童乘员' }, 'infant_seat': { 'enabled': False, 'power': 0, 'reason': '儿童座椅' }, 'object': { 'enabled': True, 'power': 100, 'reason': '物体,不影响气囊' } } return modes.get(occupant_class, modes['adult']) def update_airbag(self, image: np.ndarray) -> dict: """ 根据图像更新气囊模式 Args: image: 座椅图像 Returns: status: 气囊状态 """ result = self.classifier.predict(image) mode = self.get_airbag_mode(result['class']) return { 'occupant_class': result['class'], 'confidence': result['confidence'], 'airbag_mode': mode }
if __name__ == "__main__": classifier = OccupantClassifier() classifier.eval() dummy_image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) result = classifier.predict(dummy_image) print(f"乘员类型: {result['class']}") print(f"置信度: {result['confidence']:.2f}") controller = AirbagController() status = controller.update_airbag(dummy_image) print(f"气囊模式: {status['airbag_mode']}")
|