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
| """ 注意力融合机制
融合 CNN 分心检测和 YOLO 目标检测 """
import torch import torch.nn as nn import torch.nn.functional as F from typing import Tuple
class AttentionFusion(nn.Module): """ 注意力融合模块 核心思想: 动态调整分心检测和目标检测的权重 """ def __init__( self, distraction_dim: int = 10, hazard_dim: int = 80, hidden_dim: int = 128 ): super().__init__() self.distraction_encoder = nn.Sequential( nn.Linear(distraction_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 64) ) self.hazard_encoder = nn.Sequential( nn.Linear(hazard_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 64) ) self.attention = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 2), nn.Softmax(dim=-1) ) self.risk_classifier = nn.Sequential( nn.Linear(128, 64), nn.ReLU(), nn.Dropout(0.3), nn.Linear(64, 3) ) def forward( self, distraction_logits: torch.Tensor, hazard_features: torch.Tensor ) -> Tuple[torch.Tensor, torch.Tensor]: """ 前向传播 Args: distraction_logits: 分心检测输出 (B, 10) hazard_features: 目标特征 (B, 80) Returns: risk_logits: 风险分类 (B, 3) attention_weights: 注意力权重 (B, 2) """ d_feat = self.distraction_encoder(distraction_logits) h_feat = self.hazard_encoder(hazard_features) concat_feat = torch.cat([d_feat, h_feat], dim=-1) attention_weights = self.attention(concat_feat) weighted_distraction = attention_weights[:, 0:1] * d_feat weighted_hazard = attention_weights[:, 1:2] * h_feat fused_feat = torch.cat([weighted_distraction, weighted_hazard], dim=-1) risk_logits = self.risk_classifier(fused_feat) return risk_logits, attention_weights
class IntegratedADASSystem(nn.Module): """ 集成 ADAS 系统 包含: 1. CNN 分心检测 2. YOLOv4 目标检测 3. 注意力融合 4. 风险评估 """ def __init__(self): super().__init__() self.distraction_detector = DistractionDetector( backbone='resnet50', num_classes=10 ) self.fusion = AttentionFusion( distraction_dim=10, hazard_dim=80 ) self.risk_assessor = RoadRiskAssessor() def forward( self, driver_image: torch.Tensor, road_image: torch.Tensor ) -> Dict: """ 前向传播 Args: driver_image: 驾驶员图像 (B, 3, H, W) road_image: 道路图像 (B, 3, H, W) Returns: results: 检测结果 """ class_logits, distraction_type = self.distraction_detector(driver_image) distraction_score = F.softmax(class_logits, dim=-1) hazard_features = torch.randn(driver_image.size(0), 80) hazard_score = F.softmax(hazard_features, dim=-1) risk_logits, attention_weights = self.fusion( distraction_score, hazard_features ) risk_class = torch.argmax(risk_logits, dim=-1) return { 'distraction_score': distraction_score, 'distraction_type': distraction_type, 'hazard_score': hazard_score, 'risk_logits': risk_logits, 'risk_class': risk_class, 'attention_weights': attention_weights }
if __name__ == "__main__": system = IntegratedADASSystem() batch_size = 2 driver_image = torch.randn(batch_size, 3, 224, 224) road_image = torch.randn(batch_size, 3, 608, 608) results = system(driver_image, road_image) print("检测结果:") print(f" 分心分数: {results['distraction_score'].shape}") print(f" 风险类别: {results['risk_class']}") print(f" 注意力权重: {results['attention_weights']}")
|