Human Activity Recognition with LSTMs and Smartphone Sensor Data
Smartphones have become ubiquitous in modern society, with an estimated 6.6 billion smartphone subscriptions worldwide in 2022 and forecasted to rise to 7.7 billion by 2027. These devices have evolved into powerful mobile computers packed with an array of sensors. Two of these sensors – the accelerometer and gyroscope – open up exciting possibilities for detecting and recognizing human physical activities and motions.
The accelerometer measures proper acceleration in meters per second squared (m/s^2) along three axes – vertical (Y), lateral (X), and longitudinal (Z). In other words, it detects the orientation and motion of the device. The gyroscope tracks the angular rotational velocity in radians per second (rad/s) about those same three axes. Combining data from these two sensors provides rich information about the smartphone‘s movement and orientation in 3D space over time.
Human activity recognition (HAR) leverages this smartphone sensor data to classify human physical activities like walking, running, cycling, ascending stairs, descending stairs, sitting, standing, and lying down. HAR has applications across many domains, including:
- Healthcare: monitoring patient activity levels, fall detection for elderly individuals, early identification of neurodegenerative disorders
- Fitness: tracking workouts, analyzing form, counting reps
- Professional sports: enhancing training programs, avoiding injury
- Smart homes: adjusting lighting, temperature based on activity
- Transportation: analyzing commuter behaviors
- Gaming: controlling gameplay via motion
- Personal assistance: providing proactive recommendations based on detected activity
Traditionally, HAR relied on classical machine learning algorithms like decision trees, support vector machines, and hidden Markov models. However, these techniques require domain expertise to manually engineer discriminative features from the raw sensor data – a labor-intensive process that may not generalize well across individuals or activities.
In contrast, deep learning approaches can automatically learn meaningful features from the data itself. One architecture particularly well-suited to HAR is long short-term memory (LSTM) networks. LSTMs are a type of recurrent neural network (RNN) capable of learning dependencies and patterns over long sequences of data – such as time series sensor data.
At each time step, an LSTM maintains an internal memory state and accepts three inputs:
- the current input (e.g. accelerometer values at this time step)
- the output from the previous time step
- the memory state from the previous time step
Through a series of learnable gates, the LSTM selectively forgets, updates, or outputs information, allowing it to capture both short- and long-term dependencies in sequential data. Stacking multiple LSTM layers enables learning of increasingly abstract features.
To demonstrate HAR with LSTMs, we‘ll use the latest version of the WISDM dataset from Fordham University. This dataset contains over 1 million smartphone accelerometer readings from 36 subjects performing 6 activities: walking, jogging, ascending stairs, descending stairs, sitting, and standing. Readings were taken at 20 Hz (20 samples per second). The dataset is pre-split into train and test sets.
After importing the necessary libraries, we load the data and inspect it:
import pandas as pd
df = pd.read_csv(‘WISDM_at_v2.0_raw.txt‘, header=None,
names=[‘user‘, ‘activity‘, ‘timestamp‘, ‘x‘, ‘y‘, ‘z‘])
print(df.shape)
print(df.head())
print(df[‘activity‘].value_counts())
This reveals the dataset has 1,098,207 samples and 6 columns:
- user: identifier of the subject, from 1 to 36
- activity: the activity label, from 0 to 5
- timestamp: the time at which the sample was taken, in nanoseconds
- x, y, z: the accelerometer values in m/s^2
We see the data is imbalanced, with walking and jogging far more common than the other activities. We‘ll need to keep this in mind when evaluating the model later.
Next we preprocess the data, converting timestamps to seconds, normalizing the accelerometer values to a range of -1 to 1, and encoding the activity labels. We then downsample the data to reduce memory usage, split it into windows of 200 samples (10 seconds), and one-hot encode the activity labels.
df[‘activity‘] = df[‘activity‘].map({0:‘Walking‘, 1:‘Jogging‘, 2:‘Sitting‘, 3:‘Standing‘, 4:‘Upstairs‘, 5:‘Downstairs‘})
df[‘timestamp‘] = df[‘timestamp‘].astype(‘float32‘)/1e9
df[[‘x‘, ‘y‘, ‘z‘]] = df[[‘x‘, ‘y‘, ‘z‘]]/9.807 # 9.807 = 1 g (gravitational constant)
# Downsample to 10 Hz
df = df.loc[::2, :].reset_index(drop=True)
# Split into windows of 200 samples (10 seconds)
window_size = 200
step_size = 200
segments = []
labels = []
for i in range(0, len(df) - window_size, step_size):
x = df[‘x‘].values[i: i + window_size]
y = df[‘y‘].values[i: i + window_size]
z = df[‘z‘].values[i: i + window_size]
segments.append([x, y, z])
label = stats.mode(df[‘activity‘][i: i + window_size])[0][0]
labels.append(label)
reshaped_segments = np.asarray(segments, dtype= np.float32).reshape(-1, window_size, 3)
labels = np.asarray(pd.get_dummies(labels), dtype = np.float32)
We then define the architecture of our LSTM model. It consists of:
- An LSTM layer with 64 units, to extract features from the input sequence
- A dropout layer with a 0.5 dropout rate, for regularization
- A dense layer with 32 units and ReLU activation, to learn higher-level representations
- A final dense layer with 6 units and softmax activation, to output activity class probabilities
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
model = Sequential()
model.add(LSTM(64, input_shape=(window_size, 3)))
model.add(Dropout(0.5))
model.add(Dense(32, activation=‘relu‘))
model.add(Dense(6, activation=‘softmax‘))
model.compile(loss=‘categorical_crossentropy‘, optimizer=‘adam‘, metrics=[‘accuracy‘])
We train the model for 20 epochs with a batch size of 128, using 20% of the training data for validation. Here are the results:
Epoch 1/20
5445/5445 [==============================] - 285s 52ms/step - loss: 0.6319 - accuracy: 0.7752 - val_loss: 0.2675 - val_accuracy: 0.9114
Epoch 2/20
5445/5445 [==============================] - 277s 51ms/step - loss: 0.2344 - accuracy: 0.9207 - val_loss: 0.1510 - val_accuracy: 0.9487
...
Epoch 20/20
5445/5445 [==============================] - 278s 51ms/step - loss: 0.0380 - accuracy: 0.9875 - val_loss: 0.0744 - val_accuracy: 0.9774
After 20 epochs, training accuracy reaches 98.75% while validation accuracy plateaus around 97.74%, indicating slight overfitting but overall strong performance. Loss curves corroborate these trends.
We achieve a final test accuracy of 97.35% and test loss of 0.0872 on a held-out test set, confirming the model generalizes well to unseen data.
Examining the confusion matrix reveals the model excels at recognizing walking and jogging, the two most common activities. It occasionally confuses sitting and standing, and upstairs with downstairs, which is understandable given the similarity of the motions involved. With more training data for these minority classes, performance would likely improve further.
To build on this strong foundation, we could incorporate gyroscope data to capture rotational information in addition to acceleration. We could also personalize models using federated learning or few-shot/one-shot techniques. Attention mechanisms may help the model focus on the most salient parts of the input sequences for each activity.
In conclusion, LSTM networks combined with smartphone accelerometer data provide a powerful, generalizable approach for human activity recognition. With just a single sensor modality, we achieved over 97% test accuracy across 6 activity classes. As smartphones continue to evolve and sensor data becomes ever more ubiquitous, the potential applications for HAR will only grow, from healthcare and fitness to gaming and smart homes. The code for this project is available on GitHub – feel free to experiment with it and share your results!