相关理论&故事背景书上都有,不打算复述一遍,这里仅作代码记录&分享
此处非直接可用代码,由于学习过程中多次使用相同函数,故而将一些常用函数整理成了工具包,MxNet学习——自定义工具包
两者结合,方可运行代码
# -------------------------------------------------------------------------------
# Description: 深度卷积神经网络 AlexNet
# Description: 它是浅层神经网络和深度神经网络的分界线
# Description: 它与LeNet的区别:1、deeper,在LeNet基础上增加了3个卷积层 2、sigmoid激活函数替换成了ReLU
# Description: 3、采用了dropout 4、数据增强(翻转、裁剪、颜色变化)
# Reference:
# Author: Sophia
# Date: 2021/3/11
# -------------------------------------------------------------------------------
from IPython import display
from mxnet import autograd, nd, init, gluon, image
from mxnet.gluon import data as gdata, loss as gloss, nn
import random, sys, time, matplotlib.pyplot as plt, mxnet as mx, os
from plt_so import *
net = nn.Sequential()
net.add(
# 第一阶段
nn.Conv2D(channels=96, kernel_size=11, strides=4, activation='relu'),
nn.MaxPool2D(pool_size=3, strides=2),
# 第二阶段:减小卷积窗口,采用填充来使输入输出大小一致,此外增加了输出通道数
nn.Conv2D(channels=256, kernel_size=5, padding=2, activation='relu'),
nn.MaxPool2D(pool_size=3, strides=2),
# 第三阶段:3、4卷积层进一步增大了输出通道数,未采用池化层,第3、4、5个卷积层都采用了更小的卷积窗口
nn.Conv2D(channels=384, kernel_size=3, padding=1, activation='relu'),
nn.Conv2D(channels=384, kernel_size=3, padding=1, activation='relu'),
nn.Conv2D(channels=256, kernel_size=3, padding=1, activation='relu'),
nn.MaxPool2D(pool_size=3, strides=2),
# 第四阶段:采用丢弃法来缓解过拟合
# net.add(nn.Flatten())
nn.Dense(4096, activation='relu'), nn.Dropout(0.5),
# 第五阶段:采用丢弃法来缓解过拟合
nn.Dense(4096, activation='relu'), nn.Dropout(0.5),
# 第六阶段
nn.Dense(10) # 有多少类,就输出多大
)
# X = nd.random.uniform(shape=(1, 1, 224, 224))
# net.initialize()
# print(net)
# 输出:
# Sequential(
# (0): Conv2D(None -> 96, kernel_size=(11, 11), stride=(4, 4), Activation(relu))
# (1): MaxPool2D(size=(3, 3), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)
# (2): Conv2D(None -> 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), Activation(relu))
# (3): MaxPool2D(size=(3, 3), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)
# (4): Conv2D(None -> 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), Activation(relu))
# (5): Conv2D(None -> 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), Activation(relu))
# (6): Conv2D(None -> 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), Activation(relu))
# (7): MaxPool2D(size=(3, 3), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)
# (8): Dense(None -> 4096, Activation(relu))
# (9): Dropout(p = 0.5, axes=())
# (10): Dense(None -> 4096, Activation(relu))
# (11): Dropout(p = 0.5, axes=())
# (12): Dense(None -> 10, linear)
# )
# for layer in net:
# X = layer(X)
# print(layer.name, 'output shape:\t', X.shape)
# 输出:
# conv0 output shape: (1, 96, 54, 54)
# pool0 output shape: (1, 96, 26, 26)
# conv1 output shape: (1, 256, 26, 26)
# pool1 output shape: (1, 256, 12, 12)
# conv2 output shape: (1, 384, 12, 12)
# conv3 output shape: (1, 384, 12, 12)
# conv4 output shape: (1, 256, 12, 12)
# pool2 output shape: (1, 256, 5, 5)
# dense0 output shape: (1, 4096)
# dropout0 output shape: (1, 4096)
# dense1 output shape: (1, 4096)
# dropout1 output shape: (1, 4096)
# dense2 output shape: (1, 10)
'''
读取数据集
'''
batch_size = 128
train_iter, test_iter = load_data_fashion_mnist_ch5(batch_size, resize=224)
'''
训练模型
'''
lr, num_epochs, ctx = 0.01, 5, try_gpu()
net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr})
train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)