Python影像辨識筆記(二十一):ultralytics/yolov5使用教學及注意事項

Yanwei Liu
9 min readOct 22, 2020
2020/10/22:
找到支援多GPU的訓練的指令
python -m torch.distributed.launch --nproc_per_node 2 train.py --batch-size 64 --data coco.yaml --cfg yolov5l.yaml --weights ''
--nproc_per_node specifies how many GPUs you would like to use. In the example above, it is 2.--batch-size is now the Total batch-size. It will be divided evenly to each GPU. In the example above, it is 64/2=32 per GPU.2020/12/30:
YOLOv5的yaml解析教學影片
2021/02/03:
基于TensorRT量化部署YOLOV5s 4.0模型
2021/04/11:
針對yaml檔案進行backbone修改
Yolov4 with Efficientnet b0-b7 Backbone
Yolov4 with MobileNet V2/V3 Backbone
Yolov4 with Resnext50/ SE-Resnet50 Backbones
Yolov4 with Resnet Backbone
Yolov4 with VGG Backbone
2021/04/12:
最近在進行一個學校作業的PCB瑕疵檢測專案,發現:
1. YOLOv5s在只有1000張左右的資料訓練下,效果優於YOLOv5x
2. 在執行test.py的時候,要注意,一般我們在Training時所寫的data.yaml長這樣:
train: ./2007_train.txt # 880 images
val: ./2007_val.txt # 220 images
test: ./2007_test.txt # 100 images
但是實際上要用test set來進行testing要寫成這樣,才能正常運作:
train: ./2007_train.txt # 880 images
val: ./2007_test.txt # 100 images
test: ./2007_test.txt # 100 images
3. 如果想要自定義mAP的IoU區間(例如mAP@0.5:0.95改成只有mAP@0.1)
把test.py中的77行
只需要把test.py當中的
iouv = torch.linspace(0.5, 0.95, 10).to(device) # mAP@0.5:0.95,10代表切成10等分
改成:
iouv = torch.linspace(0.1, 0.1, 1).to(device) # mAP@0.1,1代表只有1等分
4.提升訓練時的--img參數可以提升mAP,例如原圖是640,可以用--img 1280或--img 1920來提升mAP,但是batch size要跟著下降,否則會出現GPU out of memory的錯誤,但並不是越高越好,需依照實際訓練結果判斷。5.想進一步提升AP表現,可以參考
Advanced YoloV5 tutorial — Enhancing YoloV5 with Weighted Boxes FusionFusing EfficientNet & YoloV5 — Advanced Object Detection 2 stage pipeline tutorialKaggle上的notebooks
6. YOLOv5可以使用ensemble testing提升測試結果7. YOLOv5 5.0版本,新增了AP更強的Model,YOLOv5s6,YOLOv5m6,YOLOv5l6,
YOLOv5x6。在yolov5/models/hub底下可看到yaml檔案,甚至還有融合Transformer的架構
8. 如何取得detect.py後的bounding box結果?(使用--save-txt)
python detect.py --source data/images/val --weights best.pt --save-txt
得到的格式會是:
class Xcenter Ycenter W H
5 0.153906 0.884375 0.0515625 0.05
2 0.383594 0.324219 0.0546875 0.0453125
1 0.335156 0.410937 0.0546875 0.1
5 0.53125 0.332031 0.075 0.0359375
2 0.435937 0.0578125 0.06875 0.04375
4 0.51875 0.404687 0.05625 0.04375
2 0.621094 0.36875 0.0546875 0.0625
0 0.169531 0.722656 0.0609375 0.0484375
如果想要保存成class Xmin Ymin Xmax Ymax格式,需要到detect.py的104行~109行進行修改,改成以下:
也就是說,原本的程式透過xyxy2xywh得到class Xcenter Ycenter W H的格式,而如果不需要轉換的話,直接寫入xyxy即可,不須再經過xyxy2xywh
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
line = (cls, *xyxy, conf) if opt.save_conf else (cls, *xyxy)
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
5 82 550 115 582
2 228 193 263 222
1 197 231 232 295
5 316 201 364 224
2 257 23 301 51
4 314 245 350 273
2 380 216 415 256
0 89 447 128 478

正文:

ultralytics/yolov5是由國外一間公司用PyTorch實現的YOLOv5

特點:

  1. 能自動從Google Drive下載缺少的Weight
  2. 能在不需要OpenCV(C++)的環境下進行資料增強及Model訓練
  3. 可以直接進行mAP測試,不須上傳至CodaLab,不過測試時間大概需要25分鐘(以官方weight為例)

除此之外,更多特點可以參考官方GitHub REPO:

注意事項:

ultralytics的COCO路必須放在yolov5資料夾的平行目錄。

例如:

工作目錄:/work/yanwei.liu/yolov5
COCO目錄:/work/yanwei.liu/coco

與 ultralytics/yolov3的一些差異:

model的參數設定不再使用cfg檔案,而是改用yaml格式的檔案yolov5/data資料夾中的檔案本來是coco.data;改成了coco.yaml格式

使用方式:

請注意自己的python指令預設是python2還是python3版本,如果是python3版本可以放心使用下列指令;若不是,請使用python3指令

# 基本環境建置(get_coco2014.sh用來取得coco的資料,若不需要進行mAP測試可跳過)
git clone https://github.com/ultralytics/yolov5
bash yolov5/data/scripts/get_coco.sh
cd yolov5
pip install -U pycocotools
pip install -r requirements.txt
# 安裝對應版本的相關套件(以aiForge為例)
pip install torch==1.6.0+cu92 torchvision==0.7.0+cu92 -f
https://download.pytorch.org/whl/torch_stable.html
pip install tensorboard==1.15.0
pip install thop
# Training
# 第一次訓練的weight會被保存到runs/exp0,第二次會到runs/exp1以此類推
python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --cfg yolov5s.yaml --weights yolov5s.pt# Inference
python detect.py --weights yolov5s.pt --img 416 --conf 0.4 --source inference/images/
#除了可偵測圖片外,也能偵測影片、資料夾、webcam、即時串流、網頁上的影片python detect.py --source file.jpg # image file.mp4 # video dir/ # directory 0 # webcam 'rtsp://170.93.1.139/rtplive/XXX' # rtsp 'http://11.50.2.8/PLTV/888/2/30/1.m3u8'
# http
# mAP testing
python test.py --weights yolov5x.pt --data coco.yaml --img 672 --augment
# Visualize learning curve
from utils.general import plot_results; plot_results()

--

--