darknet-训练自己的yolov3模型
目錄
- Yolo v3的使用方法
- 安裝darknet
- 訓練Pascal VOC格式的數(shù)據(jù)
- 修改cfg文件中的voc.data
- 修改VOC.names
- 下載預訓練卷積層權重
- 修改cfg/yolov3-voc.cfg
- 訓練自己的模型
- 測試Yolo模型
- 測試單張圖片:
- 生成預測結果
- 采用第三方compute_mAP
- Reference
Yolo v3的使用方法
參考自@zhaonan
安裝darknet
- 下載庫文件
- 修改Makefile
- 編譯
- 下載預訓練模型
- 用預訓練模型進行簡單的測試
訓練Pascal VOC格式的數(shù)據(jù)
- 生成Labels,因為darknet不需要xml文件,需要.txt文件(格式: )
用voc_label.py(位于./scripts)cat voc_label.py 共修改四處
import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')] #替換為自己的數(shù)據(jù)集 classes = ["head", "eye", "nose"] #修改為自己的類別def convert(size, box):dw = 1./(size[0])dh = 1./(size[1])x = (box[0] + box[1])/2.0 - 1y = (box[2] + box[3])/2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (x,y,w,h) def convert_annotation(year, image_id):in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id)) #將數(shù)據(jù)集放于當前目錄下out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, image_id), 'w')tree=ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult)==1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))bb = convert((w,h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd() for year, image_set in sets:if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):os.makedirs('VOCdevkit/VOC%s/labels/'%(year))image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()list_file = open('%s_%s.txt'%(year, image_set), 'w')for image_id in image_ids:list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'%(wd, year, image_id))convert_annotation(year, image_id)list_file.close() os.system("cat 2007_train.txt 2007_val.txt > train.txt") #修改為自己的數(shù)據(jù)集用作訓練 wget https://pjreddie.com/media/files/voc_label.py python voc_label.py在VOCdevkit/VOC2007/labels/中:
learner@learner-pc:~/darknet/scripts$ ls 2007_test.txt #0 dice_label.sh imagenet_label.sh VOCdevkit_original 2007_train.txt #1 gen_tactic.sh train.txt #3 voc_label.py 2007_val.txt #2 get_coco_dataset.sh VOCdevkit這時darknet需要一個txt文件,其中包含了所有的圖片
cat 2007_train.txt 2007_val.txt 2012_*.txt > train.txt修改cfg文件中的voc.data
classes= 3 #修改為自己的類別數(shù) train = /home/learner/darknet/data/voc/train.txt #修改為自己的路徑 or /home/learner/darknet/scripts/2007_test.txt valid = /home/learner/darknet/data/voc/2007_test.txt #修改為自己的路徑 or /home/learner/darknet/scripts/2007_test.txt names = /home/learner/darknet/data/voc.names #修改見voc.names backup = /home/learner/darknet/backup #修改為自己的路徑,輸出的權重信息將存儲其內修改VOC.names
head #自己需要探測的類別,一行一個 eye nose下載預訓練卷積層權重
wget https://pjreddie.com/media/files/darknet53.conv.74修改cfg/yolov3-voc.cfg
[net] # Testingbatch=64subdivisions=32 #每批訓練的個數(shù)=batch/subvisions,根據(jù)自己GPU顯存進行修改,顯存不夠改大一些 # Training # batch=64 # subdivisions=16 width=416 height=416 channels=3 momentum=0.9 decay=0.0005 angle=0 saturation = 1.5 exposure = 1.5 hue=.1learning_rate=0.001 burn_in=1000 max_batches = 50200 #訓練步數(shù) policy=steps steps=40000,45000 #開始衰減的步數(shù) scales=.1,.1[convolutional] batch_normalize=1 filters=32 size=3 stride=1 pad=1 activation=leaky.....[convolutional] size=1 stride=1 pad=1 filters=24 #filters = 3 * ( classes + 5 ) here,filters=3*(3+5) activation=linear[yolo] mask = 6,7,8 anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 classes=3 #修改為自己的類別數(shù) num=9 jitter=.3 ignore_thresh = .5 truth_thresh = 1 random=1[route] layers = -4[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers = -1, 61[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] size=1 stride=1 pad=1 filters=24 #filters = 3 * ( classes + 5 ) here,filters=3*(3+5) activation=linear[yolo] mask = 3,4,5 anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 classes=3 #修改為自己的類別數(shù) num=9 jitter=.3 ignore_thresh = .5 truth_thresh = 1 random=1[route] layers = -4[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers = -1, 36[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] size=1 stride=1 pad=1 filters=24 #filters = 3 * ( classes + 5 ) here,filters=3*(3+5) activation=linear[yolo] mask = 0,1,2 anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 classes=3 #修改為自己的類別數(shù) num=9 jitter=.3 ignore_thresh = .5 truth_thresh = 1 random=1訓練自己的模型
1 單GPU訓練:./darknet -i <gpu_id> detector train <data_cfg> <train_cfg> <weights>
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.742 多GPU訓練,格式為0,1,2,3:./darknet detector train <data_cfg> <model_cfg> <weights> -gpus <gpu_list>
./darknet detector train cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74 -gpus 0,1,2,3測試Yolo模型
測試單張圖片:
- 測試單張圖片,需要編譯時有OpenCV支持:./darknet detector test <data_cfg> <test_cfg> <weights> <image_file> #本次測試無opencv支持
- <test_cfg>文件中batch和subdivisions兩項必須為1。
- 測試時還可以用-thresh和-hier選項指定對應參數(shù)。
-
./darknet detector test cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_20000.weights Eminem.jpg
批量測試圖片
-
yolov3-voc.cfg(cfg文件夾下)文件中batch和subdivisions兩項必須為1。
-
在detector.c中增加頭文件:
#include <unistd.h> /* Many POSIX functions (but not all, by a large margin) */ #include <fcntl.h> /* open(), creat() - and fcntl() */
-
-
在前面添加GetFilename(char p)函數(shù)
#include "darknet.h" #include <sys/stat.h> //需增加的頭文件 #include<stdio.h> #include<time.h> #include<sys/types.h> //需增加的頭文件 static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};char *GetFilename(char *p) { static char name[30]={""};char *q = strrchr(p,'/') + 1;strncpy(name,q,20);return name; } -
用下面代碼替換detector.c文件(example文件夾下)的void test_detector函數(shù)(注意有3處要改成自己的路徑)
- 重新進行編譯
- 開始批量測試
- 輸入Image Path(所有的測試文件的路徑,可以復制voc.data中valid后邊的路徑):
- 結果都保存在./data/out文件夾下
生成預測結果
生成預測結果
- ./darknet detector valid <data_cfg> <test_cfg> <weights> <out_file>
- yolov3-voc.cfg(cfg文件夾下)文件中batch和subdivisions兩項必須為1。
- 結果生成在<data_cfg>的results指定的目錄下以<out_file>開頭的若干文件中,若<data_cfg>沒有指定results,那么默認為<darknet_root>/results。
- 執(zhí)行語句如下:在終端只返回用時,在./results/comp4_det_test_[類名].txt里保存測試結果
采用第三方compute_mAP
下載第三方庫:
git clone https://github.com/LianjiLi/yolo-compute-map.git進行如下修改:
-
修改darknet/examples/detector.c中validate_detector()
char *valid_images = option_find_str(options, "valid", "./data/2007_test.txt");//改成自己的測試文件路徑if(!outfile) outfile = "comp4_det_test_";fps = calloc(classes, sizeof(FILE *));for(j = 0; j < classes; ++j){snprintf(buff, 1024, "%s/%s.txt", prefix, names[j]);//刪除outfile參數(shù)以及對應的%sfps[j] = fopen(buff, "w"); -
重新編譯
make clean make -
運行valid
darknet文件夾下運行./darknet detector valid cfg/voc.data cfg/yolov3-tiny.cfg backup/yolov3-tiny_164000.weights(改為自己的模型路徑) -
在本文件夾下運行python compute_mAP.py
-
說明:compute_mAP.py中的test.txt文件內容只有文件名字,不帶絕對路徑,不帶后綴
Reference
YOLOv3目標檢測總結
官方網(wǎng)站
思路整理自@zhaonan
代碼改變世界
分類: 深度學習 專欄
標簽: 經(jīng)驗, yolov3, 配置, map, test
好文要頂 關注我 收藏該文
pprp
關注 - 69
粉絲 - 21
+加關注
1
0
? 上一篇:CycleGAN 配置及其實現(xiàn)
? 下一篇:(轉載)YOLO配置文件理解
posted @ 2018-08-23 18:22 pprp 閱讀(7246) 評論(0) 編輯 收藏
總結
以上是生活随笔為你收集整理的darknet-训练自己的yolov3模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像分割 | 训练集输入labe(mas
- 下一篇: Check failed: error