赞
踩
GoogLeNet(也称为Inception)是由谷歌公司的研究团队于2014年提出的深度卷积神经网络模型。它在ImageNet大规模视觉识别竞赛(ILSVRC)中取得了突出的成绩,在计算机视觉领域引起了广泛的关注。
GoogLeNet的名字来源于其创新的Inception模块,该模块采用多个不同尺寸的卷积核进行特征提取,并将这些特征在通道维度上进行拼接,从而能够捕捉多尺度的图像特征。相比于传统的串行结构,Inception模块的并行操作大大减少了参数数量,提高了网络的效率和性能。
除了Inception模块之外,GoogLeNet还采用了以下几种策略来改进网络的性能:
1x1卷积:通过使用1x1大小的卷积核,GoogLeNet可以在减少特征图的尺寸的同时改变通道数,从而控制网络的复杂度,并促进信息的交流和整合。
辅助分类器:GoogLeNet在中间层添加了辅助分类器,可以在训练过程中引入额外的监督信号,帮助梯度的向后传播,缓解梯度消失问题。
平均池化:在全局平均池化层中,GoogLeNet将整个特征图转换为单个数值,从而实现了对空间结构的整合,提取出更高级别的语义特征。
动态调整网络深度和宽度:GoogLeNet通过堆叠Inception模块以及调整每个模块的通道数和层数的方式,灵活地构建出不同复杂度和规模的网络模型。
GoogLeNet网络结构相对较深,但参数量相对较小,使得其具有较高的计算效率。它的设计理念和创新思路在后续的深度学习模型中得到了广泛的应用和发展。
- import torch
- from torch import nn
- from torch.nn import functional as F
- from d2l import torch as d2l
- from d2l.torch import evaluate_accuracy_gpu
-
- if __name__ == "__main__":
-
- def train_ch6(net, train_iter, test_iter, num_epochs, lr, device):
- def init_weights(m):
- if type(m) == nn.Linear or type(m) == nn.Conv2d:
- nn.init.xavier_uniform_(m.weight)
-
- net.apply(init_weights)
- print('training on', device)
- net.to(device)
- optimizer = torch.optim.SGD(net.parameters(), lr=lr)
- loss = nn.CrossEntropyLoss()
- timer, num_batches = d2l.Timer(), len(train_iter)
- for epoch in range(num_epochs):
- # Sum of training loss, sum of training accuracy, no. of examples
- metric = d2l.Accumulator(3)
- net.train()
- for i, (X, y) in enumerate(train_iter):
- timer.start()
- optimizer.zero_grad()
- X, y = X.to(device), y.to(device)
- y_hat = net(X)
- l = loss(y_hat, y)
- l.backward()
- optimizer.step()
- with torch.no_grad():
- metric.add(l * X.shape[0], d2l.accuracy(y_hat, y), X.shape[0])
- timer.stop()
- train_l = metric[0] / metric[2]
- train_acc = metric[1] / metric[2]
-
- test_acc = evaluate_accuracy_gpu(net, test_iter)
- print(f'epoch {epoch},loss :{train_l:.3f}, train acc :{train_acc:.3f}, '
- f'test acc :{test_acc:.3f}, time :{int(timer.sum() / 60)}m{int(timer.sum() % 60)}s')
- print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
- f'test acc {test_acc:.3f}')
- print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec '
- f'on {str(device)}')
-
-
- 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)
-
-
- 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))
-
- lr, num_epochs, batch_size = 0.1, 10, 128
- train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=96)
- train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
-
- # 模型保存
- torch.save(net.state_dict(), f'GoogleNet/GoogleNet{num_epochs}.pth')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。