本文整理汇总了C++中GestureRecognitionPipeline类的典型用法代码示例。如果您正苦于以下问题:C++ GestureRecognitionPipeline类的具体用法?C++ GestureRecognitionPipeline怎么用?C++ GestureRecognitionPipeline使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GestureRecognitionPipeline类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main (int argc, const char * argv[])
{
//Create a new gesture recognition pipeline
GestureRecognitionPipeline pipeline;
//Add an ANBC module
pipeline.setClassifier( ANBC() );
//Add a ClassLabelFilter as a post processing module with a minCount of 5 and a buffer size of 10
pipeline.addPostProcessingModule( ClassLabelFilter(5,10) );
//Load some training data to train and test the classifier
ClassificationData trainingData;
ClassificationData testData;
if( !trainingData.loadDatasetFromFile("ClassLabelFilterTrainingData.txt") ){
cout << "Failed to load training data!\n";
return EXIT_FAILURE;
}
if( !testData.loadDatasetFromFile("ClassLabelFilterTestData.txt") ){
cout << "Failed to load training data!\n";
return EXIT_FAILURE;
}
//Train the classifier
if( !pipeline.train( trainingData ) ){
cout << "Failed to train classifier!\n";
return EXIT_FAILURE;
}
//Use the test dataset to demonstrate the output of the ClassLabelFilter
for(UINT i=0; i<testData.getNumSamples(); i++){
VectorDouble inputVector = testData[i].getSample();
if( !pipeline.predict( inputVector ) ){
cout << "Failed to perform prediction for test sampel: " << i <<"\n";
return EXIT_FAILURE;
}
//Get the predicted class label (this will be the processed class label)
UINT predictedClassLabel = pipeline.getPredictedClassLabel();
//Get the unprocessed class label (i.e. the direct output of the classifier)
UINT unprocessedClassLabel = pipeline.getUnProcessedPredictedClassLabel();
//Also print the results to the screen
cout << "Processed Class Label: \t" << predictedClassLabel << "\tUnprocessed Class Label: \t" << unprocessedClassLabel << endl;
}
return EXIT_SUCCESS;
}
示例2: main
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 set\n");
return 0;
}
printf("Successfully opened training data set ...\n");
HMM hmm;
hmm.setHMMType( HMM_CONTINUOUS );
hmm.setDownsampleFactor( 5 );
hmm.setAutoEstimateSigma( true );
hmm.setSigma( 20.0 );
hmm.setModelType( HMM_LEFTRIGHT );
hmm.setDelta( 1 );
// LowPassFilter lpf(0.1, 1, 3);
// pipeline.setPreProcessingModule(lpf);
pipeline.setClassifier( hmm );
pipeline.train(trainingData, 20);
//You can then get then get the accuracy of how well the pipeline performed during the k-fold cross validation testing
double accuracy = pipeline.getCrossValidationAccuracy();
printf("Accuracy: %f\n", accuracy);
}
示例3: main
int main (int argc, const char * argv[])
{
GestureRecognitionPipeline pipeline;
ANBC anbc;
ClassificationData trainingData;
trainingData.loadDatasetFromFile("training-data.txt")
pipeline.setClassifier(anbc);
pipeline.train(trainingData);
VectorDouble inputVector(SAMPLE_DIMENSION) = getDataFromSensor();
pipeline.predict(inputVector);
UINT predictedClassLabel = pipeline.getPredictedClassLabel();
double maxLikelihood = pipeline.getMaximumLikelihood();
printf("predictedClassLabel : %d , MaximumLikelihood : %f \n", predictedClassLabel, maxLikelihood);
return EXIT_SUCCESS;
}
开发者ID:AravinthPanch,项目名称:gesture-recognition-for-human-robot-interaction,代码行数:20,代码来源:grt-pipeline.cpp
示例4: train
bool train( CommandLineParser &parser ){
infoLog << "Training regression model..." << endl;
string trainDatasetFilename = "";
string modelFilename = "";
//Get the filename
if( !parser.get("filename",trainDatasetFilename) ){
errorLog << "Failed to parse filename from command line! You can set the filename using the -f." << endl;
printHelp();
return false;
}
//Get the model filename
parser.get("model-filename",modelFilename);
//Load the training data to train the model
ClassificationData trainingData;
infoLog << "- Loading Training Data..." << endl;
if( !trainingData.load( trainDatasetFilename ) ){
errorLog << "Failed to load training data!\n";
return false;
}
const unsigned int N = trainingData.getNumDimensions();
const unsigned int K = trainingData.getNumClasses();
infoLog << "- Num training samples: " << trainingData.getNumSamples() << endl;
infoLog << "- Num input dimensions: " << N << endl;
infoLog << "- Num classes: " << K << endl;
float learningRate = 0;
float minChange = 0;
unsigned int maxEpoch = 0;
unsigned int batchSize = 0;
parser.get( "learning-rate", learningRate );
parser.get( "min-change", minChange );
parser.get( "max-epoch", maxEpoch );
parser.get( "batch-size", batchSize );
infoLog << "Softmax settings: learning-rate: " << learningRate << " min-change: " << minChange << " max-epoch: " << maxEpoch << " batch-size: " << batchSize << endl;
//Create a new softmax instance
bool enableScaling = true;
Softmax classifier(enableScaling,learningRate,minChange,maxEpoch,batchSize);
//Create a new pipeline that will hold the classifier
GestureRecognitionPipeline pipeline;
//Add the classifier to the pipeline
pipeline << classifier;
infoLog << "- Training model...\n";
//Train the classifier
if( !pipeline.train( trainingData ) ){
errorLog << "Failed to train model!" << endl;
return false;
}
infoLog << "- Model trained!" << endl;
infoLog << "- Saving model to: " << modelFilename << endl;
//Save the pipeline
if( pipeline.save( modelFilename ) ){
infoLog << "- Model saved." << endl;
}else warningLog << "Failed to save model to file: " << modelFilename << endl;
infoLog << "- TrainingTime: " << pipeline.getTrainingTime() << endl;
string logFilename = "";
if( parser.get( "log-filename", logFilename ) && logFilename.length() > 0 ){
infoLog << "Writing training log to: " << logFilename << endl;
fstream logFile( logFilename.c_str(), fstream::out );
if( !logFile.is_open() ){
errorLog << "Failed to open training log file: " << logFilename << endl;
return false;
}
Vector< TrainingResult > trainingResults = pipeline.getTrainingResults();
for(UINT i=0; i<trainingResults.getSize(); i++){
logFile << trainingResults[i].getTrainingIteration() << "\t" << trainingResults[i].getAccuracy() << endl;
}
logFile.close();
}
return true;
}
示例5: metrics_separate_data
void metrics_separate_data(){
// Training and test data
ClassificationData trainingData;
ClassificationData testData;
string file_path = "../../../data/";
if( !trainingData.loadDatasetFromFile(file_path + "train/grt/12345.txt") ){
std::cout <<"Failed to load training data!\n";
}
ANBC anbc;
anbc.enableScaling(true);
anbc.enableNullRejection(true);
SVM svm(SVM::RBF_KERNEL);
svm.enableScaling(true);
svm.enableNullRejection(true);
MinDist minDist;
minDist.setNumClusters(4);
minDist.enableScaling(true);
minDist.enableNullRejection(true);
ofstream outputFileStream("accuracy-mindist.csv");
outputFileStream << "classLabel,nullRejectionCoeff,accuracy, \n";
for(int class_name = 1; class_name<=5; class_name++){
if( !testData.loadDatasetFromFile(file_path + "test/grt/" + to_string(class_name) + ".txt") ){
std::cout <<"Failed to load training data!\n";
}
for(double nullRejectionCoeff = 0; nullRejectionCoeff <= 10; nullRejectionCoeff=nullRejectionCoeff+0.2){
// anbc.setNullRejectionCoeff(nullRejectionCoeff);
// svm.setNullRejectionCoeff(nullRejectionCoeff);
minDist.setNullRejectionCoeff(nullRejectionCoeff);
GestureRecognitionPipeline pipeline;
// pipeline.setClassifier(anbc);
// pipeline.setClassifier(svm);
pipeline.setClassifier(minDist);
// Train the pipeline
if( !pipeline.train( trainingData ) ){
std::cout << "Failed to train classifier!\n";
}
// Evaluation
double accuracy = 0;
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();
if( actualClassLabel == predictedClassLabel) accuracy++;
}
outputFileStream << class_name << ',' << nullRejectionCoeff << ',' << accuracy/double(testData.getNumSamples())*100.0 << '\n';
cout<< "Done" << endl;
}
}
//---------------------- Null Gesture Test -----------------//
int class_name = 0;
if( !testData.loadDatasetFromFile(file_path + "test/grt/" + to_string(class_name) + ".txt") ){
std::cout <<"Failed to load training data!\n";
}
for(double nullRejectionCoeff = 0; nullRejectionCoeff <= 10; nullRejectionCoeff=nullRejectionCoeff+0.2){
// anbc.setNullRejectionCoeff(nullRejectionCoeff);
// svm.setNullRejectionCoeff(nullRejectionCoeff);
minDist.setNullRejectionCoeff(nullRejectionCoeff);
GestureRecognitionPipeline pipeline;
// pipeline.setClassifier(anbc);
// pipeline.setClassifier(svm);
pipeline.setClassifier(minDist);
// Train the pipeline
if( !pipeline.train( trainingData ) ){
std::cout << "Failed to train classifier!\n";
//.........这里部分代码省略.........
示例6: main
int main (int argc, const char * argv[])
{
//Turn on the training log so we can print the training status of the LinearRegression to the screen
TrainingLog::enableLogging( true );
//Load the training data
RegressionData trainingData;
RegressionData testData;
if( !trainingData.loadDatasetFromFile("LinearRegressionTrainingData.txt") ){
cout << "ERROR: Failed to load training data!\n";
return EXIT_FAILURE;
}
if( !testData.loadDatasetFromFile("LinearRegressionTestData.txt") ){
cout << "ERROR: Failed to load test data!\n";
return EXIT_FAILURE;
}
//Make sure the dimensionality of the training and test data matches
if( trainingData.getNumInputDimensions() != testData.getNumInputDimensions() ){
cout << "ERROR: The number of input dimensions in the training data (" << trainingData.getNumInputDimensions() << ")";
cout << " does not match the number of input dimensions in the test data (" << testData.getNumInputDimensions() << ")\n";
return EXIT_FAILURE;
}
if( testData.getNumTargetDimensions() != testData.getNumTargetDimensions() ){
cout << "ERROR: The number of target dimensions in the training data (" << testData.getNumTargetDimensions() << ")";
cout << " does not match the number of target dimensions in the test data (" << testData.getNumTargetDimensions() << ")\n";
return EXIT_FAILURE;
}
cout << "Training and Test datasets loaded\n";
//Print the stats of the datasets
cout << "Training data stats:\n";
trainingData.printStats();
cout << "Test data stats:\n";
testData.printStats();
//Create a new gesture recognition pipeline
GestureRecognitionPipeline pipeline;
//Add a LinearRegression instance to the pipeline
pipeline.setRegressifier( LinearRegression() );
//Train the LinearRegression model
cout << "Training LogisticRegression model...\n";
if( !pipeline.train( trainingData ) ){
cout << "ERROR: Failed to train LinearRegression model!\n";
return EXIT_FAILURE;
}
cout << "Model trained.\n";
//Test the model
cout << "Testing LinearRegression model...\n";
if( !pipeline.test( testData ) ){
cout << "ERROR: Failed to test LinearRegression model!\n";
return EXIT_FAILURE;
}
cout << "Test complete. Test RMS error: " << pipeline.getTestRMSError() << endl;
//Run back over the test data again and output the results to a file
fstream file;
file.open("LinearRegressionResultsData.txt", fstream::out);
for(UINT i=0; i<testData.getNumSamples(); i++){
vector< double > inputVector = testData[i].getInputVector();
vector< double > targetVector = testData[i].getTargetVector();
//Map the input vector using the trained regression model
if( !pipeline.predict( inputVector ) ){
cout << "ERROR: Failed to map test sample " << i << endl;
return EXIT_FAILURE;
}
//Get the mapped regression data
vector< double > outputVector = pipeline.getRegressionData();
//Write the mapped value and also the target value to the file
for(UINT j=0; j<outputVector.size(); j++){
file << outputVector[j] << "\t";
}
for(UINT j=0; j<targetVector.size(); j++){
file << targetVector[j] << "\t";
}
file << endl;
}
//Close the file
file.close();
return EXIT_SUCCESS;
}
示例7: prediction_axis_data
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";
}
示例8: metrics_subset_data
void metrics_subset_data(){
ANBC anbc;
anbc.enableScaling(true);
anbc.enableNullRejection(true);
MinDist minDist;
minDist.setNumClusters(4);
minDist.enableScaling(true);
minDist.enableNullRejection(true);
// ofstream opRecall("anbc-recall-nr-0-10.csv");
// opRecall <<"nrCoeff,class0,class1,class2,class3,class4,class5\n";
//
// ofstream opInstanceRes("anbc-prediction-nr-2.csv");
// opInstanceRes <<"actualClass,predictedClass,maximumLikelihood,lZ,lY,lZ,rZ,rY,rZ\n";
//
// ofstream opMetrics("anbc-precision-recall-fmeasure-nr-2.csv");
// opMetrics <<"class1,class2,class3,class4,class5\n";
//
// ofstream opConfusion("anbc-confusion-nr-2.csv");
// opConfusion <<"class0,class1,class2,class3,class4,class5\n";
ofstream opRecall("mindist-recall-nr-0-10.csv");
opRecall <<"nrCoeff,class0,class1,class2,class3,class4,class5\n";
ofstream opInstanceRes("mindist-prediction-nr-2.csv");
opInstanceRes <<"actualClass,predictedClass,maximumLikelihood,lZ,lY,lZ,rZ,rY,rZ\n";
ofstream opMetrics("mindist-precision-recall-fmeasure-nr-2.csv");
opMetrics <<"class1,class2,class3,class4,class5\n";
ofstream opConfusion("mindist-confusion-nr-2.csv");
opConfusion <<"class0,class1,class2,class3,class4,class5\n";
// Training and test data
ClassificationData trainingData;
ClassificationData testData;
ClassificationData nullGestureData;
string file_path = "../../../data/";
if( !trainingData.loadDatasetFromFile(file_path + "train/grt/hri-training-dataset.txt") ){
std::cout <<"Failed to load training data!\n";
}
if( !nullGestureData.loadDatasetFromFile(file_path + "test/grt/0.txt") ){
std::cout <<"Failed to load null gesture data!\n";
}
testData = trainingData.partition(90);
testData.sortClassLabels();
// testData.saveDatasetToFile("anbc-validation-subset.txt");
testData.saveDatasetToFile("mindist-validation-subset.txt");
for(double nullRejectionCoeff = 0; nullRejectionCoeff <= 10; nullRejectionCoeff=nullRejectionCoeff+0.2){
// anbc.setNullRejectionCoeff(nullRejectionCoeff);
// GestureRecognitionPipeline pipeline;
// pipeline.setClassifier(anbc);
minDist.setNullRejectionCoeff(nullRejectionCoeff);
GestureRecognitionPipeline pipeline;
pipeline.setClassifier(minDist);
pipeline.train(trainingData);
pipeline.test(testData);
TestResult testRes = pipeline.getTestResults();
opRecall << nullRejectionCoeff << ",";
//null rejection prediction
double accuracy = 0;
for(UINT i=0; i<nullGestureData.getNumSamples(); i++){
vector< double > inputVector = nullGestureData[i].getSample();
if( !pipeline.predict( inputVector )){
std::cout << "Failed to perform prediction for test sampel: " << i <<"\n";
}
UINT predictedClassLabel = pipeline.getPredictedClassLabel();
if(predictedClassLabel == 0 ) accuracy++;
}
opRecall << accuracy/double(nullGestureData.getNumSamples()) << ",";
// other classes prediction
for(int cl = 0; cl < testRes.recall.size(); cl++ ){
opRecall << testRes.recall[cl];
if(cl < testRes.recall.size() - 1){
opRecall << ",";
}
//.........这里部分代码省略.........
示例9: train
bool train( CommandLineParser &parser ){
infoLog << "Training regression model..." << endl;
string trainDatasetFilename = "";
string modelFilename = "";
float learningRate = 0;
float minChange = 0;
unsigned int maxEpoch = 0;
unsigned int batchSize = 0;
//Get the filename
if( !parser.get("filename",trainDatasetFilename) ){
errorLog << "Failed to parse filename from command line! You can set the filename using the -f." << endl;
printHelp();
return false;
}
//Get the parameters from the parser
parser.get("model-filename",modelFilename);
parser.get( "learning-rate", learningRate );
parser.get( "min-change", minChange );
parser.get( "max-epoch", maxEpoch );
parser.get( "batch-size", batchSize );
infoLog << "settings: learning-rate: " << learningRate << " min-change: " << minChange << " max-epoch: " << maxEpoch << " batch-size: " << batchSize << endl;
//Load the training data to train the model
RegressionData trainingData;
//Try and parse the input and target dimensions
unsigned int numInputDimensions = 0;
unsigned int numTargetDimensions = 0;
if( parser.get("num-inputs",numInputDimensions) && parser.get("num-targets",numTargetDimensions) ){
infoLog << "num input dimensions: " << numInputDimensions << " num target dimensions: " << numTargetDimensions << endl;
trainingData.setInputAndTargetDimensions( numInputDimensions, numTargetDimensions );
}
if( (numInputDimensions == 0 || numTargetDimensions == 0) && Util::stringEndsWith( trainDatasetFilename, ".csv" ) ){
errorLog << "Failed to parse num input dimensions and num target dimensions from input arguments. You must supply the input and target dimensions if the data format is CSV!" << endl;
printHelp();
return false;
}
infoLog << "- Loading Training Data..." << endl;
if( !trainingData.load( trainDatasetFilename ) ){
errorLog << "Failed to load training data!\n";
return false;
}
const unsigned int N = trainingData.getNumInputDimensions();
const unsigned int T = trainingData.getNumTargetDimensions();
infoLog << "- Num training samples: " << trainingData.getNumSamples() << endl;
infoLog << "- Num input dimensions: " << N << endl;
infoLog << "- Num target dimensions: " << T << endl;
//Create a new regression instance
LogisticRegression regression;
regression.setMaxNumEpochs( maxEpoch );
regression.setMinChange( minChange );
regression.setUseValidationSet( true );
regression.setValidationSetSize( 20 );
regression.setRandomiseTrainingOrder( true );
regression.enableScaling( true );
//Create a new pipeline that will hold the regression algorithm
GestureRecognitionPipeline pipeline;
//Add a multidimensional regression instance and set the regression algorithm to Linear Regression
pipeline.setRegressifier( MultidimensionalRegression( regression, true ) );
infoLog << "- Training model...\n";
//Train the classifier
if( !pipeline.train( trainingData ) ){
errorLog << "Failed to train model!" << endl;
return false;
}
infoLog << "- Model trained!" << endl;
infoLog << "- Saving model to: " << modelFilename << endl;
//Save the pipeline
if( pipeline.save( modelFilename ) ){
infoLog << "- Model saved." << endl;
}else warningLog << "Failed to save model to file: " << modelFilename << endl;
infoLog << "- TrainingTime: " << pipeline.getTrainingTime() << endl;
return true;
}
示例10: test
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 );
}
示例11: main
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;
}
示例12: train
bool train( CommandLineParser &parser ){
string trainDatasetFilename = "";
string modelFilename = "";
unsigned int forestSize = 0;
unsigned int maxDepth = 0;
unsigned int minNodeSize = 0;
unsigned int numSplits = 0;
bool removeFeatures = false;
double bootstrapWeight = 0.0;
//Get the filename
if( !parser.get("filename",trainDatasetFilename) ){
errorLog << "Failed to parse filename from command line! You can set the filename using the -f." << endl;
printUsage();
return false;
}
//Get the model filename
parser.get("model-filename",modelFilename);
//Get the forest size
parser.get("forest-size",forestSize);
//Get the max depth
parser.get("max-depth",maxDepth);
//Get the min node size
parser.get("min-node-size",minNodeSize);
//Get the number of random splits
parser.get("num-splits",numSplits);
//Get the remove features
parser.get("remove-features",removeFeatures);
//Get the bootstrap weight
parser.get("bootstrap-weight",bootstrapWeight);
//Load some training data to train the classifier
ClassificationData trainingData;
infoLog << "- Loading Training Data..." << endl;
if( !trainingData.load( trainDatasetFilename ) ){
errorLog << "Failed to load training data!\n";
return false;
}
const unsigned int N = trainingData.getNumDimensions();
Vector< ClassTracker > tracker = trainingData.getClassTracker();
infoLog << "- Num training samples: " << trainingData.getNumSamples() << endl;
infoLog << "- Num dimensions: " << N << endl;
infoLog << "- Num classes: " << trainingData.getNumClasses() << endl;
infoLog << "- Class stats: " << endl;
for(unsigned int i=0; i<tracker.getSize(); i++){
infoLog << "- class " << tracker[i].classLabel << " number of samples: " << tracker[i].counter << endl;
}
//Create a new RandomForests instance
RandomForests forest;
//Set the decision tree node that will be used for each tree in the forest
string nodeType = "cluster-node"; //TODO: make this a command line option in the future
if( nodeType == "cluster-node" ){
forest.setDecisionTreeNode( DecisionTreeClusterNode() );
}
if( nodeType == "threshold-node" ){
forest.setTrainingMode( Tree::BEST_RANDOM_SPLIT );
forest.setDecisionTreeNode( DecisionTreeThresholdNode() );
}
//Set the number of trees in the forest
forest.setForestSize( forestSize );
//Set the maximum depth of the tree
forest.setMaxDepth( maxDepth );
//Set the minimum number of samples allowed per node
forest.setMinNumSamplesPerNode( minNodeSize );
//Set the number of random splits used per node
forest.setNumRandomSplits( numSplits );
//Set if selected features should be removed at each node
forest.setRemoveFeaturesAtEachSplit( removeFeatures );
//Set the bootstrap weight
forest.setBootstrappedDatasetWeight( bootstrapWeight );
//Add the classifier to a pipeline
GestureRecognitionPipeline pipeline;
pipeline.setClassifier( forest );
infoLog << "- Training model..." << endl;
//Train the classifier
if( !pipeline.train( trainingData ) ){
errorLog << "Failed to train classifier!" << endl;
return false;
}
//.........这里部分代码省略.........
示例13: combineModels
bool combineModels( CommandLineParser &parser ){
infoLog << "Combining models..." << endl;
string directoryPath = "";
string modelFilename = "";
if( !parser.get("data-dir",directoryPath) ){
errorLog << "Failed to parse data-directory from command line! You can set the data-directory using the --data-dir option." << endl;
printUsage();
return false;
}
//Get the filename
if( !parser.get("model-filename",modelFilename) ){
errorLog << "Failed to parse filename from command line! You can set the model filename using the --model." << endl;
printUsage();
return false;
}
Vector< string > files;
infoLog << "- Parsing data directory: " << directoryPath << endl;
//Parse the directory to get all the csv files
if( !Util::parseDirectory( directoryPath, ".grt", files ) ){
errorLog << "Failed to parse data directory!" << endl;
return false;
}
RandomForests forest; //Used to validate the random forest type
GestureRecognitionPipeline *mainPipeline = NULL; // Points to the first valid pipeline that all the models will be merged to
Vector< GestureRecognitionPipeline* > pipelineBuffer; //Stores the pipeline for each file that is loaded
unsigned int inputVectorSize = 0; //Set to zero to mark we haven't loaded any models yet
const unsigned int numFiles = files.getSize();
bool mainPipelineSet = false;
bool combineModelsSuccessful = false;
pipelineBuffer.reserve( numFiles );
//Loop over the files, load them, and add valid random forest pipelines to the pipelineBuffer so they can be combined with the mainPipeline
for(unsigned int i=0; i<numFiles; i++){
infoLog << "- Loading model " << files[i] << ". File " << i+1 << " of " << numFiles << endl;
GestureRecognitionPipeline *pipeline = new GestureRecognitionPipeline;
if( pipeline->load( files[i] ) ){
infoLog << "- Pipeline loaded. Number of input dimensions: " << pipeline->getInputVectorDimensionsSize() << endl;
if( pipelineBuffer.size() == 0 ){
inputVectorSize = pipeline->getInputVectorDimensionsSize();
}
if( pipeline->getInputVectorDimensionsSize() != inputVectorSize ){
warningLog << "- Pipeline " << i+1 << " input vector size does not match the size of the first pipeline!" << endl;
}else{
Classifier *classifier = pipeline->getClassifier();
if( classifier ){
if( classifier->getClassifierType() == forest.getClassifierType() ){ //Validate the classifier is a random forest
if( !mainPipelineSet ){
mainPipelineSet = true;
mainPipeline = pipeline;
}else pipelineBuffer.push_back( pipeline );
}else{
warningLog << "- Pipeline " << i+1 << " does not contain a random forest classifer! Classifier type: " << classifier->getClassifierType() << endl;
}
}
}
}else{
warningLog << "- WARNING: Failed to load model from file: " << files[i] << endl;
}
}
if( mainPipelineSet ){
//Combine the random forest models with the main pipeline model
const unsigned int numPipelines = pipelineBuffer.getSize();
RandomForests *mainForest = mainPipeline->getClassifier< RandomForests >();
for(unsigned int i=0; i<numPipelines; i++){
infoLog << "- Combing model " << i+1 << " of " << numPipelines << " with main model..." << endl;
RandomForests *f = pipelineBuffer[i]->getClassifier< RandomForests >();
if( !mainForest->combineModels( *f ) ){
warningLog << "- WARNING: Failed to combine model " << i+1 << " with the main model!" << endl;
}
}
if( mainPipeline->getTrained() ){
infoLog << "- Saving combined pipeline to file..." << endl;
combineModelsSuccessful = mainPipeline->save( modelFilename );
}
}else{
errorLog << "Failed to combined models, no models were loaded!" << endl;
//.........这里部分代码省略.........
示例14: main
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;
}
示例15: setup
void setup() {
stream.useNormalizer(normalize);
stream.setLabelsForAllDimensions({"red", "green", "blue"});
useStream(stream);
useOutputStream(oStream);
// useStream(stream);
pipeline.addPreProcessingModule(MovingAverageFilter(5, 3));
// use scaling, use null rejection, null rejection parameter
pipeline.setClassifier(ANBC(false, !always_pick_something, null_rej));
// null rejection parameter is multiplied by the standard deviation to determine
// the rejection threshold. the higher the number, the looser the filter; the
// lower the number, the tighter the filter.
usePipeline(pipeline);
registerTuneable(always_pick_something, "Always Pick Something",
"Whether to always pick (predict) one of the classes of training data, "
"even if it's not a very good match. If selected, 'Color Variability' "
"will not be used.", updateAlwaysPickSomething);
registerTuneable(null_rej, 1.0, 25.0, "Color Variability",
"How different from the training data a new color reading can be and "
"still be considered the same color. The higher the number, the more "
"different it can be.", updateVariability);
}