YOLOV4-Darknet环境搭建以及测试coco2017的mAP,FPS! |
文章目录
- YOLOv3网络结构图如下:
一. YOLOV4网络结构
- YOLOv4网络结构图如下:
Yolov4的结构图和Yolov3相比,因为多了CSP结构,PAN结构,如果单纯看可视化流程图,会觉得很绕,但是在绘制出上面的图形后,会觉得豁然开朗,其实整体架构和Yolov3是相同的,不过使用各种新的算法思想对各个子结构都进行了改进。
Yolov4的五个基本组件:
- 1. CBM:Yolov4网络结构中的最小组件,由Conv+Bn+Mish激活函数三者组成。
- 2. CBL:由Conv+Bn+Leaky_relu激活函数三者组成。
- 3. Res unit:借鉴Resnet网络中的残差结构,让网络可以构建的更深。
- 4. CSPX:借鉴CSPNet网络结构,由三个卷积层和X个Res unint模块Concate组成。
- 5. SPP:采用1×1,5×5,9×9,13×13的最大池化的方式,进行多尺度融合。
其他基础操作:
- 1. Concat:张量拼接,维度会扩充,和Yolov3中的解释一样,对应于cfg文件中的route操作。
- 2. Add:张量相加,不会扩充维度,对应于cfg文件中的shortcut操作。
二. YOLOV4环境搭建
2.1. darknet的配置
1. 本机的软硬件环境!
- Ubuntu18.04
- CUDA-version: 10010 (10020), cuDNN: 7.6.4,
- OpenCV version: 4.1.0
2. 安装CUDA和CUDNN!
3. 安装opencv!
############################# 1、安装依赖包
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
############################# 3、创建工作文件夹
mkdir build
cd build
############################# 3、cmake一下
make -j8
sudo make install
############################# 4、改变环境变量
sudo vim /etc/ld.so.conf.d/opencv.conf
############################# 5、创建的文件中加上/usr/local/lib,在运行以下命令
sudo ldconfig
############################# 6、测试
cd ~/下载/opencv/samples/cpp/example_cmake
cmake .
make
./opencv_example
############################# 7、如果没有摄像头将capture.open(0)改为capture.open(". ./. ./data/tree.avi")
4. 下载darknet!
git clone https://github.com/AlexeyAB/darknet
4.1. 配置makefile,下载完后,进行解压,然后进入darknet目录,找到makefile文件,打开:
sudo gedit Makefile
,然后修改以下位置:
# set GPU=1 and CUDNN=1 to speedup on GPU
# set CUDNN_HALF=1 to further speedup 3 x times (Mixed-precision on Tensor Cores) GPU: Volta, Xavier, Turing and higher
# set AVX=1 and OPENMP=1 to speedup on CPU (if error occurs then set AVX=0)
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
# NVCC的位置改成你自己的安装位置。
NVCC=/usr/local/cuda-10.0/bin/nvcc
4.2. make, 在darknet目录下进行make即可。
5. 测试使用,这里可以参考AlexeyAB大神的github
# 测试图片
./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output data/dog.jpg
# 测试视频
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output test.mp4
# 测试视频并保存结果视频
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test1.mp4 -show -ext_output -out_filename test_result1.mp4
YOLOv4 Demo!
2.2. 在COCO evaluation server验证AP
- 1. Download and unzip test-dev2017 dataset from MS COCO server: http://images.cocodataset.org/zips/test2017.zip
- 2. Download list of images for Detection taks and replace the paths with yours: https://raw.githubusercontent.com/AlexeyAB/darknet/master/scripts/testdev2017.txt
- 3. Download yolov4.weights file: https://drive.google.com/open?id=1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT
- 4. Content of the file cfg/coco.data should be
classes= 80
train = <replace with your path>/trainvalno5k.txt
valid = <replace with your path>/testdev2017.txt
names = data/coco.names
backup = backup
eval=coco
- 5. Create
/results/
folder near with ./darknet executable file- 6. Run validation:
./darknet detector valid cfg/coco.data cfg/yolov4.cfg yolov4.weights
- 7. Rename the file
/results/coco_results.json
todetections_test-dev2017_yolov4_results.json
and compress it todetections_test-dev2017_yolov4_results.zip
- 8. Submit file
detections_test-dev2017_yolov4_results.zip
to the MS COCO evaluation server for the test-dev2019 (bbox)(link: COCO Detection Challenge (Bounding Box))
- 最终在网站上下载测试结果
2.3. 在GPU上验证FPS
- 1. Compile Darknet with
GPU=1 CUDNN=1 CUDNN_HALF=1 OPENCV=1
in the Makefile (or use the same settings with Cmake)- 2. Download yolov4.weights file 245 MB:
yolov4.weights
- 3. Get any .
avi/.mp4
video file (preferably not more than 1920x1080 to avoid bottlenecks in CPU performance)- 4. Run one of two commands and look at the
AVG FPS
:- 5. include video_capturing + NMS + drawing_bboxes: (dont_show表示视频不显示,若想显示改为show)
# 不显示视频
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -dont_show -ext_output
# 显示视频
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -show
- 6. exclude video_capturing + NMS + drawing_bboxes:
./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -benchmark
三. darknet代码理解 bflops
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.10
nms_kind: greedynms (1), beta = 0.600000
151 route 147 -> 26 x 26 x 256
152 conv 512 3 x 3/ 2 26 x 26 x 256 -> 13 x 13 x 512 0.399 BF
153 route 152 116 -> 13 x 13 x1024
154 conv 512 1 x 1/ 1 13 x 13 x1024 -> 13 x 13 x 512 0.177 BF
155 conv 1024 3 x 3/ 1 13 x 13 x 512 -> 13 x 13 x1024 1.595 BF
156 conv 512 1 x 1/ 1 13 x 13 x1024 -> 13 x 13 x 512 0.177 BF
157 conv 1024 3 x 3/ 1 13 x 13 x 512 -> 13 x 13 x1024 1.595 BF
158 conv 512 1 x 1/ 1 13 x 13 x1024 -> 13 x 13 x 512 0.177 BF
159 conv 1024 3 x 3/ 1 13 x 13 x 512 -> 13 x 13 x1024 1.595 BF
160 conv 255 1 x 1/ 1 13 x 13 x1024 -> 13 x 13 x 255 0.088 BF
161 yolo
[yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.05
nms_kind: greedynms (1), beta = 0.600000
Total BFLOPS 60.137