Mon algorithme d'apprentissage automatique a déjà appris les 70000 images de la base de données MNIST. Je veux le tester sur une image non incluse dans le jeu de données MNIST. Cependant, ma fonction de prévision ne peut pas lire la représentation de tableau de mon image de test. Comment tester mon algorithme sur une image externe?Classement d'une image non-MNIST après avoir appris MNIST
Comment tester mon algorithme sur une image externe? Pourquoi mon code échoue-t-il?
PS J'utilise python3
erreur Reçues:
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
code:
# 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?????
Cette image devra être redimensionnée. Les images MNIST sont 28x28. –
Aussi votre image semble être à 3 canaux. Vous devrez l'évaluer en niveaux de gris. –
Comment redimensionner une image MNIST? (Note: voir le code original pour les modifications Merci.) – Abicus