void ValidateTheThreshold(AdaBoostClassifier& ada,ofstream& f) { int i,j; int falsepos,falseneg; for(i=0;i<validfacecount;i++) features[i] = 0.0; for(i=0;i<validfacecount;i++) for(j=0;j<ada.count;j++) if(ada.scs[j].Apply(validset[i])!=0) features[i] += ada.alphas[j]; nth_element(features,features+int(validfacecount*(1-node_det_goal)),features+validfacecount); ada.thresh = features[int(validfacecount*(1-node_det_goal))]; falseneg = facecount; for(i=0;i<facecount;i++) falseneg -= ada.ApplyImagePatch(trainset[i]); falsepos = 0; for(i=facecount;i<totalcount;i++) falsepos += ada.ApplyImagePatch(trainset[i]); f<<"-----Use the validation set to determine a threshold ------"<<endl; f<<ada.thresh<<"\t"<<falseneg<<"\t"<<falsepos<<endl; }
void FFS_Histogram(AdaBoostClassifier& ada,const int round,int* const curresult,ofstream& f,const int method) { int i,j,k; int* hist_pos; int* hist_neg; int* tempresult; int error,minerror,minindex; int thresh,minthresh; bool* used; tempresult = new int[totalcount]; ASSERT(tempresult!=NULL); hist_pos = new int[round+1]; ASSERT(hist_pos!=NULL); hist_neg = new int[round+1]; ASSERT(hist_neg!=NULL); used = new bool[totalfeatures]; ASSERT(used!=NULL); for(i=0;i<totalfeatures;i++) used[i]=false; f<<"Begin Histogram FFS"<<endl; ada.InitToGivenSize(round); for(i=0;i<totalcount;i++) curresult[i] = 0; for(i=0;i<round;i++) { minerror = INT_MAX; minindex = -1; minthresh = INT_MAX; for(j=0;j<totalfeatures;j++) { if(used[j]) continue; int temp; for(k=0;k<totalcount;k++) tempresult[k] = curresult[k] + table[j][k]; BuildHistogram(tempresult,i+2,hist_pos,hist_neg,facecount); error = temp = totalcount-facecount; for(k=0;k<=i+1;k++) { temp = temp - hist_neg[k] + hist_pos[k]; if(temp<error) { error = temp; thresh = k+1; } } if(error<minerror) { minerror = error; minindex = j; minthresh = thresh; } } used[minindex]=true; ada.alphas[ada.count] = 1; ada.scs[ada.count] = classifiers[minindex]; ada.count++; ada.thresh = REAL(minthresh); for(j=0;j<totalcount;j++) curresult[j] += table[minindex][j]; f<<i+1<<"\t"<<minindex<<"\t"<<minerror<<"\t"<<classifiers[minindex].thresh<<endl; } BuildHistogram(curresult,round+1,hist_pos,hist_neg,facecount); for(i=1;i<=round;i++) { hist_neg[i] += hist_neg[i-1]; hist_pos[i] += hist_pos[i-1]; } i=0; while(hist_neg[i]<(totalcount-facecount)*(1-node_fp_goal)) i++; switch(method) { case GOAL_METHOD_MIN_ERROR: break; case GOAL_METHOD_VALID_DR: ValidateTheThreshold(ada,f); break; case GOAL_METHOD_FIX_FP: ada.thresh = i + REAL(0.5); break; } f<<ada.thresh<<"\t"<<hist_pos[i-1]<<"\t"<<totalcount-facecount-hist_neg[i-1]<<endl; int fn,fp; f<<"Training Error:"<<endl; fn = 0; for(i=0;i<facecount;i++) if(ada.ApplyImagePatch(trainset[i])==0) fn++; fp = 0; for(i=facecount;i<totalcount;i++) if(ada.ApplyImagePatch(trainset[i])==1) fp++; f<<fn<<"\t"<<fp<<endl; f<<"Validation Error:"<<endl; fn = 0; for(i=0;i<validfacecount;i++) if(ada.ApplyImagePatch(validset[i])==0) fn++; fp = 0; for(i=validfacecount;i<validcount;i++) if(ada.ApplyImagePatch(validset[i])==1) fp++; f<<fn<<"\t"<<fp<<endl; f<<"End of Histogram FFS"<<endl; delete[] hist_pos; hist_pos=NULL; delete[] hist_neg; hist_neg=NULL; delete[] tempresult; tempresult=NULL; }