<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Activity Recognition | Sina Mehdizadeh, PhD</title><link>https://sinamehdiz.com/tag/activity-recognition/</link><atom:link href="https://sinamehdiz.com/tag/activity-recognition/index.xml" rel="self" type="application/rss+xml"/><description>Activity Recognition</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Thu, 02 Oct 2025 00:00:00 +0000</lastBuildDate><image><url>https://sinamehdiz.com/images/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_3.png</url><title>Activity Recognition</title><link>https://sinamehdiz.com/tag/activity-recognition/</link></image><item><title>Pretraining a vanilla transformer for Human Movement activity classification from Wearable IMU Signals</title><link>https://sinamehdiz.com/project/imu-vanilla-transformer/</link><pubDate>Thu, 02 Oct 2025 00:00:00 +0000</pubDate><guid>https://sinamehdiz.com/project/imu-vanilla-transformer/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Recognizing movement from wearable sensors can support human movement research and context-aware applications, but useful models must interpret noisy, multichannel time series and distinguish activities with similar postures. This project investigates whether transformer representations learned from inertial measurement unit (IMU) signals can be transferred to six-class human activity recognition (HAR). Using the UCI Human Activity Recognition dataset, I compared accelerometer (ACC), gyroscope (GYR), and combined ACC+GYR inputs, and evaluated both frozen-feature transfer and encoder fine-tuning.&lt;/p>
&lt;h2 id="methods">Methods&lt;/h2>
&lt;p>The forecasting model learns next-step sensor prediction from fixed-length UCI HAR windows. Each 128-sample window is shifted by one step to form inputs and targets; causal attention masks prevent access to future samples. The documented transformer uses a learned input projection, a six-layer encoder-decoder, eight attention heads, and mean squared error for prediction; it does not add explicit positional encodings.&lt;/p>
&lt;p>For movement classification, I transferred the pretrained encoder, retained causal attention, mean-pooled its time-step representations, and attached a linear six-class head. The three modality configurations used matching subject-level train/validation partitions and the official test set. I compared a frozen encoder with a fine-tuned encoder; runs used seed 0, batch size 32, and up to 50 epochs. Macro-F1 was the primary comparison metric to give each activity equal weight.&lt;/p>
&lt;h2 id="results">Results&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Sensor input&lt;/th>
&lt;th style="text-align:right">Frozen encoder macro-F1&lt;/th>
&lt;th style="text-align:right">Fine-tuned encoder macro-F1&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>ACC&lt;/td>
&lt;td style="text-align:right">0.5984&lt;/td>
&lt;td style="text-align:right">0.8374&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>GYR&lt;/td>
&lt;td style="text-align:right">0.5090&lt;/td>
&lt;td style="text-align:right">0.6617&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>ACC + GYR&lt;/td>
&lt;td style="text-align:right">0.6361&lt;/td>
&lt;td style="text-align:right">&lt;strong>0.8775&lt;/strong>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Fine-tuning improved macro-F1 across all three input configurations in this run. The combined accelerometer and gyroscope model achieved the strongest result, while the gyroscope-only model was lowest. The best run reached 0.8741 accuracy and 0.8775 macro-F1 across 2,947 test windows. These results demonstrate a working, controlled modality comparison and an end-to-end transfer-learning pipeline; without a matched model trained from scratch, they do not establish that pretraining caused the improvement.&lt;/p>
&lt;p>The selected run&amp;rsquo;s learning curves are shown in Figure 1. Figure 2 shows the test confusion matrix, with most remaining errors among sitting, standing, and laying.&lt;/p>
&lt;p>&lt;img src="./accgyr_finetune_history.png" alt="Training and validation cross-entropy loss and macro-F1 for the fine-tuned ACC+GYR model.">
&lt;em>Figure 1. Training and validation cross-entropy loss and macro-F1 over 50 epochs for the fine-tuned ACC+GYR model.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./accgyr_finetune_confusion.png" alt="Test confusion matrix for the fine-tuned ACC+GYR model.">
&lt;em>Figure 2. Test confusion matrix for the fine-tuned ACC+GYR model.&lt;/em>&lt;/p>
&lt;p>For comparison, Figures 3 and 4 show the corresponding frozen-encoder linear-probe run.&lt;/p>
&lt;p>&lt;img src="./accgyr_probe_history.png" alt="Training and validation curves for the ACC+GYR linear probe with a frozen encoder.">
&lt;em>Figure 3. Training and validation cross-entropy loss and macro-F1 over 50 epochs for the ACC+GYR linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./accgyr_probe_confusion.png" alt="Test confusion matrix for the ACC+GYR linear probe with a frozen encoder.">
&lt;em>Figure 4. Test confusion matrix for the ACC+GYR linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>Figures 5-12 show the ACC-only and GYR-only plots for both transfer modes.&lt;/p>
&lt;p>&lt;img src="./acc_probe_history.png" alt="Training curves for the ACC-only linear probe with a frozen encoder.">
&lt;em>Figure 5. Training history for the ACC-only linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./acc_probe_confusion.png" alt="Test confusion matrix for the ACC-only linear probe with a frozen encoder.">
&lt;em>Figure 6. Test confusion matrix for the ACC-only linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./acc_finetune_history.png" alt="Training curves for the fine-tuned ACC-only model.">
&lt;em>Figure 7. Training history for the fine-tuned ACC-only model.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./acc_finetune_confusion.png" alt="Test confusion matrix for the fine-tuned ACC-only model.">
&lt;em>Figure 8. Test confusion matrix for the fine-tuned ACC-only model.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./gyr_probe_history.png" alt="Training curves for the GYR-only linear probe with a frozen encoder.">
&lt;em>Figure 9. Training history for the GYR-only linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./gyr_probe_confusion.png" alt="Test confusion matrix for the GYR-only linear probe with a frozen encoder.">
&lt;em>Figure 10. Test confusion matrix for the GYR-only linear probe with a frozen encoder.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./gyr_finetune_history.png" alt="Training curves for the fine-tuned GYR-only model.">
&lt;em>Figure 11. Training history for the fine-tuned GYR-only model.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./gyr_finetune_confusion.png" alt="Test confusion matrix for the fine-tuned GYR-only model.">
&lt;em>Figure 12. Test confusion matrix for the fine-tuned GYR-only model.&lt;/em>&lt;/p>
&lt;h2 id="conclusions">Conclusions&lt;/h2>
&lt;p>On this UCI HAR experiment, combining acceleration and rotation signals and fine-tuning the pretrained encoder produced the strongest activity-classification result. The work applies sequence modeling, sensor-fusion comparisons, subject-aware evaluation, and transfer learning to a human movement problem. It also shows why aggregate accuracy alone is insufficient: macro-F1 helps expose uneven performance across locomotor and static activities.&lt;/p>
&lt;h2 id="limitations-and-future-directions">Limitations and Future Directions&lt;/h2>
&lt;p>These are single-seed results on one dataset, not evidence of generalization to new devices, populations, or movement settings. The pretrained models were evaluated on the official test split during pretraining; if those evaluations influenced model or checkpoint choices, the reported test scores are not an untouched final benchmark. Validation subjects' unlabeled sensor signals may also have appeared during pretraining. Most importantly, no matched from-scratch baseline is included, so the contribution of pretraining cannot be isolated.&lt;/p>
&lt;p>Next steps are to add a from-scratch baseline under identical splits and optimization settings, repeat runs across seeds, and reserve an untouched test set for final evaluation. Further experiments could compare positional encoding choices, causal versus bidirectional attention for whole-window classification, and performance across independent datasets or participant groups.&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ol>
&lt;li>Anguita, D., Ghio, A., Oneto, L., Parra, X., and Reyes-Ortiz, J. L. (2013). “A Public Domain Dataset for Human Activity Recognition Using Smartphones.” &lt;em>ESANN 2013&lt;/em>.&lt;/li>
&lt;li>Vaswani, A., et al. (2017). “Attention Is All You Need.” &lt;em>Advances in Neural Information Processing Systems&lt;/em>.&lt;/li>
&lt;/ol></description></item><item><title>Causal Temporal Convolutions for Human Activity Classification</title><link>https://sinamehdiz.com/project/imu-tcn/</link><pubDate>Sat, 13 Sep 2025 00:00:00 +0000</pubDate><guid>https://sinamehdiz.com/project/imu-tcn/</guid><description>&lt;h2 id="introduction">Introduction&lt;/h2>
&lt;p>Wearable inertial sensors provide continuous signals that can distinguish movement, but robust activity recognition requires models to capture temporal structure across multiple channels. This study explores a two-stage transfer-learning pipeline: first learn from unlabeled sensor sequences through next-sample prediction, then reuse the temporal representation to classify human activities. The experiments compare a frozen-feature linear probe with end-to-end fine-tuning on the UCI Human Activity Recognition (HAR) dataset.&lt;/p>
&lt;h2 id="methods">Methods&lt;/h2>
&lt;p>The data contain six channels: three body-gyroscope axes and three body-acceleration axes. Each UCI HAR window has 128 readings sampled at 50 Hz, spans 2.56 seconds, and overlaps adjacent windows by 50%. Channels are standardized using training-set statistics. For pretraining, each window is shifted by one sample: the first 127 readings predict the following 127 targets. The causal temporal convolutional network (TCN) uses two residual blocks with kernel size 3, dilations 1 and 2, eight hidden channels, and dropout. Its encoder has a 13-sample receptive field; a pointwise prediction head maps features back to the six sensor channels. The predictor is trained with mean squared error and Adam.&lt;/p>
&lt;p>For classification, the pretrained encoder processes the first 127 readings. Mean pooling aggregates its temporal features, and a linear head predicts one of six activities: walking, walking upstairs, walking downstairs, sitting, standing, or laying. In probe mode, encoder weights are frozen; in fine-tuning mode, the encoder and head are optimized with separate learning rates. Both reported runs used 20 epochs, with a head learning rate of 0.01 and, for fine-tuning, an encoder learning rate of 0.001. Evaluation uses subject-separated training and validation partitions and the official test set. Macro-F1 is reported alongside accuracy.&lt;/p>
&lt;h2 id="results">Results&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Transfer mode&lt;/th>
&lt;th style="text-align:right">Test accuracy&lt;/th>
&lt;th style="text-align:right">Test macro-F1&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Frozen encoder + linear probe&lt;/td>
&lt;td style="text-align:right">46.1%&lt;/td>
&lt;td style="text-align:right">0.3968&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Fine-tuned encoder + linear head&lt;/td>
&lt;td style="text-align:right">&lt;strong>77.5%&lt;/strong>&lt;/td>
&lt;td style="text-align:right">&lt;strong>0.7801&lt;/strong>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The fine-tuned run scored higher than the frozen probe on both metrics in this comparison. Its strongest class F1 scores were for walking (0.981), walking downstairs (0.949), and walking upstairs (0.943); sitting remained more difficult (0.457). Scores are from the final epoch on 2,947 test windows. With one recorded run per mode and no matched randomly initialized baseline, these outcomes are preliminary and do not isolate the benefit of pretraining.&lt;/p>
&lt;p>Figures 1 and 2 show the frozen-probe training curves and test confusion matrix. Figures 3 and 4 show the corresponding fine-tuned results.&lt;/p>
&lt;p>&lt;img src="./tcn_probe_history.png" alt="Training and validation cross-entropy loss and macro-F1 for the frozen-encoder TCN probe.">
&lt;em>Figure 1. Training and validation cross-entropy loss and macro-F1 over 20 epochs for the frozen-encoder probe.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./tcn_probe_confusion.png" alt="Test confusion matrix for the frozen-encoder TCN probe.">
&lt;em>Figure 2. Test confusion matrix for the frozen-encoder probe.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./tcn_finetune_history.png" alt="Training and validation cross-entropy loss and macro-F1 for the fine-tuned TCN.">
&lt;em>Figure 3. Training and validation cross-entropy loss and macro-F1 over 20 epochs for the fine-tuned model.&lt;/em>&lt;/p>
&lt;p>&lt;img src="./tcn_finetune_confusion.png" alt="Test confusion matrix for the fine-tuned TCN.">
&lt;em>Figure 4. Test confusion matrix for the fine-tuned model.&lt;/em>&lt;/p>
&lt;h2 id="conclusions">Conclusions&lt;/h2>
&lt;p>This TCN pipeline demonstrates how causal convolutions can learn local temporal features from multichannel inertial data and how those features can be transferred to activity classification. In the two recorded runs, fine-tuning substantially outperformed using the pretrained encoder as a frozen feature extractor. The per-class results also show that strong recognition of locomotor activities does not guarantee equally strong recognition of static postures.&lt;/p>
&lt;h2 id="limitations-and-future-directions">Limitations and Future Directions&lt;/h2>
&lt;p>The comparison contains one run per transfer mode, reports the final epoch rather than a validation-selected checkpoint, and has no fully supervised from-scratch baseline. The encoder&amp;rsquo;s 13-sample receptive field is short relative to a full activity window, and mean pooling does not explicitly preserve global temporal order. In addition, validation subjects' unlabeled signals contributed to pretraining and scaler fitting. The predictor was evaluated on the official test split during pretraining; if those scores influenced model choices, the test set is not an untouched benchmark.&lt;/p>
&lt;p>Next steps are to repeat experiments across controlled random seeds, select checkpoints using validation data, compare against a matched model trained from random initialization, and hold validation subjects out before pretraining and normalization. Expanding the receptive field and comparing pooling strategies could test how longer-term temporal context affects static and transitional activities. Final claims should use a test set that has not informed model or hyperparameter decisions.&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ol>
&lt;li>Anguita, D., Ghio, A., Oneto, L., Parra, X., and Reyes-Ortiz, J. L. (2013). “A Public Domain Dataset for Human Activity Recognition Using Smartphones.” &lt;em>ESANN 2013&lt;/em>.&lt;/li>
&lt;li>Bai, S., Kolter, J. Z., and Koltun, V. (2018). “An Empirical Evaluation of Generic Convolutional and Recurrent Networks for Sequence Modeling.”&lt;/li>
&lt;li>Project materials: &lt;a href="tcn_imu_classification_description.md">TCN study description&lt;/a>, &lt;a href="TCN_IMU.py">TCN implementation&lt;/a>, &lt;a href="tcn_classification.py">classification implementation&lt;/a>, and &lt;a href="../tcn_classification_results/comparison.md">recorded classification results&lt;/a>.&lt;/li>
&lt;/ol></description></item></channel></rss>