# Pytorch MobileNet SSD for Falling Detection
###### tags: `Pytorch` `ICL-ITRI`
2019/07/29
## A.0 at Nvidia Nano torch/torchVision install
https://gist.github.com/dusty-nv/ef2b372301c00c0a9d3203e42fd83426
https://devtalk.nvidia.com/default/topic/1049071/jetson-nano/pytorch-for-jetson-nano-with-new-torch2trt-converter/
https://github.com/zrzka/python-wheel-aarch64/releases/tag/jetson-nano-1.1
https://github.com/dusty-nv/jetson-scripts
https://github.com/dusty-nv/jetson-scripts/blob/master/pytorch_jetson_install.sh
### PyTorch v1.1.0
---
#### python3.6
```
wget https://nvidia.box.com/shared/static/j2dn48btaxosqp0zremqqm8pjelriyvs.whl -O torch-1.1.0-cp36-cp36m-linux_aarch64.whl
$ pip3 install numpy torch-1.1.0-cp36-cp36m-linux_aarch64.whl
```
#### torchvision (numpy >= 1.14)
```
$ sudo apt-get install libjpeg-dev zlib1g-dev
$ git clone -b v0.3.0 https://github.com/pytorch/vision torchvision
$ cd torchvision
$ sudo python setup.py install
```
## A.1. Data Path
### <font color="4f0099">Document/pytorch_mobilenet_ssd/</font>
├── ...
├── <font color="4f0099">backup/</font>
│ ├── ...
│ └── MobileNet_v1_ssd200.pth
├── <font color="4f0099">dataset/</font>
│ ├── ...
│ └── <font color="4f0099">fall_dataset/</font>
│ │ ├──...
│ │ ├── <font color="4f0099">images/</font>
│ │ └── <font color="4f0099">labels/</font>
├── <font color="4f0099">mAP/</font>
│ ├── ...
│ ├── <font color="4f0099">acc</font>
│ │ ├── ...
│ │ └── acc_MobileNet_v1_ssd200.txt
│ └── <font color="4f0099">mAP</font>
│ │ ├── ...
│ │ └── mAP_MobileNet_v1_ssd200.txt
├── <font color="4f0099">pretrained_model/</font>
│ ├── ...
│ └── mobilenet_v1_with_relu69.5.pth
├── <font color="4f0099">vision</font>
├── dataset_info.py
├── demo.py
├── eval.py
├── train.py
├── train.txt
└── test.txt
## B. How to Train & Test
### 1. Modify labels
* 修改 dataset_info.py
格式為標籤的排序
```=2
class_names = ['standing', 'sitting', 'lying', 'bending']
```
### 2. Training
* Command (Training):
```
$ cd /media/e200/新增磁碟區/Documents/pytorch_mobilenet_ssd
$ python3 train.py -Datasets ./dataset/fall_dataset/
```
Dataset 放置方式為兩種:
(1)包含一個全部的 images 和 labels
(2)使用者自行建立 train 和 test 資料夾後,並個別加入對應的 images 和 labels
-Datasets: 使用的dataset (初始為./dataset/fall_dataset/)
-NumWorkers: 如果大於1,使用多進程讀取檔案 (初始為4)
-Model: 使用的模型 (初始為MobileNet_v1_ssd)
-lr: 初始leraning rate (初始為0.01)
-Momentum: SGD上的 momentum (初始為0.9)
-WeightDecay: SGD上的 weight_decay (初始為1e-4)
-Epochs: 結束訓練的最大 epoch (初始為200)
-BatchSize: 每一個 batch 包含的 data 數量 (初始為64)
-Pretrained: 使用 pretrain 在 ILSVRC2012 的模型 (初始為mobilenet_v1_with_relu69.5.pth)
-Keeptxt: 保存txt內的資烙不會被亂數打亂 (初始為 False)
-SampleData: 如果為 True ,參考放置(1)將所有資料依據亂數打亂並分成 train 和 test;如果為 False ,參考放置(2)程式讀取自行建立的 train 和 test 資料夾內 images 和 labels (初始為True)
訓練同時產生每個epoch的模型
ex. 第200個epoch結束產生 Mobilenet_v1_ssd200.pth
* Command (Evaluate):
```
$ cd /media/e200/新增磁碟區/Documents/pytorch_mobilenet_ssd
$ python3 eval.py -Datasets ./dataset/fall_dataset/ -trained_model ./backup/MobileNet_v1_ssd200.pth
```
-Datasets: 使用的dataset (初始為./dataset/fall_dataset/)
-Model: 使用的模型 (初始為MobileNet_v1_ssd)
-SampleData: 如果為 True ,參考放置(1)將所有資料依據亂數打亂並分成 train 和 test;如果為 False ,參考放置(2)程式讀取自行建立的 train 和 test 資料夾內 images 和 labels (初始為True)
-iou_thresh: 計算mAP和acc的中是否加入GT的閥值 (初始為0.5)
-trained_model: 用來測試的模型 (初始為./backup/MobileNet_v1_ssd200.pth)
-use_2007_metric: 使用 Pascal VOC 在2007年用的mAP評估方式 (初始為 True)
執行完程式之後會在
(1) pytorch_mobilenet_ssd/mAP/{dataset名稱}/mAP 產生 mAP_{trained_model}.txt
=>pytorch_mobilenet_ssd/mAP/fall_dataset/mAP/mAP_MobileNet_v1_ssd200.txt
(2) pytorch_mobilenet_ssd/mAP/{dataset名稱}/acc 產生 acc_{trained_model}.txt =>pytorch_mobilenet_ssd/mAP/fall_dataset/acc/acc_MobileNet_v1_ssd200.txt
* Command (Webcam):
```
$ cd /media/e200/新增磁碟區/Documents/pytorch_mobilenet_ssd
$ python3 demo.py -trained_model ./backup/MobileNet_v1_ssd200.pth
```
-Model: 使用的模型 (初始為MobileNet_v1_ssd)
-trained_model: 用來測試的模型 (初始為./backup/MobileNet_v1_ssd200.pth)
* 0726:
* Trianing data
* standing = 14,172
* sitting = 4,248
* lying = 2,643
* bending = 2,463
* Testing data
* standing = 5,785
* sitting = 2,129
* lying = 1,637
* bending = 1,425
* Testing performance
* Precision = 96.03 (%)
* Recall = 93.55 (%)
* F1-score = 94.78
* standing accuarcy = 92.17 (%) (5332/5785)
* sitting accuarcy = 90.32 (%) (1923/2129)
* lying accuarcy = 99.39 (%) (1627/1637)
* bending accuarcy = 97.26 (%) (1386/1425)
* mAP = 90.63 (%)
* Training weight: MobileNet_v1_ssd200.pth (200 epochs 約 68,600 個 batches)
## C. Test and result
* Test Command (Webcam):
```
$ cd /media/e200/新增磁碟區/Documents/pytorch_mobilenet_ssd
$ python3 demo.py -trained_model ./backup/MobileNet_v1_ssd200.pth
```
-Model: 使用的模型 (初始為MobileNet_v1_ssd)
-trained_model: 用來測試的模型 (初始為./backup/MobileNet_v1_ssd200.pth)
#### PC intel / ubuntu 16.04 / mobileNet_SSD
---
* GPU/CPU/RAM: Nvidia 1080Ti /i7 7700/16G
* SW: cuda10/ cudnn9 /python3
* FPS : 100~110 fps
* model : mobileNet_SSD

#### Nvidia Nano / ubuntu 16.04 / mobileNet_SSD
---
* GPU/CPU/RAM: Nvidia nano /nvidia nano/4G
* SW: cuda10/ cudnn9 /python3
* FPS : 10~20 fps
* model : mobileNet_SSD
#### Nvidia Tx2 / ubuntu 16.04 / yoloV3
---
* GPU/CPU/RAM: Nvidia Tx2 /nvidia Tx2/8G
* SW: cuda8/ cudnn7 /python2
* FPS : 6~7 fps
* model : yolov3