deeplabv3+系列之ResNet骨干网络代码实现(包括了ResNet18,ResNet34,ResNet50,ResNet101,ResNet152)

基于paddlepaddle2.0版本的搭建。最近飞桨2.0版本出来啦!也挺好用的,所以就参考一些其他版本的代码,用paddlepaddle2.0版本重新写一下deeplabv3+ResNet网络,这篇文章为骨干网络部分。deeplabv3部分可以看下一篇文章:deeplabv3+系列之deeplabv3网络搭建
原论文地址:Deep Residual Learning for Image Recognition
paddlepaddle2.0版本安装教程

一.网络结构图:

二.可直接运行的代码:

import numpy as np
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.fluid.dygraph.base import to_variable
def SyncBatchNorm(*args, **kwargs):"""一个是cpu情况下的归一化,一个是GPU情况下的归一化"""if paddle.get_device() == 'cpu':return nn.BatchNorm2D(*args, **kwargs)else:return nn.SyncBatchNorm(*args, **kwargs)class ConvBNLayer(nn.Layer):def __init__(self,in_channels,out_channels,kernel_size,stride=1,groups=1,act=None,dilation=1,padding=None,name=None):super(ConvBNLayer, self).__init__(name)if padding is None:padding = (kernel_size-1)//2else:padding=paddingself.conv = nn.Conv2D(in_channels=in_channels,out_channels=out_channels,kernel_size=kernel_size,stride=stride,padding=padding,groups=groups,dilation=dilation,bias_attr=False)self.bn = SyncBatchNorm(out_channels)self.act = actself._act_op = nn.ReLU()def forward(self, inputs):y = self.conv(inputs)y = self.bn(y)if self.act is 'relu':y = self._act_op(y)else:y = yreturn yclass BasicBlock(nn.Layer):expansion = 1  # expand ratio for last conv output channel in each blockdef __init__(self,in_channels,out_channels,dilation=1,stride=1,padding=None,shortcut=True,name=None):super(BasicBlock, self).__init__(name)self.conv0 = ConvBNLayer(in_channels=in_channels,out_channels=out_channels,kernel_size=3,stride=stride,act='relu',name=name)self.conv1 = ConvBNLayer(in_channels=out_channels,out_channels=out_channels,kernel_size=3,act=None,name=name)if not shortcut:self.short = ConvBNLayer(in_channels=in_channels,out_channels=out_channels,kernel_size=1,stride=stride,act=None,name=name)self.shortcut = shortcutdef forward(self, inputs):conv0 = self.conv0(inputs)conv1 = self.conv1(conv0)if self.shortcut:short = inputselse:short = self.short(inputs)#该OP是逐元素相加算子,输入 x 与输入 y 逐元素相加,并将各个位置的输出元素保存到返回结果中y = paddle.add(x=short, y=conv1)y = F.relu(y)return yclass BottleneckBlock(nn.Layer):expansion = 4def __init__(self,in_channels,out_channels,stride=1,shortcut=True,dilation=1,padding=None,name=None):super(BottleneckBlock, self).__init__(name)self.conv0 = ConvBNLayer(in_channels=in_channels,out_channels=out_channels,kernel_size=1,act='relu')
#                                 name=name)self.conv1 = ConvBNLayer(in_channels=out_channels,out_channels=out_channels,kernel_size=3,stride=stride,padding=padding,act='relu',dilation=dilation)#                                name=name)self.conv2 = ConvBNLayer(in_channels=out_channels,out_channels=out_channels * 4,kernel_size=1,stride=1)#                                name=name)if not shortcut:self.short = ConvBNLayer(in_channels=in_channels,out_channels=out_channels * 4,kernel_size=1,stride=stride)
#                                     name=name)self.shortcut = shortcutself.num_channel_out = out_channels * 4def forward(self, inputs):conv0 = self.conv0(inputs)#print('conv0 shape=',conv0.shape)conv1 = self.conv1(conv0)#print('conv1 shape=', conv1.shape)conv2 = self.conv2(conv1)#print('conv2 shape=', conv2.shape)if self.shortcut:short = inputselse:short = self.short(inputs)#print('short shape=', short.shape)#该OP是逐元素相加算子,输入 x 与输入 y 逐元素相加,并将各个位置的输出元素保存到返回结果中y = paddle.add(x=short, y=conv2)y = F.relu(y)return yclass ResNet(nn.Layer):def __init__(self, layers=50, num_classes=1000, multi_grid=[1, 2, 4], duplicate_blocks=False):super(ResNet, self).__init__()self.layers = layerssupported_layers = [18, 34, 50, 101, 152]assert layers in supported_layersmgr = [1, 2, 4] # multi grid rate for duplicated blocksif layers == 18:depth = [2, 2, 2, 2]elif layers == 34:depth = [3, 4, 6, 3]elif layers == 50:depth = [3, 4, 6, 3]elif layers == 101:depth = [3, 4, 23, 3]elif layers == 152:depth = [3, 8, 36, 3]if layers < 50:in_channels = [64, 64, 128, 256, 512]else:in_channels = [64, 256, 512, 1024, 2048]self.out_channels = [64, 128, 256, 512]self.conv = ConvBNLayer(in_channels=3,out_channels=64,kernel_size=7,stride=2,act='relu')self.pool2d_max = nn.MaxPool2D(kernel_size=3,stride=2,padding=1)if layers < 50:block = BasicBlockl1_shortcut=Trueelse:block = BottleneckBlockl1_shortcut=Falseself.layer1 = nn.Sequential(*self.make_layer(block,in_channels[0],self.out_channels[0],depth[0],stride=1,shortcut=l1_shortcut,name='layer1'))self.layer2 = nn.Sequential(*self.make_layer(block,in_channels[1],self.out_channels[1],depth[1],stride=2,name='layer2'))self.layer3 = nn.Sequential(*self.make_layer(block,in_channels[2],self.out_channels[2],depth[2],stride=1,dilation=2,name='layer3'))# add multi grid [1, 2, 4]      self.layer4 = nn.Sequential(*self.make_layer(block,in_channels[3],self.out_channels[3],depth[3],stride=1,name='layer4',dilation=multi_grid))if duplicate_blocks:self.layer5 = nn.Sequential(*self.make_layer(block,in_channels[4],self.out_channels[3],depth[3],stride=1,name='layer5',dilation=[x*mgr[0] for x in multi_grid]))self.layer6 = nn.Sequential(*self.make_layer(block,in_channels[4],self.out_channels[3],depth[3],stride=1,name='layer6',dilation=[x*mgr[1] for x in multi_grid]))self.layer7 = nn.Sequential(*self.make_layer(block,in_channels[4],self.out_channels[3],depth[3],stride=1,name='layer7',dilation=[x*mgr[2] for x in multi_grid]))self.last_pool = nn.AdaptiveAvgPool2D(output_size=(1,1))#平均自适应池化self.fc = nn.Linear(in_features=self.out_channels[-1] * block.expansion,out_features =num_classes)self.out_dim = self.out_channels[-1] * block.expansiondef forward(self, inputs):x = self.conv(inputs)x = self.pool2d_max(x)#print(x.shape)x = self.layer1(x)#print(x.shape)x = self.layer2(x)#print(x.shape)x = self.layer3(x)#print(x.shape)x = self.layer4(x)#print(x.shape)x = self.last_pool(x)x = paddle.reshape(x, shape=[-1, self.out_dim])x = self.fc(x)return xdef make_layer(self, block, in_channels, out_channels, depth, stride, dilation=1, shortcut=False, name=None):layers = []if isinstance(dilation, int):dilation = [dilation] * depthelif isinstance(dilation, (list, tuple)):assert len(dilation) == 3, "Wrong dilation rate for multi-grid | len should be 3"assert depth ==3, "multi-grid can only applied to blocks with depth 3"padding = []for di in dilation:if di>1:padding.append(di)else:padding.append(None)layers.append(block(in_channels,out_channels,stride=stride,shortcut=shortcut,dilation=dilation[0],padding=padding[0],name=f'{name}.0'))for i in range(1, depth):layers.append(block(out_channels * block.expansion,out_channels,stride=1,dilation=dilation[i],padding=padding[i],name=f'{name}.{i}'))return layersdef ResNet18(**args):model = ResNet(layers=18)return model
def ResNet34(**args):model = ResNet(layers=34)return model
def ResNet50(**args):model = ResNet(layers=50)return model
def ResNet101(**args):model = ResNet(layers=101)return model
def ResNet152(**args):model = ResNet(layers=152)return modeldef main():x_data = np.random.rand(2, 3, 224, 224).astype(np.float32)x = to_variable(x_data)#ResNet101,其他同理model = ResNet101()model.eval()pred = model(x)print('dilated resnet50: pred.shape = ', pred.shape)main()

deeplabv3+系列之ResNet骨干网络相关推荐

  1. (pytorch-深度学习系列)ResNet残差网络的理解-学习笔记

    ResNet残差网络的理解 ResNet伴随文章 Deep Residual Learning for Image Recognition 诞生,该文章是MSRA何凯明团队在2015年ImageNet ...

  2. android 使用以太网共享4g网络_案例 | 东土科技Aquam系列重新定义列车骨干网络!...

    列车重联重组,是实现弹性运载量的关键技术,其中最重要的是解决重组列车之间的通讯问题!伴随工业互联网技术发展,新的以太网列车骨干网,可替代绞式列车总线,并运用三层交换机作为骨干网节点,来承担列车间数据转 ...

  3. 【mmdetection源码解读(一)】骨干网络--ResNet

    以下仅为个人理解,若有不正之处还请指出,欢迎交流! 本文解读的源码为mmdet/models/backbones中的resnet.py 首先附上ResNet原文地址Deep Residual Lear ...

  4. 速度提升2倍,超强悍CPU级骨干网络PP-LCNet

    yolov5-pp-lcnet: GitHub - OutBreak-hui/Yolov5-PP-LCNet 分类网络的,结果还出来: https://github.com/ngnquan/PP-LC ...

  5. 速度提升2倍,超强悍CPU级骨干网络PP-LCNet出世!

    关注公众号,发现CV技术之美 算法速度优化遇到瓶颈,达不到要求?应用环境没有高性能硬件只有CPU?是不是直接戳中了各位开发者的痛点!莫慌,今天小编就来为万千开发者破局~ 这个破局点就是:针对CPU设备 ...

  6. TPAMI 2022|金字塔池化的骨干网络!南开达摩院联合推出P2T

    作者 | 吴宇寰  编辑 | 极市平台 原文链接:https://mp.weixin.qq.com/s/iHnvQdvwWUwyOArlHU7Rxw 点击下方卡片,关注"自动驾驶之心&quo ...

  7. TPAMI 2022 | 金字塔池化的骨干网络,各大任务都涨点!南开达摩院联合推出P2T

    原文链接:https://www.techbeat.net/article-info?id=4057 作者:吴宇寰 现有的视觉Transformer中的两大问题: 传统的Multi-Head Self ...

  8. 计算机视觉中的经典骨干网络总结

    特征提取是计算机视觉任务的基础,良好的特征提取网络可以明显的提升算法的性能表现.在计算机视觉任务中,对图像进行特征提取的网络被称作为骨干网络(Backbone),可以说是下游任务的主心骨了.下面总结近 ...

  9. 计图(Jittor) 1.1版本:新增骨干网络、JIT功能升级、支持多卡训练

    计图(Jittor) 1.1版本:新增骨干网络.JIT功能升级.支持多卡训练 深度学习框架-计图(Jittor),Jittor的新版本V1.1上线了.主要变化包括: • 增加了大量骨干网络的支持,增强 ...

最新文章

  1. 第二阶段个人博客总结8
  2. python中with的用法,上下文管理器
  3. 鳞翅目动物的诅咒:玩java.time
  4. 90 Subsets II
  5. 状态机设计的一般步骤_浅谈状态机
  6. 秋式开源团队,欢迎您的加入!
  7. 《大道至简》阅读笔记
  8. 打造属于自己的 linux版(硬盘版或电子盘)view5 终端
  9. cisco ios 权限等级详解
  10. Java常量池储存什么_JAVA常量池中存储的常量是什么
  11. Webpack打包UMD的export要带default访问问题
  12. 彻底关闭windows安全中心
  13. 360导航底部的效果html,jQuery仿360导航页图标拖动排序效果代码分享
  14. LeetCode,无它,唯手熟尔(三)
  15. 酸奶糖酸比的计算机控制,PAL-BX丨ACID F5 五种水果糖酸度计
  16. 在不同领域,大家用爬虫怎么盈利的-Java网络爬虫系统性学习与实战系列(4)
  17. 人机协作,小i机器人搭档杨澜主持上海科技节闭幕式
  18. 2016年第三季度总结
  19. 多张图片合并成PDF文件,还在下载合并软件,PS就能帮你搞定
  20. 『默哀』你的梦或许因为这个新闻而碎了【用你的程序语言 抛出一行异常】

热门文章

  1. PTA(Basic Level) 1069:微博转发抽奖 (C语言实现)
  2. 干货|安服工程师技能手册详细总结
  3. C++标准模板库之map及pair
  4. BZOJ_P2461 [BeiJing2011]符环(动态规划/记忆化搜索)
  5. Idea 设置类、方法注释模板(解决params和return显示问题)---不来虚的 实测有效
  6. 虚拟内存越大越好吗_手机如何选择屏幕分辩率?720p、1080P,数值越大越好吗?...
  7. 弘辽科技:拼多多怎么去看成交关键词?拼多多怎么查看成交词?
  8. java jolt tuxedo_Java使用Jolt连接Tuxedo服务器
  9. 【原创】Xshell无限标签和永久去更新教程
  10. tf.strings.split