File size: 12,095 Bytes
136ad90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | """WISDM IMU Masked Encoder β self-supervised Transformer for activity recognition.
A pure-PyTorch model for encoding 10-second IMU sensor windows (6-channel
accelerometer + gyroscope @ 20 Hz) into 192-dim representations. Pretrained
with masked prediction, SupCon contrastive learning, and LMM frequency loss
on the WISDM smartphone+smartwatch dataset (18 activity classes).
Usage:
from modeling_imu_encoder import IMUMaskedEncoder
model = IMUMaskedEncoder.from_pretrained("NikoKKK/IMU-SelfSupEncoder-v1")
model.eval()
# Input: (B, 6, 200) tensor β 6 IMU channels, 200 timesteps
with torch.no_grad():
patch_out, intermediates, cls_out, global_freq = model(x)
# cls_out: (B, 192) β global representation for classification
# patch_out: (B, 20, 192) β per-window-patch embeddings
# intermediates: {layer_idx: (B, 20, 192)} β intermediate layer outputs
"""
import math
import json
import os
from typing import Dict, List, Optional, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
class IMUEncoderConfig:
"""Configuration for IMUMaskedEncoder.
Attributes:
n_channels (int): Number of IMU sensor channels (default: 6).
patch_size (int): Conv-stem patch size in timesteps (default: 10).
n_patches (int): Number of patches per window (default: 20).
embed_dim (int): Embedding dimension (default: 192).
n_layers (int): Number of Transformer encoder layers (default: 4).
n_heads (int): Number of attention heads (default: 6).
mlp_ratio (float): MLP hidden/embed_dim ratio (default: 3.0).
dropout (float): Dropout probability (default: 0.1).
target_layers (List[int]): Layers collected as intermediate outputs.
"""
def __init__(
self,
n_channels: int = 6,
patch_size: int = 10,
n_patches: int = 20,
embed_dim: int = 192,
n_layers: int = 4,
n_heads: int = 6,
mlp_ratio: float = 3.0,
dropout: float = 0.1,
target_layers: Optional[List[int]] = None,
**kwargs,
):
self.n_channels = n_channels
self.patch_size = patch_size
self.n_patches = n_patches
self.embed_dim = embed_dim
self.n_layers = n_layers
self.n_heads = n_heads
self.mlp_ratio = mlp_ratio
self.dropout = dropout
self.target_layers = target_layers or [2, 4]
@classmethod
def from_dict(cls, d: dict) -> "IMUEncoderConfig":
return cls(**{k: v for k, v in d.items() if not k.startswith("_")})
def to_dict(self) -> dict:
return {
"n_channels": self.n_channels,
"patch_size": self.patch_size,
"n_patches": self.n_patches,
"embed_dim": self.embed_dim,
"n_layers": self.n_layers,
"n_heads": self.n_heads,
"mlp_ratio": self.mlp_ratio,
"dropout": self.dropout,
"target_layers": self.target_layers,
}
# ---------------------------------------------------------------------------
# Sub-modules
# ---------------------------------------------------------------------------
class PatchTimeFreqEmbedding(nn.Module):
"""Conv-stem per-patch time + local frequency fusion -> embed_dim tokens."""
def __init__(self, config: IMUEncoderConfig):
super().__init__()
half_dim = config.embed_dim // 2
self.conv_stem = nn.Conv1d(
config.n_channels, half_dim, kernel_size=config.patch_size,
stride=config.patch_size, bias=False,
)
n_freq_bins = config.patch_size // 2 + 1
self.freq_proj = nn.Linear(config.n_channels * n_freq_bins, half_dim)
self.pos_embed = nn.Parameter(
torch.randn(1, config.n_patches + 1, config.embed_dim) * 0.02
)
self.fusion = nn.Linear(config.embed_dim, config.embed_dim)
self.norm = nn.LayerNorm(config.embed_dim)
self.patch_size = config.patch_size
self.n_patches = config.n_patches
# FFT over full 200-timestep signal: rfft(200) β 101 bins, then .mean(dim=1)
n_global_freq_bins = config.patch_size * config.n_patches // 2 + 1
self.global_freq_proj = nn.Linear(n_global_freq_bins, config.embed_dim, bias=False)
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
B, C, T = x.shape
time_feat = self.conv_stem(x)
x_patch = x.view(B, C, self.n_patches, self.patch_size)
x_patch_centered = x_patch - x_patch.mean(dim=-1, keepdim=True)
freq_mag = torch.abs(torch.fft.rfft(x_patch_centered, dim=-1))
n_freq = freq_mag.size(-1)
freq_flat = freq_mag.permute(0, 2, 1, 3).reshape(B, self.n_patches, C * n_freq)
freq_feat = self.freq_proj(freq_flat).transpose(1, 2)
combined = torch.cat([time_feat, freq_feat], dim=1)
tokens = self.fusion(combined.transpose(1, 2))
full_freq = torch.abs(torch.fft.rfft(x, dim=-1)).mean(dim=1)
global_freq = self.global_freq_proj(full_freq).unsqueeze(1)
tokens = tokens + self.pos_embed[:, :self.n_patches, :]
global_freq = global_freq + self.pos_embed[:, self.n_patches:self.n_patches + 1, :]
tokens = self.norm(tokens)
global_freq = self.norm(global_freq)
return tokens, global_freq
class TransformerEncoder(nn.Module):
"""Standard Transformer encoder, collects intermediate layer outputs."""
def __init__(self, config: IMUEncoderConfig):
super().__init__()
self.n_layers = config.n_layers
self.collect_layers = config.target_layers
self.cls_token = nn.Parameter(torch.randn(1, 1, config.embed_dim) * 0.02)
self.layers = nn.ModuleList([
nn.TransformerEncoderLayer(
d_model=config.embed_dim,
nhead=config.n_heads,
dim_feedforward=int(config.embed_dim * config.mlp_ratio),
dropout=config.dropout,
activation="gelu",
batch_first=True,
norm_first=True,
)
for _ in range(config.n_layers)
])
self.final_norm = nn.LayerNorm(config.embed_dim)
self.dropout = nn.Dropout(config.dropout)
def forward(self, x: torch.Tensor):
B = x.shape[0]
cls_tokens = self.cls_token.expand(B, -1, -1)
x = torch.cat([cls_tokens, self.dropout(x)], dim=1)
intermediates = {}
for i, layer in enumerate(self.layers):
x = layer(x)
layer_idx = i + 1
if layer_idx in self.collect_layers:
intermediates[layer_idx] = self.final_norm(x[:, 1:, :])
x = self.final_norm(x)
cls_out = x[:, 0, :]
patch_out = x[:, 1:, :]
if self.n_layers in self.collect_layers:
intermediates[self.n_layers] = patch_out
return patch_out, intermediates, cls_out
# ---------------------------------------------------------------------------
# Main model
# ---------------------------------------------------------------------------
class IMUMaskedEncoder(nn.Module):
"""Self-supervised encoder for IMU-based human activity recognition.
Input: (B, 6, 200) β 6-channel IMU window (accel_x/y/z, gyro_x/y/z) @ 20 Hz.
Output: patch_out (B, 20, 192), intermediates dict, cls_out (B, 192),
global_freq (B, 1, 192).
The CLS token (`cls_out`) is the primary global representation for
downstream classification. Intermediate layer outputs can be used for
multi-level feature extraction or distillation.
Usage:
model = IMUMaskedEncoder.from_pretrained("NikoKKK/IMU-SelfSupEncoder-v1")
model.eval()
x = torch.randn(8, 6, 200) # 8 windows of 10 seconds each
with torch.no_grad():
patch_out, intermediates, cls_out, global_freq = model(x)
print(cls_out.shape) # (8, 192)
"""
_HUB_URL = "https://huggingface.co/NikoKKK/IMU-SelfSupEncoder-v1"
def __init__(self, config: IMUEncoderConfig):
super().__init__()
self.config = config
self.embed = PatchTimeFreqEmbedding(config)
self.transformer = TransformerEncoder(config)
self.mask_token = nn.Parameter(torch.randn(1, 1, config.embed_dim) * 0.02)
@classmethod
def from_pretrained(cls, model_id: str = "NikoKKK/IMU-SelfSupEncoder-v1",
force_download: bool = False) -> "IMUMaskedEncoder":
"""Load pretrained model from Hugging Face Hub or local path.
Args:
model_id: Hugging Face model ID (e.g. "NikoKKK/IMU-SelfSupEncoder-v1")
or local directory path.
force_download: Force re-download from Hub.
Returns:
IMUMaskedEncoder with pretrained weights loaded.
"""
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
# Determine if model_id is a local path
if os.path.isdir(model_id):
config_path = os.path.join(model_id, "config.json")
weights_path = os.path.join(model_id, "model.safetensors")
else:
config_path = hf_hub_download(
model_id, "config.json", force_download=force_download,
)
weights_path = hf_hub_download(
model_id, "model.safetensors", force_download=force_download,
)
with open(config_path, "r") as f:
config_dict = json.load(f)
config = IMUEncoderConfig.from_dict(config_dict)
model = cls(config)
state_dict = load_file(weights_path)
model.load_state_dict(state_dict, strict=True)
return model
def forward(self, x: torch.Tensor, mask_matrix=None):
"""Forward pass.
Args:
x: (B, C, T) tensor β IMU sensor window.
Expected C=6 (accel_x,y,z + gyro_x,y,z), T=200 @ 20 Hz.
mask_matrix: Optional (B, N_patches) boolean tensor. When given,
masked positions are replaced with [MASK] tokens.
Used during pretraining only.
Returns:
patch_out: (B, N_patches, embed_dim)
intermediates: {layer_idx: (B, N_patches, embed_dim)}
cls_out: (B, embed_dim) β global representation
global_freq: (B, 1, embed_dim) β global frequency token
"""
B, C, T = x.shape
tokens, global_freq = self.embed(x)
if mask_matrix is not None:
mask_tok = self.mask_token.expand(B, tokens.size(1), -1)
tokens = torch.where(mask_matrix.unsqueeze(-1), mask_tok, tokens)
input_tokens = torch.cat([global_freq, tokens], dim=1)
full_out, intermediates, cls_out = self.transformer(input_tokens)
# Remove global_freq position from patch outputs
patch_out = full_out[:, 1:, :]
trimmed_intermediates = {k: v[:, 1:, :] for k, v in intermediates.items()}
return patch_out, trimmed_intermediates, cls_out, global_freq
def encode(self, x: torch.Tensor) -> torch.Tensor:
"""Extract CLS token embedding for downstream tasks.
Args:
x: (B, 6, 200) IMU sensor window.
Returns:
(B, embed_dim) global representation.
"""
_, _, cls_out, _ = self.forward(x)
return cls_out
def encode_with_layers(self, x: torch.Tensor) -> Dict[int, torch.Tensor]:
"""Extract multi-level intermediate representations.
Args:
x: (B, 6, 200) IMU sensor window.
Returns:
{layer_idx: (B, N_patches, embed_dim)} for configured target layers.
"""
_, intermediates, _, _ = self.forward(x)
return intermediates
# For HF AutoModel compatibility
IMUMaskedEncoder.config_class = IMUEncoderConfig
|