標籤:nim net [1] extend dom lib shape 準確率 for
大家都說gabor做Face Service是傳統方法中效果最好的,這幾天就折騰實現了下,網上的python實現實在太少,github上的某個版本還誤導了我好幾天,後來採用將C++代碼封裝成dll供python調用的方式,成功解決。
映像經多尺度多方向的gabor變換後,gabor係數的數目成倍上升,所以對gabor係數必須進行降維才能送至後續的SVM分類器。測試映像使用att_faces資料集(40種類型,每種隨機選5張訓練,5張識別),降維方式我測試了DCT、PCA兩種變換方式,說實話,dct不怎麼靠譜,居然準確率不到70%,所以我有點懷疑網頁 http://blog.csdn.net/bxyill/article/details/793785的實現效果,PCA方式也一般,平均識別率95%左右吧;同時測試了直接下採樣、均值濾波後採樣、最大值濾波後採樣三種方式,它們的平均識別率分別為98.6%、98.5%、99%左右。可見,最大值濾波後再下採樣的方式是最好的,其他的非線性降維方法沒試過,我也不太懂
下面是python實現代碼,不到50行哦
#coding:utf-8import numpy as npimport cv2, os, math, os.path, glob, randomfrom ctypes import *from sklearn.svm import LinearSVCdll = np.ctypeslib.load_library(‘zmGabor‘, ‘.‘) #調用C++動態連結程式庫print dll.gabordll.gabor.argtypes = [POINTER(c_uint8), POINTER(c_uint8), c_int32, c_int32, c_double, c_int32, c_double, c_double]def loadImageSet(folder, sampleCount=5): trainData = []; testData = []; yTrain=[]; yTest = []; for k in range(1,41): folder2 = os.path.join(folder, ‘s%d‘ %k) data = [cv2.imread(d.encode(‘gbk‘),0) for d in glob.glob(os.path.join(folder2, ‘*.pgm‘))] sample = random.sample(range(10), sampleCount) trainData.extend([data[i] for i in range(10) if i in sample]) testData.extend([data[i] for i in range(10) if i not in sample]) yTest.extend([k]* (10-sampleCount)) yTrain.extend([k]* sampleCount) return trainData, testData, np.array(yTrain), np.array(yTest)def getGaborFeature(m): res = [] for i in range(6): for j in range(4): g = np.zeros(m.shape, dtype = np.uint8) dll.gabor(m.ctypes.data_as(POINTER(c_uint8)), g.ctypes.data_as(POINTER(c_uint8)), m.shape[0], m.shape[1], i*np.pi/6, j, 2*np.pi, np.sqrt(2)) #res.append(cv2.dct(g[:10,:10].astype(np.float))) #先DCT變換再子採樣 #res.append(g[::10,::10]) #直接子採樣 #res.append(cv2.blur(g, (10,10))[5::10, 5::10]) #先均值濾波再子採樣 res.append(255-cv2.erode(255-g, np.ones((10,10)))[5::10, 5::10]) #先最大值濾波再子採樣 return np.array(res)def main(folder=u‘D:/gabor/att_faces‘): trainImg, testImg, yTrain, yTest = loadImageSet(folder) xTrain = np.array([getGaborFeature(d).ravel() for d in trainImg]) xTest = np.array([getGaborFeature(d).ravel() for d in testImg]) lsvc = LinearSVC() #支援向量機方法 lsvc.fit(xTrain, yTrain) lsvc_y_predict = lsvc.predict(xTest) print u‘支援向量機識別率: %.2f%%‘ % (lsvc_y_predict == np.array(yTest)).mean()if __name__ == ‘__main__‘: main()
gabor變換Face Service的python實現,att_faces資料集平均識別率99%