原先用了网上随手找的一段代码,gamma值设置不正确,出来的模型效果差。同事给了一段用过的带GridSearchCV参数自动搜寻,一下就ok。
训练代码:
#!/usr/bin/python #!--*-- coding:utf-8 --*-- import sys import cv2 import glob import time import numpy as np from skimage import feature as ft from sklearn import model_selection, preprocessing from sklearn.decomposition import IncrementalPCA from sklearn.externals import joblib from sklearn.svm import LinearSVC, SVC from sklearn.externals import joblib posjpgs = glob.glob('./pos/*.jpg') negjpgs = glob.glob('./neg/*.jpg') def hogfea(jpgfile): img = cv2.imread(jpgfile, cv2.IMREAD_COLOR) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) feature = ft.hog(img, # input image orientations=9, # number of bins pixels_per_cell=(20,20), # pixel per cell cells_per_block=(2,2), # cells per blcok block_norm = 'L2-Hys', # block norm : str {‘L1’, ‘L1-sqrt’, ‘L2’, ‘L2-Hys’}, optional transform_sqrt = True, # power law compression (also known as gamma correction) feature_vector=True, # flatten the final vectors visualise=False) # return HOG map return feature def train(features, labels): # train test set split X_train, X_test, Y_train, Y_test = model_selection.train_test_split(features, labels, test_size=0.2, random_state=2) # RBF kernal: exhausted search for C and gamma, cross-validation data generation & svm training time_start = time.time() C_range = np.logspace(-6, 6, 3) gamma_range = np.logspace(-3, 3, 3) param_grid = dict(gamma=gamma_range, C=C_range) clf = model_selection.GridSearchCV(SVC(kernel='rbf'), param_grid=param_grid,n_jobs=9) clf.fit(X_train, Y_train) print(" SVM success: The best parameters are %s with a score of %0.2f | costtime: %0.2fs" % (clf.best_params_, clf.best_score_, time.time() - time_start)) return clf.best_estimator_, X_test, Y_test x_train = [] y_train = [] for jpgfile in posjpgs: print 'pos ', jpgfile x_train.append(hogfea(jpgfile)) y_train.append(1) for jpgfile in negjpgs: print 'neg ', jpgfile x_train.append(hogfea(jpgfile)) y_train.append(0) clf, X_test, Y_test = train(np.array(x_train), np.array(y_train).ravel()) joblib.dump(clf, "model.clf") s = clf.score(X_test, Y_test) print ' ** Tset ** score = {}'.format(s)预测代码:
#!/usr/bin/python #!--*-- coding:utf-8 --*-- import sys import cv2 import numpy as np from skimage import feature as ft from sklearn import svm from sklearn.externals import joblib clf = joblib.load('model.clf') def hogfea(jpgfile): img = cv2.imread(jpgfile, cv2.IMREAD_COLOR) img = cv2.resize(img, (128, 128)) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) feature = ft.hog(img, # input image orientations=9, # number of bins pixels_per_cell=(20,20), # pixel per cell cells_per_block=(2,2), # cells per blcok block_norm = 'L2-Hys', # block norm : str {‘L1’, ‘L1-sqrt’, ‘L2’, ‘L2-Hys’}, optional transform_sqrt = True, # power law compression (also known as gamma correction) feature_vector=True, # flatten the final vectors visualise=False) # return HOG map return feature fea = hogfea(sys.argv[1]) fea = np.reshape(fea, (1, fea.shape[0])) print fea, clf.decision_function(fea)[0]