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
| import torch import torch.nn as nn import torch.nn.functional as F
class SeatbeltSegmentationNet(nn.Module): """ 安全带语义分割网络 输入: RGB图像 (B, 3, H, W) 输出: 分割掩码 (B, num_classes, H, W) 类别: - 0: 背景 - 1: 肩带 - 2: 腰带 - 3: buckle """ def __init__(self, num_classes=4): super().__init__() self.encoder = ResNetEncoder(pretrained=True) self.decoder = FPNDecoder( in_channels=[256, 512, 1024, 2048], out_channels=128 ) self.attention = ChannelSpatialAttention(128) self.seg_head = nn.Sequential( nn.Conv2d(128, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, num_classes, 1) ) def forward(self, x): """ Args: x: (B, 3, H, W) 输入图像 Returns: segmentation: (B, num_classes, H, W) 分割logits """ features = self.encoder(x) decoded = self.decoder(features) attended = self.attention(decoded) segmentation = self.seg_head(attended) segmentation = F.interpolate( segmentation, size=x.shape[2:], mode='bilinear', align_corners=False ) return segmentation
class ResNetEncoder(nn.Module): """ResNet编码器""" def __init__(self, pretrained=True): super().__init__() import torchvision.models as models resnet = models.resnet50(pretrained=pretrained) self.conv1 = resnet.conv1 self.bn1 = resnet.bn1 self.relu = resnet.relu self.maxpool = resnet.maxpool self.layer1 = resnet.layer1 self.layer2 = resnet.layer2 self.layer3 = resnet.layer3 self.layer4 = resnet.layer4 def forward(self, x): """返回多尺度特征""" features = [] x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) features.append(x) x = self.layer2(x) features.append(x) x = self.layer3(x) features.append(x) x = self.layer4(x) features.append(x) return features
class FPNDecoder(nn.Module): """特征金字塔解码器""" def __init__(self, in_channels, out_channels): super().__init__() self.lateral_convs = nn.ModuleList([ nn.Conv2d(in_ch, out_channels, 1) for in_ch in in_channels ]) self.smooth_convs = nn.ModuleList([ nn.Conv2d(out_channels, out_channels, 3, padding=1) for _ in in_channels ]) def forward(self, features): """ 自顶向下融合 """ laterals = [conv(f) for conv, f in zip(self.lateral_convs, features)] for i in range(len(laterals)-1, 0, -1): laterals[i-1] = laterals[i-1] + F.interpolate( laterals[i], size=laterals[i-1].shape[2:], mode='nearest' ) outputs = [conv(lat) for conv, lat in zip(self.smooth_convs, laterals)] target_size = outputs[0].shape[2:] upsampled = [outputs[0]] for out in outputs[1:]: upsampled.append(F.interpolate(out, size=target_size, mode='bilinear')) fused = torch.cat(upsampled, dim=1) fused = nn.Conv2d(fused.shape[1], 128, 1)(fused) return fused
class ChannelSpatialAttention(nn.Module): """通道-空间注意力""" def __init__(self, channels, reduction=16): super().__init__() self.channel_attention = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(channels, channels // reduction, 1), nn.ReLU(), nn.Conv2d(channels // reduction, channels, 1), nn.Sigmoid() ) self.spatial_attention = nn.Sequential( nn.Conv2d(channels, 1, 7, padding=3), nn.Sigmoid() ) def forward(self, x): ca = self.channel_attention(x) x = x * ca sa = self.spatial_attention(x) x = x * sa return x
|