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
| import torch import torch.nn as nn import torch.nn.functional as F
class GELANBlock(nn.Module): """ Grouped Efficient Layer Aggregation Network (G-ELAN) 论文核心模块:分组高效层聚合网络 """ def __init__(self, in_channels: int, out_channels: int, groups: int = 4, expansion: float = 0.5): super().__init__() hidden_channels = int(in_channels * expansion) self.conv1 = nn.Conv2d(in_channels, hidden_channels, 1, 1) self.conv2 = nn.Conv2d(hidden_channels, hidden_channels, 3, 1, 1, groups=groups) self.conv3 = nn.Conv2d(hidden_channels, hidden_channels, 3, 1, 1, groups=groups) self.conv4 = nn.Conv2d(hidden_channels, hidden_channels, 3, 1, 1, groups=groups) self.channel_attention = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(hidden_channels * 3, hidden_channels // 4, 1), nn.ReLU(inplace=True), nn.Conv2d(hidden_channels // 4, hidden_channels * 3, 1), nn.Sigmoid() ) self.conv_out = nn.Conv2d(hidden_channels * 3, out_channels, 1, 1) def forward(self, x): x1 = self.conv1(x) x2 = self.conv2(x1) x3 = self.conv3(x2 + x1) x4 = self.conv4(x3 + x2) out = torch.cat([x2, x3, x4], dim=1) attention = self.channel_attention(out) out = out * attention out = self.conv_out(out) return out
class LightweightSeatbeltYOLO(nn.Module): """ 轻量化安全带检测YOLO模型 基于YOLOv7-tiny + G-ELAN改进 """ def __init__(self, num_classes: int = 3): """ Args: num_classes: 3类(无安全带、正确佩戴、错误佩戴) """ super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 32, 3, 2, 1), nn.BatchNorm2d(32), nn.SiLU(inplace=True), nn.Conv2d(32, 64, 3, 2, 1), nn.BatchNorm2d(64), nn.SiLU(inplace=True), GELANBlock(64, 64), nn.Conv2d(64, 128, 3, 2, 1), nn.BatchNorm2d(128), nn.SiLU(inplace=True), GELANBlock(128, 128), GELANBlock(128, 128), nn.Conv2d(128, 256, 3, 2, 1), nn.BatchNorm2d(256), nn.SiLU(inplace=True), GELANBlock(256, 256), GELANBlock(256, 256), nn.Conv2d(256, 512, 3, 2, 1), nn.BatchNorm2d(512), nn.SiLU(inplace=True), GELANBlock(512, 512), ) self.head = nn.ModuleList([ nn.Conv2d(128, num_classes + 5, 1), nn.Conv2d(256, num_classes + 5, 1), nn.Conv2d(512, num_classes + 5, 1), ]) def forward(self, x): features = [] for i, layer in enumerate(self.backbone): x = layer(x) if i in [8, 12, 17]: features.append(x) outputs = [] for i, head in enumerate(self.head): outputs.append(head(features[i])) return outputs
if __name__ == "__main__": model = LightweightSeatbeltYOLO(num_classes=3) total_params = sum(p.numel() for p in model.parameters()) trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print(f"总参数量: {total_params:,}") print(f"可训练参数量: {trainable_params:,}") print(f"模型大小: {total_params * 4 / 1024 / 1024:.2f} MB") x = torch.randn(1, 3, 640, 640) outputs = model(x) for i, out in enumerate(outputs): print(f"输出尺度 P{i+3}: {out.shape}")
|