将图片矢量化以传递到分类器

vectorize a picture to pass into classifier

我按照本教程创建了一个简单的图像分类:

https://blog.hyperiondev.com/index.php/2019/02/18/machine-learning/

在训练之前,我们像这样对数据集中的图片进行矢量化处理:

train_data = scipy.io.loadmat('extra_32x32.mat')
# extract the images and labels from the dictionary object
X = train_data['X']
y = train_data['y']

# example: view an image (e.g. 25) and print its corresponding label
img_index = 25
plt.imshow(X[:,:,:,img_index])
plt.show()
print(y[img_index])

X = X.reshape(X.shape[0]*X.shape[1]*X.shape[2],X.shape[3]).T
y = y.reshape(y.shape[0],)
X, y = shuffle(X, y, random_state=42)

完成训练后,我想上传另一张图片(不在数据集中)并将其传递给分类器以检查它是否被预测(以及它的准确度得分)

可是怎么传图片呢?我试过这个:

jpgfile = Image.open("63.jpg") 
value = clf.predict(jpgfile)

并得到一个错误:

Found array with dim 3. Estimator expected <= 2.

那么,由于我没有单独的 x、y 值,我该如何相应地对其进行矢量化。

您需要在加载后重塑图像:

jpgfile = Image.open("63.jpg") 
jpgfile = jpgfile.resize((32, 32) # resize image to 32*32
img_as_matrix = numpy.array(jpgfile)  # convert to numpy array
img_as_matrix = img_as_matrix.reshape(img_as_matrix.shape[0]*img_as_matrix.shape[1]*img_as_matrix.shape[2],1).T  # Reshape and transpose image as the train images
# Here the second dim is 1, since there is only 1 image instead of X.shape[3] images 

value = clf.predict(img_as_matrix)