mnist手写数字识别 — HOG+SVM

我们首先官网下载数据,并进行加载。

需要注意的是官网文件为二进制文件,我们需要进行简单处理和格式转换

然后我们对于数据进行hog特征提取,大概测试了一下cell大小4*4比较好(

训练分类器,直接用默认参数似乎就不错(

测试集正确率可以达到98.23%

错误数据随机输出了一下(感觉很多的确很难分辨,甚至一些人眼也不好识别

整体代码:

import numpy as np
import matplotlib.pyplot as plt
import os
import struct

def load_mnist(kind='train',path='./'):
    labels_path = os.path.join(path,'%s-labels-idx1-ubyte'% kind)
    images_path = os.path.join(path,'%s-images-idx3-ubyte'% kind)

    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',lbpath.read(8))
        labels = np.fromfile(lbpath,dtype=np.uint8)
    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))
        images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)
    return images, labels

train_x,train_y=load_mnist('train')
test_x,test_y=load_mnist('t10k')

from skimage.feature import hog
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt

qaz = []
for pic in train_x:
    fd = hog(pic.reshape((28, 28)), orientations=9, pixels_per_cell=(4, 4), cells_per_block=(2, 2), visualise=False)
    qaz.append(fd)
hog_train = np.array(qaz, 'float64')
#hog_train
clf = LinearSVC() 
clf.fit(hog_train, train_y)

qaz = []
for pic in test_x:
    fd = hog(pic.reshape((28, 28)), orientations=9, pixels_per_cell=(4, 4), cells_per_block=(2, 2), visualise=False)
    qaz.append(fd)
hog_test = np.array(qaz, 'float64')
pred_y = clf.predict(hog_test)
sum=0.0
for i in range(10000):
    if pred_y[i]==test_y[i]:
        sum=sum+1
    elif i%30==0:
        plt.title(pred_y[i])
        plt.imshow(test_x[i].reshape((28, 28)))
        plt.show()
        
print("Accuracy rate:",sum/10000)

评论

还没有任何评论,你来说两句吧

发表评论

衫小寨 出品