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
| """ 座椅压力分布坐姿识别 - 三种网络架构对比
基于 Applied Sciences 2025 论文方法复现 依赖: pip install torch torchvision numpy
输入: 压力分布矩阵 (H×W), 类似灰度图像 输出: 9类坐姿分类 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from torch.utils.data import Dataset, DataLoader
class PressurePostureDataset(Dataset): """ 座椅压力分布数据集 数据格式: - 压力矩阵: shape=(1, H, W), 浮点, 0-1归一化 - 标签: 0-8, 对应9种坐姿 实际使用时应从传感器阵列采集或从论文数据集获取 """ POSTURE_NAMES = [ 'normal', 'forward_lean', 'backward_lean', 'left_lean', 'right_lean', 'left_leg_up', 'right_leg_up', 'head_on_wheel', 'out_of_seat' ] def __init__(self, n_samples=1000, H=42, W=32, mode='train'): np.random.seed(42 if mode == 'train' else 123) self.H, self.W = H, W self.n_classes = 9 self.data = np.zeros((n_samples, 1, H, W), dtype=np.float32) self.labels = np.zeros(n_samples, dtype=np.int64) for i in range(n_samples): label = i % 9 self.labels[i] = label self.data[i, 0] = self._generate_pressure_pattern(label) def _generate_pressure_pattern(self, label: int) -> np.ndarray: """根据坐姿类型生成模拟压力分布""" mat = np.random.rand(self.H, self.W) * 0.1 if label == 0: mat[10:30, 8:24] += np.random.rand(20, 16) * 0.5 + 0.3 mat[25:35, 5:12] += np.random.rand(10, 7) * 0.3 mat[25:35, 20:27] += np.random.rand(10, 7) * 0.3 elif label == 1: mat[5:20, 8:24] += np.random.rand(15, 16) * 0.6 + 0.4 elif label == 2: mat[25:40, 8:24] += np.random.rand(15, 16) * 0.5 elif label == 3: mat[10:35, 0:16] += np.random.rand(25, 16) * 0.5 elif label == 4: mat[10:35, 16:32] += np.random.rand(25, 16) * 0.5 elif label == 5: mat[10:30, 8:24] += np.random.rand(20, 16) * 0.4 mat[5:15, 0:10] += np.random.rand(10, 10) * 0.5 elif label == 6: mat[10:30, 8:24] += np.random.rand(20, 16) * 0.4 mat[5:15, 22:32] += np.random.rand(10, 10) * 0.5 elif label == 7: mat[0:10, 10:22] += np.random.rand(10, 12) * 0.7 + 0.5 elif label == 8: mat += np.random.rand(self.H, self.W) * 0.05 return np.clip(mat, 0, 1) def __len__(self): return len(self.labels) def __getitem__(self, idx): return torch.from_numpy(self.data[idx]), self.labels[idx]
class PostureFNN(nn.Module): """全连接网络(基线)""" def __init__(self, input_dim=42*32, n_classes=9): super().__init__() self.fc = nn.Sequential( nn.Linear(input_dim, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, n_classes) ) def forward(self, x): x = x.flatten(1) return self.fc(x)
class PostureCNN(nn.Module): """卷积神经网络""" def __init__(self, n_classes=9): super().__init__() self.features = nn.Sequential( nn.Conv2d(1, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)), ) self.classifier = nn.Linear(128, n_classes) def forward(self, x): x = self.features(x) x = x.flatten(1) return self.classifier(x)
class PostureResNet(nn.Module): """ResNet变体""" def __init__(self, n_classes=9): super().__init__() self.stem = nn.Sequential( nn.Conv2d(1, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), ) self.block1 = self._make_block(32, 64, 2) self.block2 = self._make_block(64, 128, 2) self.gap = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(128, n_classes) def _make_block(self, in_ch, out_ch, n_blocks): layers = [] for i in range(n_blocks): in_c = in_ch if i == 0 else out_ch layers.extend([ nn.Conv2d(in_c, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(), nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), ]) if in_c != out_ch: layers.append(nn.Conv2d(in_c, out_ch, 1, bias=False)) layers.append(nn.ReLU()) return nn.Sequential(*layers) def forward(self, x): x = self.stem(x) identity = x x = self.block1(x) x = self.block2(x) x = self.gap(x) x = x.flatten(1) return self.fc(x)
def train_and_evaluate(model_class, model_name, n_epochs=30): """训练并评估模型""" device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') train_ds = PressurePostureDataset(n_samples=900, mode='train') test_ds = PressurePostureDataset(n_samples=180, mode='test') train_loader = DataLoader(train_ds, batch_size=32, shuffle=True) test_loader = DataLoader(test_ds, batch_size=32) model = model_class().to(device) optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) criterion = nn.CrossEntropyLoss() best_acc = 0 for epoch in range(n_epochs): model.train() total_loss = 0 for data, labels in train_loader: data, labels = data.to(device), labels.to(device) optimizer.zero_grad() output = model(data) loss = criterion(output, labels) loss.backward() optimizer.step() total_loss += loss.item() model.eval() correct = 0 total = 0 with torch.no_grad(): for data, labels in test_loader: data, labels = data.to(device), labels.to(device) output = model(data) pred = output.argmax(1) correct += (pred == labels).sum().item() total += labels.size(0) acc = correct / total if acc > best_acc: best_acc = acc if (epoch + 1) % 10 == 0: print(f"[{model_name}] Epoch {epoch+1}: loss={total_loss/len(train_loader):.4f}, " f"acc={acc:.2%}") print(f"\n[{model_name}] 最佳准确率: {best_acc:.2%}") model.eval() all_preds, all_labels = [], [] with torch.no_grad(): for data, labels in test_loader: data = data.to(device) output = model(data) all_preds.extend(output.argmax(1).cpu().numpy()) all_labels.extend(labels.numpy()) from sklearn.metrics import confusion_matrix cm = confusion_matrix(all_labels, all_preds) print(f"\n混淆矩阵:\n{cm}") return best_acc
if __name__ == "__main__": print("=== 座椅压力分布坐姿识别 ===\n") results = {} for model_class, name in [(PostureFNN, "FNN"), (PostureCNN, "CNN"), (PostureResNet, "ResNet")]: print(f"\n--- 训练 {name} ---") acc = train_and_evaluate(model_class, name, n_epochs=20) results[name] = acc print(f"\n=== 最终对比 ===") for name, acc in results.items(): print(f"{name}: {acc:.2%}") for model_class, name in [(PostureFNN, "FNN"), (PostureCNN, "CNN"), (PostureResNet, "ResNet")]: n_params = sum(p.numel() for p in model_class().parameters()) print(f"{name}: {n_params:,} 参数 ({n_params*4/1024:.1f} KB)")
|