Skip to content

Commit

Permalink
classify/private/forestFindThr.cpp: return 'gain' (improvement) inste…
Browse files Browse the repository at this point in the history
…ad of impurity
  • Loading branch information
pdollar committed Sep 30, 2013
1 parent d7e6d6b commit 4e81834
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions classify/forestTrain.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
%
% See also forestApply, fernsClfTrain
%
% Piotr's Image&Video Toolbox Version 3.10
% Piotr's Image&Video Toolbox Version NEW
% Copyright 2012 Piotr Dollar. [pdollar-at-caltech.edu]
% Please email me if you find bugs, or have suggestions or questions!
% Licensed under the Simplified BSD License [see external/bsd.txt]
Expand Down Expand Up @@ -100,9 +100,9 @@
% train split and continue
fids1=wswor(fWts,F1,4); data1=data(dids1,fids1);
[~,order1]=sort(data1); order1=uint32(order1-1);
[fid,thr,gini]=forestFindThr(data1,hs1,dWts(dids1),order1,H);
[fid,thr,gain]=forestFindThr(data1,hs1,dWts(dids1),order1,H);
fid=fids1(fid); left=data(dids1,fid)<thr; count0=nnz(left);
if( gini<100 && count0>=minChild && (count(k)-count0)>=minChild )
if( gain>1e-10 && count0>=minChild && (count(k)-count0)>=minChild )
child(k)=K; fids(k)=fid-1; thrs(k)=thr;
dids{K}=dids1(left); dids{K+1}=dids1(~left);
depth(K:K+1)=depth(k)+1; K=K+2;
Expand Down
24 changes: 12 additions & 12 deletions classify/private/forestFindThr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@ inline double gini( double p ) { return p*p; }

void forestFindThr( int H, int N, int F, const float *data,
const uint32 *hs, const float *ws, const uint32 *order,
uint32 &fid, float &thr, double &impurity )
uint32 &fid, float &thr, double &gain )
{
int i, j, j1, j2, h; double *Wl, *Wr, *W; float *data1; uint32 *order1;
double impurity1, w=0, wl, wr, g=0, gl, gr;
double v, v0, v1, w=0, wl, wr, g=0, gl, gr;
Wl=new double[H]; Wr=new double[H]; W=new double[H];
for( i=0; i<H; i++ ) W[i] = 0;
for( j=0; j<N; j++ ) { w+=ws[j]; W[hs[j]-1]+=ws[j]; }
for( i=0; i<H; i++ ) g += gini(W[i]);
fid = 1; thr = 0; impurity = 1e5;
fid = 1; thr = 0; v0 = v = (1-g/w/w);
for( i=0; i<F; i++ ) {
order1=(uint32*) order+i*N; data1=(float*) data+i*size_t(N);
for( j=0; j<H; j++ ) { Wl[j]=0; Wr[j]=W[j]; } gl=wl=0; gr=g; wr=w;
for( j=0; j<N-1; j++ ) {
// gini=1-\sum_h p_h^2; impurity=ginil*wl/w+ginir*wr/w
// gini=1-\sum_h p_h^2; v=ginil*wl/w+ginir*wr/w
j1=order1[j]; j2=order1[j+1]; h=hs[j1]-1;
wl+=ws[j1]; gl-=gini(Wl[h]); Wl[h]+=ws[j1]; gl+=gini(Wl[h]);
wr-=ws[j1]; gr-=gini(Wr[h]); Wr[h]-=ws[j1]; gr+=gini(Wr[h]);
impurity1 = (wl-gl/wl)/w + (wr-gr/wr)/w;
if( impurity1<impurity && data1[j2]-data1[j1]>=1e-6f ) {
impurity=impurity1; fid=i+1; thr=0.5f*(data1[j1]+data1[j2]); }
v1 = (wl-gl/wl)/w + (wr-gr/wr)/w;
if( v1<v && data1[j2]-data1[j1]>=1e-6f ) {
v=v1; fid=i+1; thr=0.5f*(data1[j1]+data1[j2]); }
}
}
delete [] Wl; delete [] Wr; delete [] W;
delete [] Wl; delete [] Wr; delete [] W; gain = v0-v;
}

// [fid,thr,impurity] = mexFunction(data,hs,ws,order,H);
// [fid,thr,gain] = mexFunction(data,hs,ws,order,H);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int H, N, F; float *data, *ws, thr;
double impurity; uint32 *hs, *order, fid;
double gain; uint32 *hs, *order, fid;
data = (float*) mxGetData(prhs[0]);
hs = (uint32*) mxGetData(prhs[1]);
ws = (float*) mxGetData(prhs[2]);
order = (uint32*) mxGetData(prhs[3]);
H = (int) mxGetScalar(prhs[4]);
N = (int) mxGetM(prhs[0]);
F = (int) mxGetN(prhs[0]);
forestFindThr(H,N,F,data,hs,ws,order,fid,thr,impurity);
forestFindThr(H,N,F,data,hs,ws,order,fid,thr,gain);
plhs[0] = mxCreateDoubleScalar(fid);
plhs[1] = mxCreateDoubleScalar(thr);
plhs[2] = mxCreateDoubleScalar(impurity);
plhs[2] = mxCreateDoubleScalar(gain);
}
3 changes: 2 additions & 1 deletion external/history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Version NEW
-opticalFlow.m: various improvements, changes to parameters
-toolboxCompile.m: added option to compile w/o openMP
-matlab/medianw.m: added fast weighted median
-minor: imagesAlignSeq, bbApply, convConst, imResample, gradientMex
-minor: imagesAlignSeq, bbApply, convConst, imResample, gradientMex,
forestTrain.m

Version 3.23 (20-Aug-2013)
-fhog.m: added heavily optimized FHOG (Felzenszwalb's HOG) features
Expand Down

0 comments on commit 4e81834

Please sign in to comment.