-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboost_feature.py
More file actions
executable file
·66 lines (60 loc) · 2.41 KB
/
Copy pathboost_feature.py
File metadata and controls
executable file
·66 lines (60 loc) · 2.41 KB
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
#!/usr/bin/env python
from sys import argv,exit
from numpy import array,ones,load,fromiter
from pickle import dump as pdump
from Haar import haar_fns
from time import time
def train_feature(patches_fname,offsets_fname,f,haars_per_feat):
patches,offsets = load(patches_fname),load(offsets_fname)
haar_locs = load("haar_locs.pyz")
x_regressors = boost(patches,offsets[:,0],haars_per_feat,haar_locs)
y_regressors = boost(patches,offsets[:,1],haars_per_feat,haar_locs)
pdump(x_regressors,open("x_regressors_%d.pyz"%f,'w'),-1)
pdump(y_regressors,open("y_regressors_%d.pyz"%f,'w'),-1)
def boost(patches,offsets,haars_per_feat,haar_locs):
regs = []
tic = time()
for i in xrange(haars_per_feat):
rect,htype,th,lo,hi,lo_slice,hi_slice = select_haar(patches,offsets,haar_locs)
regs.append((rect,htype,th,lo,hi))
offsets[lo_slice] -= lo*0.5 # TODO: maybe use some percentage
offsets[hi_slice] -= hi*0.5
print "boost took:",time()-tic
return regs
def select_haar(patches,offsets,haar_locs):
# iterate over all the haar feature type/size/locations and learn the regressors
min_err = 99999
best_haar = (None,None,0,0,0,None,None) # rect,htype,th,lo,hi,loslice,hislice
for ul,dim in haar_locs:
for hfn in haar_fns:
vals = fromiter((hfn(p,ul,dim) for p in patches),dtype=patches.dtype,count=len(patches))
th,lo,hi,err = fit_regression_stump(vals,offsets)
if err < min_err:
min_err = err
best_haar = (ul.tolist()+dim.tolist(),haar_fns[hfn],th,lo,hi,offsets[vals<=th],offsets[vals>th])
return best_haar
# adapted from fitRegressionStump.m
def fit_regression_stump(vals,offsets):
w = ones(len(offsets))/float(len(offsets)) # fake weights
si = vals.argsort()
vals,offsets = vals[si],offsets[si]
cum_w = w.cumsum()
cum_offs = (offsets*w).cumsum()
sum_offs = cum_offs[-1]
cum_w_conj = 1 - cum_w
cum_offs_conj = sum_offs - cum_offs
b = cum_offs / cum_w # averages at each
a = cum_offs_conj/cum_w_conj - b
error = (w*offsets**2).sum() - 2*a*cum_offs_conj - 2*b*sum_offs + (a**2+2*a*b)*cum_w_conj + b**2
k = error.argmin()
if k == len(vals)-1: # last element
thresh = vals[k]
else:
thresh = (vals[k] + vals[k+1])/2.
return thresh,b[k],a[k]+b[k],error[k]
if __name__ == "__main__":
if len(argv) != 3:
exit("Usage: %s feat_idx haars_per_feat"%argv[0])
f,hpf = int(argv[1]),int(argv[2])
print "starting to train feature %d, with %d haar features"%(f,hpf)
train_feature("patches_%d.npz"%f,"offsets_%d.npz"%f,f,hpf)