1. 程式人生 > >Scaled-YOLOv4 快速開始,訓練自定義資料集

Scaled-YOLOv4 快速開始,訓練自定義資料集

- 程式碼: https://github.com/ikuokuo/start-scaled-yolov4 ## Scaled-YOLOv4 - 程式碼: https://github.com/WongKinYiu/ScaledYOLOv4 - 論文: https://arxiv.org/abs/2011.08036 - 文章: https://alexeyab84.medium.com/scaled-yolo-v4-is-the-best-neural-network-for-object-detection-on-ms-coco-dataset-39dfa22fa982 ## 環境準備 ### 基礎環境 - Nvidia 顯示卡的主機 - Ubuntu 18.04 - 系統安裝,可見 [製作 USB 啟動盤,及系統安裝](https://mp.weixin.qq.com/s/HHXh0GKaxfaUdemcL2IdRQ) - Nvidia Driver - 驅動安裝,可見 [Ubuntu 初始配置 - Nvidia 驅動](https://mp.weixin.qq.com/s/czgD2CQOnOZ_eslrRkq6LA) ### 開發環境 [anaconda]: https://www.anaconda.com/products/individual#Downloads 下載並安裝 [Anaconda][] ,之後於 Terminal 執行: ```bash # 建立 Python 虛擬環境 conda create -n scaled-yolov4 python=3.8 -y conda activate scaled-yolov4 # 安裝 PyTorch with CUDA conda install pytorch==1.7.1 torchvision==0.8.2 cudatoolkit=10.2 -c pytorch -y ``` 注意: - `pytorch` 等版本請對照表 [torch, torchvision, python](https://github.com/pytorch/vision#installation) - `cudatoolkit` 版本請對照表 [CUDA Toolkit and Compatible Driver Versions](https://docs.nvidia.com/deploy/cuda-compatibility/index.html#binary-compatibility__table-toolkit-driver) - `conda` 下載過慢,可嘗試配置國內映象源 [cuda toolkit]: https://developer.nvidia.com/cuda-toolkit-archive 下載 [CUDA Toolkit][] ,其版本也注意對應 Nvidia 驅動版本。下一步需要。命令參考: ```bash wget https://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run sudo sh cuda_10.2.89_440.33.01_linux.run ``` 注意:安裝時,請手動取消驅動安裝選項。 [mish-cuda]: https://github.com/JunnYu/mish-cuda 下載 [mish-cuda][] 並安裝: ```bash # install mish-cuda, if you use different pytorch version, you could try https://github.com/thomasbrandon/mish-cuda git clone https://github.com/JunnYu/mish-cuda cd mish-cuda python setup.py build install ``` 下載 [ScaledYOLOv4-large](https://github.com/WongKinYiu/ScaledYOLOv4/tree/yolov4-large): ```bash git clone -b yolov4-large https://github.com/WongKinYiu/ScaledYOLOv4 ``` ### 指令碼依賴 ```bash conda activate scaled-yolov4 cd start-scaled-yolov4/ pip install -r scripts/requirements.txt ``` ## 模型準備 [yolov4-p5.pt]: https://drive.google.com/file/d/1aXZZE999sHMP1gev60XhNChtHPRMH3Fz/view?usp=sharing [yolov4-p6.pt]: https://drive.google.com/file/d/1aB7May8oPYzBqbgwYSZHuATPXyxh9xnf/view?usp=sharing [yolov4-p7.pt]: https://drive.google.com/file/d/18fGlzgEJTkUEiBG4hW00pyedJKNnYLP3/view?usp=sharing 下載官方的 [yolov4-p5.pt][], [yolov4-p6.pt][], [yolov4-p7.pt][] 權重檔案到 `ScaledYOLOv4/weights/` 目錄。 ## 現有模型測試 ### 準備 COCO 資料集 下載 [COCO](http://cocodataset.org/) 資料集, ```bash coco2017 ├── annotations │   ├── instances_train2017.json │   └── instances_val2017.json ├── test2017 ├── train2017 └── val2017 ``` 轉成 [YOLOv5 資料集結構](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data#3-organize-directories), ```bash export COCO_DIR=~/datasets/coco2017 export OUTPUT_DIR=~/datasets/coco2017_yolov5 # train2017 訓練集 # - 圖片:目錄軟鏈到 images/ # - 標註:轉換儲存進 labels/*.txt # - 物體型別:全部記錄進 *.names # - 圖片列表:有物體標註的記錄進 *.txt, 無的進 *.txt.ignored python scripts/coco2yolov5.py \ --coco_img_dir $COCO_DIR/train2017/ \ --coco_ann_file $COCO_DIR/annotations/instances_train2017.json \ --output_dir $OUTPUT_DIR # val2017 驗證集 # - 物體型別:依照訓練集的記錄,保證順序 python scripts/coco2yolov5.py \ --coco_img_dir $COCO_DIR/val2017/ \ --coco_ann_file $COCO_DIR/annotations/instances_val2017.json \ --output_dir $OUTPUT_DIR \ --obj_names_file $OUTPUT_DIR/train2017.names ``` 如下: ```bash coco2017_yolov5/ ├── images │   ├── train2017 -> /home/john/datasets/coco2017/train2017 │   └── val2017 -> /home/john/datasets/coco2017/val2017 ├── labels │   ├── train2017 │   └── val2017 ├── train2017.names ├── train2017.txt ├── train2017.txt.ignored ├── val2017.txt └── val2017.txt.ignored ``` `coco2017_yolov5` 軟鏈到 `ScaledYOLOv4/` 目錄,並新增 `ScaledYOLOv4/data/coco2017_yolov5.yaml` 檔案,描述資料集: ```yaml # train and val datasets (image directory or *.txt file with image paths) train: ./coco2017_yolov5/images/train2017 val: ./coco2017_yolov5/images/val2017 test: ./coco2017_yolov5/images/val2017 # number of classes nc: 80 # class names names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] ``` ### 測試 YOLOv4-P5 ```bash cd ScaledYOLOv4 conda activate scaled-yolov4 pip install opencv-python pyyaml scipy tqdm python test.py \ --img 896 \ --conf 0.001 \ --batch 8 \ --device 0 \ --data data/coco2017_yolov5.yaml \ --weights weights/yolov4-p5.pt ``` 結果如下: ```bash Fusing layers... Model Summary: 331 layers, 7.07943e+07 parameters, 6.81919e+07 gradients Scanning labels coco2017_yolov5/labels/val2017.cache (4952 found, 0 missing, 48 empty, 0 duplicate, for 5000 images): 100%|█| 5000/5000 [00:00<00: Class Images Targets P R [email protected] [email protected]:.95: 100%|█████████████| 625/625 [01:34<00:00, 6.64it/s] all 5e+03 3.68e+04 0.456 0.76 0.687 0.494 Speed: 14.3/1.5/15.8 ms inference/NMS/total per 896x896 image at batch-size 8 ``` 進行推斷, ```bash python detect.py \ --img 896 \ --conf 0.5 \ --device 0 \ --weights weights/yolov4-p5.pt \ --source demo.jpg ``` 結果如下, ```bash Fusing layers... Model Summary: 331 layers, 7.07943e+07 parameters, 6.81919e+07 gradients image 1/1 /home/john/Codes/ScaledYOLOv4/demo.jpg: 768x896 1 cats, 1 dogs, Done. (0.029s) Results saved to inference/output Done. (0.133s) ``` ![](https://gitee.com/ikuokuo/pic/raw/master/pic/20210108161834.jpg) ### 測試 YOLOv4-P7 ```bash python test.py \ --img 1536 \ --conf 0.001 \ --batch 6 \ --device 0 \ --data data/coco2017_yolov5.yaml \ --weights weights/yolov4-p7.pt ``` 結果如下: ```bash Fusing layers... Model Summary: 503 layers, 2.87475e+08 parameters, 2.7862e+08 gradients Scanning labels coco2017_yolov5/labels/val2017.cache (4952 found, 0 missing, 48 empty, 0 duplicate, for 5000 images): 100%|█| 5000/5000 [00:00<00: Class Images Targets P R [email protected] [email protected]:.95: 100%|█████████████| 834/834 [06:57<00:00, 2.00it/s] all 5e+03 3.68e+04 0.435 0.804 0.719 0.531 Speed: 78.2/1.6/79.8 ms inference/NMS/total per 1536x1536 image at batch-size 6 ``` 進行推斷, ```bash python detect.py \ --img 1536 \ --conf 0.5 \ --device 0 \ --weights weights/yolov4-p7.pt \ --source demo.jpg ``` 結果如下, ```bash Fusing layers... Model Summary: 503 layers, 2.87475e+08 parameters, 2.7862e+08 gradients image 1/1 /home/john/Codes/ScaledYOLOv4/demo.jpg: 1152x1536 1 cats, 1 dogs, 1 chairs, 1 couchs, 1 potted plants, Done. (0.079s) Results saved to inference/output Done. (0.282s) ``` ![](https://gitee.com/ikuokuo/pic/raw/master/pic/20210108161903.jpg) ## 自定義資料集訓練 ### 準備資料集 這裡從 COCO 資料集拿出一個子集,作為自定義資料集的演示: ```bash