-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm_train.py
90 lines (78 loc) · 2.99 KB
/
svm_train.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
import cv2
import numpy as np
from numpy.linalg import norm
svm_params = dict( kernel_type = cv2.ml.SVM_RBF,
svm_type = cv2.ml.SVM_C_SVC,
C=2.67, gamma=5.383 )
class StatModel(object):
def load(self, fn):
self.model.load(fn) #python rapper bug
def save(self, fn):
self.model.save(fn)
class SVM(StatModel):
def __init__(self, C = 1, gamma = 0.5):
self.model = cv2.ml.SVM_create()
# self.model.setGamma(gamma)
# self.model.setC(C)
# self.model.setKernel(cv2.SVM_RBF)
# self.model.setType(cv2.SVM_C_SVC)
def train(self, samples, responses):
self.model.train(samples,cv2.ml.ROW_SAMPLE,responses) # inbuilt training function
def predict(self, samples):
return self.model.predict(samples)[1]
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:100,:100], bin[100:,:100], bin[:100,100:], bin[100:,100:]
mag_cells = mag[:100,:100], mag[100:,:100], mag[:100,100:], mag[100:,100:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
#Here goes my wrappers:
def hog_single(img):
samples=[]
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:100,:100], bin[100:,:100], bin[:100,100:], bin[100:,100:]
mag_cells = mag[:100,:100], mag[100:,:100], mag[:100,100:], mag[100:,100:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
def trainSVM(num):
imgs=[]
for i in range(65,num+65):
print('Class '+chr(i)+' is being loaded ')
for j in range(1,401):
imgs.append(cv2.imread('DB/'+chr(i)+'_'+str(j)+'.jpg',0)) # all images saved in a list
labels = np.repeat(np.arange(1,num+1), 400) # label for each corresponding image saved above
samples=preprocess_hog(imgs) # images sent for pre processeing using hog which returns features for the images
print('SVM is building wait some time ...')
print(len(labels))
print(len(samples))
model = SVM(C=2.67, gamma=5.383)
model.train(samples, labels) # features trained against the labels using svm
return model
def predict(model,img):
samples=hog_single(img)
resp=model.predict(samples)
return resp