如何解决Tensorflow MNIS图像分类预测错误
我正在使用colab Keras Tensorflow执行MNIST分类任务。运行模型后,我有4个示例图像要手动检查。
首先,我的模型代码如下:
import tensorflow as tf
import os
import PIL
import numpy as np
import matplotlib.pyplot as plt
import tensorflow_datasets as tfds
import tensorflow_hub as hub
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import keras_preprocessing
from keras_preprocessing import image
from tensorflow.python.keras.utils.version_utils import training
from tensorflow.keras.optimizers import RMSprop
(train_images,train_labels),(test_images,test_labels) = tf.keras.datasets.mnist.load_data()
x_trainf = train_images.astype('float32') / 255
x_testf = test_images.astype('float32') / 255
x_train_r = x_trainf.reshape(x_trainf.shape[0],28,1)
x_test_r = x_testf.reshape(x_testf.shape[0],1)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3,padding='same',activation='relu',input_shape=(28,1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=2,activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256,activation='relu'))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(10))
model.compile(optimizer='adam',loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
model.fit(x_train_r,train_labels,batch_size=32,epochs=10,validation_data=(x_test_r,test_labels))
模型似乎具有良好的性能。
现在,我有以下4个示例图像(此处未以图片形式嵌入,以便为您提供原始尺寸的真实图片文件):
https://i.stack.imgur.com/XhZjx.jpg
https://i.stack.imgur.com/4wIIC.jpg
https://i.stack.imgur.com/9VKsY.jpg
https://i.stack.imgur.com/pLhQu.jpg
所以第一张图片是一条直线。第二和第三个是数字“ 2”,最后一个是7。
我将这些文件上传到colab的tmp文件夹中:
test1=tf.keras.preprocessing.image.load_img(
'/tmp/handwritten1.jpg',color_mode='grayscale',target_size=(28,28),interpolation='nearest'
)
test2=tf.keras.preprocessing.image.load_img(
'/tmp/handwritten2.JPG',interpolation='nearest'
)
test3=tf.keras.preprocessing.image.load_img(
'/tmp/handwritten2a.JPG',interpolation='nearest'
)
test4=tf.keras.preprocessing.image.load_img(
'/tmp/handwritten7.JPG',interpolation='nearest'
)
例如显示test2:
img_array = tf.keras.preprocessing.image.img_to_array(test2)
X = img_array.reshape([28,28]);
plt.gray()
plt.imshow(X)
预测它们:
img_array1 = tf.keras.preprocessing.image.img_to_array(test1) / 255.0
img_array1 = tf.expand_dims(img_array1,0)
img_array2 = tf.keras.preprocessing.image.img_to_array(test2) / 255.0
img_array2 = tf.expand_dims(img_array2,0)
img_array3 = tf.keras.preprocessing.image.img_to_array(test3) / 255.0
img_array3 = tf.expand_dims(img_array3,0)
img_array4 = tf.keras.preprocessing.image.img_to_array(test4) / 255.0
img_array4 = tf.expand_dims(img_array4,0)
prediction1 = model.predict(img_array1)
prediction2 = model.predict(img_array2)
prediction3 = model.predict(img_array3)
prediction4 = model.predict(img_array4)
print(prediction1)
print(prediction2)
print(prediction3)
print(prediction4)
第一个图像,即直线,以0.518的最高logit分类为5? 第二张图片,最高logit为1.819作为数字3。 第三张图片,数字为6.83。 第四张图片,编号为8.08。
我承认,确实可以将7误认为是2。但是,这些都是错误的预测。特别是第一张图片,直线是我不明白的。显然这应该是1,但是数字5出现的可能性最高。我期望有更好的预测。这是什么问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。