项目地址
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
