DeepLab v3+ 是DeepLab语义分割系列网络的最新作,其前作有 DeepLab v1,v2, v3, 在最新作中,Liang-Chieh Chen等人通过encoder-decoder进行多尺度信息的融合,同时保留了原来的空洞卷积和ASSP层, 其骨干网络使用了Xception模型,提高了语义分割的健壮性和运行速率。其在Pascal VOC上达到了 89.0% 的mIoU,在Cityscape上也取得了 82.1%的好成绩,下图展示了DeepLab v3+的基本结构:


其实在DCNN中主要是做一个特征提取,至于采用哪个网络做backbone具体问题具体对待,在这里我才用的是mobilenetv2(只是将deepwise_conv中添加了dilation, 添加空洞卷积是为了增大感受野)

网络结构分为Encode部分和decoder部分
先看encoder部分:

接在DCNN后面的实际上就是一个ASPP结构(采用不同的采样率来对特征图做空洞卷积),然后再将对应的结果进行拼接,需要注意的是传入ASPP结构的是DCNN得到的高层特征图image Pooling部分其实会改变特征图的尺寸,所以可以通过使用双线插值(为什么采用双线插值,因为简单)或者其他方式保证经过ASPP结构的各个特征图尺寸相同,最后再进行拼接

再看decoder部分
decoder部分首先会对传入的低层特征图进行通道调整,然后与encoder传入的特征图进行拼接,注意encoder传入的特征图需要经过上采样处理(维持与低层特征图相同的尺寸),最后输出部分只需要将尺寸还原到输入图片的尺寸就行了

import torch
import torch.nn as nn
import torch.functional as Fclass ASPP(nn.Module):def __init__(self, feature, atrous):super(ASPP, self).__init__()self.feature = featureself.Conv1 = _Deepwise_Conv(in_channels=feature.size()[1], out_channels=256, use_bias=False)self.Conv_rate1 = _Deepwise_Conv(in_channels=feature.size()[1], out_channels=256, rate=atrous[0],padding=atrous[0], use_bias=False)self.Conv_rate2 = _Deepwise_Conv(in_channels=feature.size()[1], out_channels=256, rate=atrous[1],padding=atrous[1], use_bias=False)self.Conv_rate3 = _Deepwise_Conv(in_channels=feature.size()[1], out_channels=256, rate=atrous[2],padding=atrous[2], use_bias=False)self.globalAvgPoolAndConv = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)),Conv(in_channels=320, out_channels=256, kernel_size=1, stride=1, use_bias=False),)self.Conv4 = Conv(in_channels=256 * 5, out_channels=256, kernel_size=1, stride=1, use_bias=False)self.dropout = nn.Dropout(p=0.1)def forward(self):f1 = self.Conv1(self.feature.clone())f2 = self.Conv_rate1(self.feature.clone())f3 = self.Conv_rate2(self.feature.clone())f4 = self.Conv_rate3(self.feature.clone())f5 = self.globalAvgPoolAndConv(self.feature.clone())f5 = F.interpolate(f5, size=(self.feature.size(2), self.feature.size(3)), mode='bilinear')x = torch.cat([f1, f2, f3, f4, f5], dim=1)x = self.Conv4(x)x = self.dropout(x)class Deeplabv3(nn.Module):def __init__(self, feature, atrous, skip1, num_class):super(Deeplabv3, self).__init__()self.num_class = num_classself.feature = ASPP(atrous=atrous, feature=feature).forward()self.skip1 = skip1self.encoder = ASPP(atrous=atrous, feature=feature)self.Conv1 = Conv(in_channels=skip1.size()[1], out_channels=48, kernel_size=1,strip=1, use_bias=False)self.Conv2 = _Deepwise_Conv(in_channels=48 + 256, out_channels=256, use_bias=False)self.ConvNUM = Conv(in_channels=256, out_channels=num_class, kernel_size=1, use_bias=False)def forward(self, input_img):skip1 = self.Conv1(self.skip1)feature = F.interpolate(self.feature, size=(skip1.size()[2], skip1.size()[3]), mode='bilinear')skip1 = torch.cat([skip1, feature], dim=1)skip1 = self.Conv2(skip1)skip1 = self.ConvNUM(skip1)skip1 = F.interpolate(skip1, size=(input_img.size()[2], input_img.size()[3]))return F.softmax(skip1,dim=1)class _bottlenet(nn.Module):def __init__(self, in_channels, out_channels, rate=1, expand_ratio=1, stride=1):super(_bottlenet, self).__init__()# 步长为2以及前后通道数不同就不进行残差堆叠self.use_res_connect = (stride == 1) and (in_channels == out_channels)self.features = nn.Sequential(nn.Conv2d(in_channels=in_channels, out_channels=in_channels * expand_ratio, kernel_size=1),nn.BatchNorm2d(num_features=in_channels * expand_ratio),nn.ReLU6(inplace=True),nn.Conv2d(in_channels=in_channels * expand_ratio, out_channels=in_channels * expand_ratio, kernel_size=3, stride=stride,padding=rate, dilation=(rate, rate)),nn.BatchNorm2d(num_features=in_channels * expand_ratio),nn.ReLU6(inplace=True),nn.Conv2d(in_channels=in_channels * expand_ratio, out_channels=out_channels, stride=1, kernel_size=1,padding=0),nn.BatchNorm2d(num_features=out_channels),nn.ReLU6(inplace=True),)# self.change = nn.Conv2d()def forward(self, x):x_clone = x.clone()x = self.features(x)#         print(x.size())if self.use_res_connect:#             print("="*10)#             print(x.size())#             print(x_clone.size())x.add_(x_clone)return xclass get_mobilenetv2_encoder(nn.Module):def __init__(self, downsamp_factor=8, num_classes=3):super(get_mobilenetv2_encoder, self).__init__()if downsamp_factor == 8:self.atrous_rates = (12, 24, 36)block4_dilation = 2block5_dilation = 4block4_stride = 1else:self.atrous_rates = (6, 12, 18)block4_dilation = 1block5_dilation = 2block4_stride = 2self.features = []self.features.append(nn.Conv2d(in_channels=3, out_channels=32, kernel_size=(3, 3), padding=1, stride=2))self.features.append(nn.BatchNorm2d(num_features=32))self.features.append(nn.ReLU6(inplace=True))# ------  3 ------# block1self.features.append(_bottlenet(in_channels=32, out_channels=16, expand_ratio=1, stride=1))# block2# [t, c, n, s] = [6, 24, 2, 2]self.features.append(_bottlenet(in_channels=16, out_channels=24, expand_ratio=6, stride=2))self.features.append(_bottlenet(in_channels=24, out_channels=24, expand_ratio=6, stride=1))# ------  6  -----# block3# [t, c, n, s] = [6, 32, 3, 2]self.features.append(_bottlenet(in_channels=24, out_channels=32, expand_ratio=6, stride=2))for i in range(2):self.features.append(_bottlenet(in_channels=32, out_channels=32, expand_ratio=6))# ------  9  ------# block4# [t, c, n, s] = [6, 64, 4, 2]self.features.append(_bottlenet(in_channels=32, out_channels=64, expand_ratio=6, stride=block4_stride))for i in range(3):self.features.append(_bottlenet(in_channels=64, out_channels=64, expand_ratio=6, rate=block4_dilation))# ------  13  ------# block5# [t, c, n, s] = [6, 96, 3, 1]self.features.append(_bottlenet(in_channels=64, out_channels=96, expand_ratio=6, rate=block4_dilation))for i in range(2):self.features.append(_bottlenet(in_channels=96, out_channels=96, expand_ratio=6, rate=block4_dilation))# [t, c, n, s] = [6, 160, 3, 2]# block6self.features.append(_bottlenet(in_channels=96, out_channels=160, expand_ratio=6, stride=1))for i in range(2):self.features.append(_bottlenet(in_channels=160, out_channels=160, expand_ratio=6))# [t, c, n, s] = [6, 160, 3, 2]self.features.append(_bottlenet(in_channels=160, out_channels=320, expand_ratio=6))self.features = nn.Sequential(*self.features)def forward(self, x):skip1 = Nonefor i, op in enumerate(self.features, 0):x = op(x)if i == 5:skip1 = x.clone()return x, self.atrous_rates, skip1class pool_block(nn.Module):def __init__(self, f, stride):super(pool_block, self).__init__()in_channels = f.size()[1]kernel_size = strideself.features = nn.Sequential(nn.AvgPool2d(kernel_size=kernel_size, stride=kernel_size, padding=kernel_size // 2),nn.Conv2d(in_channels=in_channels, out_channels=512, kernel_size=1, stride=1, bias=False),nn.BatchNorm2d(num_features=512),nn.ReLU6(inplace=True),nn.Upsample(size=(INPUT_SIZE, INPUT_SIZE), mode="bilinear"))def forward(self, x):x = self.features(x)return xclass _Deepwise_Conv(nn.Module):def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, rate=1, use_bias=False):super(_Deepwise_Conv, self).__init__()self.conv1 = Conv(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size,stride=stride, padding=padding, dilation=rate, use_bias=use_bias)self.conv2 = Conv(in_channels=in_channels, out_channels=out_channels, kernel_size=1,stride=1, padding=0, use_bias=use_bias)def forward(self, x):return self.conv2(self.conv1(x))class Conv(nn.Module):'''nn.Conv2d + Batchnormlizetion + ReLU6'''def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, use_bias=False):super(Conv, self).__init__()self.features = nn.Sequential(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,stride=stride, padding=padding, dilation=dilation, bias=use_bias),nn.BatchNorm2d(num_features=out_channels),nn.ReLU6(),)def forward(self, x):return self.features(x)

参考链接如下:
https://blog.csdn.net/weixin_44791964/article/details/103017389
https://zhuanlan.zhihu.com/p/68531147

pytorch构建deeplabv3+相关推荐

  1. 第18课:项目实战——利用 PyTorch 构建 RNN 模型

    上一篇,我们主要介绍了基本的 RNN 模型和 LSTM.本文将通过一个实战项目带大家使用 PyTorch 搭建 RNN 模型. 本项目将构建一个 RNN 模型,来对 MNIST 手写数据集进行分类.可 ...

  2. 【深度学习】保姆级教程,用PyTorch构建第一个神经网络

    PyTorch是一个基于python的科学计算包,主要针对两类人群: 作为NumPy的替代品,可以利用GPU的性能进行计算 作为一个高灵活性.速度快的深度学习平台 在PyTorch中搭建神经网络并使用 ...

  3. [Python图像识别] 四十八.Pytorch构建Faster-RCNN模型实现小麦目标检测

    该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门.OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子.图像增强技术.图像分割等,后期结合深度学习研究图像识别 ...

  4. 【神经网络】Pytorch构建自己的训练数据集

    [神经网络]Pytorch构建自己的训练数据集 ​ 最近参加了一个比赛,需要对给定的图像数据进行分类,之前使用Pytorch进行神经网络模型的构建与训练过程中,都是使用的Pytorch内置的数据集,直 ...

  5. Pytorch版deeplabv3+环境配置训练自己的数据集

    这个很不错:https://blog.csdn.net/qq_39056987/article/details/106455828     [windows10]使用pytorch版本deeplabv ...

  6. 使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)01 手写字体识别

    文章目录 1 生成对抗网络基本概念 2 生成对抗网络建模 2.1 建立MnistDataset类 2.2 建立鉴别器 2.3 测试鉴别器 2.4 Mnist生成器制作 3 模型的训练 4 模型表现的判 ...

  7. 使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)02 人脸识别 下

    文章目录 1 测试鉴别器 2 建立生成器 3 测试生成器 4 训练生成器 5 使用生成器 6 内存查看 上一节,我们已经建立好了模型所必需的鉴别器类与Dataset类. 使用PyTorch构建GAN生 ...

  8. 使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)02 人脸识别 上

    文章目录 1 数据集描述 2 GPU设置 3 设置Dataset类 4 设置辨别器类 5 辅助函数与辅助类 1 数据集描述 此项目使用的是著名的celebA(CelebFaces Attribute) ...

  9. 使用PyTorch构建的“感知器”网络

    点击关注我哦 一篇文章带你使用PyTorch构建"感知器"网络 PyTorch是一个很棒的深度学习框架,简单易学.本篇文章将带领大家从头开始构建一个"原始"的神 ...

最新文章

  1. SSE图像算法优化系列八:自然饱和度(Vibrance)算法的模拟实现及其SSE优化(附源码,可作为SSE图像入门,Vibrance算法也可用于简单的肤色调整)。...
  2. orb_slam 编译错误
  3. AI:基于计算机视觉和语音识别案例项目打包过程记录20181226-19
  4. 研发和人力资源发展模式对比研究
  5. 七周七语言:Scala Day 3
  6. 解决ListView 缓存机制带来的显示不正常问题
  7. 查看Json的结构及内容:JsonViewerPackage
  8. py4j.java gateway_python 2.7-为什么PySpark无法找到py4j.java_gateway?
  9. 服务器网络问题排查各种工具
  10. JSEclipse安装后无法打开js文件_如何在你的 PC 上 下载并配置 Node.js
  11. 李沐动手学深度学习V2-语义分割和Pascal VOC2012数据集加载代码实现
  12. c++ PDFium pdf转为图片
  13. 定时语音提醒软件实现
  14. matlab 某一函数半高,1. 半高宽的知识
  15. Jetson Orin 平台MAX9296+森云SG5-IMX490C-GMSL2 RGGB(无ISP)驱动调试
  16. Linux服务器间传文件SCP命令使用方法
  17. 数据库SQL的高级查询
  18. 首届Starcoin Move黑客松源码分析——Atlaspad
  19. 100+个数据分析常用指标和术语
  20. DSM -- 软件安装

热门文章

  1. mysql存储过程查询结果分页并返回总记录数
  2. 刚毕业大学生自学一步步成为资深建模大师,他是怎么做到的?
  3. 华为双系统手机可以刷成单系统_华为EMUI10支持“双系统”操作,10款机型可升级,有你的份吗?...
  4. Photoshop制作简洁清新的插画海报图片
  5. vnc 使用gnome桌面_使用GNOME桌面工具管理Linux
  6. CSS入门七:列表样式(主要是列表前面序号的样式,圆圈、方框,罗马序号,图片等等)
  7. 计算机辅助设计在园林设计中发挥的作用,计算机辅助设计在园林设计中运用.doc...
  8. Hopfield网络的设计与实现
  9. 光线追踪技术的理论和实践(面向对象)【转载】
  10. this关键字及static关键字