# YOLOv7訓練與推論實作 https://github.com/WongKinYiu/yolov7 ## 1. 建置環境 - Step 1. 建立虛擬環境 ``` conda create --name yolov7 python=3.8 conda activate yolov7 ``` - Step 2. 請先確保已安裝正確版本的pyTorch與cuda再來pip install,避免版本衝突! > torch>=1.7.0, !=1.12.0 > torchvision>=0.8.1, !=0.13.0 ``` conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch ``` 或查詢自己適用的CUDA版本,並依照[此文件](https://hackmd.io/@franchingkao/SJo_mX8vh)安裝。 [查詢版本](https://download.pytorch.org/whl/torch_stable.html) [下載安裝檔](https://pytorch.org/get-started/previous-versions/) [推薦教學](https://www.it145.com/9/191725.html) - Step 3. 用git下載整包程式 ``` git clone https://github.com/WongKinYiu/yolov7.git ``` - Step 4. 安裝必要套件 ``` pip install -r requirements.txt ``` - Step 5. 下載權重檔 直接到[github](https://github.com/WongKinYiu/yolov7/tree/main)上抓`YOLOv7`的模型權重,並放到正確路徑 ![](https://hackmd.io/_uploads/S1wE5aP3h.png) ## 2. 建置資料集 - Step 1. 準備影像與標記檔 * `images`: 影像檔 (jpg, png...) * `labels`: 標記檔 (txt),首先要確保標記檔為YOLO可使用的**txt檔**(用LabelImg標記的點位預設儲存為xml檔,而部分公開資料集為mat檔)。若需要轉換,可透過以下方式: * xml2txt.py: `python xml2txt.py` -> TODO: 這要改為可輸入路徑config * csv2txt.py: -> TODO: 補github * mat2txt.py: -> TODO: 補github - Step 2. 將資料集分為train/val * splitFile.py: `python splitFile.py val_num` - Step 3. 建立`train.txt`和`test.txt`,分別放訓練與測試集的檔案路徑,例如: ``` egohands_yolo\images\train\CARDS_COURTYARD_B_T_frame_0011.jpg egohands_yolo\images\train\CARDS_COURTYARD_B_T_frame_0036.jpg egohands_yolo\images\train\CARDS_COURTYARD_B_T_frame_0099.jpg egohands_yolo\images\train\CARDS_COURTYARD_B_T_frame_0113.jpg ... ``` - Step 4. `classes.names`: 建立一文字檔(txt就可),逐行放物件類別,例如: ``` cls1 cls2 ... ``` - Step 5. 資料夾內容應為: ``` # 2) file: path/images.txt yourData ├── images ├├── train ├├── test ├── labels ├├── train ├├── test ├── classes.names ├── train.txt ├── test.txt ``` 或 ``` # 1) directory: path/images/ yourData ├── train ├├── images ├├── labels ├── test ├├── images ├├── labels ├── classes.names ``` ## 3. 建置配置檔 * 在`.\cfg\training\`中,複製檔案`yolov7.yaml`並命名為`myyolov7.yaml`,更改類別數量`nc`。 * 在`.\data\`中,新增檔案並命名為`mydata.yaml`,貼上以下內容並修改訓練與測試資料集的路徑`train` `test`、類別數量`nc`、類別名稱`names`。 ``` # train and val data path as # 1) directory: path/images/ # 2) file: path/images.txt # 3) list: [path1/images/, path2/images/] train: ./mydataset/train.txt val: ./mydataset/test.txt test: # number of classes nc: 1 # class names names: ['hand'] ``` > 以上`nc:`為欲使用資料集的類別數量,必須跟在步驟2建立的`classes.names`數量一致。 ## 3. 模型訓練 ``` python train.py --workers 4 --device 0 --batch-size 4 --epochs 100 --data .\data\handData.yaml --img 300 300 --cfg .\cfg\training\yolov7_hand.yaml --hyp .\data\hyp.scratch.p5.yaml --weights .\yolov7.pt ``` ``` python train.py --workers 4 --device 0 --batch-size 32 --epochs 100 --data ./data/handData.yaml --img 320 320 --cfg ./cfg/training/yolov7_hand.yaml --hyp ./data/hyp.scratch.p5.yaml --weights ./yolov7.pt ``` 根據自己的需求修改參數,若有修改其他參數的需求可執行 `python train.py help` 查看: ``` usage: train.py [-h] [--weights WEIGHTS] [--cfg CFG] [--data DATA] [--hyp HYP] [--epochs EPOCHS] [--batch-size BATCH_SIZE] [--img-size IMG_SIZE [IMG_SIZE ...]] [--rect] [--resume [RESUME]] [--nosave] [--notest] [--noautoanchor] [--evolve] [--bucket BUCKET] [--cache-images] [--image-weights] [--device DEVICE] [--multi-scale] [--single-cls] [--adam] [--sync-bn] [--local_rank LOCAL_RANK] [--workers WORKERS] [--project PROJECT] [--entity ENTITY] [--name NAME] [--exist-ok] [--quad] [--linear-lr] [--label-smoothing LABEL_SMOOTHING] [--upload_dataset] [--bbox_interval BBOX_INTERVAL] [--save_period SAVE_PERIOD] [--artifact_alias ARTIFACT_ALIAS] [--freeze FREEZE [FREEZE ...]] [--v5-metric] ``` 訓練好的模型會存放在`.\runs\train\exp\weights`中,當中`best.pt`與各epoch之準確率有提高的模型都會被記錄下來。 訓練結果會在這: ![](https://hackmd.io/_uploads/HJe7po_22.png) ## 4. 模型測試(inference) 訓練完成後,欲使用所訓練好的模型來進行預測,可透過以下指令: ``` python detect.py --weights 權重路徑 --source 檔案路徑 ``` 其中,以下兩個參數分別是放訓練好的模型路徑與欲測試的檔案路徑。若想要直接使用webcam進行即時預測,則`--source 0`即可。 測試後的結果會在這: ![](https://hackmd.io/_uploads/BJMZpi_2n.png) ## 其他 * mAP@0.5與mAP@0.5:0.95的含義 https://blog.csdn.net/ruyingcai666666/article/details/109670567 * 實作結果與應用 [YOLOv7:手勢自動標記工具](https://hackmd.io/wXiPLsQJSlWFnR_zFKvmUg) ## Reference [YOLOv7 on Windows (二) 訓練自己的資料集](https://www.larrysprognotes.com/YOLOv7_2/) [[Object Detection_YOLO] YOLOv7 論文筆記](https://hackmd.io/@YungHuiHsu/BJ7fpQyps?utm_source=preview-mode&utm_medium=rec) [手勢自動標記及分類工具](https://chtseng.wordpress.com/2019/09/20/%E6%89%8B%E5%8B%A2%E8%87%AA%E5%8B%95%E6%A8%99%E8%A8%98%E5%8F%8A%E5%88%86%E9%A1%9E%E5%B7%A5%E5%85%B7/) ## To-read https://ai.iias.sinica.edu.tw/yolov4yolov7-developing-process-minutes/