阅读量:0
HOG+SVM数字识别
1 . 步骤
- 读入数据,把数据划分为训练集和测试集
- 用hog提取特征
- 用SVM训练数据
- 测试、评价模型
- 保存模型
- 加载模型,应用模型
2 . 代码
import os import cv2 import sklearn import numpy as np from skimage.feature import hog from skimage import data,exposure from sklearn import svm from sklearn.metrics import accuracy_score import matplotlib.pyplot as plt import seaborn as sn import pandas as pd from joblib import dump,load from sklearn.metrics import confusion_matrix # 1. 读入数据 # 1.1 处理数据,获取图像的地址和标签,划分训练集、测试集,对数据集打乱顺序 data_path = r'./hand_nums' tmp_train = os.listdir(data_path+'/train') tmp_test = os.listdir(data_path+'/test') train_x ,test_x = [],[] ## 1.2 获取图像的地址,把训练集和测试集的特征和标签 test_y ,test_y = [],[] for i in tmp_train: if i.endswith('.bmp'): train_x.append(data_path+'/train/'+i) train_y.append(int(i.split('-')[0])) for i in tmp_test: if i.endswith('.bmp'): test_x.append(data_path+'/test/'+i) test_y.append(int(i.split('.')[0])) ## 1.3 打乱数据的顺序 ## 训练集、测试集的下标 train_idx = np.arange(len(train_x)) test_idx = np.arange(len(test_x)) ## 打乱顺序 np.random.shuffle(train_idx) np.random.shuffle(test_idx) ## 训练集、测试集打乱顺序 train_x = list(np.array(train_x)[train_idx]) test_x = list(np.array(test_x)[test_idx]) train_y = list(np.array(train_y)[train_idx]) test_y = list(np.array(test_y)[test_idx]) # 2. 用hog提取特征 ## 2.1 hog实例化 train_feature,test_feature = [],[] ## 获取训练集特征 for i in train_x: img_gray = cv2.imread(i,0) fd , img_hog = hog(img_gray,orientations=9,pixels_per_cell=(8,8), cells_per_block=(2,2),visualize=True) train_feature.append(img_hog.flatten()) ## 获取测试集特征 for i in test_x: img_gray = cv2.imread(i,0) fd,img_hog = hog(img_gray,orientations=9,pixels_per_cell=(8,8), cells_per_block=(2,2),visualize=True) test_feature.append(img_hog.flatten()) # 3 SVM训练 clf = svm.SVC(decision_function_shape='ovo') clf.fit(train_feature ,train_y) # 4. 测试、评价模型 dec = clf.decision_function(test_feature) pred_y = clf.predict(test_feature) accuracy_score(pred_y,test_y) # 5. 保存模型 from joblib import dump,load ## 保存模型 dump(clf,'./hand_nums/models/poly.joblib') ## 加载模型 new_cls = load('./hand_nums/models/poly.joblib') pred_y = new_cls.predict(test_feature) accuracy_score(pred_y,test_y) # 6. 加载模型,应用模型 clf_poly = svm.SVC(decision_function_shape='ovo',kernel='poly') clf_poly.fit(train_feature ,train_y) dec = clf_poly.decision_function(test_feature) pred_y = clf_poly.predict(test_feature) accuracy_score(pred_y,test_y) clf_linear = svm.SVC(decision_function_shape='ovo',kernel='linear') clf_linear.fit(train_feature ,train_y) dec = clf_linear.decision_function(test_feature) pred_y = clf_linear.predict(test_feature) accuracy_score(pred_y,test_y) cm = confusion_matrix(test_y,pred_y) df_cm = pd.Dataframe(cm,index=[i for i in ['0','1','2','3','4','5','6','7','8','9']], columns = [i for i in ['0','1','2','3','4','5','6','7','8','9']]) plt.figure(figsize=(10,7)) sn.heatmap(df_cm,annot=True,cmap='Green',fmt='d')
准确率 1.0
嘻嘻😁
数据集:链接:https://pan.baidu.com/s/1yFCJvcswdSgGcAN6n9u-nA 密码:ryqo