0

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

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 值,因此如何对其进行矢量化。

4

1 回答 1

0

加载后您需要重塑图像:

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)
于 2020-10-26T11:05:19.597 回答