Advanced CNN

Advanced CNN

GoogleNet

实现复杂神经网络的思想:在面向对象语言中构造类,把相同的块封装成一个类来减少代码冗余

Inception Module
在构建神经网络中,往往对于网络的超参数比较难以选择,例如卷积核的大小。
可以并行使用卷积核,神经网络能够自动进行权重的选择,找到最优的卷积组合。

  1. concarenate:把张量拼接起来,必须保证特征的高度和宽度
  2. 均值池化:
  3. 信息融合:本质就是得到的值通过某些运算得到信息
  4. 1*1卷积:常用于改变特征通道数

卷积运算参数量计算:
$192@28\times 28 $ -> $5\times 5$Convolution -> $32@28\times 28$
Operations:$5^2\times 28^2 \times 192 \times 32$

代码实现Inception Module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F

class InceptionA(nn.Module):
def __init__(self,in_channels):
super(InceptionA,self).__init__()
self.branch1x1 = nn.Conv2d(in_channels=in_channels,out_channels=16,kernel_size=1)

self.branch5x5_1 = nn.Conv2d(in_channels=in_channels,out_channels=16,kernel_size=1)
self.branch5x5_2 = nn.Conv2d(in_channels=16,out_channels=24,kernel_size=5,padding=2)

self.branch3x3_1 = nn.Conv2d(in_channels=in_channels,out_channels=16,kernel_size=1)
self.branch3x3_2 = nn.Conv2d(in_channels=16,out_channels=24,kernel_size=3,padding=1)
self.branch3x3_3 = nn.Conv2d(in_channels=24,out_channels=24,kernel_size=3,padding=1)

self.branch_pool = nn.Conv2d(in_channels=in_channels,out_channels=24,kernel_size=1)

def forward(self,x):
branch1x1 = self.branch1x1(x)

branch5x5 = self.branch5x5_1(x)
branch5x5 = self.branch5x5_2(branch5x5)

branch3x3 = self.branch3x3_1(x)
branch3x3 = self.branch3x3_2(branch3x3)
branch3x3 = self.branch3x3_3(branch3x3)

branch_pool = F.avg_pool2d(x,kernel_size=3,stride=1,padding = 1)
branch_pool = self.branch_pool(branch_pool)

out_puts = [branch1x1,branch5x5,branch3x3,branch_pool]

return torch.cat(out_puts,dim=1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(1,10,kernel_size=5)
self.conv2 = nn.Conv2d(88,20,kernel_size=5)

self.incep1 = InceptionA(in_channels=10)
self.incep2 = InceptionA(in_channels=20)

self.mp = nn.MaxPool2d(2)
self.fc = nn.Linear(1408,10)

def forward(self,x):
in_size = x.size(0)
x = F.relu(self.mp(self.conv1(x)))
x = self.incep1(x)
x = F.relu(self.mp(self.conv2(x)))
x = self.incep2(x)

x = view(in_size,-1)
x = self.fc(x)

return x
Donate comment here