detnet代码解析

DetNet结构+代码详细解析

由于最近的项目需要用到对模型进行改进,因此记录一些新的backbone网络,也方便自己日后查阅。

首先是detnet的基本结构描述,这是一个在resnet的基础上改进的网络,resnet的网络结构如下:
resnet基本结构

与resnet相同的前4个stage

detnet-59是在resnet50的基础上进行改进,前四个stage都是一样的,也就是1+3×3+3×4+3×6=40层是一模一样的。而在代码中,是以Bottleneck类的形式出现:

class Bottleneck(nn.Module):
    expansion = 4    #表示一个Bottleneck结构后通道数的扩张倍数

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:      #使残差部分和卷积前向出来之后通道一致
            residual = self.downsample(x)        

        out += residual                      #参差部分相加
        out = self.relu(out)

        return out

可以看出3层卷积时通道数是这样变化的:inplanes–planes–planes–4*planes。
而在创建网络时是这样用的:

    def _make_layer(self, block, planes, blocks, stride=1):  #这里的block就是上文的Bottleneck类
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,    #如果输入的通道参数不对,做下采样调整
                          kernel_size=1, stride=stride, bias=False),          
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):                                    #blocks意味着需要多少个重复的单元
            layers.append(block(self.inplanes, planes))                  

        return nn.Sequential(*layers)

然后再加入网络层数的设置就可以将前四层设置好了:

        self.layer1 = self._make_layer(block, 64, layers[0])  #layers[:3]=[3,4,6] 对应resnet50中2-4的stage
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)

后2个不同的stage

detnet后面给出自己设计的Bottleneck类,首先我们先来看一下结构:

class BottleneckA(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BottleneckA, self).__init__()
        assert inplanes == (planes * 4), 'inplanes != planes * 4'
        assert stride == 1, 'stride != 1'
        assert downsample is None, 'downsample is not None'
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)  # inplanes = 1024, planes = 256
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, dilation=2,
                               padding=2, bias=False)  # stride = 1, dilation = 2
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:  # downsample always is None, because stride=1 and inplanes=expansion * planes
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out

bottleneckA便是下图这样的结构,主干路上增加了空洞卷积,另一线路并不做处理。整个通道的变化是inplanes–planes–planes–planes*4,而通常设置planes是inplanes的四分之一,因此经过一个bottleneckA结构后,feature的通道数并没有发生改变。
在这里插入图片描述
而bottleneckB则在残差线路上加了一个卷积环节:

class BottleneckB(nn.Module):
    expansion = 4
    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BottleneckB, self).__init__()
        assert inplanes == (planes * 4), 'inplanes != planes * 4'
        assert stride == 1, 'stride != 1'
        assert downsample is None, 'downsample is not None'
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)  # inplanes = 1024, planes = 256
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, dilation=2,
                               padding=2, bias=False)  # stride = 1, dilation = 2
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * 4)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride
        self.extra_conv = nn.Sequential(     #对残差进行处理的单元
            nn.Conv2d(inplanes, planes * 4, kernel_size=1, bias=False),
            nn.BatchNorm2d(planes * 4)
        )

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        residual = self.extra_conv(x)      #区别就在这一句话

        if self.downsample is not None:  # downsample always is None, because stride=1 and inplanes=expansion * planes
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)               #继续相加

        return out

结构如下图所示:
在这里插入图片描述
stage5和6 便是一个bottleneckB后接俩个bottleneckA结构,也就是3 ×(2+1)× 2 = 18层,加上前面的40层,便是58层,再加上最后的全连接层,也就构成了Detnet-59的全部基本结构了。

整体网络生成的代码

看完了detnet每个单元的详细代码之后,再来看一看总体生成网络的代码:

class DetNet(nn.Module):
    def __init__(self, block, layers, num_classes=1000):  #block就是Bottleneck, 这里layers=[3, 4, 6, 3, 3]
        self.inplanes = 64      
        super(DetNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,    
                               bias=False)     #网络的第一层卷积,卷积核为7*7
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) #第一个池化层         这里layers=[3, 4, 6, 3, 3],与上文提到的结构相同
        self.layer1 = self._make_layer(block, 64, layers[0]) 
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_new_layer(256, layers[3]) #stage5
        self.layer5 = self._make_new_layer(256, layers[4]) #stage6
        self.avgpool = nn.AdaptiveAvgPool2d(1) #pytorch自带的roi_pooling
        self.fc = nn.Linear(1024, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layer(self, block, planes, blocks, stride=1):#与resnet一样的layer
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),          
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):     #设置了几个这样的层,就创建几个层
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def _make_new_layer(self, planes, blocks):  #创建新的detnet自己特有的单元
        downsample = None
        block_b = BottleneckB
        block_a = BottleneckA

        layers = []
        layers.append(block_b(self.inplanes, planes, stride=1, downsample=downsample))   #每个stage中开头都是bottleneckB的结构
        self.inplanes = planes * block_b.expansion
        for i in range(1, blocks):   #再加上blocks-1个bottleneckA
            layers.append(block_a(self.inplanes, planes))

        return nn.Sequential(*layers)
    
    def forward(self, x):    #简洁的前向结构
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x
展开阅读全文

没有更多推荐了,返回首页