Note
Go to the end to download the full example code.
Tutorial for UWB Occupancy Detection¶
import torch
import torch.nn as nn
import os
from pysensing.uwb.datasets.get_dataloader import *
from pysensing.uwb.models.get_model import *
from pysensing.uwb.training.occupancy_detection import *
from pysensing.uwb.inference.predict import *
from pysensing.uwb.inference.embedding import *
Download Data from Cloud Storage¶
Open the following link in your browser to download HAR datasets:
[Download nlos_human_detection Dataset](https://pysensing.oss-ap-southeast-1.aliyuncs.com/data/uwb/nlos_human_detection.zip) […]()
Unzip the downloaded file and move to your data folder. For HAR, the data folder should look like this:
`
|---data
|------|---occupancy_detection
|------|------|---nlos_human_detection
|------|------|------|---dynamic
|------|------|------|---static
`
Load the data¶
Occupancy Detection dataset:
NLOS Huaman Detection Dataset - UWB size : n x 256
Dataset name choices are: - ‘nlos_human_detection_raw_dynamic’ - ‘nlos_human_detection_raw_static’
root = './data'
train_loader, val_loader = load_occupancy_detection_dataset(root, datasetname='nlos_human_detection_raw_dynamic')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
for data in train_loader:
x, y = data
print(x.size())
print(y.size())
break
Loading nlos_human_detection time domain dynamic dataset...
torch.Size([64, 256])
torch.Size([64])
Load the model¶
Model zoo: - LSTM - AutoEncoder
model = load_occupancy_detection_model(dataset_name = 'nlos_human_detection', model_name = 'autoencoder')
print(model)
occupancy_detection_autoencoder(
(encoder): Sequential(
(0): Linear(in_features=256, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=32, bias=True)
(3): ReLU()
(4): Linear(in_features=32, out_features=16, bias=True)
)
(decoder): Sequential(
(0): Linear(in_features=16, out_features=32, bias=True)
(1): ReLU()
(2): Linear(in_features=32, out_features=64, bias=True)
)
(fc): Linear(in_features=64, out_features=2, bias=True)
)
Model train¶
criterion = nn.CrossEntropyLoss()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
nlos_human_detection_training(
root = root,
dataset_name='nlos_human_detection_raw_dynamic',
model_name='autoencoder',
num_epochs=1,
learning_rate=0.001,
save_weights=True,
)
Loading nlos_human_detection time domain dynamic dataset...
Epoch:1, Accuracy:0.7926,Loss:0.394943418
Model inference¶
occupancy_detector = predictor(
task='occupancy_detection',
dataset_name='nlos_human_detection',
model_name='autoencoder',
pt_weights='./nlos_human_detection_weights.pth'
)
for data in val_loader:
x, y = data
break
outputs = occupancy_detector.predict(x)
print("output:", outputs)
Pretrained weights loaded.
output: tensor([1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0,
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0,
1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1,
0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1,
0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1])
Generate embedding¶
noted that the model_name variable defined in load_model function represents the model structure name, and in load_pretrain_weights function represents the model structure and pretrain dataset name.
model = load_occupancy_detection_model(dataset_name = 'nlos_human_detection', model_name = 'autoencoder')
model = load_pretrain_weights(model, dataset_name = 'nlos_human_detection_static', model_name = 'autoencoder',device=device)
uwb_embedding = occupancy_detection_uwb_embedding(x, model, device)
print('uwb_embedding shape: ', uwb_embedding.shape)
uwb_embedding shape: torch.Size([256, 64])
Total running time of the script: (0 minutes 0.766 seconds)