论文: Aggregated Residual Transformations for Deep Neural Networks

论文地址: https://arxiv.org/abs/1611.05431

代码地址

  1. Keras: https://github.com/titu1994/Keras-ResNeXt

  2. Pytorch: https://github.com/prlz77/ResNeXt.pytorch/tree/R4.0[推荐,用于cifar数据集]

参考博客:

  1. ResNeXt - Aggregated Residual Transformations for DNN
  1. ResNeXt算法详解
  1. ResNext与Xception——对模型的新思考

1. 提出背景

作者提出ResNeXt的主要原因在于:

传统的要提高模型的准确率,都是通过 加深加宽 网络,但是随着超参数数量的增加(比如 channels数,filter size等等),网络设计的难度和计算开销也会增加。

因此本文提出的 ResNeXt结构可以在不增加参数复杂度的前提下

  • 提高准确率

  • 减少超参数数量(得益于子模块的拓扑结构)

2. 核心思想

  1. 作者在论文中首先提到VGG,VGG采用 堆叠网络 来实现,之前的 ResNet 也借用了这样的思想。

  2. 之后提到了Inception系列网络,简单说就是 split-transform-merge 的策略,但是存在一个问题:

    网络的超参数设定的针对性比较强,当应用在别的数据集上需要修改许多参数,因此可扩展性一般.

作者同时采用 VGG 的 堆叠思想 和 Inception 的 split-transform-merge 的思想,但是 可扩展性比较强. 可以认为在增加准确率的同时基本不改变或降低模型的复杂度。

这里提到一个名词cardinality,原文的解释是 the size of the set of transformations,如下图 Fig1 右边是 cardinality=32 的样子:

参数计算

假设在不使用偏置的情况下:

# A block of ResNet
256x1x64 + 64x3x3x64 + 64x1x256 = 69632
# A block of ResNeXt with cardinality
(256x1x4 + 4x4x3x3 + 4x256) x 32 = 70144

两者参数数量差不多,但是后面作者有更加精妙的实现。

注意:

  • 每个被聚合的拓扑结构都是一样的(这也是和 Inception 的差别,减轻设计负担)

附上原文比较核心的一句话,点明了增加 cardinality 比增加深度和宽度更有效,这句话的实验结果在后面有展示:

In particular, a 101-layer ResNeXt is able to achieve better accuracy than ResNet-200 but has only 50% complexity.

Table1 列举了 ResNet-50 和 ResNeXt-50 的内部结构,另外最后两行说明二者之间的参数复杂度差别不大。

3. 论文核心

作者要开始讲本文提出的新的 block,举全连接层(Inner product)的例子来讲,我们知道全连接层的就是以下这个公式:

再配上这个图就更容易理解其splittingtransformingaggregating的过程。

作者将其中的\(w_ix_i\)替换乘了更一般的函数,这里用了一个很形象的词:Network in Neuron,式子如下:

  • 其中C就是 cardinality

  • \(T_I\)有相同的拓扑结构(本文中就是三个卷积层的堆叠)

然后再看fig 3,这里作者展示了3种不同不同的 ResNeXt blocks:

  • fig3.a

    就是前面所说的aggregated residual transformations

  • fig3.b

    则采用两层卷积后 concatenate,再卷积,有点类似 Inception-ResNet,只不过这里的 paths 都是相同的拓扑结构

  • fig 3.c

    采用了一种更加精妙的实现,Group convolution分组卷积

作者在文中明确说明这三种结构是严格等价的,并且用这三个结构做出来的结果一模一样,在本文中展示的是fig3.c的结果,因为fig3.c的结构比较简洁而且速度更快.

4. 分组卷积

Group convolution 分组卷积,最早在AlexNet中出现,由于当时的硬件资源有限,训练AlexNet时卷积操作不能全部放在同一个GPU处理,因此作者把feature maps分给多个GPU分别进行处理,最后把多个GPU的结果进行融合。

有趣的是,分组卷积在当时可以说是一种工程上的妥协,因为今天能够简单训练的AlexNet在当时很难训练, 显存不够,Hinton跟他的学生不得不把网络拆分到两张GTX590上面训练了一个礼拜,当然,两张GPU之间如何通信是相当复杂的,幸运的是今天tensorflow这些库帮我们做好了多GPU训练的通信问题。就这样Hinton和他的学生发明了分组卷积. 另他们没想到的是:

分组卷积的思想影响比较深远,当前一些轻量级的SOTA(State Of The Art)网络,都用到了分组卷积的操作,以节省计算量。

疑问

  1. 如果分组卷积是分在不同GPU上的话,每个GPU的计算量就降低到 1/groups,但如果依然在同一个GPU上计算,最终整体的计算量是否不变?

实际上并不是这样的,Group convolution本身就大大减少了参数,比如当input_channel=256, output_channel=256,kernel size=3x3:

  • 不做分组卷积的时候,分组卷积的参数为256x256x3x3

  • 当分组卷积的时候,比如说group=2,每个group的input_channel、output_channel=128,参数数量为2x128x128x3x3,为原来的1/2.

最后输出的feature maps通过concatenate的方式组合,而不是elementwise add. 如果放到两张GPU上运算,那么速度就提升了4倍.

5. 核心代码

import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init class ResNeXtBottleneck(nn.Module):
r"""RexNeXt bottleneck type C
https://github.com/facebookresearch/ResNeXt/blob/master/models/resnext.lua
"""
def __init__(self, in_channels, out_channels, stride, cardinality, base_width, widen_factor):
""" Args:
in_channels (int): input channel dimensionality
out_channels (int): output channel dimensionality
stride: Replaces pooling layer.
cardinality: num of convolution groups.
base_width: base number of channels in each group.
widen_factor: factor to reduce the input dimensionality before convolution. """
super().__init__()
self.widel_ratio = out_channels / (widen_factor * 64.)
self.D = cardinality * int(base_width * self.widel_ratio) # 缩减的卷积层
self.conv_reduce = nn.Conv2d(in_channels=in_channels,
out_channels=self.D,
kernel_size=1,
stride=1,
padding=0,
bias=False)
self.bn_reduce = nn.BatchNorm2d(self.D) # 组卷积
self.conv_conv = nn.Conv2d(self.D, self.D, 3, stride, 1, groups=cardinality, bias=False)
self.bn = nn.BatchNorm2d(self.D) # 增加的卷积层
self.conv_expand = nn.Conv2d(self.D, out_channels, 1, 1, 0, bias=False)
self.bn_expand = nn.BatchNorm2d(out_channels) # 短接的层
self.shortcut = nn.Sequential()
# 如果是两个模块拼接,则
if in_channels != out_channels:
self.shortcut.add_module(name='shortcut_conv',
module=nn.Conv2d(in_channels,
out_channels,
kernel_size=1,
stride=stride,
padding=0,
bias=False))
self.shortcut.add_module(name='shortcut_bn',
module=nn.BatchNorm2d(out_channels)) def forward(self, x):
bottleneck = self.conv_reduce.forward(x)
bottleneck = F.relu(self.bn_reduce.forward(bottleneck), inplace=True) bottleneck = self.conv_conv.forward(bottleneck)
bottleneck = F.relu(self.bn.forward(bottleneck), inplace=True) bottleneck = self.conv_expand.forward(bottleneck)
bottleneck = self.bn_expand.forward(bottleneck) # 如果输入通道数量和输出通道数量相等,则为直接短接
# 如果不相等,短接之前还要做一个卷积操作,将通道数量扩展
residual = self.shortcut.forward(x)
return F.relu(input=(residual + bottleneck), inplace=True) class CifarResNeXt(nn.Module):
def __init__(self, cardinality, depth, nlabels, base_width, widen_factor=4):
"""Constructor Args:
cardinality: number of convolution groups.
depth: number of layers.
nlabels: number of classes
base_width: base number of channels in each group.
widen_factor: factor to adjust the channel dimensionality """
super().__init__()
self.cardinality = cardinality
self.depth = depth
self.block_depth = (self.depth - 2) // 9
self.base_width = base_width
self.widen_factor = widen_factor
self.nlabels = nlabels
self.output_size = 64
self.stages = [64, 64*self.widen_factor, 128*self.widen_factor, 256*self.widen_factor] self.conv_1_3x3 = nn.Conv2d(in_channels=3,
out_channels=64,
kernel_size=3,
stride=1,
padding=1,
bias=False)
self.bn_1 = nn.BatchNorm2d(64) self.stage_1 = self.block('stage_1',
in_channels=self.stages[0],
out_channels=self.stages[1],
pool_stride=1)
self.stage_2 = self.block('stage_2', self.stages[1], self.stages[2], 2)
self.stage_3 = self.block('stage_3', self.stages[2], self[3], 2) self.classifier = nn.Linear(in_features=self.stages[3], out_features=nlabels) self.initialize_weights() # 初始化权重 def initialize_weights(self):
init.kaiming_normal(self.classifier.weight) # 用kaiming初始化classifier
for key in self.state_dict():
if key.split('.')[-1] == 'weight':
if 'conv' in key:
init.kaiming_normal(self.state_dict()[key], mode='fan_out')
if 'bn' in key:
self.state_dict()[key][...] = 1
elif key.split('.')[-1] == 'bias':
self.state_dict()[key][...] = 0 def block(self, name, in_channels, out_channels, pool_stride=2):
"""Stack n bottleneck modules where n is inferred from the depth of the network. Args:
name: string name of the current block.
in_channels: number of input channels
out_channels: number of output channels
pool_stride: factor to reduce the spatial dimensionality in the first bottleneck of the block. Returns:
a Module consisting of n sequential bottlenecks. """
block = nn.Sequential()
for bottleneck in range(self.block_depth):
name_ = '%s_bottleneck_%d' % (name, bottleneck)
if bottleneck == 0:
block.add_module(name_, module=ResNeXtBottleneck(in_channels,
out_channels,
stride=pool_stride,
cardinality=self.cardinality,
base_width=self.base_width,
widen_factor=self.widen_factor))
else:
block.add_module(name_, module=ResNeXtBottleneck(out_channels,
out_channels,
1,
self.cardinality,
self.base_width,
self.widen_factor))
return block def forward(self, x):
x = self.conv_1_3x3.forward(x)
x = F.relu(self.bn_1.forward(x), inplace=True)
x = self.stage_1.forward(x)
x = self.stage_2.forward(x)
x = self.stage_3.forward(x)
x = F.avg_pool2d(input=x, kernel_size=8, stride=1)
x = x.view(-1, self.stages[3])
return self.classifier(x)

ResNeXt论文阅读笔记.md的更多相关文章

  1. 论文阅读笔记 - YARN : Architecture of Next Generation Apache Hadoop MapReduceFramework

    作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...

  2. 论文阅读笔记 - Mesos: A Platform for Fine-Grained ResourceSharing in the Data Center

    作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...

  3. 论文阅读笔记 Word Embeddings A Survey

    论文阅读笔记 Word Embeddings A Survey 收获 Word Embedding 的定义 dense, distributed, fixed-length word vectors, ...

  4. 论文阅读笔记 Improved Word Representation Learning with Sememes

    论文阅读笔记 Improved Word Representation Learning with Sememes 一句话概括本文工作 使用词汇资源--知网--来提升词嵌入的表征能力,并提出了三种基于 ...

  5. [置顶] 人工智能(深度学习)加速芯片论文阅读笔记 (已添加ISSCC17,FPGA17...ISCA17...)

    这是一个导读,可以快速找到我记录的关于人工智能(深度学习)加速芯片论文阅读笔记. ISSCC 2017 Session14 Deep Learning Processors: ISSCC 2017关于 ...

  6. Nature/Science 论文阅读笔记

    Nature/Science 论文阅读笔记 Unsupervised word embeddings capture latent knowledge from materials science l ...

  7. 论文阅读笔记(二十一)【CVPR2017】:Deep Spatial-Temporal Fusion Network for Video-Based Person Re-Identification

    Introduction (1)Motivation: 当前CNN无法提取图像序列的关系特征:RNN较为忽视视频序列前期的帧信息,也缺乏对于步态等具体信息的提取:Siamese损失和Triplet损失 ...

  8. 论文阅读笔记(十八)【ITIP2019】:Dynamic Graph Co-Matching for Unsupervised Video-Based Person Re-Identification

    论文阅读笔记(十七)ICCV2017的扩刊(会议论文[传送门]) 改进部分: (1)惩罚函数:原本由两部分组成的惩罚函数,改为只包含 Sequence Cost 函数: (2)对重新权重改进: ① P ...

  9. [论文阅读笔记] GEMSEC,Graph Embedding with Self Clustering

    [论文阅读笔记] GEMSEC: Graph Embedding with Self Clustering 本文结构 解决问题 主要贡献 算法原理 参考文献 (1) 解决问题 已经有一些工作在使用学习 ...

随机推荐

  1. Bestcoder Round8

    4989Summary 既然用C++了就偷懒直接用STL大法了 #include<iostream> #include<algorithm> #include<vecto ...

  2. Python三大器之生成器

    Python三大器之生成器 生成器初识 什么是生成器 生成器本身属于迭代器.继承了迭代器的特性,惰性求值,占用内存空间极小. 为什么要有生成器 我们想使用迭代器本身惰性求值的特点创建出一个可以容纳百万 ...

  3. ubuntu镜像源切换

    换源准备: 换源之前明确使用平台,X86架构.ARM架构的源,ubuntu18.04和unbuntu16.04等源均不同,切忌病急乱投医,看到一个源复制过来添加后没用. 分析一条源: deb http ...

  4. Python3-shutil模块-高级文件操作

    Python3中的shutil模块提供了对文件和容器文件的一些高级操作 shutil.copy(src, dst) 拷贝文件,src和dst为路径的字符串表示,copy()会复制文件数据和文件权限,但 ...

  5. The main method caused an error: java.util.concurrent.ExecutionException: org.apache.flink.runtime.client.JobSubmissionException: Failed to submit JobGraph.

    在使用flink run命令提交任务可能会遇到如下错误: The program finished with the following exception: org.apache.flink.cli ...

  6. JS判断 函数是否定义/变量是否定义

    函数是否定义: <script type="text/javascript"> try { if(typeof FunName === "function&q ...

  7. linux crontab 定时任务 邮件问题 及其相关的 dead.letter 问题

    最近开发项目的时候发现公司服务器用root账号登录的时候 一直会提示有新的信件,一直提示一直提示. 联想到另一台服务器上 的dead.letter文件每天都不断的变大  而且在 root家目录里 ,系 ...

  8. php抽奖功能

    在项目开发中经常会遇到花钱抽奖类型的需求.但是老板总是担心用户用小钱抽到大奖.这样会导致项目亏损.下边这段代码可以有效制止抽奖项目亏钱. 个人奖池: 语言:thinkphp redis mysql 表 ...

  9. 禁用rm命令

    (1)[root@tf ~]# alias rm='echo do not use rm command'[root@tf ~]# vim /etc/profile   alias rm='echo ...

  10. mysql主从搭建操作

    1.搭建说明准备工作:主从库已安装mysql软件以及xtracbackup备份工具.具体操作可参见mysql rpm安装文档. 介质 版本操作系统 Red Hat Enterprise Linux S ...