GoogLeNet
前面我们看了那么多卷积神经网络构建的方式,有选3 * 3卷积核的,有5 * 5卷积核的,有7 * 7卷积核的;有maxpooling,有avgpooling,那么哪种比较好呢?GoogLeNet:我全都要
Inception块
盗梦空间
Inception块
只改变通道数,不改变图片大小蓝色的块用来抽取图片信息
白色块用来改变通道数
输入 192 * 28 * 28
通道数在每层发生的变化情况
回忆一下,一组卷积核处理一个输入:一个卷积核处理一个通道,最后结果相加得到一个通道,多个卷积核组处理得到多个通道。
输出
的通道数是由你有多少组卷积核组
决定的
输入
的通道数决定的是每个卷积核组
里有多少个卷积核和 3 * 3 ,5 * 5 的卷积核相比,
Inception块
的参数更少 如 3 * 3 的卷积核有:192 * 3 * 3 * 256 = 442368 个参数
5 * 5 的卷积核有:192 * 5 * 5 * 256 = 1228800 个参数
Inception块
只有: 192 * 96 + 192 * 16 + 192 * 64 + 192 * 32 + 16 * 5 * 5 * 32 + 96 * 9 * 128 = 163328 个参数当然计算速度上也是
Inception块
更快
GoogLeNet模型结构
AlexNet和GoogLeNet对比
套公式也能算出每层的图片宽高是怎么变化的
Inception块的后续变种
代码实现
Inception块
import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l
class Inception(nn.Module):
# c1--c4是每条路径的输出通道数
def __init__(self, in_channels, c1, c2, c3, c4, **kwargs):
super(Inception, self).__init__(**kwargs)
# 线路1,单1x1卷积层
self.p1_1 = nn.Conv2d(in_channels, c1, kernel_size=1)
# 线路2,1x1卷积层后接3x3卷积层
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,1x1卷积层后接5x5卷积层
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,3x3最大汇聚层后接1x1卷积层
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(self.p4_1(x)))
# 在通道维度上连结输出
return torch.cat((p1, p2, p3, p4), dim=1)
继承nn.Module
cat((p1, p2, p3, p4), dim=1)
最后在 dim = 1 这个维度(通道数)上拼接,dim = 0 是批次数
按图实现几个stage
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))
b2 = nn.Sequential(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))
b3 = 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))
b4 = 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))
b5 = 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, b5, nn.Linear(1024, 10))
看看模型每层情况
X = torch.rand(size=(1, 1, 96, 96))
for layer in net:
X = layer(X)
print(layer.__class__.__name__,'output shape:\t', X.shape)
输出
Sequential output shape: torch.Size([1, 64, 24, 24])
Sequential output shape: torch.Size([1, 192, 12, 12])
Sequential output shape: torch.Size([1, 480, 6, 6])
Sequential output shape: torch.Size([1, 832, 3, 3])
Sequential output shape: torch.Size([1, 1024])
Linear output shape: torch.Size([1, 10])
训练
lr, num_epochs, batch_size = 0.1, 10, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
GoogLeNet效果很好,而且比较容易训练的,我970m的显卡都能跑得动,前面VGG和NiN都不行。