bool saveResults( const GestureRecognitionPipeline &pipeline, const string &filename ){ infoLog << "Saving results to file: " << filename << endl; fstream file( filename.c_str(), fstream::out ); if( !file.is_open() ){ errorLog << "Failed to open results file: " << filename << endl; return false; } file << pipeline.getTestAccuracy() << endl; vector< UINT > classLabels = pipeline.getClassLabels(); for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ file << pipeline.getTestPrecision( classLabels[k] ); if( k+1 < pipeline.getNumClassesInModel() ) file << "\t"; else file << endl; } for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ file << pipeline.getTestRecall( classLabels[k] ); if( k+1 < pipeline.getNumClassesInModel() ) file << "\t"; else file << endl; } for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ file << pipeline.getTestFMeasure( classLabels[k] ); if( k+1 < pipeline.getNumClassesInModel() ) file << "\t"; else file << endl; } MatrixDouble confusionMatrix = pipeline.getTestConfusionMatrix(); for(UINT i=0; i<confusionMatrix.getNumRows(); i++){ for(UINT j=0; j<confusionMatrix.getNumCols(); j++){ file << confusionMatrix[i][j]; if( j+1 < confusionMatrix.getNumCols() ) file << "\t"; }file << endl; } file.close(); infoLog << "Results saved." << endl; return true; }
int main (int argc, const char * argv[]) { TimeSeriesClassificationData trainingData; //This will store our training data GestureRecognitionPipeline pipeline; //This is a wrapper for our classifier and any pre/post processing modules string dirPath = "/home/vlad/AndroidStudioProjects/DataCapture/dataSetGenerator/build"; if (!trainingData.loadDatasetFromFile(dirPath + "/acc-training-set-segmented.data")) { printf("Cannot open training segmented set\n"); return 0; } printf("Successfully opened training data set ...\n"); DTW dtw; // LowPassFilter lpf(0.1, 1, 1); // pipeline.setPreProcessingModule(lpf); // DoubleMovingAverageFilter filter( 1000, 3 ); // pipeline.setPreProcessingModule(filter); //dtw.enableNullRejection( true ); //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 //dtw.setNullRejectionCoeff( 5 ); dtw.enableTrimTrainingData(true, 0.1, 90); // dtw.setOffsetTimeseriesUsingFirstSample(true); pipeline.setClassifier( dtw ); UINT KFolds = 5; /* Separate input dataset using KFold */ KfoldTimeSeriesData* kFoldTS = new KfoldTimeSeriesData(trainingData); if( !kFoldTS->spiltDataIntoKFolds(KFolds) ) { printf("BaseTGTestModel: Failed to spiltDataIntoKFolds!"); return 0; } UINT maxTrainigSetSize = trainingData.getNumSamples() * (KFolds - 1) / (KFolds * trainingData.getNumClasses()); // KFolds ofstream myfile; myfile.open ("example.txt"); Float acc = 0; for (GRT::UINT k = 1 ; k < KFolds; k++) { printf("Running tests for: %d fold", k); // maxTrainigSetSize // for (UINT trainingSetSize = 1; trainingSetSize <= maxTrainigSetSize; trainingSetSize ++) { /* Set up training datasets for current fold */ TimeSeriesClassificationData trainingDataset = kFoldTS->getTrainingFoldData(k, maxTrainigSetSize); /* Set up validation datasets for current fold */ TimeSeriesClassificationDataStream testDataset = kFoldTS->getTestFoldData(k); /* Log test dataset size */ //printf("Data set size: training %d; testing %d", // trainingDataset.getNumSamples(), testDataset.getNumSamples()); /* Run test for current fold */ pipeline.train(trainingDataset); pipeline.test(testDataset); myfile << pipeline.getTestAccuracy() << "\n"; // } } myfile.close(); printf("Accuracy = %f ; %d\n", acc, maxTrainigSetSize); }
int main (int argc, const char * argv[]) { //Load some training data from a file ClassificationData trainingData; if( !trainingData.loadDatasetFromFile("HelloWorldTrainingData.grt") ){ cout << "ERROR: Failed to load training data from file\n"; return EXIT_FAILURE; } cout << "Data Loaded\n"; //Print out some stats about the training data trainingData.printStats(); //Partition the training data into a training dataset and a test dataset. 80 means that 80% //of the data will be used for the training data and 20% will be returned as the test dataset ClassificationData testData = trainingData.partition(80); //Create a new Gesture Recognition Pipeline using an Adaptive Naive Bayes Classifier GestureRecognitionPipeline pipeline; pipeline.setClassifier( ANBC() ); //Train the pipeline using the training data if( !pipeline.train( trainingData ) ){ cout << "ERROR: Failed to train the pipeline!\n"; return EXIT_FAILURE; } //Save the pipeline to a file if( !pipeline.savePipelineToFile( "HelloWorldPipeline.grt" ) ){ cout << "ERROR: Failed to save the pipeline!\n"; return EXIT_FAILURE; } //Load the pipeline from a file if( !pipeline.loadPipelineFromFile( "HelloWorldPipeline.grt" ) ){ cout << "ERROR: Failed to load the pipeline!\n"; return EXIT_FAILURE; } //Test the pipeline using the test data if( !pipeline.test( testData ) ){ cout << "ERROR: Failed to test the pipeline!\n"; return EXIT_FAILURE; } //Print some stats about the testing cout << "Test Accuracy: " << pipeline.getTestAccuracy() << endl; cout << "Precision: "; for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ UINT classLabel = pipeline.getClassLabels()[k]; cout << "\t" << pipeline.getTestPrecision(classLabel); }cout << endl; cout << "Recall: "; for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ UINT classLabel = pipeline.getClassLabels()[k]; cout << "\t" << pipeline.getTestRecall(classLabel); }cout << endl; cout << "FMeasure: "; for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ UINT classLabel = pipeline.getClassLabels()[k]; cout << "\t" << pipeline.getTestFMeasure(classLabel); }cout << endl; MatrixDouble confusionMatrix = pipeline.getTestConfusionMatrix(); cout << "ConfusionMatrix: \n"; for(UINT i=0; i<confusionMatrix.getNumRows(); i++){ for(UINT j=0; j<confusionMatrix.getNumCols(); j++){ cout << confusionMatrix[i][j] << "\t"; }cout << endl; } return EXIT_SUCCESS; }
int main (int argc, const char * argv[]) { //We are going to use the Iris dataset, you can find more about the orginal dataset at: http://en.wikipedia.org/wiki/Iris_flower_data_set //Create a new instance of LabelledClassificationData to hold the training data LabelledClassificationData trainingData; //Load the training dataset from a file, the file should be in the same directory as this program if( !trainingData.loadDatasetFromFile("IrisData.txt") ){ cout << "Failed to load Iris data from file!\n"; return EXIT_FAILURE; } //Print some basic stats about the dataset we have loaded trainingData.printStats(); //Partition the training dataset into a training dataset and test dataset //We will use 60% of the data to train the algorithm and 40% of the data to test it //The true parameter flags that we want to use stratified sampling, which means there //should be an equal class distribution between the training and test datasets LabelledClassificationData testData = trainingData.partition( 60, true ); //Setup the gesture recognition pipeline GestureRecognitionPipeline pipeline; //Add a KNN classification algorithm as the main classifier with a K value of 10 pipeline.setClassifier( KNN(10) ); //Train the KNN algorithm using the training dataset if( !pipeline.train( trainingData ) ){ cout << "Failed to train the pipeline!\n"; return EXIT_FAILURE; } //Test the KNN model using the test dataset if( !pipeline.test( testData ) ){ cout << "Failed to test the pipeline!\n"; return EXIT_FAILURE; } //Print some metrics about how successful the classification was //Print the accuracy cout << "The classification accuracy was: " << pipeline.getTestAccuracy() << "%\n" << endl; //Print the precision for each class for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ UINT classLabel = pipeline.getClassLabels()[k]; double classPrecision = pipeline.getTestPrecision( classLabel ); cout << "The precision for class " << classLabel << " was " << classPrecision << endl; } cout << endl; //Print the recall for each class for(UINT k=0; k<pipeline.getNumClassesInModel(); k++){ UINT classLabel = pipeline.getClassLabels()[k]; double classRecall = pipeline.getTestRecall( classLabel ); cout << "The recall for class " << classLabel << " was " << classRecall << endl; } cout << endl; //Print the confusion matrix Matrix< double > confusionMatrix = pipeline.getTestConfusionMatrix(); cout << "Confusion Matrix: \n"; for(UINT i=0; i<confusionMatrix.getNumRows(); i++){ for(UINT j=0; j<confusionMatrix.getNumCols(); j++){ cout << confusionMatrix[i][j] << "\t"; } cout << endl; } cout << endl; return EXIT_SUCCESS; }
bool test( CommandLineParser &parser ){ infoLog << "Testing model..." << endl; string datasetFilename = ""; string modelFilename = ""; string resultsFilename = ""; //Get the model filename if( !parser.get("model-filename",modelFilename) ){ errorLog << "Failed to parse model filename from command line! You can set the model filename using the -m." << endl; printUsage(); return false; } //Get the filename if( !parser.get("dataset-filename",datasetFilename) ){ errorLog << "Failed to parse dataset filename from command line! You can set the dataset filename using the -f." << endl; printUsage(); return false; } //Get the model filename parser.get("results-filename",resultsFilename,string("results.txt")); //Load the pipeline from a file GestureRecognitionPipeline pipeline; infoLog << "- Loading model..." << endl; if( !pipeline.load( modelFilename ) ){ errorLog << "Failed to load model from file: " << modelFilename << endl; printUsage(); return false; } infoLog << "- Model loaded!" << endl; //Load the data to test the classifier ClassificationData data; infoLog << "- Loading Training Data..." << endl; if( !data.load( datasetFilename ) ){ errorLog << "Failed to load data!\n"; return false; } const unsigned int N = data.getNumDimensions(); infoLog << "- Num training samples: " << data.getNumSamples() << endl; infoLog << "- Num dimensions: " << N << endl; infoLog << "- Num classes: " << data.getNumClasses() << endl; //Test the classifier if( !pipeline.test( data ) ){ errorLog << "Failed to test pipeline!" << endl; return false; } infoLog << "- Test complete in " << pipeline.getTestTime()/1000.0 << " seconds with and accuracy of: " << pipeline.getTestAccuracy() << endl; return saveResults( pipeline, resultsFilename ); }