human activity recognition simulink model for smartphone deployment -凯发k8网页登录
this example shows how to prepare a simulink® model that classifies human activity based on smartphone sensor signals for code generation and smartphone deployment. the example provides two simulink models that are ready for deployment to an android™ device and an ios device. after you install the required support package for a target device, train the classification model and deploy the simulink model to the device.
load sample data set
load the humanactivity
data set.
load humanactivity
the humanactivity
data set contains 24,075 observations of five different physical human activities: sitting, standing, walking, running, and dancing. each observation has 60 features extracted from acceleration data measured by smartphone accelerometer sensors. the data set contains the following variables:
actid
— response vector containing the activity ids in integers: 1, 2, 3, 4, and 5 representing sitting, standing, walking, running, and dancing, respectivelyactnames
— activity names corresponding to the integer activity idsfeat
— feature matrix of 60 features for 24,075 observationsfeatlabels
— labels of the 60 features
the sensor har (human activity recognition) app [1] was used to create the humanactivity
data set. when measuring the raw acceleration data with this app, a person placed a smartphone in a pocket so that the smartphone was upside down and the screen faced toward the person. the software then calibrated the measured raw data accordingly and extracted the 60 features from the calibrated data. for details about the calibration and feature extraction, see [2] and [3], respectively. the simulink models described later also use the raw acceleration data and include blocks for calibration and feature extraction.
prepare data
this example uses 90% of the observations to train a model that classifies the five types of human activities and 10% of the observations to validate the trained model. use cvpartition
to specify a 10% holdout for the test set.
rng('default') % for reproducibility partition = cvpartition(actid,'holdout',0.10); traininginds = training(partition); % indices for the training set xtrain = feat(traininginds,:); ytrain = actid(traininginds); testinds = test(partition); % indices for the test set xtest = feat(testinds,:); ytest = actid(testinds);
convert the feature matrix xtrain
and the response vector ytrain
into a table to load the training data set in the classification learner app.
ttrain = array2table([xtrain ytrain]);
specify the variable name for each column of the table.
ttrain.properties.variablenames = [featlabels' 'activities'];
train boosted tree ensemble using classification learner app
train a classification model by using the classification learner app. to open the classification learner app, enter classificationlearner
at the command line. alternatively, click the apps tab, and click the arrow at the right of the apps section to open the gallery. then, under machine learning and deep learning, click classification learner.
on the classification learner tab, in the file section, click new session and select from workspace.
in the new session from workspace dialog box, click the arrow for data set variable, and then select the table ttrain
. classification learner detects the predictors and the response from the table.
the default option is 5-fold cross-validation, which protects against overfitting. click start session. classification learner loads the data set and plots a scatter plot of the first two features.
on the classification learner tab, click the arrow at the right of the models section to open the gallery. then, under ensemble classifiers, click boosted trees.
the model summary tab displays the default settings of the boosted tree ensemble model.
on the classification learner tab, in the train section, click train all and select train selected. when the training is complete, the models pane displays the 5-fold, cross-validated classification accuracy.
on the classification learner tab, click export, click export model, and select export model. in the export classification model dialog box, clear the check box to exclude the training data and export a compact model, and then click ok. the structure trainedmodel
appears in the matlab® workspace. the field classificationensemble
of trainedmodel
contains the compact model. extract the trained model from the structure.
classificationensemble = trainedmodel.classificationensemble;
train boosted tree ensemble at command line
alternatively, you can train the same classification model at the command line.
template = templatetree('maxnumsplits',20,'reproducible',true); classificationensemble = fitcensemble(xtrain,ytrain, ... 'method','adaboostm2', ... 'numlearningcycles',30, ... 'learners',template, ... 'learnrate',0.1, ... 'classnames',[1; 2; 3; 4; 5]);
perform 5-fold cross-validation for classificationensemble
and compute the validation accuracy.
partitionedmodel = crossval(classificationensemble,'kfold',5);
validationaccuracy = 1-kfoldloss(partitionedmodel)
validationaccuracy = 0.9833
evaluate performance on test data
evaluate performance on the test data set.
testaccuracy = 1-loss(classificationensemble,xtest,ytest)
testaccuracy = 0.9759
the trained model correctly classifies 97.59% of the human activities on the test data set. this result confirms that the trained model does not overfit to the training data set.
note that the accuracy values can vary slightly depending on your operating system.
save trained model
for code generation including a classification model object, use savelearnerforcoder
and loadlearnerforcoder
.
save the trained model by using savelearnerforcoder
.
savelearnerforcoder(classificationensemble,'ensemblemodel.mat');
the function block predictactivity in the simulink models loads the trained model by using loadlearnerforcoder
and uses the trained model to classify new data.
deploy simulink model to device
now that you have prepared a classification model, you can open the simulink model, depending on which type of smartphone you have, and deploy the model to your device. note that the simulink model requires the ensemblemodel.mat
file and the calibration matrix file slexharandroidcalibrationmatrix.mat
or slexharioscalibrationmatrix.mat
. if you click the button located in the upper-right section of this page and open this example in matlab, then matlab opens the example folder that includes these calibration matrix files.
type slexharandroidexample
to open the simulink model for android deployment.
type slexhariosexample
to open the simulink model for ios deployment. you can open the model on the mac os platform.
the two simulink models classify human activity based on acceleration data measured by a smartphone sensor. the models include the following blocks:
the accelerometer block receives raw acceleration data from accelerometer sensors on the device.
the calibrate block is a matlab function block that calibrates the raw acceleration data. this block uses the calibration matrix in the
slexharandroidcalibrationmatrix.mat
file or theslexharioscalibrationmatrix.mat
file. if you click the button located in the upper-right section of this page and open this example in matlab, then matlab opens the example folder that includes these files.the display blocks acc x, acc y, and acc z are connected to the calibrate block and display calibrated data points for each axis on the device.
each of the buffer blocks, x buffer, y buffer, and z buffer, buffers 32 samples of an accelerometer axis with 12 samples of overlap between buffered frames. after collecting 20 samples, each buffer block joins the 20 samples with 12 samples from the previous frame and passes the total 32 samples to the extractfeatures block. each buffer block receives an input sample every 0.1 second and outputs a buffered frame including 32 samples every 2 seconds.
the extractfeatures block is a matlab function block that extracts 60 features from a buffered frame of 32 accelerometer samples. this function block uses dsp system toolbox™ and signal processing toolbox™.
the predictactivity block is a matlab function block that loads the trained model from the
ensemblemodel.mat
file by usingloadlearnerforcoder
and classifies the user activity using the extracted features. the output is an integer between 1 and 5, corresponding to sitting, standing, walking, running, and dancing, respectively.the predicted activity block displays the classified user activity values on the device.
the video output subsystem uses a multiport switch block to choose the corresponding user activity image data to display on the device. the convert to rgb block decomposes the selected image into separate rgb vectors and passes the image to the activity display block.
to deploy the simulink model to your device, follow the steps in (simulink support package for android devices) or (simulink support package for apple ios devices). run the model on your device, place the device in the same way as described earlier for collecting the training data, and try the five activities. the model displays the classified activity accordingly.
to ensure the accuracy of the model, you need to place your device in the same way as described for collecting the training data. if you want to place your device in a different location or orientation, then collect the data in your own way and use your data to train the classification model.
the accuracy of the model can be different from the accuracy of the test data set (testaccuracy
), depending on the device. to improve the model, you can consider using additional sensors and updating the calibration matrix. also, you can add another output block for audio feedback to the output subsystem using audio toolbox™. use a thingspeak™ write block to publish classified activities and acceleration data from your device to the internet of things. for details, see
references
[1] el helou, a. sensor har recognition app. mathworks file exchange
[2] stmicroelectronics, an4508 application note. “parameters and calibration of a low-g 3-axis accelerometer.” 2014.
[3] el helou, a. sensor data analytics. mathworks file exchange
see also
loadlearnerforcoder
| savelearnerforcoder
| fitcensemble
|