PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實(shí)現(xiàn)流程
含并行連結(jié)的網(wǎng)絡(luò) GoogLeNet
在GoogleNet出現(xiàn)值前,流行的網(wǎng)絡(luò)結(jié)構(gòu)使用的卷積核從1×1到11×11,卷積核的選擇并沒有太多的原因。GoogLeNet的提出,說明有時(shí)候使用多個(gè)不同大小的卷積核組合是有利的。
import torch from torch import nn from torch.nn import functional as F
1. Inception塊
Inception塊是 GoogLeNet 的基本組成單元。Inception 塊由四條并行的路徑組成,每個(gè)路徑使用不同大小的卷積核:
路徑1:使用 1×1 卷積層;
路徑2:先對(duì)輸出執(zhí)行 1×1 卷積層,來減少通道數(shù),降低模型復(fù)雜性,然后接 3×3 卷積層;
路徑3:先對(duì)輸出執(zhí)行 1×1 卷積層,然后接 5×5 卷積層;
路徑4:使用 3×3 最大匯聚層,然后使用 1×1 卷積層;
在各自路徑中使用合適的 padding ,使得各個(gè)路徑的輸出擁有相同的高和寬,然后將每條路徑的輸出在通道維度上做連結(jié),作為 Inception 塊的最終輸出.
class Inception(nn.Module): def __init__(self, in_channels, out_channels): super(Inception, self).__init__() # 路徑1 c1, c2, c3, c4 = out_channels self.route1_1 = nn.Conv2d(in_channels, c1, kernel_size=1) # 路徑2 self.route2_1 = nn.Conv2d(in_channels, c2[0], kernel_size=1) self.route2_2 = nn.Conv2d(c2[0], c2[1], kernel_size=3, padding=1) # 路徑3 self.route3_1 = nn.Conv2d(in_channels, c3[0], kernel_size=1) self.route3_2 = nn.Conv2d(c3[0], c3[1], kernel_size=5, padding=2) # 路徑4 self.route4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) self.route4_2 = nn.Conv2d(in_channels, c4, kernel_size=1) def forward(self, x): x1 = F.relu(self.route1_1(x)) x2 = F.relu(self.route2_2(F.relu(self.route2_1(x)))) x3 = F.relu(self.route3_2(F.relu(self.route3_1(x)))) x4 = F.relu(self.route4_2(self.route4_1(x))) return torch.cat((x1, x2, x3, x4), dim=1)
2. 構(gòu)造 GoogLeNet 網(wǎng)絡(luò)
順序定義 GoogLeNet 的模塊。
第一個(gè)模塊,順序使用三個(gè)卷積層。
# 模型的第一個(gè)模塊 b1 = nn.Sequential( nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3,), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1), nn.Conv2d(64, 64, kernel_size=1), nn.ReLU(), nn.Conv2d(64, 192, kernel_size=3, padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1) )
第二個(gè)模塊,使用兩個(gè)Inception模塊。
# Inception組成的第二個(gè)模塊 b2 = nn.Sequential( Inception(192, (64, (96, 128), (16, 32), 32)), Inception(256, (128, (128, 192), (32, 96), 64)), nn.MaxPool2d(kernel_size=3, stride=2, padding=1) )
第三個(gè)模塊,串聯(lián)五個(gè)Inception模塊。
# Inception組成的第三個(gè)模塊 b3 = nn.Sequential( Inception(480, (192, (96, 208), (16, 48), 64)), Inception(512, (160, (112, 224), (24, 64), 64)), Inception(512, (128, (128, 256), (24, 64), 64)), Inception(512, (112, (144, 288), (32, 64), 64)), Inception(528, (256, (160, 320), (32, 128), 128)), nn.MaxPool2d(kernel_size=3, stride=2, padding=1) )
第四個(gè)模塊,傳來兩個(gè)Inception模塊。
GoogLeNet使用 avg pooling layer 代替了 fully-connected layer。一方面降低了維度,另一方面也可以視為對(duì)低層特征的組合。
# Inception組成的第四個(gè)模塊 b4 = nn.Sequential( Inception(832, (256, (160, 320), (32, 128), 128)), Inception(832, (384, (192, 384), (48, 128), 128)), nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten() )
net = nn.Sequential(b1, b2, b3, b4, nn.Linear(1024, 10)) x = torch.randn(1, 1, 96, 96) for layer in net: x = layer(x) print(layer.__class__.__name__, "output shape: ", x.shape)
輸出:
Sequential output shape: torch.Size([1, 192, 28, 28])
Sequential output shape: torch.Size([1, 480, 14, 14])
Sequential output shape: torch.Size([1, 832, 7, 7])
Sequential output shape: torch.Size([1, 1024])
Linear output shape: torch.Size([1, 10])
3. FashionMNIST訓(xùn)練測試
def load_datasets_Cifar10(batch_size, resize=None): trans = [transforms.ToTensor()] if resize: transform = trans.insert(0, transforms.Resize(resize)) trans = transforms.Compose(trans) train_data = torchvision.datasets.CIFAR10(root="../data", train=True, transform=trans, download=True) test_data = torchvision.datasets.CIFAR10(root="../data", train=False, transform=trans, download=True) print("Cifar10 下載完成...") return (torch.utils.data.DataLoader(train_data, batch_size, shuffle=True), torch.utils.data.DataLoader(test_data, batch_size, shuffle=False)) def load_datasets_FashionMNIST(batch_size, resize=None): trans = [transforms.ToTensor()] if resize: transform = trans.insert(0, transforms.Resize(resize)) trans = transforms.Compose(trans) train_data = torchvision.datasets.FashionMNIST(root="../data", train=True, transform=trans, download=True) test_data = torchvision.datasets.FashionMNIST(root="../data", train=False, transform=trans, download=True) print("FashionMNIST 下載完成...") return (torch.utils.data.DataLoader(train_data, batch_size, shuffle=True), torch.utils.data.DataLoader(test_data, batch_size, shuffle=False)) def load_datasets(dataset, batch_size, resize): if dataset == "Cifar10": return load_datasets_Cifar10(batch_size, resize=resize) else: return load_datasets_FashionMNIST(batch_size, resize=resize) train_iter, test_iter = load_datasets("", 128, 96) # Cifar10
訓(xùn)練結(jié)果:
到此這篇關(guān)于PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)PyTorch GoogLeNet內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)戰(zhàn)練習(xí)之終于對(duì)肯德基下手
讀萬卷書不如行萬里路,學(xué)的扎不扎實(shí)要通過實(shí)戰(zhàn)才能看出來,本篇文章手把手帶你爬下肯德基的官網(wǎng),大家可以在過程中查缺補(bǔ)漏,看看自己掌握程度怎么樣2021-10-10Python基于twisted實(shí)現(xiàn)簡單的web服務(wù)器
這篇文章主要介紹了Python基于twisted實(shí)現(xiàn)簡單的web服務(wù)器,可模擬出簡單的web服務(wù)器功能,是很實(shí)用的技巧,需要的朋友可以參考下2014-09-09如何利用Python實(shí)現(xiàn)簡單C++程序范圍分析
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)簡單C++程序范圍分析,文章以舉例說明及過程實(shí)現(xiàn)思路的方式展開講解,具有一定的的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-02-02機(jī)器學(xué)習(xí)Erdos?Renyi隨機(jī)圖生成方法及特性
這篇文章主要為大家介紹了機(jī)器學(xué)習(xí)Erdos?Renyi隨機(jī)圖生成方法及特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python中使用?zipfile創(chuàng)建文件壓縮工具
這篇文章主要介紹了Python中使用zipfile創(chuàng)建文件壓縮工具,通過使用 wxPython 模塊,我們創(chuàng)建了一個(gè)簡單而實(shí)用的文件壓縮工具,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的ca參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09Pycharm遠(yuǎn)程連接服務(wù)器并運(yùn)行與調(diào)試
本篇文章介紹一下 Pycharm 如何配置遠(yuǎn)程連接信息,使其能夠在本地使用服務(wù)器上的GPU等硬件資源,并在本地完成代碼的運(yùn)行與調(diào)試,感興趣的可以了解一下2021-08-08Python統(tǒng)計(jì)分析模塊statistics用法示例
這篇文章主要介紹了Python統(tǒng)計(jì)分析模塊statistics用法,結(jié)合實(shí)例形式分析了Python統(tǒng)計(jì)分析模塊statistics計(jì)算平均數(shù)、中位數(shù)、出現(xiàn)次數(shù)、標(biāo)準(zhǔn)差等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09