//-------------------------------------------------------------- void ofApp::setup(){ ofSetFrameRate(60); //Initialize the training and info variables infoText = ""; trainingClassLabel = 1; record = false; //The input to the training data will be the [x y] from the mouse, so we set the number of dimensions to 2 trainingData.setNumDimensions( 2 ); //Initialize the DTW classifier ANBC naiveBayes; //Turn on null rejection, this lets the classifier output the predicted class label of 0 when the likelihood of a gesture is low naiveBayes.enableNullRejection( false ); //Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection //You can increase this value if you find that your real-time gestures are not being recognized //If you are getting too many false positives then you should decrease this value naiveBayes.setNullRejectionCoeff( 3 ); //Add the classifier to the pipeline (after we do this, we don't need the naiveBayes instance anymore) pipeline.setClassifier( naiveBayes ); //Setup the colors for the 3 classes classColors.resize( 3 ); classColors[0] = ofColor(255, 0, 0); classColors[1] = ofColor(0, 255, 0); classColors[2] = ofColor(0, 0, 255); }
void prediction_axis_data(){ // Training and test data ClassificationData trainingData; ClassificationData testData; string file_path = "../../../data/"; string class_name = "5"; if( !trainingData.loadDatasetFromFile(file_path + "train/grt/" + class_name + ".txt") ){ std::cout <<"Failed to load training data!\n"; } if( !testData.loadDatasetFromFile(file_path + "test/grt/" + class_name + ".txt") ){ std::cout <<"Failed to load training data!\n"; } // Pipeline setup ANBC anbc; anbc.setNullRejectionCoeff(1); anbc.enableScaling(true); anbc.enableNullRejection(true); GestureRecognitionPipeline pipeline; pipeline.setClassifier(anbc); // Train the pipeline if( !pipeline.train( trainingData ) ){ std::cout << "Failed to train classifier!\n"; } // File stream ofstream outputFileStream(class_name + ".csv"); // Evaluation double accuracy = 0; outputFileStream << "actualClass,predictedClass,maximumLikelihood,lZ,lY,lZ,rZ,rY,rZ \n"; for(UINT i=0; i<testData.getNumSamples(); i++){ UINT actualClassLabel = testData[i].getClassLabel(); vector< double > inputVector = testData[i].getSample(); if( !pipeline.predict( inputVector )){ std::cout << "Failed to perform prediction for test sampel: " << i <<"\n"; } UINT predictedClassLabel = pipeline.getPredictedClassLabel(); double maximumLikelihood = pipeline.getMaximumLikelihood(); outputFileStream << actualClassLabel << "," << predictedClassLabel << "," << maximumLikelihood << "," << inputVector[0] << "," << inputVector[1] << "," << inputVector[2] << "," << inputVector[3] << "," << inputVector[4] << "," << inputVector[5] << "\n"; if( actualClassLabel == predictedClassLabel) accuracy++; } std::cout << "Test Accuracy testHandsUp : " << accuracy/double(testData.getNumSamples())*100.0 << " %\n"; }
int main (int argc, const char * argv[]) { //Create a new ANBC instance ANBC anbc; anbc.setNullRejectionCoeff( 10 ); anbc.enableScaling( true ); anbc.enableNullRejection( true ); //Load some training data to train the classifier ClassificationData trainingData; if( !trainingData.loadDatasetFromFile("ANBCTrainingData.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 bool trainSuccess = anbc.train( trainingData ); if( !trainSuccess ){ cout << "Failed to train classifier!\n"; return EXIT_FAILURE; } //Save the ANBC model to a file bool saveSuccess = anbc.save("ANBCModel.grt"); if( !saveSuccess ){ cout << "Failed to save the classifier model!\n"; return EXIT_FAILURE; } //Load the ANBC model from a file bool loadSuccess = anbc.load("ANBCModel.grt"); if( !loadSuccess ){ cout << "Failed to load the classifier model!\n"; return EXIT_FAILURE; } //Use the test dataset to test the ANBC 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 bool predictSuccess = anbc.predict( inputVector ); if( !predictSuccess ){ cout << "Failed to perform prediction for test sampel: " << i <<"\n"; return EXIT_FAILURE; } //Get the predicted class label UINT predictedClassLabel = anbc.getPredictedClassLabel(); vector< double > classLikelihoods = anbc.getClassLikelihoods(); vector< double > classDistances = anbc.getClassDistances(); //Update the accuracy if( classLabel == predictedClassLabel ) accuracy++; cout << "TestSample: " << i << " ClassLabel: " << classLabel << " PredictedClassLabel: " << predictedClassLabel << endl; } cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl; return EXIT_SUCCESS; }