2015_ResNet_何凯明:

图:

网络描述:

ResNet的主要思想是在网络中增加了直连通道,即Highway Network的思想。此前的网络结构是性能输入做一个非线性变换,而Highway Network则允许保留之前网络层的一定比例的输出。

第二幅图中这两种结构分别针对ResNet34(左图)和ResNet50/101/152(右图),一般称整个结构为一个”building block“。其中右图又称为”bottleneck design”,目的一目了然,就是为了降低参数的数目,第一个1x1的卷积把256维channel降到64维,然后在最后通过1x1卷积恢复,整体上用的参数数目:1x1x256x64 + 3x3x64x64 + 1x1x64x256 = 69632,而不使用bottleneck的话就是两个3x3x256的卷积,参数数目: 3x3x256x256x2 = 1179648,差了16.94倍。对于常规ResNet,可以用于34层或者更少的网络中,对于Bottleneck Design的ResNet通常用于更深的如101这样的网络中,目的是减少计算和参数量(实用目的)

特点,优点:

(1)提出residual结构(残差结构),并搭建超深的网络结构(突破1000层)。

(2)使用batch normalization 加速训练(丢弃dropout)。

(3)学习结果对网络权重的波动变化更加敏感。

(4)相比传统的VGG网络,复杂度降低,所需的参数量下降。

(5)网络深度更深,不会出现梯度消失现象。

(6)解决了深层次的网络退化问题,在增加网络层数的过程中,training accuracy 逐渐趋于饱和,继续增加层数,training accuracy 就会出现下降的现象,而这种下降不是由过拟合造成的。

代码:

Pytorch1实现:
#nn.Sequential实现
class ResidualBlock(nn.Module):#实现子module:Residual Blockdef __init__(self, in_ch, out_ch, stride=1, shortcut=None):super(ResidualBlock,self).__init__()self.left = nn.Sequential(nn.Conv2d(in_ch,out_ch,3,stride,padding=1,bias=False),nn.BatchNorm2d(out_ch),nn.ReLU(inplace = True),#inplace = True原地操作nn.Conv2d(out_ch,out_ch,3,stride=1,padding=1,bias=False),nn.BatchNorm2d(out_ch))self.right = shortcutdef forward(self,x):out = self.left(x)residual = x if self.right is None else self.right(x)out += residualreturn F.relu(out)class ResNet34(nn.Module):#224x224x3#实现主module:ResNet34def __init__(self, num_classes=1):super(ResNet34,self).__init__()self.pre = nn.Sequential(nn.Conv2d(3,64,7,stride=2,padding=3,bias=False),# (224+2*p-)/2(向下取整)+1,size减半->112nn.BatchNorm2d(64),#112x112x64nn.ReLU(inplace = True),nn.MaxPool2d(3,2,1)#kernel_size=3, stride=2, padding=1)#56x56x64#重复的layer,分别有3,4,6,3个residual blockself.layer1 = self.make_layer(64,64,3)#56x56x64,layer1层输入输出一样,make_layer里,应该不用对shortcut进行处理,但是为了统一操作。。。self.layer2 = self.make_layer(64,128,4,stride=2)#第一个stride=2,剩下3个stride=1;28x28x128self.layer3 = self.make_layer(128,256,6,stride=2)#14x14x256self.layer4 = self.make_layer(256,512,3,stride=2)#7x7x512#分类用的全连接self.fc = nn.Linear(512,num_classes)def make_layer(self,in_ch,out_ch,block_num,stride=1):#当维度增加时,对shortcut进行option B的处理shortcut = nn.Sequential(#首个ResidualBlock需要进行option B处理nn.Conv2d(in_ch,out_ch,1,stride,bias=False),#1x1卷积用于增加维度;stride=2用于减半size;为简化不考虑偏差nn.BatchNorm2d(out_ch))layers = []layers.append(ResidualBlock(in_ch,out_ch,stride,shortcut))for i in range(1,block_num):layers.append(ResidualBlock(out_ch,out_ch))#后面的几个ResidualBlock,shortcut直接相加return nn.Sequential(*layers)def forward(self,x):    #224x224x3x = self.pre(x)     #56x56x64x = self.layer1(x)  #56x56x64x = self.layer2(x)  #28x28x128x = self.layer3(x)  #14x14x256x = self.layer4(x)  #7x7x512x = F.avg_pool2d(x,7)#1x1x512x = x.view(x.size(0),-1)#将输出拉伸为一行:1x512x = self.fc(x)    #1x1# nn.BCELoss:二分类用的交叉熵,用的时候需要在该层前面加上 Sigmoid 函数return nn.Sigmoid()(x)#1x1,将结果化为(0~1)之间
Pytorch实现:
#直接堆叠
class BasicBlock(nn.Module):expansion = 1def __init__(self, in_channel, out_channel, stride=1, downsample=None):super(BasicBlock, self).__init__()self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,kernel_size=3, stride=stride, padding=1, bias=False)self.bn1 = nn.BatchNorm2d(out_channel)self.relu = nn.ReLU()self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,kernel_size=3, stride=1, padding=1, bias=False)self.bn2 = nn.BatchNorm2d(out_channel)self.downsample = downsampledef forward(self, x):identity = xif self.downsample is not None:identity = self.downsample(x)out = self.conv1(x)out = self.bn1(out)out = self.relu(out)out = self.conv2(out)out = self.bn2(out)out += identityout = self.relu(out)return outclass Bottleneck(nn.Module):expansion = 4def __init__(self, in_channel, out_channel, stride=1, downsample=None):super(Bottleneck, self).__init__()self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,kernel_size=1, stride=1, bias=False)  # squeeze channelsself.bn1 = nn.BatchNorm2d(out_channel)# -----------------------------------------self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,kernel_size=3, stride=stride, bias=False, padding=1)self.bn2 = nn.BatchNorm2d(out_channel)# -----------------------------------------self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel*self.expansion,kernel_size=1, stride=1, bias=False)  # unsqueeze channelsself.bn3 = nn.BatchNorm2d(out_channel*self.expansion)self.relu = nn.ReLU(inplace=True)self.downsample = downsampledef forward(self, x):identity = xif self.downsample is not None:identity = self.downsample(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)out += identityout = self.relu(out)return outclass ResNet(nn.Module):def __init__(self, block, blocks_num, num_classes=1000, include_top=True):super(ResNet, self).__init__()self.include_top = include_topself.in_channel = 64self.conv1 = nn.Conv2d(3, self.in_channel, kernel_size=7, stride=2,padding=3, bias=False)self.bn1 = nn.BatchNorm2d(self.in_channel)self.relu = nn.ReLU(inplace=True)self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.layer1 = self._make_layer(block, 64, blocks_num[0])self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)if self.include_top:self.avgpool = nn.AdaptiveAvgPool2d((1, 1))  # output size = (1, 1)self.fc = nn.Linear(512 * block.expansion, num_classes)for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')def _make_layer(self, block, channel, block_num, stride=1):downsample = Noneif stride != 1 or self.in_channel != channel * block.expansion:downsample = nn.Sequential(nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride, bias=False),nn.BatchNorm2d(channel * block.expansion))layers = []layers.append(block(self.in_channel, channel, downsample=downsample, stride=stride))self.in_channel = channel * block.expansionfor _ in range(1, block_num):layers.append(block(self.in_channel, channel))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)if self.include_top:x = self.avgpool(x)x = torch.flatten(x, 1)x = self.fc(x)return xdef resnet34(num_classes=1000, include_top=True):return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)def resnet101(num_classes=1000, include_top=True):return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)

ResNet网络总结相关推荐

  1. ResNet网络的训练和预测

    ResNet网络的训练和预测 简介 Introduction 图像分类与CNN 图像分类 是指将图像信息中所反映的不同特征,把不同类别的目标区分开来的图像处理方法,是计算机视觉中其他任务,比如目标检测 ...

  2. CV之IG:基于CNN网络架构+ResNet网络进行DIY图像生成网络

    CV之IG:基于CNN网络架构+ResNet网络进行DIY图像生成网络 目录 设计思路 实现代码 设计思路 实现代码 # 定义图像生成网络:image, training,两个参数# Less bor ...

  3. 使用resNet网络 进行图像分类(jupyter notebook)

    这学期做了三次的CV把他贴出来, resNet网络的结构 import torch.nn as nn import torchclass BasicBlock(nn.Module):expansion ...

  4. ResNet网络详解与keras实现

    ResNet网络详解与keras实现 ResNet网络详解与keras实现 Resnet网络的概览 Pascal_VOC数据集 第一层目录 第二层目录 第三层目录 梯度退化 Residual Lear ...

  5. ResNet网络简单理解与代码

    ResNet网络提出的文章是<Deep Residual Learning for Image Recognition> 下载地址:https://arxiv.org/pdf/1512.0 ...

  6. ResNet网络详解

    ResNet ResNet在2015年由微软实验室提出,斩获当年lmageNet竞赛中分类任务第一名,目标检测第一名.获得coco数据集中目标检测第一名,图像分割第一名. ResNet亮点 1.超深的 ...

  7. ResNet网络的改进版:ResNeXt

    之前的文章讲过ResNet网络的基本架构,其本质就是让网络的学习目的从学习 转为学习 ,也就是学习输入和输出之间的残差信息,从而缓解了梯度消失和网络退化问题. 本文讲下ResNet网络的改进版:Res ...

  8. 1. Resnet网络详解

    一.ResNet网络介绍 ResNet是2015年有微软实验室提出的,题目: 作者是何凯明等,这四个都是华人. 当年ResNet斩获了当年各种第一名.基本上只要它参加的比赛,全都是第一名. 我们来看一 ...

  9. 学习笔记1 - ResNet网络学习

    一些概念知识学习: 端到端(end-to-end):指的是输入是原始数据,输出是最后结果. 非端到端的输入端不是直接的原始数据,而是在原始数据中提取的特征.在以前是由手工提取图像的一些关键特征,称为降 ...

  10. pytorch实现简单的Resnet网络

    笔者也是最近刚学不久的深度学习,也有很多地方不懂,下面给大家使用pytorch实现一个简单的Resnet网络(残差网络),并且训练MNIST数据集.话不多说,直接上代码.   笔者认为最主要的地方就是 ...

最新文章

  1. android电视工程模式,智能电视如何打开ADB?进入工程模式方法
  2. 形象的列举-C# 枚举
  3. 安装“消息队列 (MSMQ)”
  4. U811.1接口EAI系列之二-BOM构成-委外BOM构成--VB语言
  5. 为什么c语言一用windows.h就报错_C代码里面加一行网址依然可以运行,并不会报错,为何...
  6. CPU软编码视频,比GPU更好?
  7. 使用FileZilla搭建简单的FTP
  8. linux 下tftp服务器搭建,CentOS 6下搭建TFTP服务器
  9. 微信小程序后端系统CMS开发笔记--04
  10. 【word】公式排版问题
  11. 华为云交付项目服务器配置表,云服务器交付确认表
  12. 蓝牙耳机音量控制问题
  13. Java奇数与偶数的判断
  14. mysqldatareader什么意思_Mysql的MySqlDataReader对于MysqlConnection是独占式
  15. 微信小程序之登录注册界面的实现
  16. 快手在线查询权重网站源码+接口
  17. 终于等到了,十位Java架构师整理的“阿里P7”养成计划
  18. taro 小程序编译在标签上px转rpx的api
  19. 国密算法(SM2,SM3,SM4)辅助工具升级版(OTP+PBOC3.0)
  20. 基于RNN实现垃圾邮件辨别

热门文章

  1. JeecgBoot Minio版本6.0.13升级到8.0.3修改方法
  2. 微信小程序,技术创业的时代可能要来了,但窗口期不会太长
  3. oracle DB死锁
  4. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
  5. Linux用户配置文件(第二版)
  6. PYMODM使用记录
  7. rm、shutdown、磁盘挂载、vi使用方法
  8. FTP使用外部数据源
  9. pecl安装扩展(首选)
  10. 创建定制的ASP.NET AJAX非可视化客户端组件