python机器学习之识别自己的手写数字
#关于神经网络的手写体分类并测试import tensorflow as tffrom PIL import Imageimport numpy as npmodel_path="/home/cc1997/Desktop/model_save/mnist.ckpt"model=tf.keras.models.Sequential([tf.keras.layers.Flatten(),tf.kera
·
关于神经网络的手写体分类并测试,采用的著名的手写数据集mnist,这里面没有采用卷积层,但也得到了不错的效果,大家可以将自己手写的数字图图片保存在下面程序的image_path处,程序就会识别0-9的数字内容。
import tensorflow as tf
from PIL import Image
import numpy as np
model_path="/home/Desktop/model_save/mnist.ckpt"
model=tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation="relu"),
tf.keras.layers.Dense(10,activation="softmax")
])
model.load_weights(model_path)
preNum=1
for i in range(preNum):
#将手写数字体的图片放在这个位置。
image_path="/home/cc1997/Desktop/pictureTest/example.png"
img=Image.open(image_path)
img=img.resize((28,28),Image.ANTIALIAS)
img_arr=np.array(img.convert("L"))
img_arr=255-img_arr
img_arr=img_arr/255
x_predict=img_arr[tf.newaxis,...]
result=model.predict(x_predict)
pred=tf.argmax(result,axis=1)
print("\n")
print("预测的结果为:")
tf.print(pred)
更多推荐
所有评论(0)