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
| import torch import torch.nn as nn
class ICFFusionModel(nn.Module): """ ICF-Fusion: 多模态座舱乘员姿态融合模型 融合5种传感器模态,输出3D姿态+人体测量+OOP分类 参考框架来自: Preu et al., Vehicles 8(8), 2026 """ def __init__(self, num_keypoints=17, num_oop_classes=5, num_anthro=4): super().__init__() self.rgb_encoder = RGBEncoder(in_channels=3, feat_dim=256) self.depth_encoder = DepthEncoder(in_channels=1, feat_dim=256) self.ir_encoder = IREncoder(in_channels=1, feat_dim=128) self.pressure_encoder = PressureEncoder( input_size=64, feat_dim=128 ) self.radar_encoder = RadarEncoder(feat_dim=128) total_dim = 256 + 256 + 128 + 128 + 128 self.cross_modal_attention = CrossModalAttention( d_model=total_dim, nhead=8 ) self.pose_head = nn.Linear(total_dim, num_keypoints * 3) self.anthro_head = nn.Sequential( nn.Linear(total_dim, 128), nn.ReLU(), nn.Linear(128, num_anthro) ) self.oop_head = nn.Sequential( nn.Linear(total_dim, 128), nn.ReLU(), nn.Linear(128, num_oop_classes) ) self.head_pos_head = nn.Linear(total_dim, 3) def forward(self, rgb, depth, ir, pressure, radar): rgb_feat = self.rgb_encoder(rgb) depth_feat = self.depth_encoder(depth) ir_feat = self.ir_encoder(ir) pressure_feat = self.pressure_encoder(pressure) radar_feat = self.radar_encoder(radar) fused = torch.cat([ rgb_feat, depth_feat, ir_feat, pressure_feat, radar_feat ], dim=1) fused = self.cross_modal_attention(fused.unsqueeze(1)).squeeze(1) pose = self.pose_head(fused).view(-1, 17, 3) anthro = self.anthro_head(fused) oop = self.oop_head(fused) head_pos = self.head_pos_head(fused) return { "pose_3d": pose, "anthropometrics": anthro, "oop_class": oop, "head_position": head_pos }
class RGBEncoder(nn.Module): """RGB特征提取(基于ResNet-18简化)""" def __init__(self, in_channels=3, feat_dim=256): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(in_channels, 64, 7, 2, 3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(3, 2, 1), *self._make_layer(64, 64, 2), *self._make_layer(64, 128, 2, stride=2), *self._make_layer(128, 256, 2, stride=2), nn.AdaptiveAvgPool2d(1), ) self.fc = nn.Linear(256, feat_dim) def _make_layer(self, in_c, out_c, num_blocks, stride=1): layers = [nn.Conv2d(in_c, out_c, 3, stride, 1, bias=False), nn.BatchNorm2d(out_c), nn.ReLU()] for _ in range(num_blocks - 1): layers += [nn.Conv2d(out_c, out_c, 3, 1, 1, bias=False), nn.BatchNorm2d(out_c), nn.ReLU()] return layers def forward(self, x): x = self.backbone(x) return self.fc(x.flatten(1))
class DepthEncoder(nn.Module): """深度图编码器""" def __init__(self, in_channels=1, feat_dim=256): super().__init__() self.encoder = nn.Sequential( nn.Conv2d(in_channels, 32, 5, 2, 2), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, 3, 2, 1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.fc = nn.Linear(256, feat_dim) def forward(self, x): return self.fc(self.encoder(x).flatten(1))
class PressureEncoder(nn.Module): """座椅压力阵列编码器""" def __init__(self, input_size=64, feat_dim=128): super().__init__() self.encoder = nn.Sequential( nn.Conv2d(1, 16, 3, 1, 1), nn.ReLU(), nn.Conv2d(16, 32, 3, 2, 1), nn.ReLU(), nn.Conv2d(32, 64, 3, 2, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.fc = nn.Linear(64, feat_dim) def forward(self, x): return self.fc(self.encoder(x).flatten(1))
class CrossModalAttention(nn.Module): """跨模态注意力机制""" def __init__(self, d_model=896, nhead=8): super().__init__() self.attention = nn.MultiheadAttention(d_model, nhead, batch_first=True) self.norm = nn.LayerNorm(d_model) self.ffn = nn.Sequential( nn.Linear(d_model, d_model * 2), nn.ReLU(), nn.Linear(d_model * 2, d_model) ) def forward(self, x): attn_out, _ = self.attention(x, x, x) x = self.norm(x + attn_out) x = self.norm(x + self.ffn(x)) return x
if __name__ == "__main__": model = ICFFusionModel() rgb = torch.randn(4, 3, 224, 224) depth = torch.randn(4, 1, 224, 224) ir = torch.randn(4, 1, 224, 224) pressure = torch.randn(4, 1, 64, 64) radar = torch.randn(4, 1, 64, 100) print(f"模型参数量: {sum(p.numel() for p in model.parameters()):,}")
|