我的機器學習算法已經在MNIST數據庫中學習了70000個圖像。我想在未包含在MNIST數據集中的圖像上進行測試。但是,我的預測函數無法讀取測試圖像的陣列表示。在學習MNIST後對非MNIST圖像進行分類
如何在外部圖像上測試我的算法? 爲什麼我的代碼失敗?
錯誤接收
PS我使用python3:
Traceback (most recent call last):
File "hello_world2.py", line 28, in <module>
print(sgd_clf.predict(arr))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py", line 336, in predict
scores = self.decision_function(X)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py", line 317, in decision_function
% (X.shape[1], n_features))
ValueError: X has 15 features per sample; expecting 784
代碼:
# Common Imports
import numpy as np
from sklearn.datasets import fetch_mldata
from sklearn.linear_model import SGDClassifier
from PIL import Image
from resizeimage import resizeimage
# loading and learning MNIST data
mnist = fetch_mldata('MNIST original')
x, y = mnist["data"], mnist["target"]
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(x, y)
# loading and converting to array a non-MNIST image of a "5", which is in the same folder
img = Image.open("5.png")
arr = np.array(img)
# trying to predict that the image is a "5"
img = Image.open("5.png")
img = img.convert('L') #makes it greyscale
img = resizeimage.resize_thumbnail(img, [28,28])
arr = np.array(img)
print(sgd_clf.predict(arr)) # ERROR... why????????? How do you fix it?????
該圖像將不得不調整大小。 MNIST圖像是28x28。 –
此外你的圖像似乎是3通道。你必須灰度。 –
如何調整MNIST圖像大小? (注意:請參閱原始代碼進行編輯,謝謝。) – Abicus