yolov4Linux,基于Darknet的YOLOv4目标检测
目錄
一、Windows環(huán)境下的YOLOv4目標檢測
1、環(huán)境配置
環(huán)境準備:Win10、CUDA10.1、cuDNN7.65、Visual Studio 2019、OpenCV 3.4
(1)Visual Studio2019企業(yè)版安裝
(3)下載并安裝CUDA10.1,下載安裝cuDNN7.65
對于cudnn直接將其解開壓縮包,然后需要將bin,include,lib中的文件復制粘貼到cuda的文件夾下,比如:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
(4)CUDA安裝測試
(5)OpenCV安裝
下載
2、克隆或下載YOLOv4
在GitHub上直接下載,并將解壓后的darknet-master文件夾改名為darknet
3、Visual Studio2019配置YOLOv4項目
(1)復制opencv文件
在文件夾C:\Program Files (x86)\opencv\build\x64\vc15\bin的兩個dll文件:opencv_ffffmpeg340_64.dll和opencv_world340.dll復制到D:\darknet\build\darknet\x64
(2)Visual Studio 2019項目配置
用visual studio2019打開darknet.sln文件
然后需要重啟電腦。
打開darknet.sln項目,對其進行配置:項目 ->屬性;注意應(yīng)選release和x64
(3)接下來幾個步驟比較關(guān)鍵
注意檢查Windows SDK版本
修改包含目錄和庫目錄
添加opencv3.4的包含目錄和庫目錄(按照自己的opencv3.4的路徑)包含目錄:
C:\Program Files (x86)\opencv\build\include
C:\Program Files (x86)\opencv\build\include\opencv
C:\Program Files (x86)\opencv\build\include\opencv2
庫目錄:C:\Program Files (x86)\opencv\build\x64\xc14\lib
附加依賴項
添加附加依賴項(按照自己的opencv3.4的路徑):C:\Program Files (x86)\opencv\build\x64\vc14\lib\opencv_world340.lib
4、Visual Studio2019編譯YOLOv4項目
點擊“重新生成解決方案”。可能有下述錯誤:嚴重性 代碼 說明 項目 文件 行 禁止顯示狀態(tài) 錯誤 MSB4019 找不到導入的項目“C:\Program Files(x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 10.0.props”。請確認 Import 聲明“C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 10.0.props”中的表達式正確,且文件位于磁盤上。 darknet D:\darknet\build\darknet\darknet.vcxproj
解決方法:將D:\darknet\build\darknet\darknet.vcxproj中版本號進行修改如下
仍然有錯誤時的解決方法:
將NVIDIA CUDA的安裝程序(.exe文件)數(shù)據(jù)解壓到一個指定文件夾中(最好是放在一個文件夾,解壓后會出現(xiàn)很多文件,這樣方便查找)
"D:\Temp\CUDA\VisualStudioIntegration\extras\visual_studio_integration\MSBuildExtensions"下的文件直接拷貝到“C:\Program Files (x86)\Microsoft VisualStudio\2019\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations”文件夾中。
再次點擊重新生決方案,這時成解應(yīng)該編譯成功,并產(chǎn)生可執(zhí)行文件D:\darknet\build\darknet\x64\darknet.exe
5、YOLOv4權(quán)重文件下載
yolov4.weight下載,拷貝權(quán)重文件到D:\darknet\build\darknet\x64
6、YOLOv4目標檢測測試
(1)測試圖片
在D:\darknet\build\darknet\x64\ 目錄下執(zhí)行:
darknet.exe detector test cfg\coco.data cfg\yolov4.cfg yolov4.weights data\dog.jpg
結(jié)果如下:
(2)測試視頻
在D:\darknet\build\darknet\x64\ 目錄下執(zhí)行:
darknet.exe detector demo cfg\coco.data cfg\yolov4.cfg yolov4.weights data\driving.mp4
7、使用YOLOv4訓練自己的數(shù)據(jù)集
(1)數(shù)據(jù)集準備
使用PASCAL VOC數(shù)據(jù)集的目錄結(jié)構(gòu)(建立文件夾層次為 D:\darknet\build\darknet\x64\VOCdevkit\ VOC2007):
JPEGImages放所有的訓練和測試圖片;Annotations放所有的xml標記文件
(2)訓練集和測試集生成
在D:\darknet\build\darknet\x64\目錄下執(zhí)行:python genfiles.py
在VOCdevkit \ VOC2007目錄下可以看到生成了文件夾labels ,同時在darknet下生成了兩個文件2007_train.txt和2007_test.txt。2007_train.txt和2007_test.txt分別給出了訓練圖片文件和測試圖片文件的列表,含有每個圖片的路徑和文件名。
另外,在VOCdevkit \ VOC2007\ImageSets\Main目錄下生產(chǎn)了兩個文件test.txt和train.txt,分別給出了訓練圖片文件和測試圖片文件的列表,但只含有每個圖片的文件名(不含路徑和擴展名)。labels下的文件是images文件夾下每一個圖像的yolo格式的標注文件,這是由annotations的xml標注文件轉(zhuǎn)換來的。
最終訓練只需要:2007_train.txt,2007_test.txt,labels下的標注文件和 VOCdevkit\VOC2007\JPEGImages下的圖像文件。
生成YOLO格式的txt標記文件內(nèi)容如下:
genfiles.py文件如下:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
classes=["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def clear_hidden_files(path):
dir_list = os.listdir(path)
for i in dir_list:
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith("._"):
os.remove(abspath)
else:
clear_hidden_files(abspath)
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def convert_annotation(image_id):
in_file = open('VOCdevkit\VOC2007\Annotations\%s.xml' %image_id)
out_file = open('VOCdevkit\VOC2007\labels\%s.txt' %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').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_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')
in_file.close()
out_file.close()
wd = os.getcwd()
wd = os.getcwd()
work_sapce_dir = os.path.join(wd, "VOCdevkit\\")
if not os.path.isdir(work_sapce_dir):
os.mkdir(work_sapce_dir)
work_sapce_dir = os.path.join(work_sapce_dir, "VOC2007\\")
if not os.path.isdir(work_sapce_dir):
os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations\\")
if not os.path.isdir(annotation_dir):
os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages\\")
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
clear_hidden_files(image_dir)
VOC_file_dir = os.path.join(work_sapce_dir, "ImageSets\\")
if not os.path.isdir(VOC_file_dir):
os.mkdir(VOC_file_dir)
VOC_file_dir = os.path.join(VOC_file_dir, "Main\\")
if not os.path.isdir(VOC_file_dir):
os.mkdir(VOC_file_dir)
train_file = open(os.path.join(wd, "2007_train.txt"), 'w')
test_file = open(os.path.join(wd, "2007_test.txt"), 'w')
train_file.close()
test_file.close()
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets\\Main\\train.txt"), 'w')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets\\Main\\test.txt"), 'w')
VOC_train_file.close()
VOC_test_file.close()
if not os.path.exists('VOCdevkit\\VOC2007\\labels'):
os.makedirs('VOCdevkit\\VOC2007\\labels')
train_file = open(os.path.join(wd, "2007_train.txt"), 'a')
test_file = open(os.path.join(wd, "2007_test.txt"), 'a')
VOC_train_file = open(os.path.join(work_sapce_dir, "ImageSets\\Main\\train.txt"), 'a')
VOC_test_file = open(os.path.join(work_sapce_dir, "ImageSets\\Main\\test.txt"), 'a')
list = os.listdir(image_dir) # list image files
probo = random.randint(1, 100)
print("Probobility: %d" % probo)
for i in range(0,len(list)):
path = os.path.join(image_dir,list[i])
if os.path.isfile(path):
image_path = image_dir + list[i]
voc_path = list[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
annotation_path = os.path.join(annotation_dir, annotation_name)
probo = random.randint(1, 100)
print("Probobility: %d" % probo)
if(probo < 75):
if os.path.exists(annotation_path):
train_file.write(image_path + '\n')
VOC_train_file.write(voc_nameWithoutExtention + '\n')
convert_annotation(nameWithoutExtention)
else:
if os.path.exists(annotation_path):
test_file.write(image_path + '\n')
VOC_test_file.write(voc_nameWithoutExtention + '\n')
convert_annotation(nameWithoutExtention)
train_file.close()
test_file.close()
VOC_train_file.close()
VOC_test_file.close()
(3)修改配置文件
新建data\voc.names文件,該文件內(nèi)容存放數(shù)據(jù)標簽名,可以復制data\voc.names再根據(jù)自己情況的修改(修改成自己的分類);可以重新命名等。
新建 data\voc.data文件,可以復制data\voc.data再根據(jù)自己情況的修改;可以重新命等。
classes = N ,N為自己的分類數(shù)量;train 訓練集完整路徑列表;valid 測試集完整路徑列表;names = data/voc.names? 類別文件;backup = backup/ 訓練結(jié)果保存在darknet/backup/目錄下。
新建cfg\yolov4-voc.cfg,可以復制cfg\yolov4-custom.cfg再根據(jù)自己情況的修改;可以重新命名等。
yolov4-voc.cfg文件參數(shù)說明:
batch:?每一次迭代送到網(wǎng)絡(luò)的圖片數(shù)量,也叫批數(shù)量。增大這個可以讓網(wǎng)絡(luò)在較少的迭代次數(shù)內(nèi)完成一個epoch。在固定最大迭代次數(shù)的前提下,增加batch會延長訓練時間,但會更好的尋找到梯度下降的方向。如果你顯存夠大,可以適當增大這個值來提高內(nèi)存利用率。這個值是需要大家不斷嘗試選取的,過小的話會讓訓練不夠收斂,過大會陷入局部最優(yōu)。
subdivision:這個參數(shù)它會讓你的每一個batch不是一下子都丟到網(wǎng)絡(luò)里。而是分成subdivision對應(yīng)數(shù)字的份數(shù),一份一份的跑完后,在一起打包算作完成一次iteration。這樣會降低對顯存的占用情況。如果設(shè)置這個參數(shù)為1的話就是一次性把所有batch的圖片都丟到網(wǎng)絡(luò)里,如果為2的話就是一次丟一半。(subdivisions=8 ,如果顯存溢出改為16,32或64)
angle:圖片旋轉(zhuǎn)角度,這個用來增強訓練效果的。從本質(zhì)上來說,就是通過旋轉(zhuǎn)圖片來變相的增加訓練樣本集。
saturation,exposure,hue:飽和度,曝光度,色調(diào),這些都是為了增強訓練效果用的。
learning_rate:學習率,訓練發(fā)散的話可以降低學習率。學習遇到瓶頸,loss不變的話也減低學習率。
max_batches:?最大迭代次數(shù)。
policy:學習策略,一般都是step這種步進式。
step,scales:這兩個是組合一起的,舉個例子:learn_rate: 0.001, step:100,25000,35000?? scales: 10, .1, .1 這組數(shù)據(jù)的意思就是在0-100次iteration期間learning rate為原始0.001,在100-25000次iteration期間learning rate為原始的10倍0.01,在25000-35000次iteration期間learning rate為當前值的0.1倍,就是0.001, 在35000到最大iteration期間使用learning rate為當前值的0.1倍,就是0.0001。隨著iteration增加,降低學習率可以是模型更有效的學習,也就是更好的降低train loss。
在cfg\yolov4-voc.cfg文件中,三個yolo層和各自前面的convolutional層的參數(shù)需要修改:filters=num╳(classes+5),5的意義是4個坐標加一個置信率num表示YOLO中每個cell預測的框的個數(shù),yolo層中的classes為類別,YOLOV3中為3。此 處 的 值 要 根 據(jù) 自 己 的 數(shù) 據(jù) 集 進 行 更 改 , 例 如 識 別 4 個 類 別 , 則 :filters=3 ╳(4+5)=27。
(4)使用YOLOv4進行訓練
下載權(quán)重文件:yolov4.conv.137,放置在D:\darknet\build\darknet\x64目錄下這里的訓練使用遷移學習,所以下載的yolov4在coco數(shù)據(jù)集上的預訓練權(quán)重文件(不含全連接層)
訓練網(wǎng)絡(luò)(如需要顯示訓練過程的map變化,在命令末尾加-map):
darknet.exe detector train data\voc.data cfg\yolov4.cfg yolov4.conv.137 -map
網(wǎng)絡(luò)訓練建議:
batch=16
subdivisions=8 (如果顯存溢出改為16,64)
把max_batches設(shè)置為 (classes*2000);但最小為4000。例如如果訓練3個目標類別,max_batches=6000
把steps改為max_batches的80% and 90%;例如steps=4800, 5400。
為增加網(wǎng)絡(luò)分辨率可增大height和width的值,但必須是32的倍數(shù) (height=608, width=608 or 32的整數(shù)倍) 。這有助于提高檢測精度。
(5)mAP性能統(tǒng)計
統(tǒng)計 mAP@IoU=0.50:
darknet.exe detector map data\voc.data cfg\yolov4-test.cfg backup\yolov4.weights
統(tǒng)計 mAP@IoU=0.75:
darknet.exe detector map data\voc.data cfg\yolov4-test.cfg backup\yolov4.weights -iou_thresh 0.75
8、Anchor Box先驗框聚類分析與修改
使用k-means聚類獲得自己數(shù)據(jù)集的先驗框大小:
darknet.exe detector calc_anchors data\voc.data -num_of_clusters 9 -width 608 -height 608
D:\darknet\build\darknet\x64目錄下生成anchors.txt文件,修改cfg文件中的先驗框大小,重新訓練和測試
二、Linux環(huán)境下的YOLOv4目標檢測
1、環(huán)境配置
環(huán)境準備:Ubuntu16.04、CUDA10.1、cuDNN 7.65、OpenCV 3.4
opencv安裝可以基于源碼,可以使用命令安裝:apt-get install libopencv-dev
2、YOLOv4下載和編譯
cd darknet
make #直接make則使用的是darknet原始配置(使用CPU)
如果使用GPU和OpenCV,則將Makefile文件中的對應(yīng)項改為1,然后再執(zhí)行make命令
最后,在終端輸入:./darknet
出現(xiàn)以下輸出則說明安裝成功:usage: ./darknet
3、測試圖片
測試單張圖片:
./darknet detect cfg/yolov4.cfg yolov4.weights data/dog.jpg
測試多張圖片(根據(jù)提示輸入圖片路徑):
./darknet detect cfg/yolov4.cfg yolov4.weights
總結(jié)
以上是生活随笔為你收集整理的yolov4Linux,基于Darknet的YOLOv4目标检测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 性激素检查多少钱啊?
- 下一篇: 10万元一年利息多少钱啊?