A self-supervised Transformer encoder for Human Activity Recognition (HAR) from IMU sensor data. Trained on the
WISDM smartphone+smartwatch dataset with a masked-prediction objective, SupCon contrastive learning, and LMM frequency-domain loss.
1 from modeling_imu_encoder import IMUMaskedEncoder
2
3 model = IMUMaskedEncoder . from_pretrained ( "NikoKKK/IMU-SelfSupEncoder-v1" )
4 model . eval ( )
5
6 # Input: (batch, 6 channels, 200 timesteps)
7 x = torch . randn ( 8 , 6 , 200 )
8
9 with torch . no_grad ( ) :
10 patch_out , intermediates , cls_out , global_freq = model ( x )
11
12 # cls_out: (8, 192) — use for classification
13 # patch_out: (8, 20, 192) — per-patch features
14 # intermediates: {2: (8, 20, 192), 4: (8, 20, 192)}
1 import torch . nn as nn
2
3 # Freeze encoder
4 for p in model . parameters ( ) :
5 p . requires_grad = False
6 model . eval ( )
7
8 # Simple classifier on CLS token
9 classifier = nn . Sequential (
10 nn . Linear ( 192 , 256 ) , nn . ReLU ( ) , nn . Dropout ( 0.3 ) ,
11 nn . Linear ( 256 , 18 ) , # 18 activity classes
12 )
13
14 # Extract features and train classifier
15 with torch . no_grad ( ) :
16 cls_features = model . encode ( imu_windows ) # (N, 192)
Input: (B, 6, 200)
│
├── Conv1d Stem (6→96, kernel=10, stride=10)
│ └── Time tokens: (B, 20, 96)
│
├── Per-patch FFT → Linear
│ └── Freq tokens: (B, 20, 96)
│
├── Concat + Fusion → (B, 20, 192)
│
├── Global FFT (full 200-pt) → Linear → (B, 1, 192)
│
├── Position Embedding (learned, 21 positions)
│
└── Transformer Encoder (4 layers, 6 heads, 192-dim, MLP ratio 3.0)
├── Layer 2 → intermediate output
├── Layer 4 → intermediate output
└── CLS token + 20 patch tokens + global_freq token
1 @misc{imu-selfsup-encoder,
2 author = {Li, Yu},
3 title = {IMU-SelfSupEncoder-v1: Self-Supervised Transformer for IMU Activity Recognition},
4 year = {2026},
5 url = {https://huggingface.co/NikoKKK/IMU-SelfSupEncoder-v1},
6 }
7
8 @inproceedings{weiss2019wisdm,
9 title={Smartphone and Smartwatch-Based Biometrics Using Activities of Daily Living},
10 author={Weiss, Gary M and Yoneda, Kenichi and Hayajneh, Thaier},
11 booktitle={IEEE Access},
12 volume={7},
13 pages={133190--133202},
14 year={2019},
15 publisher={IEEE}
16 }