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
| import torch import torch.nn as nn
class LiteFatModel(nn.Module): """ LiteFat 轻量级时空图疲劳检测 输入: (B, T, 68, 2) 关键点序列 输出: (B, 2) 疲劳/清醒 """ def __init__(self, n_keypoints=68, hidden=64): super().__init__() self.spatial_gcn = nn.Sequential( nn.Linear(2, hidden), nn.ReLU(), nn.Linear(hidden, hidden), nn.ReLU(), ) self.temporal_encoder = nn.GRU( input_size=hidden * n_keypoints, hidden_size=hidden, num_layers=1, batch_first=True, ) self.classifier = nn.Sequential( nn.Linear(hidden, 32), nn.ReLU(), nn.Linear(32, 2) ) def forward(self, kpts_seq): """ Args: kpts_seq: (B, T, 68, 2) 关键点序列 Returns: logits: (B, 2) 疲劳/清醒 """ B, T, K, D = kpts_seq.shape x = self.spatial_gcn(kpts_seq) x = x.reshape(B, T, -1) out, hn = self.temporal_encoder(x) feat = hn[-1] logits = self.classifier(feat) return logits
if __name__ == "__main__": model = LiteFatModel() x = torch.randn(1, 30, 68, 2) out = model(x) print(f"输入: {x.shape}") print(f"输出: {out.shape}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}") print(f"模型大小(INT8): ~{sum(p.numel() for p in model.parameters()) // 1024} KB")
|