void compute_boosting_score_factor(const PartApp &part_app, int pidx, kma::ImageContent *kmaimg, const vector<PartBBox> &part_samples, const vector<double> &vect_scales, Factor1d &factor) { assert(part_samples.size() == vect_scales.size()); factor.varidx = pidx; /** add border */ int added_border = 0; kma::ImageContent *kmaimg_border = add_image_border2(kmaimg, 50, added_border); cout << "added_border: " << added_border << endl; AdaBoostClassifier abc; part_app.loadClassifier(abc, pidx); int num_samples = part_samples.size(); factor.fval.resize(boost::extents[num_samples]); for (int sampidx = 0; sampidx < num_samples; ++sampidx) { PartBBox bbox = part_samples[sampidx]; /** add offset to position of bounding box */ bbox.part_pos(0) += added_border; bbox.part_pos(1) += added_border; /** comput features */ vector<float> all_features; PartBBox adjusted_rect; // bool bres = part_detect::compute_part_bbox_features_scale(part_app.m_abc_param, part_app.m_window_param, // bbox, kmaimg_border, pidx, 1, // all_features, adjusted_rect); // assert(vect_scales[sampidx] == 1.0); bool bres = part_detect::compute_part_bbox_features_scale(part_app.m_abc_param, part_app.m_window_param, bbox, kmaimg_border, pidx, vect_scales[sampidx], all_features, adjusted_rect); factor.fval[sampidx] = part_app.m_abc_param.min_score(); /** evaluate classifier */ if (bres) { float score = abc.evaluateFeaturePoint(all_features, true); if (score > part_app.m_abc_param.min_score()) factor.fval[sampidx] = score; } else { cout << "warning: failed to compute features" << endl; } }// samples delete kmaimg_border; }
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 ForwardFeatureSelection(AdaBoostClassifier& ada,const int round,const bool update,const int method) { int* curresult; int* indexes; ofstream f; ada.Clear(); curresult = new int[totalcount]; ASSERT(curresult!=NULL); indexes = new int[totalfeatures]; ASSERT(indexes != NULL); f.open(FFS_log_filename); //cerr << "training weak classifier " << endl; TrainWeakClassifiers(); LoadWeakClassifiers(indexes); FFS_Histogram(ada,round,curresult,f,method); if(update) AppendAdaBoostClassifier(ada); delete[] curresult; curresult=NULL; delete[] indexes; indexes=NULL; f.flush(); f.close(); }
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; }