網(wǎng)站首頁 編程語言 正文
PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實現(xiàn)流程_python
作者:峽谷的小魚 ? 更新時間: 2022-06-30 編程語言含并行連結(jié)的網(wǎng)絡(luò) GoogLeNet
在GoogleNet出現(xiàn)值前,流行的網(wǎng)絡(luò)結(jié)構(gòu)使用的卷積核從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ù),降低模型復(fù)雜性,然后接 3×3 卷積層;
路徑3:先對輸出執(zhí)行 1×1 卷積層,然后接 5×5 卷積層;
路徑4:使用 3×3 最大匯聚層,然后使用 1×1 卷積層;
在各自路徑中使用合適的 padding ,使得各個路徑的輸出擁有相同的高和寬,然后將每條路徑的輸出在通道維度上做連結(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 的模塊。
第一個模塊,順序使用三個卷積層。
# 模型的第一個模塊
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
訓練結(jié)果:
原文鏈接:https://blog.csdn.net/weixin_43276033/article/details/124545743
相關(guān)推薦
- 2022-12-30 解決React報錯The?tag?is?unrecognized?in?this?browser_R
- 2022-02-17 Springboot 獲取響應(yīng)信息,修改響應(yīng)信息
- 2022-05-18 ASP.NET?MVC自定義操作過濾器_實用技巧
- 2023-06-05 C++頭文件和cpp文件的原理分析_C 語言
- 2022-05-10 golang使用zookeeper進行增刪改查
- 2022-03-14 寶塔中nginx配置websocket的wss協(xié)議
- 2023-01-17 解讀MaxPooling1D和GlobalMaxPooling1D的區(qū)別_python
- 2023-11-15 Latex文獻報錯 Something‘s wrong--perhaps a missing \it
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支