DenseNet

原文:

《Densely Connected Convolutional Networks》(CVPR 2017)

参考文章:

DenseNet算法详解

代码实现

densenet-pytorch
pytorch官方实现
densenet原文代码

网络结构

优点

  • 减轻了 vanishing-gradient(梯度消失)
  • 相当于对每一层的 feature 都使用了 deep supervision
  • 加强了 feature 的传递
  • 更有效地利用了 feature
  • 一定程度上减少了参数数量

特征重用(Feature Reuse)

  • 一些较早层提取出的特征仍可被较深层直接使用
  • 即使是Transition layer也会使用到之前Denseblock中所有层的特征
  • 第2-3个Denseblock中的层对之前Transition layer利用率很低,说明transition layer输出大量冗余特征。这也为DenseNet-BC提供了证据支持,既Compression的必要性。
  • 最后的分类层虽然使用了之前Denseblock中的多层信息,但更偏向于使用最后几个feature map。说明在网络的最后几层,某些high-level的特征可能被产生.

补充

为什么文中 conv 是指 BN-ReLU-Conv,而不是 Conv-BN-ReLU

参考 resnet v1 和 resnet v2 的区别:
详解深度学习之经典网络架构(六):ResNet 两代(ResNet v1和ResNet v2)

代码细节

参考代码: densenet-pytorch

网络结构如下图:


使用如下命令构建 depth=100,growth_rate=12,具有3个DenseBlock的 DenseNet-BC 网络

model = DenseNet3(depth=100, num_classes=10, growth_rate=12, reduction=0.5, bottleneck=True, dropRate=0)

BottleneckBlock

根据原文,设置 1x1 卷积输出的 feature maps 数量为 4k(k = growth_rate),然后将每次经过 bottleneck 层 1x1 和 3x3 卷积运算得到的 feature maps 与该模块输入的原始 feature maps 进行 concat,从而实现 Dense 连接方式。

class BottleneckBlock(nn.Module):def __init__(self, in_planes, out_planes, dropRate=0.0):super(BottleneckBlock, self).__init__()inter_planes = out_planes * 4self.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 = dropRatedef 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)

TransitionBlock

因为每个 DenseBlock 结束后输出的 channel 个数很多(in_planes+n*growth_rate),使用 1x1 卷积减少 feature maps 数量。默认 reduction=0.5,此时传给下一个 DenseBlock 的时候 feature maps 数量就会减少一半

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 = dropRatedef 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)

DenseBlock

实现单个 DenseBlock

class DenseBlock(nn.Module):def __init__(self, nb_layers, in_planes, growth_rate, block, dropRate=0.0):# DenseBlock(nb_layers=32, in_planes=24, growth_rate=12, block=BottleneckBlock, 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):# _make_layer(block=BottleneckBlock, in_planes=24, growth_rate=12, nb_layers=32, dropRate=0)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)

DenseNet

在该 DenseNet 的实现中,共有三个 DenseBlock,每个 DenseBlock 均由 32 个 BottleneckBlock 组成。

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     # 24n = (depth - 4) / 3                # 32if bottleneck == True:n = n/2block = BottleneckBlockelse:block = BasicBlockn = int(n)# 1st conv before any dense block  第一层卷积没有使用7x7, s=2的卷积核,且没有进行max poolingself.conv1 = nn.Conv2d(3, in_planes, kernel_size=3, stride=1,padding=1, bias=False)# 1st blockself.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 blockself.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 blockself.block3 = DenseBlock(n, in_planes, growth_rate, block, dropRate)in_planes = int(in_planes+n*growth_rate)# global average pooling and classifierself.bn1 = nn.BatchNorm2d(in_planes)self.relu = nn.ReLU(inplace=True)self.fc = nn.Linear(in_planes, num_classes)self.in_planes = in_planesfor m in self.modules():if isinstance(m, nn.Conv2d):n = m.kernel_size[0] * m.kernel_size[1] * m.out_channelsm.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)

【深度学习】DenseNet相关推荐

  1. 以图搜图 图像匹配_图像匹配,基于深度学习DenseNet实现以图搜图功能

    原标题:图像匹配,基于深度学习DenseNet实现以图搜图功能 度学习的发展使得在此之前以机器学习为主流算法的相关实现变得简单,而且准确率更高,效果更好,在图像检索这一块儿,目前有谷歌的以图搜图,百度 ...

  2. 图像匹配,基于深度学习DenseNet实现以图搜图功能

    向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程   公众号:datayx 度学习的发展使得在此之前以机器学习为主流算法的相关实现变得简单,而且准确率更高,效果更好,在图 ...

  3. 深度学习之卷积神经网络(13)DenseNet

    深度学习之卷积神经网络(13)DenseNet  Skip Connection的思想在ResNet上面获得了巨大的成功,研究人员开始尝试不同的Skip Connection方案,其中比较流行的就是D ...

  4. [深度学习-总结]Deep learning中8大模型介绍与比较(LeNet5,AlexNet,VGG,Inception,MobileNets,ResNet,DenseNet,Senet)

    深度学习 9中模型介绍与比较 0. CNN 结构演化 1. LeNet5 2. AlexNet 3. VGG 为什么使用2个3x3卷积核可以来代替5*5卷积核 4. 1*1卷积 5. Inceptio ...

  5. Densenet论文解读 深度学习领域论文分析博主

    深度学习领域论文分析博主 博客链接: https://my.csdn.net/u014380165 其中一篇文章: DenseNet算法详解: https://blog.csdn.net/u01438 ...

  6. 《动手学深度学习》(四) -- LeNet、AlexNet、VGG、NiN、GoogLeNet、ResNet、DenseNet 实现

    上一小节学习了卷积神经网络的卷积层和池化层的实现,趁热打铁继续学习现代卷积神经网络的搭建,欢迎小伙伴们一起学习和交流~ 为了能够应⽤softmax回归和多层感知机,我们⾸先将每个⼤小为28×2828 ...

  7. unet是残差网络吗_深度学习系列(三)卷积神经网络模型(ResNet、ResNeXt、DenseNet、DenceUnet)...

    深度学习系列(三)卷积神经网络模型(ResNet.ResNeXt.DenseNet.Dence Unet) 内容目录 1.ResNet2.ResNeXt3.DenseNet4.Dence Unet 1 ...

  8. 【深度学习】基于pytorch的FER2013人脸表情图像识别(ResNet/VGG/DenseNet)

    题目要求 1.1. 任务要求 数据集:Facial Expression Recognition Challenge,共有7类:生气.恶心.害怕.快乐.悲伤.惊讶.中性. 基本要求(50%):构建Re ...

  9. 【动手撸深度学习】细粒辨花 一文实践清华博士Densenet

    hello ,大家好,欢迎来到动手撸深度学习,一个必看系列,我会将动手撸深度学习一直完善下去,让文章更精彩,更优秀,之所以选择付费,主要是文章原创,全是干货,大家可以看我的个人信息,点赞数,收藏数,可 ...

最新文章

  1. HTML5调用本地摄像头画面,拍照,上传服务器
  2. java spring框架 注解_史上最全的java spring注解
  3. NMAP - A Stealth Port Scanner--reference
  4. 软件开发最重要的十件事
  5. Java GUI中实现文件拷贝
  6. 您好您拨打电话已停机_您好GroovyFX
  7. 图像目标分割_4 DeepLab-V1
  8. C#学习记录1——Hello World! 补充
  9. 敏捷思维- 架构设计中的方法学(1)
  10. unity2019 vuforia 使用小记
  11. 用ERStudio生成带注释的SQL,为每个column生成注释
  12. 数据库三范式简单理解
  13. 计算机主机箱内的硬件设备主要有哪些,电脑主机有哪些硬件设备
  14. DMAC计算机组成原理,计算机组成原理
  15. jbox弹窗_Jquery多功能提示通知弹出对话框插件jBox中文文档
  16. agv系统介绍_AGV小车控制系统介绍
  17. uniapp对接腾讯即时通讯TIM 发图片消息问题
  18. MATLAB与高等数学--极限计算
  19. 介绍几个预览效果不错的BIM网站链接
  20. GPS授时系统(北斗授时设备)应用及案例

热门文章

  1. 医用显示器,样机试用,接受退货
  2. 动态图二 APNG解码与播放
  3. Information Retrieval(信息检索)笔记01:Boolean Retrieval(布尔检索)
  4. 521数字代表什么意思?521表白女朋友的话如何用云便签传达爱意
  5. godaddy php mail,PHPMailer GoDaddy服务器SMTP连接被拒绝
  6. 英语学习单词篇(9)
  7. 基于多目标粒子群算法在分布式电源选址和定容中的应用matlab程序
  8. 雅思阅读真经总纲_考雅思你没用过这些书?那你雅思上7难了...
  9. 奇异雅可比矩阵 (BVP4c)
  10. com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure