Keras + mnist +测试自己的图像.错误的预测 [英] Keras + mnist + test own images. Bad prediction

查看:248
本文介绍了Keras + mnist +测试自己的图像.错误的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过测试mnist自己的测试图像可以很好地工作,但是一旦我使用mnist外部的图像,它就可以预测错误.我什至尝试从mnist数据集中复制其中一张图像,但是它仍然无法预测正确的数字(即使在mnist数据集中使用完全相同的图像也可以(预测)).

It works fine by testing mnist's own test images, but as soon as i use images from outside mnist, it predicts wrong. I even tried to copy one of the images from the mnist dataset, and it still could'nt predict the right digit (even though the exact same image was OK (predicted) when inside the mnist dataset).

有人可以看到我做错了吗?我猜测图像的尺寸或形状有些问题.

Could someone see what i do wrong? I'm guessing there's something with the dimensions or shape of the image.

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

我所预测的数字. mnist数据集中的上一个(正确预测),下一个复制并放入(错误预测)

推荐答案

我知道了.通过这段代码,我没有得到正确的归一化值.

I figured it out. I didn't get the right normalized values out with this block of code.

image = cv.imread(file, cv.IMREAD_GRAYSCALE)
image = cv.resize(image, (28,28))
image = 255-image     

相反,我不得不用底部(这里是底部)的分隔来更正它,我在较早的尝试中错误地将其放在了 image = 255-image 之前.这是错误之一,并且缺少将类型强制转换为 float32 的功能,这使得规范化以及之间的重塑成为可能.

Instead, I had to correct it with the division at the bottom (here at the bottom), which i mistakenly had put before the image = 255-image in earlier attempt. This was one of the fault, together with missing casting the type to float32 which made it possible to normalize, as well as the reshape there in between.

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

这篇关于Keras + mnist +测试自己的图像.错误的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆