.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "uwb/uwb_occupancy_detection_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_uwb_uwb_occupancy_detection_tutorial.py: Tutorial for UWB Occupancy Detection ======= .. GENERATED FROM PYTHON SOURCE LINES 5-16 .. code-block:: Python 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 * .. GENERATED FROM PYTHON SOURCE LINES 17-33 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 ``` .. GENERATED FROM PYTHON SOURCE LINES 35-45 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' .. GENERATED FROM PYTHON SOURCE LINES 45-55 .. code-block:: Python 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 .. rst-class:: sphx-glr-script-out .. code-block:: none Loading nlos_human_detection time domain dynamic dataset... torch.Size([64, 256]) torch.Size([64]) .. GENERATED FROM PYTHON SOURCE LINES 56-61 Load the model ----------------------------------- Model zoo: - LSTM - AutoEncoder .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. code-block:: Python model = load_occupancy_detection_model(dataset_name = 'nlos_human_detection', model_name = 'autoencoder') print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none 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) ) .. GENERATED FROM PYTHON SOURCE LINES 66-68 Model train ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 68-81 .. code-block:: Python 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, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Loading nlos_human_detection time domain dynamic dataset... Epoch:1, Accuracy:0.7926,Loss:0.394943418 .. GENERATED FROM PYTHON SOURCE LINES 82-84 Model inference ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 84-97 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none 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]) .. GENERATED FROM PYTHON SOURCE LINES 98-101 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. .. GENERATED FROM PYTHON SOURCE LINES 101-108 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none uwb_embedding shape: torch.Size([256, 64]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.766 seconds) .. _sphx_glr_download_uwb_uwb_occupancy_detection_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: uwb_occupancy_detection_tutorial.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: uwb_occupancy_detection_tutorial.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_