Note
Go to the end to download the full example code.
CSI classification tasks Tutorial¶
# !pip install pysensing
In this tutorial, we will be implementing codes for CSI classification tasks, including Human activity recognition and Human identity detection
import sys
sys.path.append('../..')
import pysensing.csi.dataset.get_dataloader as get_dataloader
import pysensing.csi.model.get_model as get_model
import pysensing.csi.inference.predict as predict
import pysensing.csi.inference.train as train
import pysensing.csi.inference.embedding as embedding
import torch
Load the data¶
# Human action recognition dataset:
# UT-HAR
# CSI size : 1 x 250 x 90
# number of classes : 7
# classes : lie down, fall, walk, pickup, run, sit down, stand up
# train number : 3977
# test number : 996
# NTU-HAR
# CSI size : 3 x 114 x 500
# number of classes : 6
# classes : box, circle, clean, fall, run, walk
# train number : 936
# test number : 264
# Widar
# BVP size : 22 x 20 x 20
# number of classes : 22
# classes :
# Push&Pull, Sweep, Clap, Slide, Draw-N(H), Draw-O(H),Draw-Rectangle(H),
# Draw-Triangle(H), Draw-Zigzag(H), Draw-Zigzag(V), Draw-N(V), Draw-O(V), Draw-1,
# Draw-2, Draw-3, Draw-4, Draw-5, Draw-6, Draw-7, Draw-8, Draw-9, Draw-10
# train number : 34926
# test number : 8726
# Human identity detection dataset:
# NTU-HumanID
# CSI size : 3 x 114 x 500
# number of classes : 14
# classes : gaits of 14 subjects
# train number : 546
# test number : 294
# Examples of NTU-Fi data
train_loader, test_loader = get_dataloader.load_classification_dataset('UT_HAR', batch_size=1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
for data in train_loader:
csi, label = data
csi = csi.to(device)
label = label.type(torch.LongTensor).to(device)
print('data:', csi)
print('label:', label)
break
/data1/msc/zyj/yunjiao_csi/1028/yunjiao_csi/tutorials/csi_source/data/csi
using dataset: UT-HAR DATA
data: tensor([[[0.5545, 0.5545, 0.6269, ..., 0.7542, 0.7898, 0.7559],
[0.5545, 0.5545, 0.6269, ..., 0.7542, 0.7898, 0.7559],
[0.5545, 0.5545, 0.6269, ..., 0.7542, 0.7898, 0.7559],
...,
[0.5694, 0.6034, 0.6449, ..., 0.7600, 0.7859, 0.7552],
[0.5810, 0.6046, 0.6436, ..., 0.7526, 0.7971, 0.7458],
[0.5810, 0.6046, 0.6436, ..., 0.7526, 0.7971, 0.7458]]],
device='cuda:0')
label: tensor([1], device='cuda:0')
Load the model¶
Model zoo: MLP LeNet ResNet RNN GRU LSTM BiLSTM CNN+GRU ViT
model = get_model.load_har_model('UT_HAR', 'MLP')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = get_model.load_pretrain(model, 'UT_HAR', 'MLP', device=device)
print(model)
har_MLP(
(fc): Sequential(
(0): Linear(in_features=22500, out_features=1024, bias=True)
(1): ReLU()
(2): Linear(in_features=1024, out_features=128, bias=True)
(3): ReLU()
(4): Linear(in_features=128, out_features=7, bias=True)
)
)
Model train¶
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
epoch_num = 1
train.har_train(train_loader, model, epoch_num, optimizer, criterion, device)
Epoch:1, Loss:1.901668259
Model inference¶
model = get_model.load_pretrain(model, 'UT_HAR', 'MLP', device=device)
output = predict.har_predict(csi, 'UT_HAR', model, device).type(torch.FloatTensor).to(device)
print("output:", output)
output: tensor([[-11.4981, 7.3044, -7.5377, -2.6977, -4.2921, 0.1082, -0.2130]],
device='cuda:0')
Evaluate loss¶
criterion = torch.nn.CrossEntropyLoss()
loss = criterion(output, label)
print(loss)
tensor(0.0013, device='cuda:0')
Generate embeddings¶
csi_embedding = embedding.har_csi_embedding(csi, 'UT_HAR', model, device)
print('csi_embedding: ', csi_embedding)
csi_embedding: tensor([[ 0.0000, 11.0925, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 6.3804, 9.9213, 0.0000, 0.0000, 29.4593, 0.0000, 2.3160,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 9.4756, 0.0000, 12.5232,
17.5423, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 11.4202,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 8.0701, 0.0000, 15.2518,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0420,
0.0000, 0.0000, 0.0000, 9.9100, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 4.6838, 0.0000, 0.0000,
5.1499, 0.0000, 0.0000, 7.9135, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 3.1761, 0.0000, 15.9863, 0.0000, 0.0000, 0.0000,
0.0000, 17.0392, 4.3671, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
device='cuda:0', grad_fn=<ReluBackward0>)
And that’s it. We’re done with our CSI humna activity recognition and human identity detection tutorials. Thanks for reading.
Total running time of the script: (0 minutes 24.704 seconds)