(Python, MNIST) how to load/import my own img for AI to train
Questions like these are difficult to debug, but you'll hopefully find more help on datascience.stackexchange.com than here
I see the suggestion, that post my questionin Data science is also a good option
I have read through the following discussion (not saying how to load pic to MNIST database)
https://stackoverflow.com/questions/69625822/mnist-trained-network-tested-with-my-own-samples
I also planning to train my own mnist by input img, but most of the tutorial doen't teach how to load our personal img (with answer, teach AI to reconize)
such as load all img "5" image into MNIST database, and teach them that number 5
how can we do so?
the following .py script is training by MNIST own database (credit by student_DC), then reconize, but accuracy result is not ideal (about 10%), so I come up training my samply img too, before doing the MNIST
but if I store the img on local file how to load them to MNIST model to train?
* script after trained (both train and test from MNIST img databse) can load my own img to pridict
``` import keras from keras.datasets import mnist import matplotlib.pyplot as plt import PIL from PIL import Image (train_images,train_labels),(test_images,test_labels) = mnist.load_data() train_images.shape len(train_labels) train_labels test_images.shape len(test_labels) test_labels
from keras import models from keras import layers network = models.Sequential() network.add(layers.Dense(512,activation='relu',input_shape=(28*28,))) network.add(layers.Dense(10,activation='softmax'))
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
train_images = train_images.reshape((60000,2828)) train_images = train_images.astype('float32')/255 test_images = test_images.reshape((10000,2828)) test_images = test_images.astype('float32')/255
from keras.utils import to_categorical
train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels)
network.fit(train_images,train_labels,epochs= 3 ,batch_size=128)
test_loss , test_acc = network.evaluate(test_images,test_labels) print('test_acc:',test_acc)
network.save('m_lenet.h5')
import numpy as np from keras.models import load_model import matplotlib.pyplot as plt from PIL import Image
model = load_model('/content/m_lenet.h5')
picPath = '/content/02_a.png' img = Image.open(picPath)
reIm = img.resize((28,28),Image.ANTIALIAS)
plt.imshow(reIm) plt.savefig('/content/result.png')
im1 = np.array(reIm.convert("L"))
im1 = im1.reshape((1,28*28))
im1 = im1.astype('float32')/255
predict = model.predict_classes(im1)
predict_x=model.predict(im1) classes_x=np.argmax(predict_x,axis=1)
print ("---------------------------------")
print ('predict as：') print (predict_x)
print ("") print ("")
print ('predict number as：') print (classes_x) print ("---------------------------------") print ("Original img : ")
```
the sample img screenshot:
* by export suggestion:
to pass img to model.fit ,
that mine network. 392