GoogLeNet
总结
- Inception块用4条有不同超参数的卷积层和池化层的路来抽取不同的信息。
- 他的一个主要优点是模型参数小,计算复杂度低
- GoogleNet使用了9个Inception块,是第一个到达上百层的网络
- 后续有一系列改进
- Inception块相当于一个有4条路径的子网络,它通过不同窗口形状的卷积层和最大池化层来并行提取信息,并使用1×1的卷积层来减少像素级上的通道维数从而降低模型复杂度。
- GoogLeNet将多个设计精细的Inception块与其他层(卷积层、全连接层)串联起来,其中Inception块的通道分配之比是在ImageNet数据集上通过大量的实验得来的。
- GoogLeNet和它的后继者们一度是ImageNet上最有效的模型之一:它以较低的计算复杂度提供了类似的测试精度。
GoogLeNet代码实现
- 导入相关库
import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l
import time
- 定义网路模型
# 定义Inception块
class Inception(nn.Module):
# c1--c4是每条路径的输出通道数
def __init__(self, in_channels, c1, c2, c3, c4, **kwargs):
super().__init__()
# 路径1:单1×1卷积层
self.p1_1 = nn.Conv2d(in_channels, c1, kernel_size=1)
# 路径2:1×1卷积层、3×3卷积层
self.p2_1 = nn.Conv2d(in_channels, c2[0], kernel_size=1)
self.p2_2 = nn.Conv2d(c2[0], c2[1], kernel_size=3, padding=1)
# 路径3:1×1卷积层、5×5卷积层
self.p3_1 = nn.Conv2d(in_channels, c3[0], kernel_size=1)
self.p3_2 = nn.Conv2d(c3[0], c3[1], kernel_size=5, padding=2)
# 路径4:3×3最大池化层、1×1卷积层
self.p4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
self.p4_2 = nn.Conv2d(in_channels, c4, kernel_size=1)
def forward(self, X):
p1 = F.relu(self.p1_1(X))
p2 = F.relu(self.p2_2(F.relu(self.p2_1(X))))
p3 = F.relu(self.p3_2(F.relu(self.p3_1(X))))
p4 = F.relu(self.p4_2(F.relu(self.p4_1(X))))
# 在输出通道维度上连结输出
return torch.cat((p1, p2, p3, p4), dim=1)
b1 = nn.Sequential( # 输入:[1, 1, 96, 96]
nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3), nn.ReLU(), # [1, 64, 48, 48]
nn.MaxPool2d(3, stride=2, padding=1) # [1, 64, 24, 24]
)
b2 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=1), nn.ReLU(), # [1, 64, 24, 24]
nn.Conv2d(64, 192, kernel_size=3, padding=1), nn.ReLU(), # [1, 192, 24, 24]
nn.MaxPool2d(3, stride=2, padding=1) # [1, 192, 12, 12]
)
b3 = nn.Sequential(
Inception(192, 64, (96, 128), (16, 32), 32), # 64+128+32+32= 256 [1, 256, 12, 12]
Inception(256, 128, (128, 192), (32, 96), 64), # 128+192+96+64= 480 [1, 480, 12, 12]
nn.MaxPool2d(3, stride=2, padding=1) # [1, 480, 6, 6]
)
b4 = nn.Sequential(
Inception(480, 192, (96, 208), (16, 48), 64), # 192+208+48+64= 512 [1, 512, 6, 6]
Inception(512, 160, (112, 224), (24, 64), 64), # 160+224+64+64= 512 [1, 512, 6, 6]
Inception(512, 112, (144, 288), (32, 64), 64), # 112+288+64+64= 528 [1, 528, 6, 6]
Inception(528, 256, (160, 320), (32, 128), 128), # 256+320+128+128= 832 [1, 832, 6, 6]
nn.MaxPool2d(3, stride=2, padding=1) # [1, 832, 3, 3]
)
b5 = nn.Sequential(
Inception(832, 256, (160, 320), (32, 128), 128), # 256+320+128+128= 832 [1, 832, 3, 3]
Inception(832, 384, (192, 384), (48, 128), 128), # 384+384+128+128= 1024 [1, 1024, 3, 3]
nn.AdaptiveAvgPool2d((1, 1)), # [1, 1024, 1, 1]
nn.Flatten() # [1, 1024, 1, 1] = [1, 1024]
)
net = nn.Sequential( # 输入:[1, 1, 96, 96]
b1, # [1, 64, 24, 24]
b2, # [1, 192, 12, 12]
b3, # [1, 480, 6, 6]
b4, # [1, 832, 3, 3]
b5, # [1, 1024]
nn.Linear(1024, 10) # [1, 10]
)
- 查看模型
- 为了使Fashion——MNIST上的训练短小精悍, 我们将输入的高和宽从224降到96
X = torch.randn(size=(1, 1, 96, 96))
for layer in net:
X = layer(X)
print(layer.__class__.__name__, 'output shape: \t', X.shape)
- 加载数据集
batch_size = 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size,resize=96)
- 训练模型
lr, num_epochs = 0.1, 10
start = time.perf_counter()
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
end = time.perf_counter()
print("运行耗时%.4f" % (end-start))