为了账号安全,请及时绑定邮箱和手机立即绑定

王权富贵:GoogLeNet的pytorch实现

标签:
Html5

王权富贵

本文详细介绍了GoogLeNet网络结构及其核心组件Inception模块的设计原理与实现代码。通过PyTorch框架,从Inception模块的构建到整个GoogLeNet网络的搭建过程,展示了深度学习中模块化设计的重要性。

感谢kuangliu,参考自这个项目里面的一篇叫googlenet.py的文档。

https://img1.sycdn.imooc.com/568d7068083f919605930309.jpg

先构建一个基本的Inception模块。构造如上图所示,具体代码如下所示。

'''GoogLeNet with PyTorch.'''import torchimport torch.nn as nnimport torch.nn.functional as F  class Inception(nn.Module):    def __init__(self, in_planes, n1x1, n3x3red, n3x3, n5x5red, n5x5, pool_planes):        super(Inception, self).__init__()        # 1x1 conv branch        self.b1 = nn.Sequential(            nn.Conv2d(in_planes, n1x1, kernel_size=1),            nn.BatchNorm2d(n1x1),            nn.ReLU(True),        )         # 1x1 conv -> 3x3 conv branch        self.b2 = nn.Sequential(            nn.Conv2d(in_planes, n3x3red, kernel_size=1),            nn.BatchNorm2d(n3x3red),            nn.ReLU(True),            nn.Conv2d(n3x3red, n3x3, kernel_size=3, padding=1),            nn.BatchNorm2d(n3x3),            nn.ReLU(True),        )         # 1x1 conv -> 5x5 conv branch        self.b3 = nn.Sequential(            nn.Conv2d(in_planes, n5x5red, kernel_size=1),            nn.BatchNorm2d(n5x5red),            nn.ReLU(True),            nn.Conv2d(n5x5red, n5x5, kernel_size=3, padding=1),            nn.BatchNorm2d(n5x5),            nn.ReLU(True),            nn.Conv2d(n5x5, n5x5, kernel_size=3, padding=1),            nn.BatchNorm2d(n5x5),            nn.ReLU(True),        )         # 3x3 pool -> 1x1 conv branch        self.b4 = nn.Sequential(            nn.MaxPool2d(3, stride=1, padding=1),            nn.Conv2d(in_planes, pool_planes, kernel_size=1),            nn.BatchNorm2d(pool_planes),            nn.ReLU(True),        )AI写代码python运行

GooLeNet的构建如下代码所示,对应结构图,在代码下面显示:

class GoogLeNet(nn.Module):    def __init__(self):        super(GoogLeNet, self).__init__()        self.pre_layers = nn.Sequential(            nn.Conv2d(3, 192, kernel_size=3, padding=1),            nn.BatchNorm2d(192),            nn.ReLU(True),        )         self.a3 = Inception(192,  64,  96, 128, 16, 32, 32)        self.b3 = Inception(256, 128, 128, 192, 32, 96, 64)         self.maxpool = nn.MaxPool2d(3, stride=2, padding=1)         self.a4 = Inception(480, 192,  96, 208, 16,  48,  64)        self.b4 = Inception(512, 160, 112, 224, 24,  64,  64)        self.c4 = Inception(512, 128, 128, 256, 24,  64,  64)        self.d4 = Inception(512, 112, 144, 288, 32,  64,  64)        self.e4 = Inception(528, 256, 160, 320, 32, 128, 128)         self.a5 = Inception(832, 256, 160, 320, 32, 128, 128)        self.b5 = Inception(832, 384, 192, 384, 48, 128, 128)         self.avgpool = nn.AvgPool2d(8, stride=1)        self.linear = nn.Linear(1024, 10)     def forward(self, x):        out = self.pre_layers(x)        out = self.a3(out)        out = self.b3(out)        out = self.maxpool(out)        out = self.a4(out)        out = self.b4(out)        out = self.c4(out)        out = self.d4(out)        out = self.e4(out)        out = self.maxpool(out)        out = self.a5(out)        out = self.b5(out)        out = self.avgpool(out)        out = out.view(out.size(0), -1)        out = self.linear(out)        return outAI写代码python运行

Inception模块中通道的选取参考如下:

https://img1.sycdn.imooc.com/0141e168083f919f14000902.jpg

上面代码是简化版,它在开始部分没有经过一个7*7和1*1的卷积,直接从3*3开始。辅助分类器也没有写上。

https://img1.sycdn.imooc.com/a4d22368083f91a804161600.jpg


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消