|
| 1 | +import torch |
| 2 | +import torch.nn.functional as F |
| 3 | +import torch.nn as nn |
| 4 | +import math |
| 5 | +from collections import OrderedDict |
| 6 | + |
| 7 | +#-------------------------------------------------# |
| 8 | +# MISH激活函数 |
| 9 | +#-------------------------------------------------# |
| 10 | +class Mish(nn.Module): |
| 11 | + def __init__(self): |
| 12 | + super(Mish, self).__init__() |
| 13 | + |
| 14 | + def forward(self, x): |
| 15 | + return x * torch.tanh(F.softplus(x)) |
| 16 | + |
| 17 | +#-------------------------------------------------# |
| 18 | +# 卷积块 |
| 19 | +# CONV+BATCHNORM+MISH |
| 20 | +#-------------------------------------------------# |
| 21 | +class BasicConv(nn.Module): |
| 22 | + def __init__(self, in_channels, out_channels, kernel_size, stride=1): |
| 23 | + super(BasicConv, self).__init__() |
| 24 | + |
| 25 | + self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, kernel_size//2, bias=False) |
| 26 | + self.bn = nn.BatchNorm2d(out_channels) |
| 27 | + self.activation = Mish() |
| 28 | + |
| 29 | + def forward(self, x): |
| 30 | + x = self.conv(x) |
| 31 | + x = self.bn(x) |
| 32 | + x = self.activation(x) |
| 33 | + return x |
| 34 | + |
| 35 | +#---------------------------------------------------# |
| 36 | +# CSPdarknet的结构块的组成部分 |
| 37 | +# 内部堆叠的残差块 |
| 38 | +#---------------------------------------------------# |
| 39 | +class Resblock(nn.Module): |
| 40 | + def __init__(self, channels, hidden_channels=None, residual_activation=nn.Identity()): |
| 41 | + super(Resblock, self).__init__() |
| 42 | + |
| 43 | + if hidden_channels is None: |
| 44 | + hidden_channels = channels |
| 45 | + |
| 46 | + self.block = nn.Sequential( |
| 47 | + BasicConv(channels, hidden_channels, 1), |
| 48 | + BasicConv(hidden_channels, channels, 3) |
| 49 | + ) |
| 50 | + |
| 51 | + def forward(self, x): |
| 52 | + return x + self.block(x) |
| 53 | + |
| 54 | +#---------------------------------------------------# |
| 55 | +# CSPdarknet的结构块 |
| 56 | +# 存在一个大残差边 |
| 57 | +# 这个大残差边绕过了很多的残差结构 |
| 58 | +#---------------------------------------------------# |
| 59 | +class Resblock_body(nn.Module): |
| 60 | + def __init__(self, in_channels, out_channels, num_blocks, first): |
| 61 | + super(Resblock_body, self).__init__() |
| 62 | + |
| 63 | + self.downsample_conv = BasicConv(in_channels, out_channels, 3, stride=2) |
| 64 | + |
| 65 | + if first: |
| 66 | + self.split_conv0 = BasicConv(out_channels, out_channels, 1) |
| 67 | + self.split_conv1 = BasicConv(out_channels, out_channels, 1) |
| 68 | + self.blocks_conv = nn.Sequential( |
| 69 | + Resblock(channels=out_channels, hidden_channels=out_channels//2), |
| 70 | + BasicConv(out_channels, out_channels, 1) |
| 71 | + ) |
| 72 | + self.concat_conv = BasicConv(out_channels*2, out_channels, 1) |
| 73 | + else: |
| 74 | + self.split_conv0 = BasicConv(out_channels, out_channels//2, 1) |
| 75 | + self.split_conv1 = BasicConv(out_channels, out_channels//2, 1) |
| 76 | + |
| 77 | + self.blocks_conv = nn.Sequential( |
| 78 | + *[Resblock(out_channels//2) for _ in range(num_blocks)], |
| 79 | + BasicConv(out_channels//2, out_channels//2, 1) |
| 80 | + ) |
| 81 | + self.concat_conv = BasicConv(out_channels, out_channels, 1) |
| 82 | + |
| 83 | + def forward(self, x): |
| 84 | + x = self.downsample_conv(x) |
| 85 | + |
| 86 | + x0 = self.split_conv0(x) |
| 87 | + |
| 88 | + x1 = self.split_conv1(x) |
| 89 | + x1 = self.blocks_conv(x1) |
| 90 | + |
| 91 | + x = torch.cat([x1, x0], dim=1) |
| 92 | + x = self.concat_conv(x) |
| 93 | + |
| 94 | + return x |
| 95 | + |
| 96 | +class CSPDarkNet(nn.Module): |
| 97 | + def __init__(self, layers): |
| 98 | + super(CSPDarkNet, self).__init__() |
| 99 | + self.inplanes = 32 |
| 100 | + self.conv1 = BasicConv(3, self.inplanes, kernel_size=3, stride=1) |
| 101 | + self.feature_channels = [64, 128, 256, 512, 1024] |
| 102 | + |
| 103 | + self.stages = nn.ModuleList([ |
| 104 | + Resblock_body(self.inplanes, self.feature_channels[0], layers[0], first=True), |
| 105 | + Resblock_body(self.feature_channels[0], self.feature_channels[1], layers[1], first=False), |
| 106 | + Resblock_body(self.feature_channels[1], self.feature_channels[2], layers[2], first=False), |
| 107 | + Resblock_body(self.feature_channels[2], self.feature_channels[3], layers[3], first=False), |
| 108 | + Resblock_body(self.feature_channels[3], self.feature_channels[4], layers[4], first=False) |
| 109 | + ]) |
| 110 | + |
| 111 | + self.num_features = 1 |
| 112 | + # 进行权值初始化 |
| 113 | + for m in self.modules(): |
| 114 | + if isinstance(m, nn.Conv2d): |
| 115 | + n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels |
| 116 | + m.weight.data.normal_(0, math.sqrt(2. / n)) |
| 117 | + elif isinstance(m, nn.BatchNorm2d): |
| 118 | + m.weight.data.fill_(1) |
| 119 | + m.bias.data.zero_() |
| 120 | + |
| 121 | + |
| 122 | + def forward(self, x): |
| 123 | + x = self.conv1(x) |
| 124 | + |
| 125 | + x = self.stages[0](x) |
| 126 | + x = self.stages[1](x) |
| 127 | + out3 = self.stages[2](x) |
| 128 | + out4 = self.stages[3](out3) |
| 129 | + out5 = self.stages[4](out4) |
| 130 | + |
| 131 | + return out3, out4, out5 |
| 132 | + |
| 133 | +def darknet53(pretrained, **kwargs): |
| 134 | + model = CSPDarkNet([1, 2, 8, 8, 4]) |
| 135 | + if pretrained: |
| 136 | + if isinstance(pretrained, str): |
| 137 | + model.load_state_dict(torch.load(pretrained)) |
| 138 | + else: |
| 139 | + raise Exception("darknet request a pretrained path. got [{}]".format(pretrained)) |
| 140 | + return model |
0 commit comments