void testAdaBoost(int colorspace) { ImageLoader loaderTrue("c:\\Images\\true", colorspace); ImageLoader loaderFalse("c:\\Images\\false", colorspace); ImageLoader loaderTest("c:\\Images\\test", colorspace); vector< Image* > slikeTrue = loaderTrue.loadNextImages(); vector< Image* > slikeFalse= loaderFalse.loadNextImages(); Feature::loadBaseFeatures("bftest.txt"); int tmpChans[] = {0, 1, 2}; vector< int > channels(tmpChans, tmpChans + 3 ); //kanali nad kojima ce se raditi vector < Feature > tmp = Feature::generateAll( 20, 20, 3, 1.25, channels ); writeFeatures( tmp ); AdaBoost boost; vector< Feature > level = boost.startTraining( slikeTrue, slikeFalse, tmp, 20); cout << "IZABRANI FEATURE-i" << endl; writeFeatures( level ); vector< Image * > testSlike = loaderTest.loadNextImages(); //testSlike[0]->showImageOverlappedWithFeature( level[1], true ); testSlike[0]->evaluirajLevel( level ); //for(int i=0; i<testSlike.size(); i++) { // testSlike[i]->evaluirajLevel( level ); //} }
int main(int argc, char* argv[]) { ParameterABCrossValidate parameters = parseCommandline(argc, argv); if (parameters.verbose) { std::string boostingTypeName[3] = {"discrete", "real", "gentle"}; std::cerr << std::endl; std::cerr << "Traing data: " << parameters.trainingDataFilename << std::endl; std::cerr << " Type: " << boostingTypeName[parameters.boostingType] << std::endl; std::cerr << " #rounds: " << parameters.roundTotal << std::endl; std::cerr << " #" << parameters.k_cv << " fold cross-valiation" << std::endl; std::cerr << std::endl; } AdaBoost adaBoost; adaBoost.setBoostingType(parameters.boostingType); adaBoost.readAllSamples(parameters.trainingDataFilename, parameters.k_cv); double accuracyAll = 0.0; for (int i = 0; i < parameters.k_cv; i++) { if (parameters.verbose) std::cout << "====== CV Round " << i << " ======" << std::endl; adaBoost.setCrossValidationSampels(i, parameters.k_cv); adaBoost.train(parameters.roundTotal, parameters.verbose); TestResult res = adaBoost.test(false, ""); accuracyAll += res.accuracyAll; if (parameters.verbose) { std::cout << "Accuracy = " << res.accuracyAll << std::endl; std::cout << " precision: " << res.precision << " , negative: " << res.recall << std::endl; } } std::cout << "\nCross Validation Accuracy: " << accuracyAll / parameters.k_cv << std::endl; }
void CascadeTrainer::Train(float f, float d, float f_target) { float F[1000]; float D[1000]; F[0] = 1.0; D[0] = 1.0; int i = 0; while (F[i] > f_target) { i++; F[i] = F[i - 1]; int n = 0; AdaBoost *stage = new AdaBoost(training_set); stages.push_back(stage); while (F[i] > f * F[i - 1]) { n++; stage->Clear(); for (int i = 0; i < n; i++) stage->TrainWeak(); Mat_<label_t> labels; Classify(test_set.data, labels); // Discrease threshold } if (F[i] > f_target) { // Adjust negative set } } }
int main (int argc, const char * argv[]) { //Create a new AdaBoost instance AdaBoost adaBoost; //Set the weak classifier you want to use adaBoost.setWeakClassifier( DecisionStump() ); //Load some training data to train the classifier ClassificationData trainingData; if( !trainingData.load("AdaBoostTrainingData.grt") ){ cout << "Failed to load training data!\n"; return EXIT_FAILURE; } //Use 20% of the training dataset to create a test dataset ClassificationData testData = trainingData.partition( 80 ); //Train the classifier if( !adaBoost.train( trainingData ) ){ cout << "Failed to train classifier!\n"; return EXIT_FAILURE; } //Save the model to a file if( !adaBoost.save("AdaBoostModel.grt") ){ cout << "Failed to save the classifier model!\n"; return EXIT_FAILURE; } //Load the model from a file if( !adaBoost.load("AdaBoostModel.grt") ){ cout << "Failed to load the classifier model!\n"; return EXIT_FAILURE; } //Use the test dataset to test the AdaBoost model double accuracy = 0; for(UINT i=0; i<testData.getNumSamples(); i++){ //Get the i'th test sample UINT classLabel = testData[i].getClassLabel(); vector< double > inputVector = testData[i].getSample(); //Perform a prediction using the classifier if( !adaBoost.predict( inputVector ) ){ cout << "Failed to perform prediction for test sampel: " << i <<"\n"; return EXIT_FAILURE; } //Get the predicted class label UINT predictedClassLabel = adaBoost.getPredictedClassLabel(); double maximumLikelhood = adaBoost.getMaximumLikelihood(); vector< double > classLikelihoods = adaBoost.getClassLikelihoods(); vector< double > classDistances = adaBoost.getClassDistances(); //Update the accuracy if( classLabel == predictedClassLabel ) accuracy++; cout << "TestSample: " << i << " ClassLabel: " << classLabel; cout << " PredictedClassLabel: " << predictedClassLabel << " Likelihood: " << maximumLikelhood; cout << endl; } cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl; return EXIT_SUCCESS; }