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
| import torch import torch.nn as nn import torch.nn.functional as F
class DualStreamLSTMBiGRU(nn.Module): """ 双流 LSTM + Attention-BiGRU 手势识别 Stream 1: 距离-多普勒图 → LSTM Stream 2: 角度-多普勒图 → LSTM 融合: Attention + BiGRU → 分类 """ def __init__(self, n_classes=8, input_size=64, hidden_dim=128): super().__init__() self.stream1_conv = nn.Sequential( nn.Conv2d(1, 32, 3, 1, 1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, 1, 1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.AdaptiveAvgPool2d(1) ) self.stream1_lstm = nn.LSTM( input_size=64, hidden_size=hidden_dim, num_layers=2, batch_first=True, dropout=0.3 ) self.stream2_conv = nn.Sequential( nn.Conv2d(1, 32, 3, 1, 1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, 1, 1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.AdaptiveAvgPool2d(1) ) self.stream2_lstm = nn.LSTM( input_size=64, hidden_size=hidden_dim, num_layers=2, batch_first=True, dropout=0.3 ) self.attention = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.Tanh(), nn.Linear(hidden_dim, 2), nn.Softmax(dim=-1) ) self.bigru = nn.GRU( input_size=hidden_dim, hidden_size=hidden_dim, num_layers=2, batch_first=True, bidirectional=True, dropout=0.3 ) self.classifier = nn.Sequential( nn.Linear(hidden_dim * 2, 64), nn.ReLU(), nn.Dropout(0.5), nn.Linear(64, n_classes) ) def forward(self, rd_map, ad_map): """ Args: rd_map: (B, T, 1, H, W) 距离-多普勒图序列 ad_map: (B, T, 1, H, W) 角度-多普勒图序列 Returns: logits: (B, n_classes) """ B, T = rd_map.shape[:2] rd_features = [] for t in range(T): feat = self.stream1_conv(rd_map[:, t]) rd_features.append(feat.squeeze(-1).squeeze(-1)) rd_seq = torch.stack(rd_features, dim=1) rd_lstm, _ = self.stream1_lstm(rd_seq) ad_features = [] for t in range(T): feat = self.stream2_conv(ad_map[:, t]) ad_features.append(feat.squeeze(-1).squeeze(-1)) ad_seq = torch.stack(ad_features, dim=1) ad_lstm, _ = self.stream2_lstm(ad_seq) combined = torch.cat([rd_lstm, ad_lstm], dim=-1) attn_weights = self.attention(combined) fused = (rd_lstm * attn_weights[..., 0:1] + ad_lstm * attn_weights[..., 1:2]) gru_out, _ = self.bigru(fused) output = gru_out[:, -1, :] return self.classifier(output)
CABIN_GESTURES = { 0: "swipe_left", 1: "swipe_right", 2: "swipe_up", 3: "swipe_down", 4: "rotate_cw", 5: "rotate_ccw", 6: "push", 7: "pull", }
if __name__ == "__main__": model = DualStreamLSTMBiGRU(n_classes=8) rd = torch.randn(4, 30, 1, 64, 64) ad = torch.randn(4, 30, 1, 64, 64) logits = model(rd, ad) print(f"RD输入: {rd.shape}") print(f"AD输入: {ad.shape}") print(f"输出: {logits.shape} (8类手势)") print(f"参数: {sum(p.numel() for p in model.parameters()):,}")
|