Mnist手写体识别实验报告

实验(二)

代码段:

# 实验环境:MindSpore-python3.7-aarch64

import os
# os.environ['DEVICE_ID'] = '0'

import mindspore as ms
import mindspore.context as context
import mindspore.dataset.transforms.c_transforms as C
import mindspore.dataset.vision.c_transforms as CV

from mindspore import nn
from mindspore.train import Model
from mindspore.train.callback import LossMonitor

context.set_context(mode=context.GRAPH_MODE, device_target='Ascend') # Ascend, CPU, GPU

data_train = os.path.join("MNIST", 'train') # train set
data_test = os.path.join("MNIST", 'test') # test set
ds = ms.dataset.MnistDataset(data_train)
print(data_train)

Mnist手写体识别实验报告
以上在华为云上不断报错,而且无法排除错误,于是我参考了网上视频教程,使用keras(基于tensorflow框架)第三方库在本地电脑上运行:

# 准备工作:

pip install keras
pip install tensorflow

第一次代码:

# 实验环境:pycharm, python 3.8
# 框架:tensorflow

from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist # 导入数据集
import matplotlib.pyplot as plt

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 下载数据集
# print(train_images.shape, test_images.shape) # 打印输出形状
# print(train_images[0])
# print(train_labels[0])
# plt.imshow(train_images[0])
# plt.show()

# 将二维数据铺开成一维
train_images = train_images.reshape((60000, 28*28)).astype('float') # 784,输入层有784个神经元
test_images = test_images.reshape((10000, 28*28)).astype('float')
# 标签值编码
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# print(train_labels[0])

# 搭建神经网络
# 输入层28*28个神经元,隐藏层15个神经元,输出层10个神经元(0~9)
network = models.Sequential() # 序列式模型
network.add(layers.Dense(units=15, activation='relu', input_shape=(28*28, ), )) # 隐藏层
# Dense:全连接层
# units:15个神经元
# 激活函数:ReLu(sigmoid和tanh会产生梯度弥散现象,自变量很大时,图像很平缓,梯度下降十分缓慢)
network.add(layers.Dense(units=10, activation='softmax')) # 输出层
# 激活函数选择softmax,输出为概率值

# 训练神经网络
# 编译:确定优化器(学习率/梯度下降步长)和损失函数等
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# epochs表示训练多少个回合,batch_size表示每次训练给多大的数据
network.fit(train_images, train_labels, epochs=20, batch_size=128, verbose=2)
# 经过20轮训练,训练集准确率达到80%
# print(network.summary())

# 用模型进行预测
# y_pre = network.predict(test_images[:5])
# print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)
print("test_loss: ", test_loss, "          test_accuracy: ", test_accuracy)

加载完数据输出第一张图片预览:
Mnist手写体识别实验报告

对标签值进行编码:
Mnist手写体识别实验报告

神经网络的训练结构:
Mnist手写体识别实验报告

第一次训练集结果:
Mnist手写体识别实验报告

第一次测试集结果:
Mnist手写体识别实验报告

第一次测试输出的结果准确率为89.85%,不过为什么只有313张测试图片?

然后按照教程对模型进行改进:

首先将原来隐藏层的神经元数量增加至128,同时再增加一层神经元数量为32的隐藏层。

第二次代码:

from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist # 导入数据集
import matplotlib.pyplot as plt

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 下载数据集
# print(train_images.shape, test_images.shape) # 打印输出形状
# print(train_images[0])
# print(train_labels[0])
# plt.imshow(train_images[0])
# plt.show()

# 将二维数据铺开成一维
train_images = train_images.reshape((60000, 28*28)).astype('float') # 784,输入层有784个神经元
test_images = test_images.reshape((10000, 28*28)).astype('float')
# 标签值编码
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# print(train_labels[0])

# 搭建神经网络
# 输入层28*28个神经元,隐藏层15个神经元,输出层10个神经元(0~9)
network = models.Sequential() # 序列式模型
network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ), )) # 隐藏层
# Dense:全连接层
# units:15个神经元
# 激活函数:ReLu(sigmoid和tanh会产生梯度弥散现象,自变量很大时,图像很平缓,梯度下降十分缓慢)
network.add(layers.Dense(units=32, activation='relu')) # 第二层隐藏层
network.add(layers.Dense(units=10, activation='softmax')) # 输出层
# 激活函数选择softmax,输出为概率值

# 训练神经网络
# 编译:确定优化器(学习率/梯度下降步长)和损失函数等
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# epochs表示训练多少个回合,batch_size表示每次训练给多大的数据
network.fit(train_images, train_labels, epochs=20, batch_size=128, verbose=2)
# 经过20轮训练,训练集准确率达到80%
# print(network.summary())

# 用模型进行预测
# y_pre = network.predict(test_images[:5])
# print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)
print("test_loss: ", test_loss, "          test_accuracy: ", test_accuracy)

测试结果:
Mnist手写体识别实验报告
相同情况下,训练集准确率提升至97.23%(过拟合),测试集准确率提升至95.91%,同时运行时长也增加了。
为了解决过拟合的问题,在每一层隐藏层都加入正则化和Dropout方法。

第三次代码:

from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist # 导入数据集
import matplotlib.pyplot as plt

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 下载数据集
# print(train_images.shape, test_images.shape) # 打印输出形状
# print(train_images[0])
# print(train_labels[0])
# plt.imshow(train_images[0])
# plt.show()

# 将二维数据铺开成一维
train_images = train_images.reshape((60000, 28*28)).astype('float') # 784,输入层有784个神经元
test_images = test_images.reshape((10000, 28*28)).astype('float')
# 标签值编码
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# print(train_labels[0])

# 搭建神经网络
# 输入层28*28个神经元,隐藏层15个神经元,输出层10个神经元(0~9)
network = models.Sequential() # 序列式模型
network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),
                         kernel_regularizer=regularizers.l1(0.0001))) # 隐藏层
# 正则化消除过拟合问题
# Dense:全连接层
# units:15个神经元
# 激活函数:ReLu(sigmoid和tanh会产生梯度弥散现象,自变量很大时,图像很平缓,梯度下降十分缓慢)
network.add(layers.Dropout(0.01)) # 0.01的概率使神经元丧失功能
network.add(layers.Dense(units=32, activation='relu', 
                         kernel_regularizer=regularizers.l1(0.0001))) # 第二层隐藏层
network.add(layers.Dropout(0.01)) # 0.01的概率使神经元丧失功能
network.add(layers.Dense(units=10, activation='softmax')) # 输出层
# 激活函数选择softmax,输出为概率值

# 训练神经网络
# 编译:确定优化器(学习率/梯度下降步长)和损失函数等
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# epochs表示训练多少个回合,batch_size表示每次训练给多大的数据
network.fit(train_images, train_labels, epochs=20, batch_size=128, verbose=2)
# 经过20轮训练,训练集准确率达到80%
# print(network.summary())

# 用模型进行预测
# y_pre = network.predict(test_images[:5])
# print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)
print("test_loss: ", test_loss, "          test_accuracy: ", test_accuracy)

现在过拟合现象在大多数情况下消失了:
Mnist手写体识别实验报告
最后再尝试使用简单的卷积神经网络模型:

第四次代码:

from keras.utils import to_categorical
from keras import models, layers, regularizers
from keras.optimizers import RMSprop
from keras.datasets import mnist # 导入数据集
import matplotlib.pyplot as plt

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 下载数据集
# print(train_images.shape, test_images.shape) # 打印输出形状
# print(train_images[0])
# print(train_labels[0])
# plt.imshow(train_images[0])
# plt.show()

# 将二维数据铺开成一维
# train_images = train_images.reshape((60000, 28*28)).astype('float') # 784,输入层有784个神经元
# test_images = test_images.reshape((10000, 28*28)).astype('float')
# # 标签值编码
# train_labels = to_categorical(train_labels)
# test_labels = to_categorical(test_labels)
# print(train_labels[0])

# # 搭建神经网络
# # 输入层28*28个神经元,隐藏层15个神经元,输出层10个神经元(0~9)
# network = models.Sequential() # 序列式模型
# network.add(layers.Dense(units=128, activation='relu', input_shape=(28*28, ),
#                          kernel_regularizer=regularizers.l1(0.0001))) # 隐藏层
# # 正则化消除过拟合问题
# # Dense:全连接层
# # units:15个神经元
# # 激活函数:ReLu(sigmoid和tanh会产生梯度弥散现象,自变量很大时,图像很平缓,梯度下降十分缓慢)
# network.add(layers.Dropout(0.01)) # 0.01的概率使神经元丧失功能
# network.add(layers.Dense(units=32, activation='relu',
#                          kernel_regularizer=regularizers.l1(0.0001))) # 第二层隐藏层
# network.add(layers.Dropout(0.01)) # 0.01的概率使神经元丧失功能
# network.add(layers.Dense(units=10, activation='softmax')) # 输出层
# # 激活函数选择softmax,输出为概率值

# 尝试使用卷积神经网络
def Lenet():
    network = models.Sequential()
    network.add(layers.Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
    network.add(layers.AveragePooling2D((2, 2)))
    network.add(layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
    network.add(layers.AveragePooling2D((2, 2)))
    network.add(layers.Conv2D(filters=120, kernel_size=(3, 3), activation='relu'))
    network.add(layers.Flatten())
    network.add(layers.Dense(84, activation='relu'))
    network.add(layers.Dense(10, activation='softmax'))
    return network

network = Lenet()

# 训练神经网络
# 编译:确定优化器(学习率/梯度下降步长)和损失函数等
network.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

train_images = train_images.reshape((60000, 28, 28, 1)).astype('float') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# epochs表示训练多少个回合,batch_size表示每次训练给多大的数据
network.fit(train_images, train_labels, epochs=10, batch_size=128, verbose=2)
# 经过20轮训练,训练集准确率达到80%
# print(network.summary())

# 用模型进行预测
# y_pre = network.predict(test_images[:5])
# print(y_pre, test_labels[:5])
test_loss, test_accuracy = network.evaluate(test_images, test_labels)
print("test_loss: ", test_loss, "          test_accuracy: ", test_accuracy)

在这个简单的卷积神经网络模型上面,训练回合减少到了一半,而运行时间延长了不少,不过最终的测试结果准确率可以达到约99%:

Mnist手写体识别实验报告

上一篇:Angular Component 延迟加载 Lazy Load 的一个依赖注入的问题以及解决方案


下一篇:[CSS]10.背景图像应用