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
| """ Aptiv 风格的纯视觉乘员分类框架 基于单摄像头实现 FMVSS 208 合规 """
import torch import torch.nn as nn import numpy as np from enum import Enum
class OccupantClass(Enum): EMPTY = 0 CHILD_SEAT = 1 CHILD = 2 SMALL_ADULT = 3 ADULT = 4 LARGE_ADULT = 5 UNKNOWN = -1
class VisualOccupantClassifier(nn.Module): """ 纯视觉乘员分类器 输入: 座椅区域裁剪图像 输出: 乘员分类 + 置信度 """ def __init__(self): 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), nn.Flatten() ) self.classifier = nn.Sequential( nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.3), nn.Linear(128, 6) ) self.size_regressor = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 2) ) def forward(self, x): feat = self.backbone(x) logits = self.classifier(feat) size = self.size_regressor(feat) return logits, size
if __name__ == "__main__": model = VisualOccupantClassifier() img = torch.randn(4, 3, 224, 224) logits, size = model(img) pred = logits.argmax(dim=-1) classes = [OccupantClass(p.item()).name for p in pred] for i in range(4): print(f"样本{i}: 分类={classes[i]}, " f"体型={size[i].detach().tolist()}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}")
|