DeepLabv3+ 中Dilation ASPP 以及Decoder模块

🐎一个很好的讲解
Pytorch 完整代码1
Pytorch 完整代码2

对于DilatedFCN,主要是修改分类网络的后面block,用空洞卷积来替换stride=2的下采样层,引入空洞卷积,在维持特征图大小的同时保证了感受野和原始网络一致。

在DeepLab中,采用空间金字塔池化模块来进一步提取多尺度信息,这里是采用不同rate的空洞卷积来实现这一点。ASPP模块主要包含以下几个部分: (1) 一个1×1卷积层,以及三个3x3的空洞卷积,对于output_stride=16,其rate为(6, 12, 18) ,若output_stride=8,rate加倍(这些卷积层的输出channel数均为256,并且含有BN层); (2)一个全局平均池化层得到image-level特征,然后送入1x1卷积层(输出256个channel),并双线性插值到原始大小; (3)将(1)和(2)得到的4个不同尺度的特征在channel维度concat在一起,然后送入1x1的卷积进行融合并得到256-channel的新特征。

空洞卷积计算公式:
o u t p u t = ⌊ i n p u t + 2 ∗ p a d d i n g − k − ( k − 1 ) ∗ ( d − 1 ) s ⌋ + 1 output = \lfloor \frac{input + 2*padding - k - (k-1)*(d-1)}{s} \rfloor + 1 output=sinput+2paddingk(k1)(d1)+1
d为塞入的空格数。

import torch
import torch.nn as nn
import torchvision
from torch.nn import functional as F
import numpy as np
from torchsummary import summary
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

def aspp_branch(in_channels, out_channels, kernel_size, dilation):
    padding = 0 if kernel_size == 1 else dilation
    return nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, dilation=dilation, bias=False),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
    
class ASPP(nn.Module):
    def __init__(self, in_channels, output_stride):
        super(ASPP, self).__init__()
        
        assert output_stride in [8, 16], 'Only output stride of 8 and 16 are supported'
        if output_stride == 16: dilation = [1,6,12,18]
        elif output_stride == 8: dilation = [1,12,24,36]
        
        self.aspp1 = aspp_branch(in_channels, 256, 1, dilation=dilation[0])
        self.aspp2 = aspp_branch(in_channels, 256, 3, dilation = dilation[1])
        self.aspp3 = aspp_branch(in_channels, 256, 3, dilation = dilation[2])
        self.aspp4 = aspp_branch(in_channels, 256, 3, dilation = dilation[3])
        
        self.avg_pool = nn.Sequential(
            nn.AdaptiveAvgPool2d((1,1)), #[batch, channel, 1, 1]
            nn.Conv2d(in_channels, 256, 1, bias = False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True)
        )
        
        self.conv1 = nn.Conv2d(256 * 5, 256, 1, bias = False)
        self.bn1 = nn.BatchNorm2d(256)
        self.relu = nn.ReLU(inplace=True)
        self.dropout = nn.Dropout(0.5)
        
    def forward(self, x):
        x1 = self.aspp1(x)
        x2 = self.aspp2(x)
        x3 = self.aspp3(x)
        x4 = self.aspp4(x)
        x5 = F.interpolate(self.avg_pool(x), size=(x.size(2), x.size(3)), mode='bilinear', align_corners=True)
        
        x = self.conv1(torch.cat((x1, x2, x3, x4, x5), dim = 1))
        x = self.bn1(x)
        x = self.dropout(self.relu(x))
        
        return x

class Decoder(nn.Module):
    def __init__(self, low_level_channels, num_classes):
        super(Decoder, self).__init__()
        self.conv1 = nn.Conv2d(low_level_channels, 48, 1, bias=False)
        self.bn1 = nn.BatchNorm2d(48)
        self.relu = nn.ReLU(inplace=True)
        
        self.output = nn.Sequential(
            nn.Conv2d(48+256, 256, 3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, num_classes, 1, stride = 1)
        )
        
    def forward(self, x, low_level_features):
        low_level_features = self.conv1(low_level_features)
        low_level_features = self.relu(self.bn1(low_level_features))
        H, W = low_level_features.size(2), low_level_features.size(3)
        
        x = F.interpolate(x, size = (H,W), mode = 'bilinear', align_corners=True)
        x = self.output(torch.cat((low_level_features, x), dim = 1))
        return x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值