当前位置:   article > 正文

卷积神经网络五:GoogleNet

卷积神经网络五:GoogleNet

在2014年的ImageNet图像识别大赛中,一个名叫GoogleNet的网络架构大放异彩。GoogleNet使用了一种叫作Inception的结构。其实GoogleNet本质上就是一种Inception网络,而一个Inception网络又是由多个Inception模块和少量的汇聚层堆叠而成。

Inception模块

在Inception网络结构中,一个卷积层包含多个不同大小的卷积操作,成为Inception模块。Inception模块使用1x1、3x3、5x5等不同大小的卷积核,并将得到的特征映射在深度上拼接(堆叠)起来作为输出特征映射。

GoogleNet的Inception模块结构如图所示:

Inception模块采用了4组平行的特征抽取方式。分别为1x1、3x3、5x5的卷积和3x3的池化层。同时,为了提高计算效率,减少参数数量,Inception模块在进行3x3和5x5的卷积之前和3x3的池化层后进行一次1x1卷积来减少特征映射的深度,其实本质上就是通过1x1卷积来改变输出的通道数量。最后所谓特征映射在深度上拼接其实就是在通道维度上进行合并。

下面我们来实现一下GoogleNet的Inception模块:

  1. import torch.nn as nn
  2. import torch
  3. import torch.optim as optim
  4. import torchvision.transforms as transforms
  5. from torch.nn import functional as F
  6. # 构建inception模块
  7. class Inception(nn.Module):
  8. # c1,c2,c3,c4代码每一路输出的通道数,这些通道最后进行拼接成为最终输出
  9. def __init__(self, in_channels, c1, c2, c3, c4) -> None:
  10. super(Inception, self).__init__()
  11. # 线路1,1x1卷积层
  12. self.p1 = nn.Sequential(nn.Conv2d(in_channels, c1, kernel_size=1), nn.ReLU())
  13. # 线路2,1x1卷积层
  14. self.p2_1 = nn.Sequential(nn.Conv2d(in_channels, c2[0], kernel_size=1), nn.ReLU())
  15. # 线路2,3x3卷积层,填充1
  16. self.p2_2 = nn.Sequential(nn.Conv2d(c2[0], c2[1], kernel_size=3, padding=1), nn.ReLU())
  17. # 线路3,1x1卷积层
  18. self.p3_1 = nn.Sequential(nn.Conv2d(in_channels, c3[0], kernel_size=1), nn.ReLU())
  19. # 线路3,5x5卷积层
  20. self.p3_2 = nn.Sequential(nn.Conv2d(c3[0], c3[1], kernel_size=5, padding=2), nn.ReLU())
  21. # 线路4,3x3池化层
  22. self.p4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) # MaxPool2d的stride默认跟kernel_size一样
  23. # 线路4,1x1卷积层
  24. self.p4_2 = nn.Sequential(nn.Conv2d(in_channels, c4, kernel_size=1), nn.ReLU())
  25. def forward(self, x):
  26. p1 = self.p1(x)
  27. p2 = self.p2_2(self.p2_1(x))
  28. p3 = self.p3_2(self.p3_1(x))
  29. p4 = self.p4_2(self.p4_1(x))
  30. print("p1.shape:{},p2.shape:{},p3.shape:{},p4.shape:{}".format(p1.shape, p2.shape, p3.shape, p4.shape))
  31. # 在通道维度上拼接
  32. return torch.cat((p1, p2, p3, p4), dim=0)
  33. if __name__ == '__main__':
  34. x = torch.zeros(3, 224, 224)
  35. m = Inception(3, 192, (96,208), (16,48), 64)
  36. print(m(x).shape)
  37. # 输出:
  38. p1.shape:torch.Size([192, 224, 224]),p2.shape:torch.Size([208, 224, 224]),p3.shape:torch.Size([48, 224, 224]),p4.shape:torch.Size([64, 224, 224])
  39. torch.Size([512, 224, 224])

这里我们是完全根据inception模块的结构图来实现的,我们假设线路1的输出通道数192,线路2的输出通道数(96,208),线路3的输出通道数(16,48),线路四的输出通道数64。可以看到输入一个3*224*224的张量,最终输出如下:线路1输出192*224*224,线路2输出208*224*224,线路3输出48*224*224,线路4输出64*224*224,按第0维通道维拼接,结果为512*224*224。

这里要注意两个问题,首先拼接的维度选择,我们这里因为维度0是通道维,所以torch.cat函数中选择dim=0,完整模型代码中,dim=1,这是因为第0维一般是批量数batch_size。第二个问题是,池化层MaxPool2d的stride=1不能忘,因为MaxPool2d默认的stride是和kernel_size一样的,如果不指定,那么线路4的3x3池化层输出会变成3*75*75,[(224-3+2*1)/3+1=75],导致线路四最终输出为64*75*75,如果这样的话,就无法按通道维度拼接了,因为[192,224,224],[208,224,224],[48,224,224]和[64,75,75]前三个张量无法和最后一个张量拼接。

Googlenet网络模型

完整的googlenet网络架构如下:

具体流程如下:

输入为3x224x224的张量

1.经过7x7的卷积层,输出64通道,步幅为2,填充为3,输出大小为64x112x112

2.经过3x3的池化层,通道数不变,步幅为2,填充为1,输出大小为64x56x56

3.经过1x1的卷积层,输出64通道,输出大小为64x56x56

4.经过3x3的卷积层,输出192通道,步幅为1,填充为1,输出大小为192x56x56

5.经过3x3的池化层,通道数不变,步幅为2,填充为1,输出大小为192x2828

6.经过两个Inception模块:

1)第一个Inception,输入通道数192,输出通道数为 64+128+32+32=256,由于Inception模块并不改变特征输出的长宽大小,所以输出大小为256x28x28

2)第二个Inception,输入通道数256,输出通道数为128+192+96+64=480,输出大小为480x28x28

7.经过3x3池化层,通道数不变,步幅为2,填充为1,输出大小为480x14x14

8.经过五个Inception模块:

1)第一个Inception,输入通道数480,输出通道数192+208+48+64=512,输出大小为512x14x14

2)第二个Inception,输入通道数512,输出通道数160+224+64+64=512,输出大小为512x14x14

3)第三个Inception,输入通道数512,输出通道数128+256+64+64=512,输出大小为512x14x14

4)第四个Inception,输入通道数512,输出通道数112+228+64+64=528,输出大小为528x14x14

5)第五个Inception,输入通道数528,输出通道数256+320+128+128=832,输出大小为832x14x14

9.经过3x3池化层,通道数不变,步幅为2,填充为1,输出大小为832x7x7

10.经过两个Inception模块:

1)第一个Inception,输入通道数832,输出通道数256+320+128+128=832,输出大小为832x7x7

2)第二个Inception,输入通道数832,输出通道数384+384+128+128=1024,输出大小为1024x7x7

11.经过全局平均汇聚层,输出1024x1x1

12.全连接层,输出10个分类结果。

下面我们实现一下代码:

  1. class GoogleNet(nn.Module):
  2. def __init__(self, num_classes) -> None:
  3. super(GoogleNet, self).__init__()
  4. b1 = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),
  5. nn.ReLU(),
  6. nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
  7. b2 = nn.Sequential(nn.Conv2d(64, 64, kernel_size=1),
  8. nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1),
  9. nn.ReLU(),
  10. nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
  11. b3 = nn.Sequential(Inception(192, 64, (96,128), (16,32), 32),
  12. Inception(256, 128, (128,192), (32,96), 64),
  13. nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
  14. b4 = nn.Sequential(Inception(480, 192, (96,208), (16,48), 64),
  15. Inception(512, 160, (112,224), (24,64), 64),
  16. Inception(512, 128, (128,256), (24,64), 64),
  17. Inception(512, 112, (144,288), (32,64), 64),
  18. Inception(528, 256, (160,320), (32,128), 128),
  19. nn.MaxPool2d(kernel_size=3, stride=2, padding=1))
  20. b5 = nn.Sequential(Inception(832, 256, (160,320), (32,128), 128),
  21. Inception(832, 384, (192,384), (48,128), 128),
  22. nn.AdaptiveAvgPool2d((1,1)),
  23. nn.Flatten())
  24. self.features = nn.Sequential(b1,b2,b3,b4,b5)
  25. self.classifier = nn.Sequential(
  26. nn.Linear(1024, num_classes)
  27. )
  28. def forward(self, x):
  29. x =self.features(x)
  30. print(x.shape)
  31. x = self.classifier(x)
  32. return x
  33. if __name__ == '__main__':
  34. X = torch.rand(size=(1,3,224,224))
  35. net = GoogleNet(num_classes=10)
  36. X = net(X)
  37. print('output shape:', X.shape)
  38. # 输出:
  39. p1.shape:torch.Size([1, 64, 28, 28]),p2.shape:torch.Size([1, 128, 28, 28]),p3.shape:torch.Size([1, 32, 28, 28]),p4.shape:torch.Size([1, 32, 28, 28])
  40. p1.shape:torch.Size([1, 128, 28, 28]),p2.shape:torch.Size([1, 192, 28, 28]),p3.shape:torch.Size([1, 96, 28, 28]),p4.shape:torch.Size([1, 64, 28, 28])
  41. p1.shape:torch.Size([1, 192, 14, 14]),p2.shape:torch.Size([1, 208, 14, 14]),p3.shape:torch.Size([1, 48, 14, 14]),p4.shape:torch.Size([1, 64, 14, 14])
  42. p1.shape:torch.Size([1, 160, 14, 14]),p2.shape:torch.Size([1, 224, 14, 14]),p3.shape:torch.Size([1, 64, 14, 14]),p4.shape:torch.Size([1, 64, 14, 14])
  43. p1.shape:torch.Size([1, 128, 14, 14]),p2.shape:torch.Size([1, 256, 14, 14]),p3.shape:torch.Size([1, 64, 14, 14]),p4.shape:torch.Size([1, 64, 14, 14])
  44. p1.shape:torch.Size([1, 112, 14, 14]),p2.shape:torch.Size([1, 288, 14, 14]),p3.shape:torch.Size([1, 64, 14, 14]),p4.shape:torch.Size([1, 64, 14, 14])
  45. p1.shape:torch.Size([1, 256, 14, 14]),p2.shape:torch.Size([1, 320, 14, 14]),p3.shape:torch.Size([1, 128, 14, 14]),p4.shape:torch.Size([1, 128, 14, 14])
  46. p1.shape:torch.Size([1, 256, 7, 7]),p2.shape:torch.Size([1, 320, 7, 7]),p3.shape:torch.Size([1, 128, 7, 7]),p4.shape:torch.Size([1, 128, 7, 7])
  47. p1.shape:torch.Size([1, 384, 7, 7]),p2.shape:torch.Size([1, 384, 7, 7]),p3.shape:torch.Size([1, 128, 7, 7]),p4.shape:torch.Size([1, 128, 7, 7])
  48. torch.Size([1, 1024])
  49. output shape: torch.Size([1, 10])

可以看到,每一路的大小都输出来了,最终得到分类结果。这里要注意一点,之前的Inception模块中的按通道拼接操作,由于这里加入了batch_size,所以合并的torch.cat函数dim=1,也就是按维度1来进行拼接。

下面我们来测试一下,尝试一下该数据集在cifar-10上的训练效果:

根据我们上一次在VGG中阐述的批量归一化层的作用,我们同样在Inception模块中引入批量归一化层:

  1. # 构建inception模块
  2. class Inception(nn.Module):
  3. # c1,c2,c3,c4代码每一路输出的通道数,这些通道最后进行拼接成为最终输出
  4. def __init__(self, in_channels, c1, c2, c3, c4) -> None:
  5. super(Inception, self).__init__()
  6. # 线路1,1x1卷积层
  7. self.p1 = nn.Sequential(nn.Conv2d(in_channels, c1, kernel_size=1), nn.BatchNorm2d(c1), nn.ReLU())
  8. # 线路2,1x1卷积层
  9. self.p2_1 = nn.Sequential(nn.Conv2d(in_channels, c2[0], kernel_size=1), nn.BatchNorm2d(c2[0]), nn.ReLU())
  10. # 线路2,3x3卷积层,填充1
  11. self.p2_2 = nn.Sequential(nn.Conv2d(c2[0], c2[1], kernel_size=3, padding=1), nn.BatchNorm2d(c2[1]), nn.ReLU())
  12. # 线路3,1x1卷积层
  13. self.p3_1 = nn.Sequential(nn.Conv2d(in_channels, c3[0], kernel_size=1), nn.BatchNorm2d(c3[0]), nn.ReLU())
  14. # 线路3,5x5卷积层
  15. self.p3_2 = nn.Sequential(nn.Conv2d(c3[0], c3[1], kernel_size=5, padding=2), nn.BatchNorm2d(c3[1]), nn.ReLU())
  16. # 线路4,3x3池化层
  17. self.p4_1 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) # MaxPool2d的stride默认跟kernel_size一样
  18. # 线路4,1x1卷积层
  19. self.p4_2 = nn.Sequential(nn.Conv2d(in_channels, c4, kernel_size=1), nn.BatchNorm2d(c4), nn.ReLU())
  20. def forward(self, x):
  21. p1 = self.p1(x)
  22. p2 = self.p2_2(self.p2_1(x))
  23. p3 = self.p3_2(self.p3_1(x))
  24. p4 = self.p4_2(self.p4_1(x))
  25. #print("p1.shape:{},p2.shape:{},p3.shape:{},p4.shape:{}".format(p1.shape, p2.shape, p3.shape, p4.shape))
  26. # 在通道维度上拼接
  27. return torch.cat((p1, p2, p3, p4), dim=1)

然后,用cifar-10数据集进行训练,我将cifar-10数据集放大至224x224进行训练。

  1. # 加载cifar数据集
  2. import torchvision.datasets as datasets
  3. from torchvision.transforms.functional import InterpolationMode
  4. transform = transforms.Compose([transforms.Resize((224, 224), interpolation=InterpolationMode.BICUBIC),
  5. transforms.ToTensor(),
  6. transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.247, 0.2435, 0.2616]) # 此为训练集上的均值与方差
  7. ])
  8. train_dataset_cifar = datasets.CIFAR10('./data', train=True, transform=transform, download=True)
  9. test_dataset_cifar = datasets.CIFAR10('./data', train=False, transform=transform, download=True)
  10. train_loader_cifar = torch.utils.data.DataLoader(dataset=train_dataset_cifar, batch_size=24, shuffle=True)
  11. test_loader_cifar = torch.utils.data.DataLoader(dataset=test_dataset_cifar, batch_size=24, shuffle=False)
  12. train(net, train_loader_cifar, test_loader_cifar, lr=0.01, epochs=10)

训练和测试结果如下:

  1. Epoch 1, Loss: 1.4117, Time 00:04:44
  2. Epoch 2, Loss: 0.8174, Time 00:04:42
  3. Epoch 3, Loss: 0.5929, Time 00:05:07
  4. Epoch 4, Loss: 0.4710, Time 00:05:05
  5. Epoch 5, Loss: 0.3846, Time 00:05:13
  6. Epoch 6, Loss: 0.3080, Time 00:05:18
  7. Epoch 7, Loss: 0.2601, Time 00:05:03
  8. Epoch 8, Loss: 0.2079, Time 00:04:58
  9. Epoch 9, Loss: 0.1662, Time 00:04:50
  10. Epoch 10, Loss: 0.1411, Time 00:04:50
  11. Test Accuracy: 83.74%

可以看到,仅仅训练了10个epoch,测试精度就达到了83.74%。下次我们来看看微软亚洲研究院何凯明大神等人提出的一个里程碑式的卷积神经网络:残差网络ResNet。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/271594
推荐阅读
相关标签
  

闽ICP备14008679号