通过测试mnist自己的测试图像可以很好地工作,但是一旦我使用mnist外部的图像,它就可以预测错误。我什至尝试从mnist数据集中复制其中一张图像,但它仍然无法预测正确的数字(即使在mnist数据集中使用完全相同的图像也可以(预测))。
有人可以看到我做错了吗?我猜测图像的尺寸或形状有些问题。
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.reshape(1, 28, 28, 1))
,
以及pred = model.predict_classes(image.reshape(1, 28, 28, 1))
答案 0 :(得分:2)
我知道了。我没有用这段代码得到正确的规范化值。
image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(image, (28,28))
image = 255-image
相反,我不得不用底部(这里是底部)的分隔来更正它,我在较早的尝试中错误地将其放在了 image = 255-image 之前。这是错误之一,并且缺少将类型强制转换为 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
答案 1 :(得分:0)
一些猜测
1)您标准化了火车和测试数据。我假设您可能已经忘了在进行预测之前对输入数据进行标准化?
x_train /= 255
x_test /= 255
2)您是否已验证模型是否正确加载?保存和加载后,请确认它在测试集上仍然执行相同的操作。如果结果不好,则表明您没有正确加载砝码。
3)是否对tf.keras.datasets.mnist.load_data()
提供的数据集进行了任何预处理(在您自己的规范化之外)?如果是这样,则必须在推理之前对您自己的输入数据进行相同的转换
答案 2 :(得分:0)
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