Sobhaan ul Husan

Projects / Sensor-Based Exercise Classifier

Sensor-Based Exercise Classifier

Classifying 8 gym exercises from smartphone sensor data. Caught a data leak that was inflating accuracy to 100%; on unseen data the best model reached about 30%.

Date
Jun 2024
Type
Machine Learning
Status
Done
Tags
Deep Learning, LSTM, Time Series, scikit-learn

Overview

Group project for Machine Learning for Quantitative Self (ML4QS) at Vrije Universiteit Amsterdam (M.Sc. AI). Built a system that classifies gym exercises from smartphone sensor data strapped to the upper arm, comparing classical ML and an LSTM. Most of what I learned came from finding a data leak in our own pipeline.

Data collection

  • Smartphone running PhyPhox strapped to the upper left arm during workouts.
  • Sensors: 3-axis gyroscope, 3-axis accelerometer, compensated accelerometer, light, proximity, GPS, magnetometer.
  • Polling at ~500Hz (0.002s intervals) for accelerometer and gyroscope; sporadic for GPS and light (interrupt-driven).
  • 8 exercises across 4 muscle groups: Chest (Bench Press, Cable Flys), Back (Deadlift, Pull-ups), Arms (Bicep Curls, Shoulder Press), Core (Crunches, Russian Twists). With the non-exercise labels, including rest, that makes 10 classes, so a random baseline is 10%.

Feature engineering

Time aggregated to 0.02s intervals, velocity from acceleration integration, resultant acceleration magnitude, Butterworth low-pass filter, with the cutoff tuned by trial and error until the sensor plots were largely free of noise and jitter. KNN imputation for the gaps left by the time aggregation. Removed GPS and magnetometer features after correlation analysis showed they were not picking up signal in stationary exercises.

The data leak

Random Forest hit 100% accuracy on the test set. Stratified split also gave 100%. SVM and KNN, same. That was the red flag. After tracing the pipeline, the cause was an aggregation step that produced a column behaving like a row index, strongly correlated with class because exercises were appended in a fixed order.

After dropping the leaked column, accuracy dropped to ~99% on stratified splits, which still felt high. Testing on a separate set of truly unseen data gave the realistic number.

Results on unseen data

| Model | Test accuracy | | :-- | :-- | | Random Forest | ~30% (best classical model) | | RF + Recursive Feature Elimination | 23% | | RF + PCA (n=8, 95% variance retained) | 16% | | Fully connected network (deep-learning baseline) | 7.8% (temporally ordered split) / 8.3% (stratified split) | | LSTM (batch sizes 32 / 64 / 128; reported run uses 32) | 15.9% | | Random baseline | ~11% |

Better than chance, but a long way from a deployable classifier. For the deep-learning side we built a plain fully connected network as a baseline, then added an LSTM block in front of it. Neither came close to the Random Forest, and the report concludes that the temporal model could not be successfully implemented for this task within the project scope.

What I'd do differently

The post-mortem in the report identifies the real fixes: train one model per body group (much smaller class space), use TCNs over LSTMs for time-series, and derive frequency-domain features instead of stopping at time-domain. The current pipeline conflates very different motion signatures (e.g. bench press and pull-ups are both upper-body but produce nothing alike at the arm-mounted sensor).

The main lesson from this project was catching our own data leak before someone else did, and being able to explain why a 30% classifier is closer to the truth than a 100% one.

Stack

Python, scikit-learn (Random Forest, SVM, KNN, RFE, PCA), TensorFlow/Keras (LSTM), Pandas, NumPy, PhyPhox.