我训练了CNN以预测图像中的数字并保存了它。如果我在lib内部的x_test
数据上进行测试,则可以预测正常。但是,当我加载自定义图像时-它的预测效果不佳,我也不知道为什么。
from tensorflow import keras
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import PIL
def normalize_image (img) :
"""
Normalize image
:param img: input image with range 0-255 int
:return: normalized image (with range 0 - 1 float)
"""
# taken from https://stackoverflow.com/questions/46689428/convert-np-array-of-type-float64-to-type-uint8-scaling-values
info = np.iinfo(img.dtype)
norm_img = img.astype(np.float64) / info.max
# tensorflow mnist use black background - so change bg from white to black
for i in range(len(norm_img)):
for j in range(len(norm_img[i])):
norm_img[i][j] = abs(norm_img[i][j] - 1)
return norm_img
# load my model
new_model = keras.models.load_model('numbers.model')
# image with number 4
img = PIL.Image.open('C:/Users/Horseman.mini/Desktop/4.png').convert('L').resize((28, 28), PIL.Image.ANTIALIAS)
img = np.array(img)
img = normalize_image(img)
# create my custom tests list
my_tests = np.array([img])
# show the image which value I want to predict
plt.imshow(img, cmap="gray")
plt.show()
predictions = new_model.predict(my_tests)
print("Prediction value:", np.argmax(predictions[0])) # value that should be predicted
My 4.png image as is
My 4.png image plotted after resizing and normalizing
output
Prediction value: 1
预测是错误的,所以我认为dtypes不同:
for comparing x_test data and my custom data lists
mnist = tf.keras.datasets.mnist # mnist is a dataset of 28x28 images of handwritten digits and their labels
(x_train, y_train),(x_test, y_test) = mnist.load_data() # unpacks images to x_train/x_test and labels to y_train/y_test
x_train = tf.keras.utils.normalize(x_train, axis=1) # scales data between 0 and 1
x_test = tf.keras.utils.normalize(x_test, axis=1) # scales data between 0 and 1
print(x_test[0])
print(my_tests[0])
输出很大,我将发布一行以代表数字和数据类型
# x_test[0]
[0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.33861822 0.450819
0.32890224 0.02719964 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
# my_tests[0]
[0. 0. 0. 0. 0. 0.
0. 0. 0.00392157 0. 0.09803922 0.01960784
0. 0. 0. 0.09019608 0.19607843 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
# x_test[0].dtype
float64
# my_tests[0].dtype
float64
无法理解为什么它不起作用。数据类型相同,图像数组从0到1统一,图像正确,经过训练且自定义图像为28x28像素(自定义大小在调整大小后为自定义),但是出了点问题,我不完全是。如果有人可以帮助我,我将不胜感激!