首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将自己的图像转换为MNIST的图像

将自己的图像转换为MNIST的图像
EN

Stack Overflow用户
提问于 2016-03-07 11:15:10
回答 2查看 12.7K关注 0票数 8

我是tensorflow的新手。我用MNIST的训练数据训练了数字预测模型。然后我用我自己的形象来测试模型。它无法预测实际结果。

问题是:

  1. MNIST的图像需要黑白
  2. 图像是标准化的大小,以适应20x20像素盒和有中心在28x28图像使用质心。
  3. 我不想使用OpenCV

问题是如何将我自己的手写数字图像转移到28x28图像的中心。自己的图像可以是任何颜色,而该图像可以改变黑白MNIST的图像

EN

回答 2

Stack Overflow用户

发布于 2019-05-03 11:00:51

代码语言:javascript
运行
复制
from PIL import Image, ImageFilter


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('./image.png')#file path here
print(len(x))# mnist IMAGES are 28x28=784 pixels
票数 5
EN

Stack Overflow用户

发布于 2016-03-08 02:04:02

我会用像这张一样的小秘方-- https://www.kaggle.com/c/digit-recognizer/forums/t/6366/normalization-and-centering-of-images-in-mnist

您可能可以将其重新映射到纯TensorFlow管道,但考虑到它是很小的图像,我不确定它是否必要。

另外,如果采用另一种方式,您将获得更高的准确性--而不是标准化输入数据,而是通过在更大的数据集上进行随机移位/重标的MNIST数字的培训,使您的网络对缺乏规范化的情况更加健壮。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35842274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档