-
Notifications
You must be signed in to change notification settings - Fork 0
/
Train & Test SVM.py
executable file
·127 lines (98 loc) · 3.73 KB
/
Train & Test 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!coding: utf-8
from svm import *
import os, sys
import Image
import psyco
psyco.full()
##C=10
##KERNEL = RBF
if not os.path.isfile(os.path.join(MODEL_FOLDER, MODEL_FILE)) or GENERATE_ANYWAY:
#Si le modèle n'existe pas ou que l'on veut spécifie GENERATE_ANYWAY=True, on le génère. Sinon, on le charge.
print """
##############################################################################
############################ TRAINING ##################################
##############################################################################
"""
labels = []
samples = []
print "LOADING IMAGES..."
train_elem = '3de2mt'
#Train everything
train_elem = ''
for folder, subfolders, files in os.walk(TRAINING_FOLDER):
if (folder[0] != ".") and (folder[-1] in train_elem or train_elem == ''):
loaded = False
for file in [file for file in files if 'bmp' in file]:
if not loaded:
print "folder", folder, "loaded"
loaded = True
im = Image.open(os.path.join(folder, file))
labels.append(ord(folder[-1])-65)
samples.append(map(lambda e:e/255., list(im.getdata())))
print "Done.\n"
print "GENERATING MODEL..."
problem = svm_problem(labels, samples);
size = len(samples)
#param = svm_parameter(C = 10,nr_weight = 2,weight_label = [1,0],weight = [10,1], probability=1)
param = svm_parameter(kernel_type = KERNEL, C=C, probability = 1)
#kernels : LINEAR, POLY, RBF, and SIGMOID
#types : C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, and NU_SVR
model = svm_model(problem,param)
model.save(os.path.join(MODEL_FOLDER, MODEL_FILE))
print "Done.\n"
else:
model = svm_model(os.path.join(MODEL_FOLDER, MODEL_FILE))
print "Model successfully loaded."
def predict(model, chemin_image):
if not os.path.isfile(chemin_image):
print "FICHIER INEXISTANT"
return
data = list(Image.open(chemin_image).convert('L').getdata())
data = map(lambda e:e/255., data)
prediction = model.predict(data)
probability = model.predict_probability(data)
if VERBOSE:
print probability
return prediction
def analyze_folder(folder):
errors = 0
nb = 0
for folder, subfolders, files in os.walk(folder):
for file in [file for file in files if 'bmp' in file]:
prediction = predict(model, os.path.join(folder, file))
if prediction != ord(folder[-1])-65:
errors += 1
nb += 1
print "Errors: %d / %d\n" % (errors, nb)
return 100.*errors/nb
##print """
################################################################################
############################## TEST MODEL ################################
################################################################################
##"""
##
##error_rate_tr = 0
##nb_tr = 0
##print "Test on training set:"
##print "---------------------"
##for subdir in os.listdir(TRAINING_FOLDER):
## if subdir[0] != ".":
## print "Testing on", subdir[-1]
## error_rate_tr += analyze_folder(os.path.join(TRAINING_FOLDER, subdir))
## nb_tr += 1
##error_rate_tr /= nb_tr
##
##error_rate_test = 0
##nb_test = 0
##print "Test on test set:"
##print "-----------------"
##for subdir in os.listdir(TEST_FOLDER):
## if subdir[0] != ".":
## print "Testing on", subdir[-1]
## error_rate_test += analyze_folder(os.path.join(TEST_FOLDER, subdir))
## nb_test += 1
##error_rate_test /= nb_test
##
##print
##print "Error on training set:", error_rate_tr, '%'
##print "Error on test set:", error_rate_test, '%'