NiNNet是2014提出的一个网络结构,NinNet有这些特征:
1) 使用alobal average pool替代fc层
2) 使用kernel_size=1的conv层来实现各个channel之间的像素级别的fc
使用mxnet进行一下代码演示。mxnet的安装方法
pip install d2l==0.14.3
pip install -U mxnet-cu101mkl==1.6.0.post0
pip install gluoncv
创建一个VGGNet中的标准块
from d2l import mxnet as d2l
from mxnet import np, npx
from mxnet.gluon import nn
npx.set_np()
def nin_block(num_channels, kernel_size, strides, padding):
blk = nn.Sequential()
blk.add(nn.Conv2D(num_channels, kernel_size, strides, padding,
activation='relu'),
nn.Conv2D(num_channels, kernel_size=1, activation='relu'),
nn.Conv2D(num_channels, kernel_size=1, activation='relu'))
return blk
VGG网络和NiNNet的比较:
NiN网络是在AlexNet之后不久提出的,网络的设计参考了alexNet,NiN使用conv形状为11×11,5×5和3×3的卷积层,相应的输出通道数与AlexNet中的相同。 每个NiN块之后是步幅为2且窗口形状为3×3的max pooling层。NiN和AlexNet之间的一个重要区别是NiN没有全连接层。NiN使用NiN块,其输出通道的数量等于标签类的数量,然后是全局平均池层,NiN设计的优点之一是可以大大减少所需模型参数的数量。 但是这种设计会增加模型训练时间。下面是NiNNet的结构代码
net = nn.Sequential()
net.add(nin_block(96, kernel_size=11, strides=4, padding=0),
nn.MaxPool2D(pool_size=3, strides=2),
nin_block(256, kernel_size=5, strides=1, padding=2),
nn.MaxPool2D(pool_size=3, strides=2),
nin_block(384, kernel_size=3, strides=1, padding=1),
nn.MaxPool2D(pool_size=3, strides=2),
nn.Dropout(0.5),
# There are 10 label classes
nin_block(10, kernel_size=3, strides=1, padding=1),
# The global average pooling layer automatically sets the window shape
# to the height and width of the input
nn.GlobalAvgPool2D(),
# Transform the four-dimensional output into two-dimensional output
# with a shape of (batch size, 10)
nn.Flatten())
测试一下输出shape
X = np.random.uniform(size=(1, 1, 224, 224))
net.initialize()
for layer in net:
X = layer(X)
print(layer.name, 'output shape:\t', X.shape)
sequential1 output shape: (1, 96, 54, 54)
pool0 output shape: (1, 96, 26, 26)
sequential2 output shape: (1, 256, 26, 26)
pool1 output shape: (1, 256, 12, 12)
sequential3 output shape: (1, 384, 12, 12)
pool2 output shape: (1, 384, 5, 5)
dropout0 output shape: (1, 384, 5, 5)
sequential4 output shape: (1, 10, 5, 5)
pool3 output shape: (1, 10, 1, 1)
flatten0 output shape: (1, 10)
加载fashion_mnist训练数据
batch_size=128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
训练代码
lr, num_epochs = 0.1, 10
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr)
在T4下训练大概30分钟就可以完成了
训练结果
loss 0.616, train acc 0.760, test acc 0.773
3009.7 examples/sec on gpu(0)
最后的话:
这篇文章发布在CSDN/蓝色的杯子, 没事多留言,让我们一起爱智求真吧.我的邮箱wisdomfriend@126.com.