主题
卷积神经网络(高级篇)
总结:Inception
-
通常的神经网络是串行结构的,但也有并行结构的神经网络。
-
Inception Module:思想上与盗梦空间中梦的嵌套类似。提供候选的卷积算子,类似网络结构搜索。
-
1*1的卷积可以改变通道数量,减少参数量。
参数量.png
代码:Inception
在__init__()
中实例化模型的节点
在forward()
中建立从输入到输入的数据流

每个网络分支都有对应于在
__init()__
与forward()
之中的代码。
class InceptionNet(nn.Module):
def __init__(self, in_channels):
super(InceptionNet, self).__init__()
self.y1_1_conv2d_1 = nn.Conv2d(in_channels, 24, kernel_size=1)
self.y1_2_avg_pool2d = nn.AvgPool2d(kernel_size=3, stride=1, padding=1)
self.y2_conv2d_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
self.y3_1_conv2d_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
self.y3_2_conv2d_5 = nn.Conv2d(16, 24, kernel_size=5, padding=2)
self.y4_1_conv2d_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
self.y4_2_conv2d_3 = nn.Conv2d(16, 24, kernel_size=3, padding=1)
self.y4_3_conv2d_3 = nn.Conv2d(24, 24, kernel_size=3, padding=1)
def forward(self, x):
y1 = self.y1_2_avg_pool2d(self.y1_1_conv2d_1(x))
y2 = self.y2_conv2d_1(x)
y3 = self.y3_2_conv2d_5(self.y3_1_conv2d_1(x))
y4 = self.y4_3_conv2d_3(self.y4_2_conv2d_3(self.y4_1_conv2d_1(x)))
y = [y1, y2, y3, y4]
return torch.cat(y, dim=1)
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.mp = nn.MaxPool2d(2)
self.incep1 = InceptionNet(10)
self.incep2 = InceptionNet(20)
self.fc = nn.Linear(1408, 10)
def forward(self, x):
n = 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 = x.view(n, -1)
x = self.fc(x)
return x
[1, 300] Loss: 0.908
[1, 600] Loss: 0.195
[1, 900] Loss: 0.133
Accuracy on test set:96.68 %
[2, 300] Loss: 0.114
[2, 600] Loss: 0.098
[2, 900] Loss: 0.092
Accuracy on test set:97.80 %
[3, 300] Loss: 0.081
[3, 600] Loss: 0.078
[3, 900] Loss: 0.074
Accuracy on test set:98.02 %
[4, 300] Loss: 0.062
[4, 600] Loss: 0.062
[4, 900] Loss: 0.065
Accuracy on test set:98.30 %
[5, 300] Loss: 0.056
[5, 600] Loss: 0.057
[5, 900] Loss: 0.054
Accuracy on test set:98.16 %
[6, 300] Loss: 0.048
[6, 600] Loss: 0.055
[6, 900] Loss: 0.049
Accuracy on test set:98.36 %
[7, 300] Loss: 0.049
[7, 600] Loss: 0.041
[7, 900] Loss: 0.047
Accuracy on test set:98.63 %
[8, 300] Loss: 0.040
[8, 600] Loss: 0.042
[8, 900] Loss: 0.045
Accuracy on test set:98.77 %
[9, 300] Loss: 0.033
[9, 600] Loss: 0.040
[9, 900] Loss: 0.043
Accuracy on test set:98.65 %
[10, 300] Loss: 0.035
[10, 600] Loss: 0.037
[10, 900] Loss: 0.037
Accuracy on test set:98.85 %
-
NotImplementedError 错误原因:子类没有实现父类要求一定要实现的接口。比如继承
nn.Module
一定要实现__init__()
与forward()
,当没有实现这两个,或者因为失误方法名拼错,都会报错。 -
torch.cat()
是沿某个维度把各分支的输出拼接为一个整体。 -
Inception结构的输出通道数是一定的(例如上述为88通道),长宽是与输入保持一致的。所以设置输入的通道数in_channels为参数。
网友评论