# DeepLabCut 安裝及使用步驟
:::info
首先進入 [DLC github頁面](https://github.com/DeepLabCut/DeepLabCut) 或 [官方網站](https://deeplabcut.github.io/DeepLabCut/README.html) 可以看到詳細介紹。
點擊 [Installation: how to install DeepLabCut](https://deeplabcut.github.io/DeepLabCut/docs/installation.html)看安裝步驟
:::
## STEP 1
安裝 `Anaconda` (自己想辦法)
## STEP 2
### 進入此網站
https://github.com/DeepLabCut/DeepLabCut/blob/main/conda-environments/DEEPLABCUT.yaml
### 點擊`...`

### 點擊Download
會下載一個檔案 `DEEPLABCUT.yaml`

### 移動 `DEEPLABCUT.yaml`
看你想在哪裡建立虛擬環境就移到哪,我是放在 `C:\Users\我的名字` 。
### 進入 `cmd` 操作
以`系統管理員身分`執行 `cmd`

進入這個畫面

### 進入存放 `DEEPLABCUT.yaml` 存放的資料夾
`cd \Users\你的名字`
### 建立虛擬環境 `DEEPLABCUT`
`conda env create -f DEEPLABCUT.yaml`
看到以下畫面就是建立成功

### 進入虛擬環境
`conda activate DEEPLABCUT`
下載 tensorflow `pip install deeplabcut[tf]`
:::warning
離開虛擬環境
`conda deactivate`
刪除虛擬環境
`conda env remove --name DEEPLABCUT`
:::
### GPU設置
先確定有無抓到 GPU,return True 代表已抓到 GPU,False代表還沒
```
python -c "import torch; print(torch.version); print(torch.cuda.is_available())"
```

如果 return False,先確定cuda版本,如下圖右上角的 CUDA Version: 12.6
```
nvidia-smi
```

再到pytorch官網找對應的版本(https://pytorch.org/)

並執行下面的 command 即可抓到 GPU
```
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
```
### 啟動 `DeepLabCut` (簡稱`DLC`)
`python -m deeplabcut`

成功就會出現這個畫面
## 狀況排除
### 出現跟 `fbgemm.dll` 有關的錯誤
:::danger
>OSError: [WinError 126] The specified module could not be found. Error loading “C:\Users\pc\ali\Lib\site-packages\torch\lib\fbgemm.dll” or one of its dependencies.
:::
#### 點擊
https://www.dllme.com/dll/files/libomp140_x86_64/00637fe34a6043031c9ae4c6cf0a891d ,按下 `Start Download` 。
#### 開啟下載好的壓縮檔

將 `libomp140.x86_64.dll` 丟進 `C:\Windows\System32`
:::success
重新執行 `python -m deeplabcut` 。
:::
## `DLC` 操作
:::info
可以參考官方演示影片,https://www.youtube.com/watch?v=tr3npnXWoD4
每步驟有選項不知道作用就看 [Beginner's Guide to DeepLabCut](https://deeplabcut.github.io/DeepLabCut/docs/beginner-guides/beginners-guide.html)
:::
### 建立專案
#### `Create New Project`

`Project` 跟 `Experimeter` 自己輸入
-> Location : **專案存檔路徑**
-> Browse folders 路徑:`\\192.168.1.46\jupyter-nas\raiso_data\WuLab\side-video`
-> choose -> 選 S1 和 S18 兩個影片( -> Copy videos to project folder )
-> **create**
:::warning
有彈出視窗記得點掉(按ok)
:::
---
### 基本設定
#### `Manage project`

點擊 `Edit config.yaml` 自己設定標註點 (bodypart) 以及骨架 (skeleton) 的**數量**和**名字**。
`numframes2pick` 可以選擇要標註幾張圖片。
:::warning
介面太難用可以直接改 `config.yaml`,例如 skeleton 組合要增加
:::
:::info

bodypart 是圖中的點,skeleton 則是線。(skeleton 可有可無)
:::
---
### 標註圖片 (label)
#### `Extract frames`

`Extraction method` 可以調 `manual` 來選擇自己挑選要被標註的圖片或 `automatic` 用下方的 algorithm 自動挑選。
下方 `Add more videos`(`Select videos`) 及 `Clear selection` 來選擇有幾個影片要標註,`avi` `mp4` 都可以。
**設定完按右邊的 `Extract Frames`**。
#### `Label frames`
按 `Label frames` 選擇要 label 的 frame 的資料夾

點圈起來的地方開始標點,下面講幾個比較重要的,其它自己試。
`point size` 調整 label 點顯示在圖上的大小,
`labeling mode` 自己試試看,我是用 quick。
`view shorcut` 可以看快捷鍵,我只用到方向鍵和 Ctrl+s。
方向鍵左右選擇要點的圖,全部點完按 Ctrl+s存檔就可以把視窗關掉了。
:::warning
如果無法儲存 label 後的圖片或按 Label frames 後沒有圖片可以 label,則照前面流程重開一個新的 project,同時將要處理的影片載到本機端,並且 location 也設在本機即可
:::
#### `Check Labels`
可以看剛剛點完的結果,不看也沒差。
---
### 訓練模型
#### `Create training dataset`

可以只改 `Network architecture`,試試看[不同模型](https://deeplabcut.github.io/DeepLabCut/docs/recipes/nn.html)效果有沒有差。
設定完按 `Creating Training Dataset`
:::warning
按右上角 `Engine` 切換成 pytorch
:::
#### `train network`

可以調整 `Maximum epochs` 就好,更多設定可以從 `pose_cfg.yaml` 裡調整。
設定完按 `Train Network`
:::info
GPU setting
在`pose_cfg.yaml`進行設定
1. model->backbone->freeze_bn_stats: `True` 改成 `False`
1. train_settings->batch_size: `1` 改成 `16`或其他更大的數
2. dataloader_workers: 可以調大,如 `2`、`4`...
3. dataloader_pin_memory: `False` 改成 `True`
:::


:::warning
要注意batch_size越大,記憶體吃得越多,滿載電腦會當機
最好調16就好,試過幾次32都會讓terminal卡死
:::
:::info
參數設定可以參考
https://deeplabcut.github.io/DeepLabCut/docs/beginner-guides/Training-Evaluation.html
:::
#### `Evaluate network`

全部勾起來,按 `Evaluate Network` 慢慢等他跑。
---
### 生成標註影片
#### `Analyze videos`
用來分析 likelihood 以及 body part xy座標 的一些統計資訊,產出一些精美的圖。

影片全選,全部勾起來 `bodypart` 也全選,最後按 `Analyze Videos` 慢慢等他跑。
#### `Create videos`

`Plot all bodyparts` 把每個點畫出來
`Draw skeleton` 把骨架畫出來
`Plotting confidence cutoff (pcoff)` 設定 likelihood 的閾值,超過才會畫出來。
:::warning
bodyparts 的點或 skeleton 線太醜要去 `Edit config.yaml` 想辦法改
:::
#### 看影片
去一開始設定的 `Location` 找專案存檔位置,點開 `videos` ,可以找到原始影片跟加了 label 的影片,`plot-poses` 裡的則是 `Analyze videos` 產生的精美圖片。