一、Mnist数据集介绍
数据集的内容:包含0-9的手写数字
数据集的数量:60000个训练集/10000个测试集
数据集的格式:28*28
数据集通道数:灰度图(单通道通道)
二、LeNet七层模型
1、C1卷积层:6个卷积核,大小为5*5,激活函数ReLu
1
| model.add(Conv2D(6, kernel_size = (5,5), activation = 'relu', input_shape = (28,28,1)))
|
2、S2池化层:最大池化
1
| model.add(MaxPooling2D(pool_size = (2,2)))
|
3、C3卷积层:16个卷积核,大小为5*5,激活函数ReLu
1
| model.add(Conv2D(16, kernel_size = (5,5), activation = 'relu'))
|
4、S4池化层:最大池化
1
| model.add(MaxPooling2D(pool_size = (2,2)))
|
5、C5全连接层:参数扁平化,在LeNet5称之为卷积层,实际上这一层是一维向量,和全连接层一样
1 2
| model.add(Flatten()) model.add(Dense(120, activation = 'relu'))
|
6、F6全连接层:输出节点为84个
1
| model.add(Dense(84, activation = 'relu'))
|
7、Output输出层:用softmax激活函数计算分类概率
1
| model.add(Dense(10, activation = 'softmax'))
|
三、模型训练
1、数据加载
1
| (train_x,train_y),(test_x,test_y) = mnist.load_data()
|
2、输入数据为mnist数据集
1 2 3 4
| train_x = train_x.reshape(train_x.shape[0], 28, 28, 1) / 255 test_x = test_x.reshape(test_x.shape[0], 28, 28, 1) / 255 train_y = keras.utils.to_categorical(train_y, 10) test_y = keras.utils.to_categorical(test_y, 10)
|
3、设置损失函数和优化器配置
1
| model.compile(loss = keras.metrics.categorical_crossentropy, optimizer = keras.optimizers.Adam(), metrics = ['accuracy'])
|
4、传入训练数据进行训练
1
| model.fit(train_x, train_y, batch_size = 128, epochs = 5, verbose = 1, validation_data = (test_x,test_y))
|
5、对结果进行评估
1
| score = model.evaluate(test_x, test_y)
|
四、训练结果及准确率