GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
ClassificationModulesExamples/DTWExample/DTWExample.cpp

This class implements Dynamic Time Warping. Dynamic Time Warping (DTW) is a powerful classifier that works very well for recognizing temporal gestures. Temporal gestures can be defined as a cohesive sequence of movements that occur over a variable time period. The DTW algorithm is a supervised learning algorithm that can be used to classify any type of N-dimensional, temporal signal. The DTW algorithm works by creating a template time series for each gesture that needs to be recognized, and then warping the realtime signals to each of the templates to find the best match. The DTW algorithm also computes rejection thresholds that enable the algorithm to automatically reject sensor values that are not the K gestures the algorithm has been trained to recognized (without being explicitly told during the prediction phase if a gesture is, or is not, being performed). You can find out more about the DTW algorithm in Gillian, N. (2011) Recognition of multivariate temporal musical gestures using n-dimensional dynamic time warping.The DTW algorithm is part of the GRT classification modules.

Remarks
This implementation is based on Gillian, N. (2011) Recognition of multivariate temporal musical gestures using n-dimensional dynamic time warping.
/*
GRT MIT License
Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
GRT DTW Example
This examples demonstrates how to initialize, train, and use the DTW algorithm for classification.
The Dynamic Time Warping (DTW) algorithm is a powerful classifier that works very well for recognizing temporal gestures.
In this example we create an instance of an DTW algorithm and then train the algorithm using some pre-recorded training data.
The trained DTW algorithm is then used to predict the class label of some test data.
This example shows you how to:
- Create an initialize the DTW algorithm
- Load some LabelledTimeSeriesClassificationData from a file and partition the training data into a training dataset and a test dataset
- Trim any periods of non-movement from the start and end of each timeseries recording
- Train the DTW algorithm using the training dataset
- Test the DTW algorithm using the test dataset
- Manually compute the accuracy of the classifier
*/
//You might need to set the specific path of the GRT header relative to your project
#include "GRT.h"
using namespace GRT;
using namespace std;
int main (int argc, const char * argv[])
{
//Create a new DTW instance, using the default parameters
DTW dtw;
//Load some training data to train the classifier - the DTW uses TimeSeriesClassificationData
if( !trainingData.load("DTWTrainingData.grt") ){
cout << "Failed to load training data!\n";
return EXIT_FAILURE;
}
//Use 20% of the training dataset to create a test dataset
TimeSeriesClassificationData testData = trainingData.split( 80 );
//Trim the training data for any sections of non-movement at the start or end of the recordings
dtw.enableTrimTrainingData(true,0.1,90);
//Train the classifier
if( !dtw.train( trainingData ) ){
cout << "Failed to train classifier!\n";
return EXIT_FAILURE;
}
//Save the DTW model to a file
if( !dtw.save("DTWModel.grt") ){
cout << "Failed to save the classifier model!\n";
return EXIT_FAILURE;
}
//Load the DTW model from a file
if( !dtw.load("DTWModel.grt") ){
cout << "Failed to load the classifier model!\n";
return EXIT_FAILURE;
}
//Use the test dataset to test the DTW model
double accuracy = 0;
for(UINT i=0; i<testData.getNumSamples(); i++){
//Get the i'th test sample - this is a timeseries
UINT classLabel = testData[i].getClassLabel();
MatrixDouble timeseries = testData[i].getData();
//Perform a prediction using the classifier
if( !dtw.predict( timeseries ) ){
cout << "Failed to perform prediction for test sampel: " << i <<"\n";
return EXIT_FAILURE;
}
//Get the predicted class label
UINT predictedClassLabel = dtw.getPredictedClassLabel();
double maximumLikelihood = dtw.getMaximumLikelihood();
VectorDouble classLikelihoods = dtw.getClassLikelihoods();
VectorDouble classDistances = dtw.getClassDistances();
//Update the accuracy
if( classLabel == predictedClassLabel ) accuracy++;
cout << "TestSample: " << i << "\tClassLabel: " << classLabel << "\tPredictedClassLabel: " << predictedClassLabel << "\tMaximumLikelihood: " << maximumLikelihood << endl;
}
cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl;
return EXIT_SUCCESS;
}