Signal processing with machine learning (Human Activity Recognition) Part-III (Neural Networks)

Source: Deep Learning on Medium

Welcome back, this is the final part of signal processing with machine learning hands-on. We are going to leverage the power of deep learning on our same dataset of accelerometer and gyroscope signals for human activity classification.

What is deep learning?
Deep learning is an advanced machine learning technique based on neural networks inspired by the human brain. Deep learning is performed using neural networks which are mathematically matrices or tensors that resemble self learnt features of fed data. Deep learning outperforms classical machine learning techniques in terms of model building time, accuracy and most important expense. Deep learning works with passing batches of high dimensional data often called tensors through a network repeatedly. Each neuron of the network is associated with some linear or non linear weight and optional bias. After passing every batch of data, the weight of each neuron is adjusted by an optimizer using backpropagation, which is passing the loss backward through the neurons, so that the neurons can adjust their weight to reduce the final loss using gradient descent technique. The most useful factor of deep learning is the low-cost development of a model. Wonder why?? The most important part of machine learning is the feature engineering and feature selection. Organizations spend a lot in domain experts and data analysts who filter and generate the most important features for the model development. The best part of neural networks is we can mostly skip the feature engineering part and feed preprocessed data directly into the neural networks. The deep and dense neural network figures out important and unimportant features itself and adjusts their weight. Deep learning needs no domain expertise and can be developed by a team of developers only.

We will see how deep learning shows similar performance in prediction with our human activity classification task with raw sensor data fed into deep neural networks like LSTM(Long Short Term Memory) and CNN(Convolutional Neural Networks)

We will work with Keras which is a deep learning wrapper on top of TensorFlow for designing and testing our neural network.

To start with we will import the raw time-sliced signals data from 9 sensors. The time slice is a sequence of 128 float values corresponding to sensor value each millisecond. The window is slid by 1 step and the next data point is a sequence of 128 values with 127 old ones and 1 new one. The labels are categorical activities.

Following is how we will import the data and split into train and test sets. This section also has some helper functions that we will be using most often.

Now, when we are ready with our signals dataset, we will be creating an LSTM network first. The LSTM networks are a kind of RNN(Recurrent Neural Networks) which were designed to solve time series or sequential data problems. LSTM is an advanced RNN that was developed to solve vanishing gradient problems that often occur in deep neural networks. Following is the implementation for LSTM and it’s just 4 lines of code, how simple is that. You can simply compare this with the previous part of classical machine learning implementation where we had to use domain expert processed dataset, which had taken months of time and a specialized team of signal processing experts.

Just look at that 90% accuracy in just 10 lines of code and 30 minutes of training time. The confusion matrix looks promising too.

This is the power of deep learning, prototyping is much quicker. However, It hasn’t beaten the classical accuracy, but after some hyper tuning and using other networks like convolutional neural networks, there will be no difference in the accuracy. Let’s analyze the confusion matrix and compare it with the best one we achieved with classical algos.

We will do some hyperparameter tuning and see how the model performs. Following is the implementation. I have used gridsearchCV for the best model selection.

After hyperparameter tuning accuracy is improved by 1 percent. The confusion between sitting and standing classes is also reduced by 1% and 4% respectively. We can do further experiments with deeper neural network and add try adding another LSTM layer and see if model performance improves.

After adding another LSTM layer the model performance is further improved by a half percent. Convolutional Neural Networks are also getting popular in sequential machine learning. Let us try som ConvNets and a combination of ConvNets with LSTMs and see if this can raise the performance of the model significantly.

After trying a few combinations of ConvNets and LSTMs we can see that we got around 95% accuracy using neural networks. I have achieved even 96.5% accuracy which even outperformed the domain expert’s engineered dataset.

This is the end of Signal Processing with machine learning implementation using the Human Activity Recognition dataset. I tried to cover all aspects of machine learning in this series starting from exploratory data analysis to classical machine learning to advanced deep learning.

If I missed something important, or if I can implement something better, please let me know. Hope you enjoyed this post, I’d be very grateful if you’d help it spreading and sharing.

Happy learning… 🙂