ResNet _make_layer代码理解

ResNet

在这里插入图片描述
上图为ResNet的5个 基本结构,为了方便理解,此处以最简单的18-layer为例来展开:
首先我们知道ResNet中对于50层以下的构建块采用的是BasicBlock,而大于50的深层则采用的是Bottleneck,BasicBlock的构建代码如下:

class BasicBlock(nn.Module):
    expansion = 1
    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        if self.downsample is not None:
            identity = self.downsample(x)
        out += identity
        out = self.relu(out)
        return out

首先我想解释下 BasicBlock 在ResNet 里代表的是什么,我们知道在图1所示,ResNet18由以下几部分组成,conv1,conv2_x,conv3_x,conv4_x,conv5_x,以及最后的全连接层,BasicBlock指的是 conv2_x,conv3_x,conv4_x,conv5_x里的这些具有重复结构的块,如Conv3_x层中具有两个([33,128]+[33,128])块,BasicBlock就指的是这些块,在ResNet中,通过重复调用BasicBlock方法就能方便的构建出这些块。

构建过程

在ResNet中通过BasicBlock构建一个完整网络利用的方法是_make-layer(通过循环创建块来创建层),其代码如下:

# block:基础块的类型,是BasicBlock,还是Bottleneck
# planes:当前块的输入输入通道数
# blocks:块的数目
def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        # downSample的作用于在残差连接时 将输入的图像的通道数变成和卷积操作的尺寸一致
        # 根据ResNet的结构特点,一般只在每层开始时进行判断
        if stride != 1 or self.inplanes != planes * block.expansion:
        	# 通道数恢复成一致
            downsample = nn.Sequential(
                conv1x1(self.inplanes, planes * block.expansion, stride),
                nn.BatchNorm2d(planes * block.expansion),
            )
        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes))
        return nn.Sequential(*layers)

为了方便理解,此处我分别对BasicBlock和Bottleneck根据结构进行推导下:

BasicBlock理解

Layer18–Conv2_x:假设我们的初始输入大小为2242243在conv1和max pool后尺寸变成:565664
则此时的self.inplanes=64,Conv2_x的创建代码为

# layer = [2,2,2,2]
self.layer1 = self._make_layer(Basicblock, 64, block_num=layer[0], stride=1)

stride=1 && 64 = 641(也就是self.inplanes = planes * block.expansion),无需downsample
在执行完成_make_layer后len(layers) = 2,结构均为(3
3,64)
然后是Conv3_x:此时的self.inplanes = 64,Conv3_x的代码为:

self.layer2 = self._make_layer(Basicblock, 128, block_num=layers[1], stride=2)

stride=2 需要downsample,此时downsample的主要修改是将通道由上层的64,变成本层的128,所以第一个BasicBlock的结构是:
64—>128,128—<128,第二个结构是,128—>128.128—>128,此阶段后的 self.inplanes=128,往后类似。也就是说,下一层的self.inplanes是上一层的最后的输出通道数。

Bottleneck理解

Layer50–Conv2_x:假设我们的初始输入大小为2242243在conv1和max pool后尺寸变成:565664
则此时的self.inplanes=64,Conv2_x的创建代码为

# layer = [3,4,6,3]
self.layer1 = self._make_layer(block,64,layer[0])

64 <> 644(也就是self.inplanes <> planes * block.expansion),需downsample
通道数 64—>64
4=256
第一个块的结构为:64—>64,64—>64,64—>256然后在残差连接时,使用downsample对上一层的输出进行调整,以方便进行连接,后续则无需downsample,然后进入下一层,与此类似。

  • 30
    点赞
  • 120
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
In the fall of 1998 Bill Joy and Mike Clary asked us to help establish a community of developers and companies around Sun's then-new Jini network technology. Jini is a simple distributed computing model based on the Java programming language that enables services running on different computer systems to spontaneously interact with each other over the network with minimal pre- planning for such interactions. For Jini to succeed, it was clear that the underlying Jini protocols and infrastructure would need to become pervasive, and to accomplish that would require a strong community of participants and partners. Moreover Sun did not have the expertise to define Jini services in areas like printing, digital photography, storage, white goods, or the many other potential markets for products that would benefit from being network enabled. As a preliminary step the Sun Community Source License (SCSL) had been developed to provide a framework where the source code for Jini could be safely shared. SCSL is not true open source, but has many of the same characteristics. However a license and providing the source code were not enough~there had to be more to motivate people to participate. We worked with the Jini engineering and marketing teams to apply the lessons we had learned from being involved in various open source and community development projects and from our studies of complexity science to create a true Jini Community. We worked hard to help establish an identity for the community. To build identity and culture requires face-to-face interaction, so we organized community meetings that were held in interesting places and featuring non-traditional speakers. The first Jini Community meeting was at the Aspen Institute in Aspen, Colorado. We played team-building games, we engaged a recording graphic artist, we broke into groups in unusual ways. 1 We encouraged nonstandard thinking and speaking as individuals rather than as representatives of companies at times. The keynote speakers included Robert Dahl, a political scientist, talking "On Democracy," and Thomas Petzinger, a Wall Street Journal columnist, speaking about new business models based on cooperation. We also helped create a website for the project (http://www.jini.org) as a meeting place, a newspaper, a repository of shared documents, a totem, and a place to share work and projects. Most of the shared assets of the community could be accessed there. The website had a public part that advertised the community, and private parts for members only to help foster community identity. Common vocabulary leads to shared stories and then to shared beliefs. We created a pattern language on how the Jini Community could work, which has served to create a shared vocabulary--terms like dangerous waterhole, cut ~r run, and microcosm that serve as linguistic markers for the uniqueness of the community and culture. The pattern language was designed to teach how to build (self-) governance structures and procedures on the fly which have proven to be comfortable and delightful in the past but which were to be tailored to the specific on-the-spot needs of the community. We also worked with the community on developing a formal decision-making process and a "community process" for the purpose of ratifying definitions for Jini services. The former was to make it clear how community-wide decisions are made--how are proposals created, how are they voted on, how are appeals made--and the latter was to specify how the community can officially bless or endorse a service definition. The request to help build a community around a technology for business purposes led us to research and think about business in the commons. How do open-source projects work? How can a company participate? Why would a company engage in an activity centered around giving things away? How and why could such a thing work? Since the spring of 2000 we have done similar work with other Sun-sponsored open-source projects such as NetBeans, OpenOffice, Project JXTA, and java.net. We have worked with each of those teams to help them to define their open source strategy along with how to implement it. This book contains not only the lessons we have learned from helping to create and nurture communities around these Sun-sponsored and other open source and community development projects, but the fruit of years of participation in innovative communities of many sorts. Specifically, one of us 2 was the originator of the Common Lisp Group, which is the first, large-scale, email-based design effort; this group worked from 1981 until the early 1990s. The same author 2 founded Lucid, Inc, one of whose products~designed and implemented in the late 1980s~used a suite of free-software tools tied together by a proprietary, 2 RPG. Preface xxi database-centered coordination layer. The tools included GCC, GDB, and a window-based version of Emacs originally called Lucid Emacs and now called Xemacs. The interaction of the suite of tools with the coordination layer was through an open set of protocols, and several compilers were modified to support these protocols. Lucid was one of the first companies to do significant work on open-source code (in this case, Free Software Foundation code) as part of a business strategy, and in fact, the existence of LGPL is partly due to this early commercial use of GPLed code. This book also includes insights from studying other open-source projects such as Linux, Apache, Emacs, Xemacs, GCC, GDB, and Mozilla. We have had experience with projects and companies that were trying to achieve something creative in the context of a passionate user, developer, and partner landscape where the knowledge, expertise, and innovation in that landscape needed to be part of the creativity. Ron Goldman Richard P. Gabrid March, 2005

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值