PyTorch詳解經典網絡種含并行連結的網絡GoogLeNet實現(xiàn)流程
含并行連結的網絡 GoogLeNet
在GoogleNet出現(xiàn)值前,流行的網絡結構使用的卷積核從1×1到11×11,卷積核的選擇并沒有太多的原因。GoogLeNet的提出,說明有時候使用多個不同大小的卷積核組合是有利的。
import torch from torch import nn from torch.nn import functional as F
1. Inception塊
Inception塊是 GoogLeNet 的基本組成單元。Inception 塊由四條并行的路徑組成,每個路徑使用不同大小的卷積核:
路徑1:使用 1×1 卷積層;
路徑2:先對輸出執(zhí)行 1×1 卷積層,來減少通道數(shù),降低模型復雜性,然后接 3×3 卷積層;
路徑3:先對輸出執(zhí)行 1×1 卷積層,然后接 5×5 卷積層;
路徑4:使用 3×3 最大匯聚層,然后使用 1×1 卷積層;
在各自路徑中使用合適的 padding ,使得各個路徑的輸出擁有相同的高和寬,然后將每條路徑的輸出在通道維度上做連結,作為 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. 構造 GoogLeNet 網絡
順序定義 GoogLeNet 的模塊。
第一個模塊,順序使用三個卷積層。
# 模型的第一個模塊
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)
)
第二個模塊,使用兩個Inception模塊。
# Inception組成的第二個模塊
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)
)第三個模塊,串聯(lián)五個Inception模塊。
# Inception組成的第三個模塊
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)
)第四個模塊,傳來兩個Inception模塊。
GoogLeNet使用 avg pooling layer 代替了 fully-connected layer。一方面降低了維度,另一方面也可以視為對低層特征的組合。
# Inception組成的第四個模塊
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訓練測試
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訓練結果:

到此這篇關于PyTorch詳解經典網絡種含并行連結的網絡GoogLeNet實現(xiàn)流程的文章就介紹到這了,更多相關PyTorch GoogLeNet內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python基于twisted實現(xiàn)簡單的web服務器
這篇文章主要介紹了Python基于twisted實現(xiàn)簡單的web服務器,可模擬出簡單的web服務器功能,是很實用的技巧,需要的朋友可以參考下2014-09-09
Python中使用?zipfile創(chuàng)建文件壓縮工具
這篇文章主要介紹了Python中使用zipfile創(chuàng)建文件壓縮工具,通過使用 wxPython 模塊,我們創(chuàng)建了一個簡單而實用的文件壓縮工具,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的ca參考借鑒價值,需要的朋友可以參考下2023-09-09
Python統(tǒng)計分析模塊statistics用法示例
這篇文章主要介紹了Python統(tǒng)計分析模塊statistics用法,結合實例形式分析了Python統(tǒng)計分析模塊statistics計算平均數(shù)、中位數(shù)、出現(xiàn)次數(shù)、標準差等相關操作技巧,需要的朋友可以參考下2019-09-09

