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
| class MultiViewSpatioTemporalModel(nn.Module): """多视角时空特征融合模型""" def __init__(self): super().__init__() self.spatial_conv = nn.Sequential( nn.Conv2d(1, 32, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(2) ) self.temporal_lstm = nn.LSTM( input_size=64 * 16 * 16, hidden_size=128, num_layers=2, batch_first=True ) self.channel_attention = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(64, 16, kernel_size=1), nn.ReLU(), nn.Conv2d(16, 64, kernel_size=1), nn.Sigmoid() ) self.classifier = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Dropout(0.5), nn.Linear(64, 2) ) def forward(self, x): batch, time, H, W = x.shape spatial_features = [] for t in range(time): feat = self.spatial_conv(x[:, t:t+1]) att = self.channel_attention(feat) feat = feat * att spatial_features.append(feat.view(batch, -1)) temporal_input = torch.stack(spatial_features, dim=1) temporal_output, _ = self.temporal_lstm(temporal_input) output = self.classifier(temporal_output[:, -1]) return output
|