使用yolo3模型訓練自己的數據集
本項目地址:https://github.com/Cw-zero/Retrain-yolo3
一、運行環境
1. Ubuntu16.04、
2. TensorFlow-gpu 1.4.0 或更高版本、
3. Keras 2.2.4 、
4. numpy 1.15.2(實測1.16.1會報錯)。
二、創建數據集
1. 使用VOC2007數據集的文件結構:
文件結構如下圖,可以自己創建,也可以下載VOC2007數據集后刪除文件內容。
注:數據集中沒有 test.py,你需要將其拷貝到VOC2007文件夾下。
2. 標注圖片:
使用LabelImg(https://github.com/tzutalin/labelImg)對訓練圖片進行標注生成標簽,然后將所有圖片放在 JPEGImages 文件夾下,將所有標簽(.xml)放在 Annotations 文件夾下。
3. 划分數據集:
python test.py 90%為訓練集(train.txt), 9%為測試集(test.txt), 1%為評估數據集(val.txt)。txt文件保存在 ImageSets/Main 下。
4. 轉換標簽格式:
python voc_annotation.py, 在執行之前,需要根據數據集的實際類別,修改voc_annotation.py中的classes(與標注時的class名字一樣)。執行之后,將會得到train.txt、test.txt、val.txt,文本內容是:
img_path、box位置(top left bottom right)、類別索引(0 1 2 ...)
三、准備訓練
1.修改yolo3的網絡結構:打開yolo3.cfg,搜索"yolo",如下圖所示,需要修改filters、classes、random,一共可搜索到三處yolo,每處都需要修改。
filters = 3x(5+len(classes))
classes = len(classes)
random:默認是1,顯存小改為0
2. 確認相關依賴文件:
a. model_data/voc_classes.txt:與你的class一致,一般需要手動修改;
b. model_data/yolo_anchors.txt:確認存在即可;
c. model_data/yolo_weights.h5:由 yolov3.weights 轉換得到,需要先下載yolov3.weights,然后 python convert.py yolov3.cfg yolov3.weights model_data/yolo_weights.h5
d. 存在logs文件夾
3. 開始訓練:python train.py
參考:
https://github.com/qqwweee/keras-yolo3