項目地址
https://github.com/AlexeyAB/darknet
下載解壓包

默認工程

安裝基礎環境
安裝VS2015
安裝cuda(如果需要cuda加速)
- 安裝cuda10.1
- 安裝cudnn
安裝opencv(非必需)
- 使用world版本,官方工程默認依賴的是world版本。
第二種安裝環境
vs2019
cuda11.3
cudnn113
opencv451
編譯工程
方式一 直接編譯官方自帶的工程
但是我的opencv349編譯的不是opencv_world版本,所以一直提示缺少這個庫,我就是用自己的cmake了。
方式二 自己cmake編譯
1 自帶的工程需要opencv_world,我的opencv沒有這個,只能重新自己camke了。

報錯提示
如果遇到cuda找不到,就把工程拷貝到c盤,和vs2019還有cuda11.3放在同一 盤編譯
opencv也是報錯找不大,手動指定下位置

我什么也沒有改直接編譯
要么使用3按鈕打開工程或者手動目錄打開


選擇平台-我在這個平台下,且默認配置了opencv349和cuda

配置opencv

F:\dongdong\0tool\navidia_cuda_opencv\OPENCV\Opencv349\opencv-3.4.9\build\install\include\opencv2 F:\dongdong\0tool\navidia_cuda_opencv\OPENCV\Opencv349\opencv-3.4.9\build\install\include\opencv F:\dongdong\0tool\navidia_cuda_opencv\OPENCV\Opencv349\opencv-3.4.9\build\install\include

F:\dongdong\0tool\navidia_cuda_opencv\OPENCV\Opencv349\opencv-3.4.9\build\install\x64\vc14\lib

手動添加
F:\dongdong\0tool\navidia_cuda_opencv\OPENCV\Opencv349\opencv-3.4.9\build\install\x64\vc14\lib\*.lib
下面是cmake階段自動帶的,不用手動加,確認下。
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudnn.lib ..\..\3rdparty\pthreads\lib\pthreadVC2.lib wsock32.lib ws2_32.lib curand.lib cublas.lib cuda.lib cudadevrt.lib cudart_static.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib

(上述不需要自己設置)
修改gpu算力(必須)
每一個項目都改 6次修改


compute_75,compute_75 compute_75,sm75
可以去英偉達顯卡官網查詢計算能力:https://developer.nvidia.com/cuda-gpus#collapseOne

修改darknet.vcxproj
\darknet\build\darknet下,右鍵,可以txt打開 檢查下cuda版本是否為電腦的,cmake生成會自動修改。
ctrl+f搜索 10.1 全部改成 11.3 (因為我們的CUDA版本是11.3)大概10十幾處


全部生成工程

生成文件

這幾個並不足以運行,去代碼對應文件夾 拷貝文件

最終可執行要文件

如果是在c盤生成的,可以拷貝出來到別的盤執行
使用測試
1直接測試
https://blog.csdn.net/weixin_41010198/article/details/106072347
測試單張圖片
1下載yolov3 的預訓練權重
wget https://pjreddie.com/media/files/yolov3.weights
需要將訓練好的weights文件放到darknet和exe同目錄下,運行如下指令:
2運行demo測試
- 測試模型:yolov3
- cfg/yolov3.cfg:中定義的是yolov3的模型
- yolov3.weights:是預訓練的yolov3的權重
- data/dog.jpg:是要測試的圖片
darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
上面其他文件可在下載的YOLO項目中找到



detect命令是detector test命令的簡寫,`因此在測試單張圖片時,下面的兩個命令寫法是等價的:
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg # 或(上下等價) ./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg
darknet.exe detect cfg/yolov4.cfg weight/yolov4.weights data/dog.jpg
執行過程會輸出如下信息:
layer filters size input output
0 conv 32 3 x 3 / 1 608 x 608 x 3 -> 608 x 608 x 32 0.639 BFLOPs
1 conv 64 3 x 3 / 2 608 x 608 x 32 -> 304 x 304 x 64 3.407 BFLOPs
2 conv 32 1 x 1 / 1 304 x 304 x 64 -> 304 x 304 x 32 0.379 BFLOPs
3 conv 64 3 x 3 / 1 304 x 304 x 32 -> 304 x 304 x 64 3.407 BFLOPs
4 res 1 304 x 304 x 64 -> 304 x 304 x 64
5 conv 128 3 x 3 / 2 304 x 304 x 64 -> 152 x 152 x 128 3.407 BFLOPs
...
93 conv 255 1 x 1 / 1 38 x 38 x 512 -> 38 x 38 x 255 0.377 BFLOPs
94 yolo
95 route 91
96 conv 128 1 x 1 / 1 38 x 38 x 256 -> 38 x 38 x 128 0.095 BFLOPs
97 upsample 2x 38 x 38 x 128 -> 76 x 76 x 128
98 route 97 36
99 conv 128 1 x 1 / 1 76 x 76 x 384 -> 76 x 76 x 128 0.568 BFLOPs
100 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
101 conv 128 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BFLOPs
102 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
103 conv 128 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BFLOPs
104 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
105 conv 255 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BFLOPs
106 yolo
Loading weights from yolov3.weights...Done!
data/dog.jpg: Predicted in 0.033660 seconds.
dog: 100%
truck: 92%
bicycle: 99%

注意:./darknet其他測試的時候使用其他參數
-i:(索引index)如果有多個GPU,可以設置使用哪一個GPU,eg: -i 1 使用第一塊GPU-nogpu:使用CPU訓練,我測試了同樣上面的一張圖片,測試時間為17秒,而且置信度也發生了變化
連續測試多張圖片
連續測試多張圖片命令(不在命令中指定測試圖片路徑):
./darknet detect cfg/yolov3.cfg yolov3.weights
當配置和權重加載完成時,會看到如下輸出信息提示:
./darknet detect cfg/yolov3.cfg yolov3.weights
layer filters size input output
0 conv 32 3 x 3 / 1 416 x 416 x 3 -> 416 x 416 x 32 0.299 BFLOPs
1 conv 64 3 x 3 / 2 416 x 416 x 32 -> 208 x 208 x 64 1.595 BFLOPs
.......
104 conv 256 3 x 3 / 1 52 x 52 x 128 -> 52 x 52 x 256 1.595 BFLOPs
105 conv 255 1 x 1 / 1 52 x 52 x 256 -> 52 x 52 x 255 0.353 BFLOPs
106 detection
Loading weights from yolov3.weights...Done!
Enter Image Path:
輸入一張圖片路徑:data/horse.jpg 以使其預測該圖像的框,結果如下:

完成后,可以輸入更多次不同圖片的路徑進行測試,輸入Ctrl+C退出測試
更改檢測的閾值(thresh)
默認情況下,yolo僅顯示置信度為0.25或更高,可以通過-thresh <val>來設置閾值。例如:要顯示所有檢測,可以將閾值設置為0
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -thresh 0
使用Tiny YOLOv3預訓練模型測試
如果受自己機器計算資源的限制,可以使用更小的模型yolov3-tiny,其參數也更少,該預訓練模型下載:
wget https://pjreddie.com/media/files/yolov3-tiny.weights
用該模型做測試:
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
使用網絡攝像頭實時檢測
實時檢測,必須要有Nvidia GPU,因為在CPU上檢測一張圖片的時間是10秒左右,不能夠達到實時性的要求。因為實時檢測會用到CUDA 和 OpenCV,因此要在/darknet/Makefile被編譯之前,修改Makefie文件中的GPU=1 和OPENCV=1都設置為1。
1、實時網絡攝像頭檢測,運行如下命令:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights
如果你有多個攝像頭,可以使用-c <num>參數指定使用哪一個攝像頭
2、實時檢測視頻文件,運行如下命令:
./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights <video file>
2封裝成庫調用使用
https://www.cnblogs.com/kekeoutlook/p/13538874.html
