为了方便之後丢多点图进行测试,我将图片放进了 img 资料夹,并使用 glob 获得图片列表。
同时改了印出格式,方便转换为 markdown 表格。
import tensorflow as tf
saved_model_path = "mnist"
# 读取模型
model = tf.keras.models.load_model(saved_model_path)
# 显示模型资讯
model.summary()
import cv2
import glob
def readimg(imgpath):
img = cv2.imread(imgpath)
imggray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
resized = cv2.resize(imggray, (28, 28))
return 1 - resized / 255.0
imgfiles = glob.glob("img/*.png")
imgs = [readimg(i) for i in imgfiles]
results = model(tf.constant(imgs))
s = [imgfiles[i] + "\t| " + "\t| ".join([ str(round(float(j), 2)) for j in results[i]]) for i in range(len(imgfiles))]
head = "| {} |".format("|".join([" " + str(i) + " " for i in range(-1, 10)]))
tablesep = " ------ ".join(["|"] * 10)
table = ["| {} \t| {} |".format(imgfiles[i], "\t| ".join([ str(round(float(j), 2)) for j in results[i]])) for i in range(len(imgfiles))]
print(head)
print(tablesep)
print(*table, sep="\n")
追加了几张图进行测试,结果如下
-1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
0.png | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
1.png | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
2.png | 0.0 | 0.26 | 0.53 | 0.03 | 0.01 | 0.03 | 0.05 | 0.04 | 0.05 | 0.0 |
3.png | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
4.png | 0.0 | 0.0 | 0.01 | 0.0 | 0.96 | 0.02 | 0.0 | 0.0 | 0.01 | 0.0 |
5.png | 0.0 | 0.01 | 0.31 | 0.03 | 0.12 | 0.47 | 0.05 | 0.0 | 0.0 | 0.0 |
6.png | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.86 | 0.13 | 0.0 | 0.0 | 0.0 |
7.png | 0.0 | 0.32 | 0.12 | 0.08 | 0.0 | 0.0 | 0.0 | 0.48 | 0.0 | 0.0 |
8.png | 0.0 | 0.0 | 0.01 | 0.35 | 0.0 | 0.0 | 0.07 | 0.0 | 0.57 | 0.0 |
9.png | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 |
不确定是我画得太丑还是滑鼠绘的不像手绘,有些数字非常的不准确。
看一下 mnist 的画风长什麽样子。
也许是因为我画的不够潦草...
前言 前一天我们在 K8s Cluster 建置好了 ArgoCD 服务,今天就来实际操作看看,透过...
工欲善其事,必先利其器,插件是Coding效率提升的利器 今天要来介绍的主角是Visual Stud...
在测试这项范例之前我一直搞不懂在过去测试的时候我记得 test db 不会清除资料,後来查资料才发现...
鬼故事 - 印表机最终还是挂了 https://twitter.com/System32Comics...
嘿~昨天的内容还行吗? 「兔兔,昨天突然就接收到大量语法!」 齁,这个我就要说声不好意思~ 原本是...