-
Notifications
You must be signed in to change notification settings - Fork 2
/
SVM.py
executable file
·55 lines (51 loc) · 1.39 KB
/
SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
######
#
# Mail npuxpli@mail.nwpu.edu.cn
# Author LiXiping
# Date 2019/09/20 16:14:26
#
######
import argparse
import time
from lib.svm import svm_problem, svm_parameter
from lib.svmutil import *
def train(args):
"""
使用图像的特征文件 来训练生成model文件
:return:
"""
y, x = svm_read_problem(args.train_file)
param = svm_parameter('-s 0 -t 1 -c 5 -b 1')
prob = svm_problem(y, x)
model = svm_train(prob, param)
svm_save_model(args.model_path, model)
def decode(args):
yt, xt = svm_read_problem(args.test_file)
model = svm_load_model(args.model_path)
p_label, p_acc, p_val = svm_predict(yt, xt, model)
cnt = 0
for item in p_label:
print('%d' % item, end =',')
cnt += 1
if cnt % 8 == 0:
print('')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--train_file")
parser.add_argument("--model_path")
parser.add_argument("--test_file")
parser.add_argument("--decode", default= True)
#parser.add_argument("--decode", default=False)
args = parser.parse_args()
if args.decode:
print("decoding...")
t2 = time.time()
decode(args)
print("t2:",time.time()-t2)
else:
print("training...")
t1 = time.time()
train(args)
print("t1:",time.time()-t1)