cvpr2017 Densenet

作者: 风之羁绊 | 来源:发表于2018-11-11 14:55 被阅读0次

    正在做实验,要用到特征图连接,不知道怎么下手,以及是否符合反向传播,所以过来重新看下densenet以及它的代码。
    下面论文内容主要引用博客https://blog.csdn.net/u014380165/article/details/75142664

    densenet
    上面是主要模型图
    在传统的卷积神经网络中,如果你有L层,那么就会有L个连接,但是在DenseNet中,会有L(L+1)/2个连接。简单讲,就是每一层的输入来自前面所有层的输出。如下图:x0是input,H1的输入是x0(input),H2的输入是x0和x1(x1是H1的输出)
    公式

    然后呢 densenet实际工作就是把之前的特征图进行融合在了一起,作为新的输入。


    图片.png

    然后我们来看下代码,在github找了个新一点的代码https://github.com/andreasveit/densenet-pytorch
    模型代码只有100多行过一点,就全看一下好了。

    class BasicBlock(nn.Module):
        def __init__(self, in_planes, out_planes, dropRate=0.0):
            super(BasicBlock, self).__init__()
            self.bn1 = nn.BatchNorm2d(in_planes)
            self.relu = nn.ReLU(inplace=True)
            self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1,
                                   padding=1, bias=False)
            self.droprate = dropRate
        def forward(self, x):
            out = self.conv1(self.relu(self.bn1(x)))
            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, training=self.training)
    return torch.cat([x, out], 1)
    

    BasicBlock: bn,relu,conv三件套再加个可选择的dropout,然后用torch.cat在通道上进行融合。

    class BottleneckBlock(nn.Module):
        def __init__(self, in_planes, out_planes, dropRate=0.0):
            super(BottleneckBlock, self).__init__()
            inter_planes = out_planes * 4
            self.bn1 = nn.BatchNorm2d(in_planes)
            self.relu = nn.ReLU(inplace=True)
            self.conv1 = nn.Conv2d(in_planes, inter_planes, kernel_size=1, stride=1,
                                   padding=0, bias=False)
            self.bn2 = nn.BatchNorm2d(inter_planes)
            self.conv2 = nn.Conv2d(inter_planes, out_planes, kernel_size=3, stride=1,
                                   padding=1, bias=False)
            self.droprate = dropRate
        def forward(self, x):
            out = self.conv1(self.relu(self.bn1(x)))
            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, inplace=False, training=self.training)
            out = self.conv2(self.relu(self.bn2(out)))
            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, inplace=False, training=self.training)
            return torch.cat([x, out], 1)
    

    BottleneckBlock似乎就是把BasicBlock中间三件套重复了2遍???

    class TransitionBlock(nn.Module):
        def __init__(self, in_planes, out_planes, dropRate=0.0):
            super(TransitionBlock, self).__init__()
            self.bn1 = nn.BatchNorm2d(in_planes)
            self.relu = nn.ReLU(inplace=True)
            self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1,
                                   padding=0, bias=False)
            self.droprate = dropRate
        def forward(self, x):
            out = self.conv1(self.relu(self.bn1(x)))
            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, inplace=False, training=self.training)
    return F.avg_pool2d(out, 2)
    

    TransitionBlock在3件套加了个AvgPool2d() ,二维平均池化层,用来减小featuremap数量以及pooling提取出图片本来的特征

    class DenseBlock(nn.Module):
        def __init__(self, nb_layers, in_planes, growth_rate, block, dropRate=0.0):
            super(DenseBlock, self).__init__()
            self.layer = self._make_layer(block, in_planes, growth_rate, nb_layers, dropRate)
        def _make_layer(self, block, in_planes, growth_rate, nb_layers, dropRate):
            layers = []
            for i in range(nb_layers):
                layers.append(block(in_planes+i*growth_rate, growth_rate, dropRate))
            return nn.Sequential(*layers)
        def forward(self, x):
          return self.layer(x)
    

    DenseBlock定义了denset-net连接方式,通用的layers层

    class DenseNet3(nn.Module):
        def __init__(self, depth, num_classes, growth_rate=12,
                     reduction=0.5, bottleneck=True, dropRate=0.0):
            super(DenseNet3, self).__init__()
            in_planes = 2 * growth_rate
            n = (depth - 4) / 3
            if bottleneck == True:
                n = n/2
                block = BottleneckBlock
            else:
                block = BasicBlock
            n = int(n)
            # 1st conv before any dense block
            self.conv1 = nn.Conv2d(3, in_planes, kernel_size=3, stride=1,
                                   padding=1, bias=False)
            # 1st block
            self.block1 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
            in_planes = int(in_planes+n*growth_rate)
            self.trans1 = TransitionBlock(in_planes, int(math.floor(in_planes*reduction)), dropRate=dropRate)
            in_planes = int(math.floor(in_planes*reduction))
            # 2nd block
            self.block2 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
            in_planes = int(in_planes+n*growth_rate)
            self.trans2 = TransitionBlock(in_planes, int(math.floor(in_planes*reduction)), dropRate=dropRate)
            in_planes = int(math.floor(in_planes*reduction))
            # 3rd block
            self.block3 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
            in_planes = int(in_planes+n*growth_rate)
            # global average pooling and classifier
            self.bn1 = nn.BatchNorm2d(in_planes)
            self.relu = nn.ReLU(inplace=True)
            self.fc = nn.Linear(in_planes, num_classes)
            self.in_planes = in_planes
    
            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_()
                elif isinstance(m, nn.Linear):
                    m.bias.data.zero_()
        def forward(self, x):
            out = self.conv1(x)
            out = self.trans1(self.block1(out))
            out = self.trans2(self.block2(out))
            out = self.block3(out)
            out = self.relu(self.bn1(out))
            out = F.avg_pool2d(out, 8)
            out = out.view(-1, self.in_planes)
            return self.fc(out)
    

    bottleBlock和basicblock二选一,作为重复的最小的快
    1st conv 提取特征,把feature map数量扩大
    self.conv1 = nn.Conv2d(3, in_planes, kernel_size=3, stride=1,
    padding=1, bias=False)
    若干个(dense block和TransitionBlock)+全连接

    相关文章

      网友评论

        本文标题:cvpr2017 Densenet

        本文链接:https://www.haomeiwen.com/subject/ehmxfqtx.html