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
| import torch import torch.nn as nn
class SeatbeltMisuseDetector(nn.Module): """ 安全带误用检测模型 检测 5 种误用类型: 0. 正常佩戴 1. 未系安全带 2. 仅系肩带 3. 仅系腰带 4. 肩带在臂下 5. 肩带在背后 """ def __init__(self): super().__init__() self.backbone = ResNet34(pretrained=True) self.keypoint_head = nn.Sequential( nn.Conv2d(512, 256, 3, padding=1), nn.ReLU(), nn.Conv2d(256, 8, 1) ) self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(512, 128), nn.ReLU(), nn.Dropout(0.3), nn.Linear(128, 6) ) def forward(self, x): """ 前向传播 Args: x: 输入图像, shape=(B, 3, H, W) Returns: keypoints: 安全带关键点, shape=(B, 8, 2) logits: 分类 logits, shape=(B, 6) """ features = self.backbone(x) keypoints = self.keypoint_head(features) logits = self.classifier(features) return keypoints, logits
|