Python Keras+;mnist+;测试自己的图像。坏预测
通过测试mnist自己的测试图像,它工作得很好,但只要我使用来自mnist外部的图像,它就预测错了。我甚至试图从mnist数据集中复制一幅图像,但它仍然无法预测正确的数字(即使在mnist数据集中,完全相同的图像是可以预测的) 有人能看出我做错了什么吗?我猜这是图像的尺寸或形状Python Keras+;mnist+;测试自己的图像。坏预测,python,tensorflow,keras,mnist,Python,Tensorflow,Keras,Mnist,通过测试mnist自己的测试图像,它工作得很好,但只要我使用来自mnist外部的图像,它就预测错了。我甚至试图从mnist数据集中复制一幅图像,但它仍然无法预测正确的数字(即使在mnist数据集中,完全相同的图像是可以预测的) 有人能看出我做错了什么吗?我猜这是图像的尺寸或形状 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from keras.models import Sequential
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
import cv2 as cv
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255
# -------------------------- CREATE MODEL ------------------------------
'''
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10,activation=tf.nn.softmax))
# ----------------------------------------------------------------------
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=x_train,y=y_train, epochs=1)
# ----------------------------------------------------------------------
'''
model = tf.keras.models.load_model("C:/Users/A551110/PycharmProjects/keras_mnist/venv/mnistv2.model")
file = "C:/Users/A551110/Documents/images/7.png"
model.evaluate(x_test, y_test)
image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(image, (28,28))
image = 255-image #inverts image. Always gets read inverted.
plt.imshow(image.reshape(28, 28),cmap='Greys')
plt.show()
pred = model.predict(image.reshape(1, 28, 28, 1), batch_size=1)
print(pred.argmax())
我已经尝试了pred=model.predict(image.reformate(1,28,28,1))
以及pred=model.predict_类(image.reformate(1,28,28,1))
一些猜测
1) 你规范化了你的训练和测试数据。我想你可能忘了在做预测之前规范化你的输入数据
x_train /= 255
x_test /= 255
2) 您是否已验证模型已正确加载?保存和加载后,验证它在测试集上是否仍执行相同的操作。如果结果不好,则表明未正确加载权重
3) 是否对
tf.keras.datasets.mnist.load_data()
提供的数据集进行了任何预处理(您自己的规范化之外)?如果是这样的话,您必须在推断之前使用相同的转换预处理您自己的输入数据。我没有用这段代码得到正确的规范化值
image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(image, (28,28))
image = 255-image
相反,我必须用底部的除法(这里是底部)来纠正它,在之前的尝试中,我错误地将除法放在image=255图像之前。这是其中一个错误,加上缺少将类型铸造到float32的功能,这使得规范化成为可能,以及在两者之间的重塑
image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(file, (28, 28))
image = image.astype('float32')
image = image.reshape(1, 28, 28, 1)
image = 255-image
image /= 255
2) 当在mnist数据集上测试时(当得到正确的预测时),模型的加载方式完全相同,因此我不认为权重加载错误。3) 不太确定。根据尺寸不同,可从列车上进行样品测试。我不知道这是不是说在某种程度上进行了预处理。如果你在训练结束时打印一些重量,在加载后它们是一样的吗?
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
y=cv2.imread("/content/download.png") #image outside mnist data
y1=cv2.resize(y,(28,28)) #you need to resize it on the bsis pf your modeL's image shape
plt.imshow(y1)
temp = cv2.cvtColor(y1,cv2.COLOR_BGR2YCrCb) #since its a three channel image i hav econverted into this so rbg are represented in the luminance one
temp=255-temp #negative image
plt.imshow(temp)
print(temp.shape)
Y = np.zeros((temp.shape[0], temp.shape[1],1), dtype=float) #array of (28,28,1)
Y[:,:,0] = temp[:, :,0].astype(float) / 255 #fitting the data of temp image in that zeros and normalizing it
yh= model.predict_classes(Y.reshape(1,28,28,1)) #finally the value of image
yh