话不多说,看代码吧!
传入参数的话,需要自己整理 特征 和对应标签,特征要求二维
例如,我这里 (1327, 10) (1327,)
每张图 10个特征 一共1327张图
觉得好用的话,麻烦点点关注或者赞哈!
import cv2
import sklearn
import numpy as np
import matplotlib.pyplot as plt
from sklearn import metrics
def creat_svm_Auto(training_attributes,training_class_labels):
'''这个默认rbf,且每次都进行1000次的参数选择,交叉验证10个子集'''
svm = cv2.ml.SVM_create()
svm.trainAuto(training_attributes, cv2.ml.ROW_SAMPLE, training_class_labels.astype(int),
kFold=10);# 2分类
return svm
def score_svm(svm, xTest, yTest):
"""svm, xTest, yTest"""
from sklearn.metrics import accuracy_score
_, y_pre = svm.predict(xTest)
return accuracy_score(y_pre, yTest)
def get_best_svm_Auto(xTrain, xTest, yTrain, yTest,itera = 100):
'''itera,迭代次数'''
score_train, score_test = [], []
for j in range(itera):
svm = creat_svm_Auto(xTrain, yTrain)
score_train.append(score_svm(svm, xTrain, yTrain))
score_test.append(score_svm(svm, xTest, yTest))
_, y_pred = svm.predict(xTest)
'''将预测失败的图片添加进train中'''
flag1, flag2 = yTest.ravel() == 0, y_pred.ravel() == 1
flag = flag1 == flag2 # flag 中记载了所有预测失败的位置信息
xTrain_append = xTest[flag, :] # (40, 18000, 1)
yTrain_append = yTest[flag] # 1维数据
print(f"目前为第{j+1}次训练,训练精度为:{score_train[j]},测试集准确度为:{score_test[j]}")
if yTrain_append.shape[0] == 0: # 已经没有预测错误的数据了
print('no more false predict')
return svm, score_train, score_test
else:
xTrain = np.concatenate((xTrain, xTrain_append), axis=0)
yTrain = np.concatenate((yTrain, yTrain_append))
return svm, score_train, score_test
'''svm 训练集'''
'''把数据都加载或者保存为np格式有助于提升计算机的加载运算速度,特别是数据量大,特征维度多的时候'''
features = np.load('../data/temp_file/Train_Feature_Fusion_(1327, 10).npy')
labels = np.load('../data/temp_file/Target.npy')
print(labels.shape)
features = features.astype(np.float32)
print(features.shape,labels.shape)
# label int ,feature 3维度
'''训练svm'''
from sklearn.model_selection import train_test_split
xTrain,xTest,yTrain,yTest = train_test_split(features,labels,test_size=0.1,random_state=7)
SVM_best,score_train, score_test=get_best_svm_Auto(xTrain, xTest, yTrain, yTest,itera=10)
print(xTrain.shape)
_,ypre = SVM_best.predict(xTest)
_,xpre = SVM_best.predict(xTrain)
plt.plot(list(range(1,len(score_train)+1)),score_train)
plt.title("Accuracy_score in Train",fontsize = 20)
plt.xlabel("The times of train")
plt.ylabel("Accuracy score")
plt.savefig("../figures/train_score.jpg")
plt.show()
plt.plot(list(range(1,len(score_test)+1)),score_test)
plt.title("Accuracy_score in Test",fontsize = 20)
plt.xlabel("The times of test")
plt.ylabel("Accuracy score")
plt.savefig("../figures/test_score.jpg")
plt.show()
print(metrics.accuracy_score(ypre,yTest))
print(metrics.accuracy_score(xpre,yTrain))
print(yTest,yTrain)
'''保存svm'''
SVM_best.save(f"../data/temp_file/cv2-svm-{features.shape[1]}.mat")
(1327,)
(1327, 10) (1327,)
目前为第1次训练,训练精度为:0.8375209380234506,测试集准确度为:0.7669172932330827
目前为第2次训练,训练精度为:0.8261224489795919,测试集准确度为:0.8571428571428571
目前为第3次训练,训练精度为:0.9043408360128617,测试集准确度为:0.9323308270676691
目前为第4次训练,训练精度为:0.897047086991221,测试集准确度为:0.9398496240601504
目前为第5次训练,训练精度为:0.9714512291831879,测试集准确度为:0.9097744360902256
目前为第6次训练,训练精度为:0.8177533385703064,测试集准确度为:0.9172932330827067
目前为第7次训练,训练精度为:0.9088785046728972,测试集准确度为:0.9849624060150376
目前为第8次训练,训练精度为:0.9066874027993779,测试集准确度为:0.9849624060150376
目前为第9次训练,训练精度为:0.907608695652174,测试集准确度为:0.9924812030075187
目前为第10次训练,训练精度为:0.9123351435221102,测试集准确度为:1.0
no more false predict
(1194, 10)
1.0
0.9053601340033501
[1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 0 1
0 1 1 1 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 1 0 0 0 1 0
0 0 1 1 0 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 0 1 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0
0 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 1] [1 1 0 ... 1 0 1]