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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
| """ KPGBeltNet: 基于人体关键点引导的安全带检测算法 论文:KPGBeltNet: in-vehicle seatbelt detection algorithm based on human keypoint-guided sampling and local–global attention """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple, List
class KeypointGuidedSampling(nn.Module): """ 关键点引导采样模块 思路:根据人体关键点(肩、腰)裁剪安全带感兴趣区域 """ def __init__(self): super().__init__() self.shoulder_left = 5 self.shoulder_right = 6 self.hip_left = 11 self.hip_right = 12 def forward( self, image: torch.Tensor, keypoints: torch.Tensor ) -> Tuple[torch.Tensor, torch.Tensor]: """ 根据关键点裁剪安全带区域 Args: image: 输入图像 (B, 3, H, W) keypoints: 关键点坐标 (B, 17, 3) - x, y, confidence Returns: shoulder_crop: 肩部裁剪区域 (B, 3, crop_size, crop_size) hip_crop: 腰部裁剪区域 """ B, C, H, W = image.shape left_shoulder = keypoints[:, self.shoulder_left, :2] right_shoulder = keypoints[:, self.shoulder_right, :2] shoulder_center = (left_shoulder + right_shoulder) / 2 crop_size = 128 left_hip = keypoints[:, self.hip_left, :2] right_hip = keypoints[:, self.hip_right, :2] hip_center = (left_hip + right_hip) / 2 shoulder_boxes = self._get_crop_boxes( shoulder_center, crop_size, H, W ) shoulder_crop = self._crop_and_resize(image, shoulder_boxes, crop_size) hip_boxes = self._get_crop_boxes(hip_center, crop_size, H, W) hip_crop = self._crop_and_resize(image, hip_boxes, crop_size) return shoulder_crop, hip_crop def _get_crop_boxes( self, centers: torch.Tensor, size: int, H: int, W: int ) -> torch.Tensor: """计算裁剪框坐标""" B = centers.shape[0] boxes = torch.zeros(B, 4, device=centers.device) half = size // 2 boxes[:, 0] = centers[:, 0] - half boxes[:, 1] = centers[:, 1] - half boxes[:, 2] = centers[:, 0] + half boxes[:, 3] = centers[:, 1] + half boxes[:, [0, 2]] /= W boxes[:, [1, 3]] /= H return boxes def _crop_and_resize( self, image: torch.Tensor, boxes: torch.Tensor, size: int ) -> torch.Tensor: """裁剪并调整大小""" B = image.shape[0] grid = self._boxes_to_grid(boxes, size) cropped = F.grid_sample( image, grid, mode='bilinear', padding_mode='zeros', align_corners=True ) return cropped def _boxes_to_grid(self, boxes: torch.Tensor, size: int) -> torch.Tensor: """将boxes转换为grid坐标""" B = boxes.shape[0] y = torch.linspace(-1, 1, size, device=boxes.device) x = torch.linspace(-1, 1, size, device=boxes.device) grid_y, grid_x = torch.meshgrid(y, x, indexing='ij') grid = torch.stack([grid_x, grid_y], dim=-1) grid = grid.unsqueeze(0).expand(B, -1, -1, -1) return grid
class LocalGlobalAttention(nn.Module): """ 局部-全局注意力模块 局部:精细检测安全带边缘 全局:理解安全带与身体的关系 """ def __init__(self, in_channels: int): super().__init__() self.local_attention = nn.Sequential( nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1, groups=in_channels), nn.BatchNorm2d(in_channels), nn.ReLU(), nn.Conv2d(in_channels, in_channels, kernel_size=1), nn.Sigmoid() ) self.global_attention = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(in_channels, in_channels // 8, kernel_size=1), nn.ReLU(), nn.Conv2d(in_channels // 8, in_channels, kernel_size=1), nn.Sigmoid() ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ 前向传播 Args: x: 输入特征 (B, C, H, W) Returns: out: 注意力增强后的特征 """ local_weight = self.local_attention(x) local_out = x * local_weight global_weight = self.global_attention(x) global_out = x * global_weight out = local_out + global_out return out
class SeatbeltMisuseDetector(nn.Module): """ 安全带误用检测器 检测项: 1. 肩带是否正确位置 2. 腰带是否正确位置 3. 安全带是否扣合 """ def __init__(self, config: dict): """ Args: config: 配置参数 - backbone: 骨干网络类型(resnet18/resnet34) - num_classes: 分类数(默认5:正常、肩带滑落、腰带过松、腰带位置错误、背后扣合) """ super().__init__() self.num_classes = config.get('num_classes', 5) self.kgp_sampling = KeypointGuidedSampling() self.shoulder_backbone = self._build_backbone(config.get('backbone', 'resnet18')) self.hip_backbone = self._build_backbone(config.get('backbone', 'resnet18')) self.shoulder_attention = LocalGlobalAttention(512) self.hip_attention = LocalGlobalAttention(512) self.classifier = nn.Sequential( nn.Linear(512 * 2, 256), nn.ReLU(), nn.Dropout(0.5), nn.Linear(256, self.num_classes) ) def _build_backbone(self, name: str) -> nn.Module: """构建骨干网络""" import torchvision.models as models if name == 'resnet18': model = models.resnet18(pretrained=True) model = nn.Sequential(*list(model.children())[:-1]) elif name == 'resnet34': model = models.resnet34(pretrained=True) model = nn.Sequential(*list(model.children())[:-1]) else: raise ValueError(f"Unknown backbone: {name}") return model def forward( self, image: torch.Tensor, keypoints: torch.Tensor ) -> torch.Tensor: """ 前向传播 Args: image: 输入图像 (B, 3, H, W) keypoints: 人体关键点 (B, 17, 3) Returns: logits: 分类输出 (B, num_classes) """ shoulder_crop, hip_crop = self.kgp_sampling(image, keypoints) shoulder_feat = self.shoulder_backbone(shoulder_crop) hip_feat = self.hip_backbone(hip_crop) shoulder_feat = self.shoulder_attention(shoulder_feat) hip_feat = self.hip_attention(hip_feat) shoulder_feat = shoulder_feat.flatten(1) hip_feat = hip_feat.flatten(1) fused_feat = torch.cat([shoulder_feat, hip_feat], dim=1) logits = self.classifier(fused_feat) return logits
if __name__ == "__main__": config = { 'backbone': 'resnet18', 'num_classes': 5 } model = SeatbeltMisuseDetector(config) model.eval() B = 2 H, W = 480, 640 image = torch.randn(B, 3, H, W) keypoints = torch.zeros(B, 17, 3) keypoints[:, 5, :2] = torch.tensor([[200, 150], [220, 160]]) keypoints[:, 6, :2] = torch.tensor([[400, 150], [380, 160]]) keypoints[:, 11, :2] = torch.tensor([[240, 350], [260, 360]]) keypoints[:, 12, :2] = torch.tensor([[360, 350], [340, 360]]) keypoints[:, :, 2] = 1.0 with torch.no_grad(): logits = model(image, keypoints) misuse_types = ['正常', '肩带滑落', '腰带过松', '腰带位置错误', '背后扣合'] predictions = torch.argmax(logits, dim=-1) print(f"预测结果:") for i, pred in enumerate(predictions): print(f" 样本{i+1}: {misuse_types[pred.item()]}") total_params = sum(p.numel() for p in model.parameters()) print(f"\n模型参数量: {total_params:,} ({total_params/1e6:.2f}M)")
|