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 183 184 185 186 187 188 189 190 191 192 193 194
| import torch import torch.nn as nn import numpy as np from dataclasses import dataclass
@dataclass class FusionConfig: """融合配置""" cam_resolution: tuple = (480, 640) cam_fps: int = 30 radar_fps: int = 10 radar_range: float = 2.0 radar_points: int = 200 fusion_method: str = 'hybrid' confidence_threshold: float = 0.7
class CameraBranch(nn.Module): """摄像头分支""" def __init__(self, embed_dim: int = 128): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 16, 3, stride=2, padding=1), nn.ReLU6(), nn.Conv2d(16, 32, 3, stride=2, padding=1), nn.ReLU6(), nn.Conv2d(32, 64, 3, stride=2, padding=1), nn.ReLU6(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.ReLU6(), nn.AdaptiveAvgPool2d(1), ) self.fc = nn.Linear(128, embed_dim) def forward(self, x): return self.fc(self.backbone(x).flatten(1))
class RadarBranch(nn.Module): """雷达分支(点云)""" def __init__(self, n_points: int = 200, embed_dim: int = 128): super().__init__() self.mlp = nn.Sequential( nn.Linear(5, 32), nn.ReLU(), nn.Linear(32, 64), nn.ReLU(), ) self.fc = nn.Linear(64, embed_dim) self.n_points = n_points def forward(self, points): """points: (B, N, 5)""" x = self.mlp(points) x = x.max(dim=1)[0] return self.fc(x)
class HybridFusion(nn.Module): """ 混合融合:特征级+决策级 特征级:联合特征向量→分类 决策级:各模态独立分类→加权融合 """ def __init__(self, cam_dim=128, radar_dim=128, n_classes=5): super().__init__() self.cam_branch = CameraBranch(cam_dim) self.radar_branch = RadarBranch(embed_dim=radar_dim) self.feature_fusion = nn.Sequential( nn.Linear(cam_dim + radar_dim, 128), nn.ReLU(), nn.Dropout(0.1), nn.Linear(128, n_classes) ) self.cam_classifier = nn.Sequential( nn.Linear(cam_dim, 64), nn.ReLU(), nn.Linear(64, n_classes) ) self.radar_classifier = nn.Sequential( nn.Linear(radar_dim, 64), nn.ReLU(), nn.Linear(64, n_classes) ) def forward(self, camera_input, radar_input): cam_feat = self.cam_branch(camera_input) radar_feat = self.radar_branch(radar_input) fused_feat = torch.cat([cam_feat, radar_feat], dim=1) feat_output = self.feature_fusion(fused_feat) cam_output = self.cam_classifier(cam_feat) radar_output = self.radar_classifier(radar_feat) cam_weight = 0.2 radar_weight = 0.2 feat_weight = 0.6 cam_conf = torch.softmax(cam_output, dim=-1).max(dim=-1)[0] cam_weight = torch.where( cam_conf < 0.5, cam_weight * 0.5, cam_weight ) final = (feat_weight * feat_output + cam_weight * cam_output + radar_weight * radar_output) final = torch.softmax(final, dim=-1) return final, { 'camera': torch.softmax(cam_output, dim=-1), 'radar': torch.softmax(radar_output, dim=-1), 'feature': torch.softmax(feat_output, dim=-1), 'cam_confidence': cam_conf, }
class CabinSensorFusionSystem: """完整座舱传感器融合系统""" def __init__(self): self.model = HybridFusion() self.config = FusionConfig() self.labels = ['无人', '成人', '儿童', '儿童座椅', '宠物'] def process(self, camera_frame, radar_points): """处理一帧""" with torch.no_grad(): probs, details = self.model(camera_frame, radar_points) pred = probs.argmax(dim=-1) confidence = probs.max(dim=-1)[0] result = { 'label': self.labels[pred.item()], 'confidence': confidence.item(), 'modality_details': { k: v[0].tolist() if hasattr(v, '__getitem__') else v for k, v in details.items() }, 'redundancy': 'both' if details['cam_confidence'] > 0.5 else 'radar_dominant', } if details['cam_confidence'] < 0.5: result['warning'] = '摄像头低置信度,依赖雷达' return result
if __name__ == "__main__": system = CabinSensorFusionSystem() print("=== Murata×Smart Eye摄像头-雷达融合 ===") cam = torch.randn(1, 3, 480, 640) radar = torch.randn(1, 200, 5) result = system.process(cam, radar) print(f"\n正常场景:") print(f" 标签: {result['label']}") print(f" 置信度: {result['confidence']:.3f}") print(f" 冗余模式: {result['redundancy']}") cam_occluded = torch.randn(1, 3, 480, 640) * 0.1 result_occ = system.process(cam_occluded, radar) print(f"\n遮挡场景:") print(f" 标签: {result_occ['label']}") print(f" 置信度: {result_occ['confidence']:.3f}") print(f" 冗余模式: {result_occ['redundancy']}") print(f" 警告: {result_occ.get('warning', '无')}") params = sum(p.numel() for p in system.model.parameters()) print(f"\n参数: {params:,}") print(f"模型大小: {params * 4 / 1024:.1f} KB (FP32)")
|