今天就跟大家聊聊有關(guān)怎么在Pytorch中實現(xiàn)一個GoogLeNet方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)是一家專業(yè)提供吉利企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為吉利眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。pytorch的優(yōu)點1.PyTorch是相當(dāng)簡潔且高效快速的框架;2.設(shè)計追求最少的封裝;3.設(shè)計符合人類思維,它讓用戶盡可能地專注于實現(xiàn)自己的想法;4.與google的Tensorflow類似,F(xiàn)AIR的支持足以確保PyTorch獲得持續(xù)的開發(fā)更新;5.PyTorch作者親自維護的論壇 供用戶交流和求教問題6.入門簡單
GoogLeNet也叫InceptionNet,在2014年被提出,如今已到V4版本。GoogleNet比VGGNet具有更深的網(wǎng)絡(luò)結(jié)構(gòu),一共有22層,但是參數(shù)比AlexNet要少12倍,但是計算量是AlexNet的4倍,原因就是它采用很有效的Inception模塊,并且沒有全連接層。
最重要的創(chuàng)新點就在于使用inception模塊,通過使用不同維度的卷積提取不同尺度的特征圖。左圖是最初的Inception模塊,右圖是使用的1×1得卷積對左圖的改進,降低了輸入的特征圖維度,同時降低了網(wǎng)絡(luò)的參數(shù)量和計算復(fù)雜度,稱為inception V1。
GoogleNet在架構(gòu)設(shè)計上為保持低層為傳統(tǒng)卷積方式不變,只在較高的層開始用Inception模塊。
inception V2中將5x5的卷積改為2個3x3的卷積,擴大了感受野,原來是5x5,現(xiàn)在是6x6。Pytorch實現(xiàn)GoogLeNet(inception V2):
'''GoogLeNet with PyTorch.''' import torch import torch.nn as nn import torch.nn.functional as F # 編寫卷積+bn+relu模塊 class BasicConv2d(nn.Module): def __init__(self, in_channels, out_channals, **kwargs): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d(in_channels, out_channals, **kwargs) self.bn = nn.BatchNorm2d(out_channals) def forward(self, x): x = self.conv(x) x = self.bn(x) return F.relu(x) # 編寫Inception模塊 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 = BasicConv2d(in_planes, n1x1, kernel_size=1) # 1x1 conv -> 3x3 conv branch self.b2_1x1_a = BasicConv2d(in_planes, n3x3red, kernel_size=1) self.b2_3x3_b = BasicConv2d(n3x3red, n3x3, kernel_size=3, padding=1) # 1x1 conv -> 3x3 conv -> 3x3 conv branch self.b3_1x1_a = BasicConv2d(in_planes, n5x5red, kernel_size=1) self.b3_3x3_b = BasicConv2d(n5x5red, n5x5, kernel_size=3, padding=1) self.b3_3x3_c = BasicConv2d(n5x5, n5x5, kernel_size=3, padding=1) # 3x3 pool -> 1x1 conv branch self.b4_pool = nn.MaxPool2d(3, stride=1, padding=1) self.b4_1x1 = BasicConv2d(in_planes, pool_planes, kernel_size=1) def forward(self, x): y1 = self.b1(x) y2 = self.b2_3x3_b(self.b2_1x1_a(x)) y3 = self.b3_3x3_c(self.b3_3x3_b(self.b3_1x1_a(x))) y4 = self.b4_1x1(self.b4_pool(x)) # y的維度為[batch_size, out_channels, C_out,L_out] # 合并不同卷積下的特征圖 return torch.cat([y1, y2, y3, y4], 1) class GoogLeNet(nn.Module): def __init__(self): super(GoogLeNet, self).__init__() self.pre_layers = BasicConv2d(3, 192, kernel_size=3, padding=1) 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 out def test(): net = GoogLeNet() x = torch.randn(1,3,32,32) y = net(x) print(y.size()) test()
看完上述內(nèi)容,你們對怎么在Pytorch中實現(xiàn)一個GoogLeNet方法有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章題目:怎么在Pytorch中實現(xiàn)一個GoogLeNet方法-創(chuàng)新互聯(lián)
URL分享:http://vcdvsql.cn/article19/cciigh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、云服務(wù)器、網(wǎng)站設(shè)計、Google、電子商務(wù)、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容