linux-基于tensorflow2.x的手写数字识别-基于MNIST数据集
数据集
数据集下载MNIST
首先读取数据集, 并打印相关信息
包括
- 图像的数量, 形状
- 像素的最大, 最小值
- 以及看一下第一张图片
path = 'MNIST/mnist.npz'
with np.load(path, allow_pickle=True) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
print(f'dataset info: shape: {x_train.shape}, {y_train.shape}')
print(f'dataset info: max: {x_train.max()}')
print(f'dataset info: min: {x_train.min()}')
print("A sample:")
print("y_train: ", y_train[0])
# print("x_train: \n", x_train[0])
show_pic = x_train[0].copy()
show_pic = cv2.resize(show_pic, (28 * 10, 28 * 10))
cv2.imshow("A image sample", show_pic)
key = cv2.waitKey(0)
# 按 q 退出
if key == ord('q'):
cv2.destroyAllWindows()
print("show demo over")

转换为tf 数据集的格式, 并进行归一化
# convert to tf tensor
x_train = tf.convert_to_tensor(x_train, dtype=tf.float32) // 255.
x_test = tf.convert_to_tensor(x_test, dtype=tf.float32) // 255.
dataset_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset_train = dataset_train.batch(batch_size).repeat(class_num)
定义网络
在这里定义一个简单的全连接网络
def build_simple_net():
net = Sequential([
layers.Dense(256, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(class_num)
])
net.build(input_shape=(None, 28 * 28))
# net.summary()
return net
训练
使用 SGD 优化器进行训练
def train(print_info_step=250):
net = build_simple_net()
# 优化器
optimizer = optimizers.SGD(lr=0.01)
# 计算准确率
acc = metrics.Accuracy()
for step, (x, y) in enumerate(dataset_train):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28 * 28))
# [b, 784] => [b, 10]
out = net(x)
# [b] => [b, 10]
y_onehot = tf.one_hot(y, depth=class_num)
# [b, 10]
loss = tf.square(out - y_onehot)
# [b]
loss = tf.reduce_sum(loss) / batch_size
# 反向传播
acc.update_state(tf.argmax(out, axis=1), y)
grads = tape.gradient(loss, net.trainable_variables)
optimizer.apply_gradients(zip(grads, net.trainable_variables))
if acc.result() >= 0.90:
net.save_weights(save_path)
print(f'final acc: {acc.result()}, total step: {step}')
break
if step % print_info_step == 0:
print(f'step: {step}, loss: {loss}, acc: {acc.result().numpy()}')
acc.reset_states()
if step % 500 == 0 and step != 0:
print('save model')
net.save_weights(save_path)
验证
验证在测试集的模型效果, 这里仅取出第一张进行验证
def test_dataset():
net = build_simple_net()
# 加载模型
net.load_weights(save_path)
# 拿到测试集第一张图片
pred_image = x_test[0]
pred_image = tf.reshape(pred_image, (-1, 28 * 28))
pred = net.predict(pred_image)
# print(pred)
print(f'pred: {tf.argmax(pred, axis=1).numpy()}, label: {y_test[0]}')

应用
分割手写数字, 并进行逐一识别
- 先将图像二值化
- 找到轮廓
- 得到数字的坐标
- 转为模型的需要的输入格式, 并进行识别
- 显示
def split_number(img):
result = []
net = build_simple_net()
# 加载模型
net.load_weights(save_path)
image = cv2.cvtColor(img.copy(), cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(image, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
for cnt in contours[:-1]:
x, y, w, h = cv2.boundingRect(cnt)
image = img[y:y+h, x:x+w]
image = cv2.resize(image, (28, 28))
pred_image = tf.convert_to_tensor(image, dtype=tf.float32) / 255.
pred_image = tf.reshape(pred_image, (-1, 28 * 28))
pred = net.predict(pred_image)
out = tf.argmax(pred, axis=1).numpy()
result = [out[0]] + result
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("demo", img)
print(result)
k = cv2.waitKey(0)
# 按 q 退出
if k == ord('q'):
pass
cv2.destroyAllWindows()
效果
单数字

多数字


附录
所有代码, 文件 tf2_mnist.py
import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, Sequential, optimizers, metrics
# 屏蔽通知信息和警告信息
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 每批几张图片
batch_size = 2
# 类别数
class_num = 10
# 保存模型的路径
save_path = "./models/mnist.ckpt"
# 展示样例
show_demo = False
# 验证测试集
evaluate_dataset = False
# 是否训练
run_train = False
# 图片路径, 仅用于 detect_image(), 当为False时不识别
image_path = 'images/36.png'
path = 'MNIST/mnist.npz'
with np.load(path, allow_pickle=True) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
if show_demo:
print(f'dataset info: shape: {x_train.shape}, {y_train.shape}')
print(f'dataset info: max: {x_train.max()}')
print(f'dataset info: min: {x_train.min()}')
print("A sample:")
print("y_train: ", y_train[0])
# print("x_train: \n", x_train[0])
show_pic = x_train[0].copy()
show_pic = cv2.resize(show_pic, (28 * 10, 28 * 10))
cv2.imshow("A image sample", show_pic)
key = cv2.waitKey(0)
if key == ord('q'):
cv2.destroyAllWindows()
print("show demo over")
# convert to tf tensor
x_train = tf.convert_to_tensor(x_train, dtype=tf.float32) // 255.
x_test = tf.convert_to_tensor(x_test, dtype=tf.float32) // 255.
dataset_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset_train = dataset_train.batch(batch_size).repeat(class_num)
def build_simple_net():
net = Sequential([
layers.Dense(256, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(class_num)
])
net.build(input_shape=(None, 28 * 28))
# net.summary()
return net
def train(print_info_step=250):
net = build_simple_net()
# 优化器
optimizer = optimizers.SGD(lr=0.01)
# 计算准确率
acc = metrics.Accuracy()
for step, (x, y) in enumerate(dataset_train):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28 * 28))
# [b, 784] => [b, 10]
out = net(x)
# [b] => [b, 10]
y_onehot = tf.one_hot(y, depth=class_num)
# [b, 10]
loss = tf.square(out - y_onehot)
# [b]
loss = tf.reduce_sum(loss) / batch_size
# 反向传播
acc.update_state(tf.argmax(out, axis=1), y)
grads = tape.gradient(loss, net.trainable_variables)
optimizer.apply_gradients(zip(grads, net.trainable_variables))
if acc.result() >= 0.90:
net.save_weights(save_path)
print(f'final acc: {acc.result()}, total step: {step}')
break
if step % print_info_step == 0:
print(f'step: {step}, loss: {loss}, acc: {acc.result().numpy()}')
acc.reset_states()
if step % 500 == 0 and step != 0:
print('save model')
net.save_weights(save_path)
def test_dataset():
net = build_simple_net()
# 加载模型
net.load_weights(save_path)
# 拿到测试集第一张图片
pred_image = x_test[0]
pred_image = tf.reshape(pred_image, (-1, 28 * 28))
pred = net.predict(pred_image)
# print(pred)
print(f'pred: {tf.argmax(pred, axis=1).numpy()}, label: {y_test[0]}')
def split_number(img):
result = []
net = build_simple_net()
# 加载模型
net.load_weights(save_path)
image = cv2.cvtColor(img.copy(), cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(image, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 2)
for cnt in contours[:-1]:
x, y, w, h = cv2.boundingRect(cnt)
image = img[y:y+h, x:x+w]
image = cv2.resize(image, (28, 28))
pred_image = tf.convert_to_tensor(image, dtype=tf.float32) / 255.
pred_image = tf.reshape(pred_image, (-1, 28 * 28))
pred = net.predict(pred_image)
out = tf.argmax(pred, axis=1).numpy()
result = [out[0]] + result
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("demo", img)
print(result)
k = cv2.waitKey(0)
if k == ord('q'):
pass
cv2.destroyAllWindows()
if __name__ == '__main__':
if run_train:
train()
elif evaluate_dataset:
test_dataset()
elif image_path:
image = cv2.imread(image_path)
# detect_image(image)
split_number(image)
linux-基于tensorflow2.x的手写数字识别-基于MNIST数据集的更多相关文章
- 基于Numpy的神经网络+手写数字识别
基于Numpy的神经网络+手写数字识别 本文代码来自Tariq Rashid所著<Python神经网络编程> 代码分为三个部分,框架如下所示: # neural network class ...
- 手写数字识别——基于LeNet-5卷积网络模型
在<手写数字识别——利用Keras高层API快速搭建并优化网络模型>一文中,我们搭建了全连接层网络,准确率达到0.98,但是这种网络的参数量达到了近24万个.本文将搭建LeNet-5网络, ...
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...
- 【TensorFlow-windows】(三) 多层感知器进行手写数字识别(mnist)
主要内容: 1.基于多层感知器的mnist手写数字识别(代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...
- TensorFlow.NET机器学习入门【5】采用神经网络实现手写数字识别(MNIST)
从这篇文章开始,终于要干点正儿八经的工作了,前面都是准备工作.这次我们要解决机器学习的经典问题,MNIST手写数字识别. 首先介绍一下数据集.请首先解压:TF_Net\Asset\mnist_png. ...
- 基于卷积神经网络的手写数字识别分类(Tensorflow)
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
- 基于多层感知机的手写数字识别(Tensorflow实现)
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...
- 吴裕雄--天生自然python机器学习:基于支持向量机SVM的手写数字识别
from numpy import * def img2vector(filename): returnVect = zeros((1,1024)) fr = open(filename) for i ...
- 【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)
博文主要内容有: 1.softmax regression的TensorFlow实现代码(教科书级的代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3 ...
随机推荐
- 3道常见的vue面试题,你都会了吗?
最近流传各大厂纷纷裁员,导致很多人"被迫"毕业,显然很多人还是想留级,无奈出现在名单中,只能感叹命运不公,不过拿了N+1,也算是很欣慰. 又得去面试了,接下来一起来巩固下vue的3 ...
- 都2022年了,HDFS为何还如此能战!
摘要:HDFS也许不是最好的大数据存储技术,但依然是最重要的大数据存储技术. 本文分享自华为云社区<HDFS为何在大数据领域经久不衰?>,作者: JavaEdge. 1.概述 1.1 简介 ...
- vue 修改单页标题 --- document.title
方法1. 在需要的组件或者页面内设置 document.title = response.data.res.title 方法2. <head> <meta http-equiv=&q ...
- Java语言学习day33--8月8日
今日内容介绍1.基本类型包装类2.System类3.Math类4.Arrays类5.大数据运算 ###01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实 ...
- Java语言学习day13--7月14日
今日内容介绍1.循环练习2.数组方法练习 ###01奇数求和练习 * A: 奇数求和练习 * a: 题目分析 * 为了记录累加和的值,我们需要定义一个存储累加和的变量 * 我们要获取到1-100范围内 ...
- 黑客入门——渗透必备神器Burpsuit的安装和简单使用教程
很多人没有听说过burp全称(BurpSuite)BurpSuite是一款白帽子,黑帽子渗透测试必备工具,通过拦截HTTP/HTTPS的web数据包,当浏览器和相关应用程序的中间人,进行拦截.修改 ...
- 从压测碰到的诡异断连问题聊聊Nginx的连接管理
本文主要分享一个在压测Nginx反向代理服务过程中碰到的连接异常断开问题,包括问题的定位与复现,最后由这个实际问题引申聊一下Nginx的连接管理. 本博客已迁移至CatBro's Blog,那是我自己 ...
- Python生成短uuid的方法
python的uuid都是32位的,比较长,处理起来效率比较低, 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62 ...
- .NET Core企业微信网页授权登录
1.开发前准备 参数获取 corpid 每个企业都拥有唯一的corpid,获取此信息可在管理后台"我的企业"-"企业信息"下查看"企业ID" ...
- IDEA打包javaFX及踩坑解决
开门见山的说,先打包,再说坑. File-->Project Structure --> Artifacts-->(此处点加号)JAR-->From modules with ...