SVM图片归类

原先用了网上随手找的一段代码,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]

 

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值