iT邦幫忙

2021 iThome 鐵人賽

DAY 5
1
AI & Data

機器學習與前端網頁系列 第 5

Day 5 追加測試

為了方便之後丟多點圖進行測試,我將圖片放進了 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
img\0.png 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
img\1.png 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
img\2.png 0.0 0.26 0.53 0.03 0.01 0.03 0.05 0.04 0.05 0.0
img\3.png 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
img\4.png 0.0 0.0 0.01 0.0 0.96 0.02 0.0 0.0 0.01 0.0
img\5.png 0.0 0.01 0.31 0.03 0.12 0.47 0.05 0.0 0.0 0.0
img\6.png 0.0 0.0 0.0 0.0 0.0 0.86 0.13 0.0 0.0 0.0
img\7.png 0.0 0.32 0.12 0.08 0.0 0.0 0.0 0.48 0.0 0.0
img\8.png 0.0 0.0 0.01 0.35 0.0 0.0 0.07 0.0 0.57 0.0
img\9.png 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0

不確定是我畫得太醜還是滑鼠繪的不像手繪,有些數字非常的不準確。
看一下 mnist 的畫風長什麼樣子。

也許是因為我畫的不夠潦草...


上一篇
Day 4 tensorflow 模型的存取與 mnist 分類
下一篇
Day 6 追加訓練
系列文
機器學習與前端網頁30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
阿瑜
iT邦研究生 4 級 ‧ 2021-09-20 17:16:25

太酷了!!!
程式產 markdown格式(高招)/images/emoticon/emoticon24.gif

johnting iT邦新手 5 級 ‧ 2021-09-21 01:47:05 檢舉

只是應急用的手刻加上 markdown 格式 的-橫線和|直線,比較方便轉換為表格。
正規做法是利用 pandas 之類的函式庫整理資料再匯出為 markdown。
這篇雖然也手刻了測試輸入的格式,但實際上 tensorflow 本身也有轉換圖片大小+灰階的函式。

0
長風青雲
iT邦新手 4 級 ‧ 2021-09-20 23:26:26

我覺得應該是pattern還不夠多@@

johnting iT邦新手 5 級 ‧ 2021-09-21 05:54:47 檢舉

訓練的資料量和種類確實很影響模型

我要留言

立即登入留言