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
| import torch import torch.nn as nn import numpy as np from typing import Tuple
class PostureResNet(nn.Module): """ 座椅压力分布坐姿识别ResNet 论文: Applied Sciences, 2025 输入: 42×48压力分布矩阵 输出: 9类坐姿 准确率: 94.2%(混合座面训练) 关键: 压力分布→2D图像→CNN处理 """ def __init__(self, num_postures=9): super().__init__() self.conv1 = nn.Conv2d(1, 64, 7, stride=2, padding=3) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU() self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.layer1 = self._make_layer(64, 64, 2, stride=1) self.layer2 = self._make_layer(64, 128, 2, stride=2) self.layer3 = self._make_layer(128, 256, 2, stride=2) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(256, num_postures) def _make_layer(self, in_ch, out_ch, blocks, stride): layers = [] layers.append(self._basic_block(in_ch, out_ch, stride)) for _ in range(1, blocks): layers.append(self._basic_block(out_ch, out_ch, 1)) return nn.Sequential(*layers) def _basic_block(self, in_ch, out_ch, stride): return ResidualBlock(in_ch, out_ch, stride) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.avgpool(x) x = torch.flatten(x, 1) x = self.fc(x) return x
class ResidualBlock(nn.Module): def __init__(self, in_ch, out_ch, stride=1): super().__init__() self.conv1 = nn.Conv2d(in_ch, out_ch, 3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(out_ch) self.conv2 = nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(out_ch) self.shortcut = nn.Sequential( nn.Conv2d(in_ch, out_ch, 1, stride=stride, bias=False), nn.BatchNorm2d(out_ch) ) if stride != 1 or in_ch != out_ch else nn.Identity() def forward(self, x): out = torch.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out += self.shortcut(x) return torch.relu(out)
class SeatPosturePipeline: """ 座椅压力坐姿检测完整管道 输入: 压力传感阵列 输出: 坐姿分类 + OOP风险评估 应用: Euro NCAP OOP检测要求 """ POSTURE_RISK = { 'P1_normal': {'risk': 'safe', 'airbag': 'normal'}, 'P2_forward': {'risk': 'medium', 'airbag': 'reduce_power'}, 'P3_backward': {'risk': 'medium', 'airbag': 'normal'}, 'P4_left': {'risk': 'medium', 'airbag': 'disable_side'}, 'P5_right': {'risk': 'medium', 'airbag': 'disable_side'}, 'P6_cross_left': {'risk': 'high', 'airbag': 'disable'}, 'P7_cross_right': {'risk': 'high', 'airbag': 'disable'}, 'P8_sideways': {'risk': 'high', 'airbag': 'disable'}, 'P9_on_dashboard': {'risk': 'critical', 'airbag': 'disable'}, } def __init__(self): self.model = PostureResNet() self.model.eval() def classify_posture(self, pressure_matrix: np.ndarray) -> dict: """ 分类坐姿 Args: pressure_matrix: 42×48 压力分布矩阵 (0-100 kPa) Returns: dict: 坐姿分类 + 风险评估 """ normalized = pressure_matrix / 100.0 x = torch.FloatTensor(normalized).unsqueeze(0).unsqueeze(0) with torch.no_grad(): logits = self.model(x) probs = torch.softmax(logits, dim=1) pred_idx = probs.argmax(1).item() confidence = probs[0, pred_idx].item() posture_key = f"P{pred_idx+1}" posture_names = ['normal', 'forward', 'backward', 'left', 'right', 'cross_left', 'cross_right', 'sideways', 'on_dashboard'] posture_name = posture_names[pred_idx] risk = self.POSTURE_RISK[f'P{pred_idx+1}_{posture_name}'] return { 'posture': posture_name, 'posture_id': posture_key, 'confidence': confidence, 'risk_level': risk['risk'], 'airbag_action': risk['airbag'], 'all_probs': {f'P{i+1}_{posture_names[i]}': probs[0, i].item() for i in range(9)} }
if __name__ == "__main__": pipeline = SeatPosturePipeline() np.random.seed(42) pressure = np.zeros((42, 48)) pressure[25:35, 15:35] = np.random.uniform(40, 80, (10, 20)) pressure[5:15, 15:35] = np.random.uniform(20, 40, (10, 20)) pressure[35:42, 10:40] = np.random.uniform(10, 30, (7, 30)) result = pipeline.classify_posture(pressure) print("=== 座椅压力坐姿检测 ===") print(f"坐姿: {result['posture']} ({result['posture_id']})") print(f"置信度: {result['confidence']:.2%}") print(f"风险: {result['risk_level']}") print(f"安全气囊: {result['airbag_action']}") print(f"\n=== 论文性能 ===") print(f"| 架构 | 硬座准确率 | 软座准确率 | 混合训练 |") print(f"|------|-----------|-----------|---------|") print(f"| FNN | 87.3% | 84.1% | 82.5% |") print(f"| CNN | 91.5% | 88.7% | 89.3% |") print(f"| ResNet| 94.2% | 92.8% | 94.2% |") print(f"\nResNet在混合训练下最优: 94.2%")
|