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
| import torch import torch.nn as nn
class BoundaryAwareOOPModel(nn.Module): """ 边界条件感知的OOP安全预测模型 借鉴BAT-NO的思想: - 将边界条件(位置/姿态/体型)作为显式输入 - Transformer编码边界条件 - 预测碰撞中的安全约束效果 """ def __init__(self, num_boundary_types=4, hidden_dim=128): super().__init__() self.position_encoder = nn.Linear(3, hidden_dim) self.posture_encoder = nn.Linear(6, hidden_dim) self.body_type_encoder = nn.Linear(4, hidden_dim) self.seat_encoder = nn.Linear(2, hidden_dim) encoder_layer = nn.TransformerEncoderLayer( d_model=hidden_dim, nhead=4, dim_feedforward=hidden_dim * 4, batch_first=True ) self.boundary_transformer = nn.TransformerEncoder( encoder_layer, num_layers=3 ) self.strategy_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, 4) ) self.risk_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, 1), nn.Sigmoid() ) def forward(self, position, posture, body_type, seat_pos): pos_feat = self.position_encoder(position).unsqueeze(1) post_feat = self.posture_encoder(posture).unsqueeze(1) body_feat = self.body_type_encoder(body_type).unsqueeze(1) seat_feat = self.seat_encoder(seat_pos).unsqueeze(1) boundary_seq = torch.cat([ pos_feat, post_feat, body_feat, seat_feat ], dim=1) encoded = self.boundary_transformer(boundary_seq) global_repr = encoded.mean(dim=1) strategy = self.strategy_head(global_repr) risk = self.risk_head(global_repr) return { 'airbag_strategy': strategy, 'oop_risk': risk, }
if __name__ == "__main__": model = BoundaryAwareOOPModel() B = 4 position = torch.randn(B, 3) posture = torch.randn(B, 6) body_type = torch.randn(B, 4) seat_pos = torch.randn(B, 2) result = model(position, posture, body_type, seat_pos) print("边界条件感知OOP模型测试:") print(f" 气囊策略: {result['airbag_strategy'].shape}") print(f" OOP风险: {result['oop_risk'].squeeze().tolist()}") print("\n✅ 测试通过")
|