Python - 如何使用 MNIST 预测具有不同形状的 np.darray

Python - How to use MNIST to predict a np.darray with different shape

import numpy as np
import scipy.ndimage
from PIL import Image
import urllib.request

url = 'http://static.bn-static.com/pg/0rmrKX8jCvpmF8b7ab+coivEApi2iNNpgVTrfyFFA0g==.gif'
img = Image.open(urllib.request.urlopen((url)))
img = img.convert('1').convert('L') #convert to graysclae

# Optional to visualize it all:
# plt.imshow(img)
# plt.show()
# a = array(img)
# a = a.transpose()
# np.place(a,a==0,1)
# np.place(a,a==255,0)

# Croping only one number out of it and vectorize it with binary values.
data = array(img.crop((7, 0, 14, 15)))
np.place(data, data == 0, 1)
np.place(data, data == 255, 0)
plt.imshow(data, cmap=plt.cm.binary)
# visualize crop
plt.show()
# visualize matrix
data

我想使用 Tensorflow 或任何需要的东西来预测这个裁剪图像中的数字,这样我就可以继续这样做,直到可以预测所有数字。

数组属性与 MNIST 数据库有很大不同,因为这不是 28x28 图像。

有什么方法可以让我找出线性变换来为我做这件事吗?

谢谢

可以在第一个卷积前加一个resize操作:

x = tf.image.resize_images(x, [28, 28])

这样可以接受不同尺寸的图片。确保您的输入数组的形状为:

x.get_shape().as_list() == [ None, width, height, channels]