Skip to content

Commit 232da4b

Browse files
committed
libsvm usage
1 parent 63605c6 commit 232da4b

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#coding:utf-8
2+
"""
3+
讲文件下的所有图像写进csv文件,一行代表一张图
4+
"""
5+
import os
6+
from PIL import Image
7+
import numpy as np
8+
import csv
9+
10+
#获取图像标签,以jaffe数据库为例。
11+
def getlabel(img_name):
12+
face_expression = img_name.split('.')[1]
13+
face_expression = face_expression[0:2]
14+
table={'HA':1,'AN':2,'SU':3,'FE':4,'DI':5,'SA':6,'NE':7}
15+
return table.get(face_expression)
16+
17+
18+
f = csv.writer(open("trainlbp.csv","wb"))
19+
20+
21+
direction = "./jaffe"
22+
img_list = os.listdir(direction)
23+
for imgname in img_list:
24+
img = Image.open(direction+imgname)
25+
width,height = img.size
26+
data = np.empty((width*height+1))
27+
28+
data[0] = getlabel(imgname)
29+
img_mat = np.array(img,dtype="float")
30+
img_mat = img_mat.flatten()
31+
data [1:] = img_mat
32+
#write into file
33+
f.writerow(data)
34+
35+
36+
37+
38+
39+
40+
41+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
##libsvm和liblinear的使用总结
2+
3+
0.安装方法
4+
---
5+
6+
unix系统下的安装方法:到[官网](http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html)下载源包(目前最新版本为libsvm-3.20、liblinear-1.96),解压后,打开终端进入makefile所在的目录,键入make即可。
7+
8+
以下为一些基本的使用命令,ubuntu系统下。
9+
10+
11+
1.生成符合要求的数据格式,以图像数据为例
12+
---
13+
14+
1. 从图像库得到csv文件 (csv文件里每一行存储一张图:label,feat1,feat2,.....),在终端下键入:
15+
16+
python gen_datafile.py
17+
18+
>注:gen_datafile.py是我自己写的一个python脚本,放在我的[github]()
19+
20+
2. 生成可执行文件 a.out,在终端下键入:
21+
22+
gcc convert.c
23+
24+
25+
3. 用上面得到的csv文件和a.out文件生成libsvm格式的文件, 在终端下键入:
26+
27+
./a.out csvfile > targetfile
28+
>注:targetfile是存放最终数据的文件。
29+
30+
2.训练模型的命令
31+
---
32+
33+
在终端下切换到目录liblinear-1.96或libsvm-3.20,然后键入以下命令,会提示具体用法:
34+
35+
./svm-train (liblinear为./train)
36+
./svm-predict (liblinear为./predict)
37+
./svm-scale (数据缩放)
38+
39+
40+
41+
3.tools中easy.py的使用
42+
----
43+
easy.py是一条龙服务,从data scaling到参数选取都帮你做。
44+
45+
需要先安装gnuplot,安装命令:
46+
47+
sudo apt-get install gnuplot-x11
48+
49+
之后键入:
50+
51+
python easy.py training_file [testing_file]
52+
53+
4.tools中grid.py的使用:
54+
---
55+
56+
grid.py用于自动搜索参数。用法,在终端下键入:
57+
58+
pyhton grid.py [grid_options] [svm_options] dataset
59+
60+
>要查看options的具体信息,可以先不带参数地键入 pyhton grid.py,这是libsvm的通用方法。
61+
62+
63+
64+
5.tools中subset.py的使用
65+
--
66+
subset.py用于分割数据集。用法:
67+
68+
Usage: subset.py [options] dataset subset_size [output1] [output2]
69+
70+
This script randomly selects a subset of the dataset.
71+
72+
options:
73+
-s method : method of selection (default 0)
74+
0 -- stratified selection (classification only)
75+
1 -- random selection
76+
77+
78+
例如要随机选取dataset中的2000个样本作为trainset,剩下的作为testset,则键人:
79+
80+
python subset.py dataset 2000 trainset testset
81+
82+
83+
84+
6.tools中checkdata.py的使用
85+
--
86+
checkdata.py检查数据格式符不符合要求。键入:
87+
88+
python checkdata.py dataset
89+
90+
7.其他:
91+
---
92+
93+
- 使用交叉验证是不能生成model文件的?(我使用过程中发现不能,不知道是不是真的不能)
94+
95+
96+
97+
- 训练完的结果解读(选自网友博文):
98+
99+
optimization finished, #iter = 162
100+
101+
nu = 0.431029
102+
103+
obj = -100.877288, rho = 0.424462
104+
105+
nSV = 132, nBSV = 107
106+
107+
Total nSV = 132
108+
109+
  其中,#iter为迭代次数,nu 是你选择的核函数类型的参数,obj为SVM文件转换为的二次规划求解得到的最小值,rho为判决函数的偏置项b,nSV 为标准支持向量个数(0<a[i]<c),nBSV为边界上的支持向量个数(a[i]=c),Total nSV为支持向量总个数(对于两类来说,因为只有一个分类模型Total nSV = nSV,但是对于多类,这个是各个分类模型的nSV之和)。
110+
111+
  在目录下,还可以看到产生了一个train.model文件,可以用记事本打开,记录了训练后的结果。
112+
113+
svm_type c_svc //所选择的svm类型,默认为c_svc
114+
115+
kernel_type rbf //训练采用的核函数类型,此处为RBF核
116+
117+
gamma 0.0769231 //RBF核的参数γ
118+
119+
nr_class 2 //类别数,此处为两分类问题
120+
121+
total_sv 132 //支持向量总个数
122+
123+
rho 0.424462 //判决函数的偏置项b
124+
125+
label 1 -1 //原始文件中的类别标识
126+
127+
nr_sv 64 68 //每个类的支持向量机的个数
128+
129+
SV //以下为各个类的权系数及相应的支持向量
130+
131+
1 1:0.166667 2:1 3:-0.333333 … 10:-0.903226 11:-1 12:-1 13:1
132+
133+
0.5104832128985164 1:0.125 2:1 3:0.333333 … 10:-0.806452 12:-0.333333 13:0.5
134+
135+
………..
136+
137+
-1 1:-0.375 2:1 3:-0.333333…. 10:-1 11:-1 12:-1 13:1
138+
139+
-1 1:0.166667 2:1 3:1 …. 10:-0.870968 12:-1 13:0.5
140+
141+

SVM/use Python and NumPy/readme.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)