【问题标题】:How to use own image to predict handwritten digits using MNIST Dataset and neural netwrok如何使用自己的图像使用 MNIST 数据集和神经网络预测手写数字
【发布时间】:2020-04-06 06:12:09
【问题描述】:

我已经使用神经网络构建了一个模型,该模型使用 MNIST 数据集来预测数字。

现在我想对我提供给模型的图像进行预测。

我正在使用此代码将图像转换为 MNIST 图像。

from PIL import Image, ImageFilter
from matplotlib import pyplot as plt


def imageprepare(argv):
    """
    This function returns the pixel values.
    The imput is a png file location.
    """
    im = Image.open(argv).convert('L')
    width = float(im.size[0])
    height = float(im.size[1])
    newImage = Image.new('L', (28, 28), (255))  # creates white canvas of 28x28 pixels

    if width > height:  # check which dimension is bigger
        # Width is bigger. Width becomes 20 pixels.
        nheight = int(round((20.0 / width * height), 0))  # resize height according to ratio width
        if (nheight == 0):  # rare case but minimum is 1 pixel
            nheight = 1
            # resize and sharpen
        img = im.resize((20, nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wtop = int(round(((28 - nheight) / 2), 0))  # calculate horizontal position
        newImage.paste(img, (4, wtop))  # paste resized image on white canvas
    else:
        # Height is bigger. Heigth becomes 20 pixels.
        nwidth = int(round((20.0 / height * width), 0))  # resize width according to ratio height
        if (nwidth == 0):  # rare case but minimum is 1 pixel
            nwidth = 1
            # resize and sharpen
        img = im.resize((nwidth, 20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
        wleft = int(round(((28 - nwidth) / 2), 0))  # caculate vertical pozition
        newImage.paste(img, (wleft, 4))  # paste resized image on white canvas

    # newImage.save("sample.png

    tv = list(newImage.getdata())  # get pixel values

    # normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
    tva = [(255 - x) * 1.0 / 255.0 for x in tv]
    print(tva)
    return tva

x=[imageprepare('./zero.png')]#file path here
print(len(x))# mnist IMAGES are 28x28=784 pixels
print(x[0])
#Now we convert 784 sized 1d array to 24x24 sized 2d array so that we can visualize it
newArr=[[0 for d in range(28)] for y in range(28)]
k = 0
for i in range(28):
    for j in range(28):
        newArr[i][j]=x[0][k]
        k=k+1

for i in range(28):
    for j in range(28):
        print(newArr[i][j])
        # print(' , ')
    print('\n')


plt.imshow(newArr, interpolation='nearest')
plt.savefig('MNIST_IMAGE2.png')#save MNIST image
plt.show()#Show / plot that image

我正在使用以下代码来预测结果:

image = cv2.imread("MNIST_IMAGE2.png")
image = cv2.resize(image,(28,28))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
data = asarray(gray)
data=data/255.0
predictions = model.predict(np.expand_dims(data, 0))

但是使用上述代码生成的输出非常不准确。

我该怎么办??

【问题讨论】:

  • 您不能指望通过 MNIST 训练的模型对您提供的任何手写图像有很好的结果。如果您输入的图像与 MNIST 风格的图像稍有不同,您将得到很差的结果(因为网络仅使用非常少的数据集进行训练)。
  • 嗯...那你建议我怎么做。
  • 如果您想处理真实世界的数据,请使用更多数据对其进行训练。您可以尝试不同的选项,使用其他基于数字的数据集扩充您的 mnist 数据集/在 mnist 上对其进行训练,并使用迁移学习方法并在看起来像您想要使用它的较小数据集上进行训练。

标签: tensorflow machine-learning image-processing neural-network mnist


【解决方案1】:

如果您想要更准确地识别您书写的数字,您可能需要使用您书写的数字图像而不是 mnist 的图像来训练网络。

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2018-02-02
    • 2015-04-29
    • 2017-11-20
    • 2019-05-01
    • 2017-04-19
    • 1970-01-01
    • 2017-04-11
    相关资源
    最近更新 更多