1
$\begingroup$

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 in Data science is also a good option

Using python AI mnist to recognize my picture, trained accuracy is 97.99%, but accuracy to my img is less than 20%

I'm hoping can use MNIST doing 0~9 number recognition, and trainning accuracy rate reach up to 97% , I thought it will be fine to reconize my pic

but predict/recognize my 2 picture as number 7
predict/recognize my 3 picture as number 6
predict/recognize my 5 picture as number 2

here is the share pic link : https://i.sstatic.net/DRH6G.jpg

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 : ")

what should I do for this?

  • should I also import my img with ans for AI to trainning?
  • add more layers?

that all the idea I came up, if there is more, just let me know? If that the only two idea to slove, also tell me how to implement (ex:import my img with ans for AI to trainning)


The discussion I read through https://stackoverflow.com/questions/69625822/mnist-trained-network-tested-with-my-own-samples

$\endgroup$

1 Answer 1

2
$\begingroup$

It means that your model is overfitting- when you see that the model performs well on the training data but does not perform well on the test data. There are various steps to deal with this type of problems.

  1. Perform Data Augmentation such as Flip, rotate, etc..
  2. Cross-validation
  3. Regularization
$\endgroup$
6
  • $\begingroup$ thanks for your idea, can you tell me how to implement on python code too? I just know MNIST less than a week, so I'm not confidence with adding/improving this to my code right away $\endgroup$
    – DC con
    Nov 22, 2022 at 6:20
  • $\begingroup$ such as concept Cross-validation and Regularization I might need sample code that at least I can have some part to begin, if luckily you can correct my code that excellent too $\endgroup$
    – DC con
    Nov 22, 2022 at 6:24
  • $\begingroup$ For Data Augmentation code you can follow this link -analyticsvidhya.com/blog/2021/06/…. They performed rotation, zoom, horizontal flip, and other $\endgroup$
    – Rina
    Nov 22, 2022 at 6:54
  • $\begingroup$ For Cross-validation check out this link: datascience.stackexchange.com/questions/29246/…, stackoverflow.com/questions/58996242/… $\endgroup$
    – Rina
    Nov 22, 2022 at 6:55
  • $\begingroup$ Regularization - analyticsvidhya.com/blog/2018/04/… $\endgroup$
    – Rina
    Nov 22, 2022 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.