我已经通读了下面的讨论(没有说如何将pic加载到MNIST数据库)
MNIST trained network tested with my own samples
我也计划通过输入img来训练我自己的mnist,但是大多数教程都没有教如何加载我们的个人img(答案是,教AI识别)
- 例如将所有img "5"图像加载到MNIST数据库中,并教他们数字5**
我们怎样才能做到呢?
下面的. py脚本是由MNIST自己的数据库训练的(* credit by student_DC *),然后重新识别,但是准确率结果不理想(大约10%),所以我在做MNIST之前也来训练我的样本img
- 但如果我将img存储在本地文件中,如何将其加载到MNIST模型中进行训练?**
- 经过训练的脚本(训练和测试从MNIST图像数据库)可以加载我自己的图像预测
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,28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28*28))
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 : ")
示例img屏幕截图:
- 按导出建议:
为了将img传递到model.fit
,
挖掘network.fit(train_images,train_labels,epochs= 3 ,batch_size=128)
并让AI逻辑地训练我的img可以解决这个问题
我在如何创建一个火车图像阵列和相应的标签阵列库存现在,我在网上搜索没有得到类似的教程(与主题:* 通过加载我自己的img来培训MNIST *)
1条答案
按热度按时间bjg7j2ky1#
cridet由这个网站,似乎这是你什么加载自己的img训练
https://blog.tanka.la/2018/10/28/build-the-mnist-model-with-your-own-handwritten-digits-using-tensorflow-keras-and-python/