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
| import torch import torch.nn as nn import torch.nn.functional as F from torchvision.models import efficientnet_v2_s
class OccupantClassifier(nn.Module): """乘员分类器(多任务)""" def __init__(self, backbone: str = 'efficientnet_v2_s', num_age_groups: int = 8, pretrained: bool = True): super().__init__() if backbone == 'efficientnet_v2_s': self.backbone = efficientnet_v2_s(pretrained=pretrained) feature_dim = self.backbone.classifier[1].in_features self.backbone.classifier = nn.Identity() self.shared_features = nn.Sequential( nn.Linear(feature_dim, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 256), nn.ReLU(), ) self.age_head = nn.Sequential( nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.2), nn.Linear(128, num_age_groups) ) self.gender_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, 2) ) self.type_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, 3) ) self.child_seat_head = nn.Sequential( nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, 2) ) def forward(self, x: torch.Tensor) -> dict: """ Args: x: (B, C, H, W) 输入图像(乘员区域) Returns: { 'age_logits': (B, num_age_groups), 'gender_logits': (B, 2), 'type_logits': (B, 3), 'child_seat_logits': (B, 2) } """ features = self.backbone(x) shared = self.shared_features(features) return { 'age_logits': self.age_head(shared), 'gender_logits': self.gender_head(shared), 'type_logits': self.type_head(shared), 'child_seat_logits': self.child_seat_head(shared) }
class OccupantClassifierLoss(nn.Module): """多任务损失函数""" def __init__(self, age_weight: float = 1.0, gender_weight: float = 1.0, type_weight: float = 2.0, child_seat_weight: float = 1.5): super().__init__() self.age_weight = age_weight self.gender_weight = gender_weight self.type_weight = type_weight self.child_seat_weight = child_seat_weight self.ce_loss = nn.CrossEntropyLoss() def forward(self, predictions: dict, targets: dict) -> dict: """ Args: predictions: 模型输出 targets: { 'age': (B,), 'gender': (B,), 'type': (B,), 'child_seat': (B,) } Returns: { 'total_loss': tensor, 'age_loss': tensor, 'gender_loss': tensor, 'type_loss': tensor, 'child_seat_loss': tensor } """ age_loss = self.ce_loss(predictions['age_logits'], targets['age']) gender_loss = self.ce_loss(predictions['gender_logits'], targets['gender']) type_loss = self.ce_loss(predictions['type_logits'], targets['type']) child_seat_loss = self.ce_loss(predictions['child_seat_logits'], targets['child_seat']) total_loss = ( self.age_weight * age_loss + self.gender_weight * gender_loss + self.type_weight * type_loss + self.child_seat_weight * child_seat_loss ) return { 'total_loss': total_loss, 'age_loss': age_loss, 'gender_loss': gender_loss, 'type_loss': type_loss, 'child_seat_loss': child_seat_loss }
|