keras 使用python AI mnist来识别我的图片,经过训练的准确率是97.99%,但对我的img的准确率不到20%

dxxyhpgq  于 2022-11-24  发布在  Python
关注(0)|答案(2)|浏览(174)

使用python AI mnist来识别我的图片,经过训练的准确率为97.99%,但对我的img的准确率不到20%

我希望能用MNIST做0~9的数字识别,训练的准确率达到97%,我想这对我的图片识别是没问题的
但是预测/识别出我2张图片是数字7
预测/识别出我3张图片是第6张
预测/识别出我5张图片为数字2
下面是分享图片链接:https://imgur.com/a/yDJ8ujc

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与ans的AI训练)

已试用Maven建议的代码:

使用ImageDataGenerator在Keras数据集中进行数据扩充

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)



# Here is image data augmentation example:
from tensorflow.keras.preprocessing.image import ImageDataGenerator

data_generator = ImageDataGenerator(rotation_range=10,
                   width_shift_range=8,
                   height_shift_range=8,
                   brightness_range=[0.6,1.1],
                   zoom_range=.15,
                   validation_split=.2,
                   rescale=1./255)

train_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='training')
validation_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='validation')

# Now it's time to train model with augmented dataset
network.fit(train_dataset, validation_data=validation_dataset, epochs=30)

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

输出:

Epoch 1/3
469/469 [==============================] - 10s 15ms/step - loss: 0.2555 - accuracy: 0.9268
Epoch 2/3
469/469 [==============================] - 5s 10ms/step - loss: 0.1023 - accuracy: 0.9695
Epoch 3/3
469/469 [==============================] - 5s 10ms/step - loss: 0.0678 - accuracy: 0.9796
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-476f532516e9> in <module>
     51                    rescale=1./255)
     52 
---> 53 train_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='training')
     54 validation_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='validation')
     55 

1 frames
/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, ignore_class_split, dtype)
    675           'Input data in `NumpyArrayIterator` '
    676           'should have rank 4. You passed an array '
--> 677           'with shape', self.x.shape)
    678     channels_axis = 3 if data_format == 'channels_last' else 1
    679     if self.x.shape[channels_axis] not in {1, 3, 4}:

ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (48000, 784))
gmxoilav

gmxoilav1#

正如史努比博士提到的,MNIST是一个学术数据集,手写数字大小相同,而且都在图像的中心,但我们知道在真实的世界中这种情况很少发生,我认为最好的做法是使用data augmentation
通过数据增强,您可以使用不同缩放、不同亮度的图像来训练模型,并在不同方向移动数字,在这种情况下,模型不会习惯于特定的缩放、亮度和数字位置,并且它有更多的机会在真实的世界中正常工作。
您可以在Keras中使用ImageDataGenerator在影像数据集中进行数据扩充。以下是一个非常简单的代码,可能会对您有所帮助:

# Here is image data augmentation example:
from tensorflow.keras.preprocessing.image import ImageDataGenerator

data_generator = ImageDataGenerator(rotation_range=10,
                   width_shift_range=8,
                   height_shift_range=8,
                   brightness_range=[0.6,1.1],
                   zoom_range=.15,
                   validation_split=.2,
                   rescale=1./255)

train_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='training')
validation_dataset = data_generator.flow(train_images, train_labels, batch_size=32, subset='validation')

# Now it's time to train model with augmented dataset
network.fit(train_dataset, validation_data=validation_dataset, epochs=10)
jutyujz0

jutyujz02#

MNIST数据集是黑色背景上的白色数字,而您提供的是白色背景上的黑色数字,这将颠倒所有内容。

相关问题