# Yolov4 Setting and How to train ###### tags: `YOLOv4` :::info :pushpin: 參考網址 ➜ [PyTorch Yolov4配置](https://medium.com/@whchen0630/pytorch-yolov4%E9%85%8D%E7%BD%AE-8947eb17b547) :pushpin: Pytorch_yolov4 ➜ [WongKinYiu/PyTorch_YOLOv4](https://github.com/WongKinYiu/PyTorch_YOLOv4) :pushpin: v4流程教學 ➜ [TSR_交通號誌辨識系統 教育訓練](https://hackmd.io/@Hsiang1118/ry5b--1q5) ::: ### 創建虛擬環境(使用Anaconda) ```yaml= conda create --name xxx python="your version" ``` * **進入虛擬環境** ```javascript= conda activate xxx ``` ### Yolov4 環境配置 * **requirements.txt 設定** ```javascript= numpy == 1.21.5 opencv-python >= 4.1 torch == 1.12.1 torchvision == 0.13.1 matplotlib pycocotools tqdm pillow tensorboard >= 1.14 ``` > 其餘直接手動下載 ex : (pyyaml) * 安裝requirements.txt 指令 ```yaml= pip install -r requirements.txt ``` * Ubuntu環境之pycocotools請參照以下指令 ```javascript= sudo apt-get installl Cython pip install cython git clone https://github.com/cocodataset/cocoapi cd PythonAPI ``` * 若使用過程中有套件遺失,在使用pip install指令下載 ### Yolov4 Training Steps 1. git clone https://github.com/WongKinYiu/PyTorch_YOLOv4.git 2. Install requirements.txt **pip install -r PyTorch_YOLOv4/requirements.txt** 3. Edit yolov4.cfg (Change [yolo] classes numbers && [convolutional] Filters numbers) * Filters numbers = $(classes+5)*3$ 4. Create xxx.yaml in /data and input your path and nc(number of classes) ```javascript= # 設定圖檔路徑 train: images/train # 訓練用資料集(相對於 path) val: images/train # 驗證用資料集(相對於 path) test: # 測試用資料集(相對於 path,可省略) # 物件類別設定 nc: 1 # 類別數量 names: ['egg'] # 類別名稱 ``` * 資料路徑擺放位置 ```javascript= yolov4 ├── data │ └── ├── │ ├── train.txt/val.txt/test.txt │ ├── xxx.names(object names) │ ├── xxx.yaml(img's txt path) ├── img file(include jpg and label txt) ├── hyp.scratch.yaml │ └── img.cache3 └── weights └── xxx.weights │ └── cfg └── xxx.cfg │ └── xxx.yml(Need to be same with your conda env) │ └──train.py/test.py/detect.py │ └── requirements.txt ``` 5. creat xxx.names in /data and put your classes name in it 6. Run python code to cahnge JPG to txt file (it will create train.txt/test.txt) 7. **Train your data** : python train.py --batch-size 8 --epochs 100 --img 640 640 --data data/xxx.yaml --cfg cfg/yolov4.cfg --weights weights/yolov4.weights 8. **Test your model** : python test.py --img 640 --conf 0.001 --batch-size 8 --device 0 --data data/bsw.yaml --cfg cfg/bsw.cfg --weights weights/last.pt --names data/bsw.names --name bsw 9. **Detect your source** : python detect.py --weight ../weights/best.pt --source data/xxx.avi --cfg cfg/xxx.cfg --names data/xxx.names :::info :pushpin: PS ➜ 以上weights / cfg / data字型自行調整 ::: ### Error Solved * OSError: [WinError 1455] 分頁檔太小,無法完成操作。 Problem : batch size is too big Solved : reduce batch size * RuntimeError: result type Float can‘t be cast to the desired output type Long Solved : 於loss.py裡將原本的code更改為以下的code ```yaml= anchors, shape = model.module.module_list[jj].anchor_vec if multi_gpu else model.module_list[jj].anchor_vec, p[i].shape ``` ```yaml= indices.append((b, a, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up