MINIST手写数字识别——06.总结

MINIST手写数字识别——06.总结

训练之后,检查模型的预测准确度。用 MNIST 训练的时候,一般 softmax 回归模型的分类准确率为约为 92.64%,多层感知器为98.15%,卷积神经网络可以达到 99.01%。

应用模型

可以使用训练好的模型对手写体数字图片进行分类,下面程序展示了如何使用训练好的模型进行推断。

生成预测输入数据

infer_3.png 是数字 3 的一个示例图像。把它变成一个 numpy 数组以匹配数据feed格式。

import os
import numpy as np
import tensorflow as tf
from PIL import Image # 导入图像处理模块
import matplotlib.pyplot as plt
# 打印 infer_3.png 这张图片
filename = 'infer_3.png'
img=Image.open(os.getcwd() + '/images/' + filename)
plt.imshow(img)

在这里插入图片描述

def load_image(file):
    im = Image.open(file).convert('L')
    im = im.resize((28, 28), Image.ANTIALIAS)
    im = np.array(im).reshape(1, 28, 28, 1).astype(np.float32)
    im = im / 255.0 * 2.0 - 1.0
    return im

tensor_img = load_image(os.getcwd() + '/images/' + filename)
# tensor_img

模型预测

model_dir = "./mnist/cnn-model/"
model_name = 'keras_mnist.h5'
model_path = os.path.join(model_dir, model_name)
print('CNN trained model at %s ' % model_path)

CNN trained model at ./mnist/cnn-model/keras_mnist.h5

mnist_model = tf.keras.models.load_model(model_path)
pred_results = mnist_model.predict(tensor_img)
lab = np.argsort(pred_results)
lab

array([[0, 8, 6, 4, 1, 9, 2, 5, 7, 3]])

print("Inference result of infer_3.png is: %d" % lab[0][-1])

Inference result of infer_3.png is: 3

预测结果

如果顺利,预测结果输入如下:
Inference result of infer_3.png is: 3 , 说明我们的网络成功的识别出了这张图片!

总结

本案例的softmax回归、多层感知机和卷积神经网络是最基础的深度学习模型,很多复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的softmax回归变换到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层具有局部连接和共享权重的特性。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本教程还介绍了Tensorflow 2模型搭建的基本流程,从网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务了。

猜你喜欢

转载自blog.csdn.net/qq_43299522/article/details/108771027