一、前言
最近需要使用object detection,就把yolo4配置一下,这个检测效率和效果还是非常到位的。和其他各种Net对比参考图1,当然在这个时候,我也测试了Yolov5,后面会再出一个文档来讲如何配置Yolov5。对于Yolov4这会是一个系列的文章,主要分两个方向,基于ROS和非ROS的。图1 各种Net的检测效率和精度
至于为什么会有两个方向,由于我们主要研究机器人控制系统,所以配置Yolo主要用在机器人系统上,哪肯定要和ROS结合的,所以本文先描述如何配置,后面会陆陆续续出一些关于如何进行标注自己的数据集,然后进行迁移学习,移植到自己的项目上。
二、安装YoloV4
git clone https://github.com/AlexeyAB/darknet
2. 非GPU编译,并建立weights,testfiles文件夹,用于后面测试和放置权重文件
cd darknet
make
mkdir weights
mkdir testfiles
3.非GPU测试:
需要下载权重文件yolov4.weights ,这是作者训练好的,我们可以直接用来测试。
下载链接:Yolov4-Resource.zip-机器学习文档类资源-CSDN下载download.csdn.net
测试命令如下:
./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights
data/dog.jpg
效果如下花费大概25s左右:图2 CPU测试的结果
4.GPU测试,命令是一样的,但在这之前你需要安装Cuda,Cudacnn,Opencv等
安装参考步骤三.
测试命令:
./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights
data/dog.jpg
计算过程:
[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 128.459
avg_outputs = 1068395
Allocate additional workspace_size = 52.43 MB
Loading weights from ./weights/yolov4.weights...
seen 64, trained: 32032 K-images (500 Kilo-batches_64)
Done! Loaded 162 layers from weights-file
./data/dog.jpg: Predicted in 26.903000 milli-seconds.
bicycle: 92%
dog: 98%
truck: 92%
pottedplant: 33%
测试效果:图3 GPU使用效果
三、安装Cuda10.1,Cudacnn7.6.5,Nvidia driver,opencv 3.4.10和opencv_contrib
[1] Nvidia driver 安装删除之前驱动
sudo apt-get purge nvidia*
sudo apt --purge remove "cublas*" "cuda*"
2.添加源
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
3.安装
ubuntu-drivers devices
最后根据自己需求安装,这里我安装的是:
sudo apt-get install --no-install-recommends nvidia-driver-440
4.重启和检测
reboot
nvidia-smi
5.出现如下说明安装完毕。
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100 Driver Version: 440.100 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:01:00.0 Off | N/A |
| 0% 53C P5 16W / 275W | 1422MiB / 11178MiB | 3% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1379 G /usr/lib/xorg/Xorg 36MiB |
| 0 1469 G /usr/bin/gnome-shell 49MiB |
| 0 2500 G /usr/lib/xorg/Xorg 451MiB |
| 0 2626 G /usr/bin/gnome-shell 322MiB |
| 0 9746 G ...AAAAAAAAAAAACAAAAAAAAAA= --shared-files 181MiB |
| 0 10883 G ...uest-channel-token=15615578348179024402 374MiB |
+-----------------------------------------------------------------------------+
[2] Cuda10.1
1.根据自己的系统选择Cuda,这里我选择Cuda10.1(选择参考图3),如果下载慢可以直接这里下载CUDA Toolkit 10.1 original Archivedeveloper.nvidia.com
wget http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run图3 Cuda选择参考
2.安装,注意这里需要把NVIDIA驱动去掉,因为我们之前已经安装了这里不需要再安装
sudo sh cuda_10.1.243_418.87.00_linux.run
这里默认安装路径于:/usr/local/cuda-10.1
3.添加到bashrc让启动terminal可找到,也就是添加环境变量
sudo vim ~/.bashrc
在最后插入如下环境变量
#added by cuda10.1 installer
export CUDA_HOME=/usr/local/cuda-10.1
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
使用如下命令检查是否生效
source ~/.bashrc
nvcc -V
输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Fri_Feb__8_19:08:17_CDT_2019
Cuda compilation tools, release 10.1, V10.1.105
[3] 安装Cudacnn7.6.5
1.下载Cudacnn,这里你需要注册才能下载:Cudacnn下载地址developer.nvidia.com
这里选择Cuda10.1的7.6.5Cudacnn
Download cuDNN v8.0.1 RC2 (June 26th, 2020), for CUDA 11.0
Download cuDNN v8.0.1 RC2 (June 26th, 2020), for CUDA 10.2
Download cuDNN v7.6.5 (November 18th, 2019), for CUDA 10.2
Download cuDNN v7.6.5 (November 5th, 2019), for CUDA 10.1
Library for Windows, Mac, Linux, Ubuntu and RedHat/Centos(x86_64architecture)
cuDNN Runtime Library for Ubuntu18.04 (Deb)
2.拷贝到Cuda文件夹
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
3.最后检测是否成功安装和查询安装版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
[4] 编译安装Opencv3.4.10和opencv_contrib
1.下载opencv3.4.10和对应版本的opencv_contrib,这里一定要下载对应版本,不然很容易遇到错误。opencv/opencvgithub.comopencv/opencv_contribgithub.com
2.编译opencv 编译命令如下:
编译方式一:
第一步:进入对应路径
cd opencv-3.4.10
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.10/modules \ #注意这里选择自己对应路径
-D PYTHON_EXCUTABLE=/usr/bin/python \
-D WITH_CUDA=ON \ # 使用CUDA
-D WITH_CUBLAS=ON \
-D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" \
-D CUDA_ARCH_BIN="7.5" \ # 需要去官网查询自己显卡的算力
-D CUDA_ARCH_PTX="" \
-D CUDA_FAST_MATH=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D BUILD_EXAMPLES=ON
最后再:make
sudo make install
编译方式二:
直接使用如下命令进行:
cd opencv-3.4.10
mkdir build
cd build
ccmake .. 然后选择对应的模块
选择参考如下,然后按c键保存,按g键生成对应的makefile。如果是warnning 可以忽略。
ANT_EXECUTABLE ANT_EXECUTABLE-NOTFOUND
Atlas_BLAS_LIBRARY Atlas_BLAS_LIBRARY-NOTFOUND
Atlas_CBLAS_INCLUDE_DIR /usr/include/x86_64-linux-gnu
Atlas_CBLAS_LIBRARY Atlas_CBLAS_LIBRARY-NOTFOUND
Atlas_CLAPACK_INCLUDE_DIR Atlas_CLAPACK_INCLUDE_DIR-NOTFOUND
Atlas_LAPACK_LIBRARY /usr/lib/x86_64-linux-gnu/liblapack.so
BUILD_CUDA_STUBS OFF
BUILD_DOCS OFF
BUILD_EXAMPLES ON
BUILD_IPP_IW ON
BUILD_ITT ON
BUILD_JASPER ON
BUILD_JAVA ON
BUILD_JPEG ON
BUILD_LIST
BUILD_OPENEXR OFF
BUILD_PACKAGE ON
BUILD_PERF_TESTS ON
BUILD_PNG OFF
BUILD_PROTOBUF ON
BUILD_SHARED_LIBS ON
BUILD_TBB ON
BUILD_TESTS ON
BUILD_TIFF OFF
BUILD_USE_SYMLINKS OFF
BUILD_WEBP OFF
BUILD_WITH_DEBUG_INFO OFF
BUILD_WITH_DYNAMIC_IPP OFF
BUILD_ZLIB OFF
BUILD_opencv_apps ON
BUILD_opencv_aruco ON
BUILD_opencv_bgsegm ON
BUILD_opencv_bioinspired ON
BUILD_opencv_calib3d ON
BUILD_opencv_ccalib ON
BUILD_opencv_core ON
BUILD_opencv_cudaarithm ON
BUILD_opencv_cudabgsegm ON
BUILD_opencv_cudacodec ON
BUILD_opencv_cudafeatures2d ON
BUILD_opencv_cudafilters ON
BUILD_opencv_cudaimgproc ON
BUILD_opencv_cudalegacy ON
BUILD_opencv_cudaobjdetect ON
BUILD_opencv_cudaoptflow ON
BUILD_opencv_cudastereo ON
BUILD_opencv_cudawarping ON
BUILD_opencv_cudev ON
BUILD_opencv_datasets ON
BUILD_opencv_dnn ON
BUILD_opencv_dnn_objdetect ON
BUILD_opencv_dpm ON
BUILD_opencv_face ON
BUILD_opencv_features2d ON
BUILD_opencv_flann ON
BUILD_opencv_freetype ON
BUILD_opencv_fuzzy ON
BUILD_opencv_hdf ON
BUILD_opencv_hfs ON
BUILD_opencv_highgui ON
BUILD_opencv_img_hash ON
BUILD_opencv_imgcodecs ON
BUILD_opencv_imgproc ON
BUILD_opencv_java_bindings_gen ON
BUILD_opencv_js OFF
BUILD_opencv_line_descriptor ON
BUILD_opencv_ml ON
BUILD_opencv_objdetect ON
BUILD_opencv_optflow ON
BUILD_opencv_phase_unwrapping ON
BUILD_opencv_photo ON
BUILD_opencv_plot ON
BUILD_opencv_python2 ON
BUILD_opencv_python3 ON
BUILD_opencv_python_bindings_g ON
BUILD_opencv_python_tests ON
BUILD_opencv_reg ON
BUILD_opencv_rgbd ON
BUILD_opencv_saliency ON
BUILD_opencv_shape ON
BUILD_opencv_stereo ON
BUILD_opencv_stitching ON
BUILD_opencv_structured_light ON
BUILD_opencv_superres ON
BUILD_opencv_surface_matching ON
BUILD_opencv_text ON
BUILD_opencv_tracking ON
BUILD_opencv_ts ON
BUILD_opencv_video ON
BUILD_opencv_videoio ON
BUILD_opencv_videostab ON
BUILD_opencv_viz ON
BUILD_opencv_world OFF
BUILD_opencv_xfeatures2d ON
BUILD_opencv_ximgproc ON
BUILD_opencv_xobjdetect ON
BUILD_opencv_xphoto ON
CCACHE_PROGRAM CCACHE_PROGRAM-NOTFOUND
CLAMDBLAS_INCLUDE_DIR CLAMDBLAS_INCLUDE_DIR-NOTFOUND
CLAMDBLAS_ROOT_DIR CLAMDBLAS_ROOT_DIR-NOTFOUND
CLAMDFFT_INCLUDE_DIR CLAMDFFT_INCLUDE_DIR-NOTFOUND
CLAMDFFT_ROOT_DIR CLAMDFFT_ROOT_DIR-NOTFOUND
CMAKE_BUILD_TYPE
CMAKE_CONFIGURATION_TYPES Debug;Release
CMAKE_INSTALL_PREFIX /usr/local
CPU_BASELINE SSE3
CPU_DISPATCH SSE4_1;SSE4_2;AVX;FP16;AVX2;AVX512_SKX
CUDA_ARCH_BIN 3.0 3.5 3.7 5.0 5.2 6.0 6.1 7.0 7.5
CUDA_ARCH_PTX
CUDA_FAST_MATH ON
CUDA_GENERATION
CUDA_HOST_COMPILER /usr/bin/cc
CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda-10.0
CUDA_USE_STATIC_CUDA_RUNTIME ON
CUDA_rt_LIBRARY /usr/lib/x86_64-linux-gnu/librt.so
CV_DISABLE_OPTIMIZATION OFF
CV_ENABLE_INTRINSICS ON
CV_TRACE ON
Caffe_INCLUDE_DIR Caffe_INCLUDE_DIR-NOTFOUND
Caffe_LIBS Caffe_LIBS-NOTFOUND
Ceres_DIR Ceres_DIR-NOTFOUND
ENABLE_BUILD_HARDENING OFF
ENABLE_CCACHE ON
ENABLE_CONFIG_VERIFICATION OFF
ENABLE_COVERAGE OFF
ENABLE_CXX11 OFF
ENABLE_FAST_MATH ON
ENABLE_FLAKE8 OFF
ENABLE_GNU_STL_DEBUG OFF
ENABLE_IMPL_COLLECTION OFF
ENABLE_INSTRUMENTATION OFF
ENABLE_LTO OFF
ENABLE_NOISY_WARNINGS OFF
ENABLE_OMIT_FRAME_POINTER ON
ENABLE_PIC ON
ENABLE_PRECOMPILED_HEADERS OFF
ENABLE_PROFILING OFF
ENABLE_PYLINT OFF
ENABLE_SOLUTION_FOLDERS OFF
EXECUTABLE_OUTPUT_PATH /software/opencv-3.4.10/build/bin
Eigen3_DIR /usr/local/share/eigen3/cmake
GENERATE_ABI_DESCRIPTOR OFF
GFLAGS_INCLUDE_DIR GFLAGS_INCLUDE_DIR-NOTFOUND
GFLAGS_NAMESPACE
GLOG_INCLUDE_DIR GLOG_INCLUDE_DIR-NOTFOUND
Glog_LIBS Glog_LIBS-NOTFOUND
HDF5_C_LIBRARY_dl /usr/lib/x86_64-linux-gnu/libdl.so
HDF5_C_LIBRARY_hdf5 /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so
HDF5_C_LIBRARY_m /usr/lib/x86_64-linux-gnu/libm.so
HDF5_C_LIBRARY_pthread /usr/lib/x86_64-linux-gnu/libpthread.so
HDF5_C_LIBRARY_sz /usr/lib/x86_64-linux-gnu/libsz.so
HDF5_C_LIBRARY_z /usr/lib/x86_64-linux-gnu/libz.so
INSTALL_CREATE_DISTRIB OFF
INSTALL_C_EXAMPLES OFF
INSTALL_PYTHON_EXAMPLES OFF
INSTALL_TESTS OFF
INSTALL_TO_MANGLED_PATHS OFF
LAPACKE_INCLUDE_DIR LAPACKE_INCLUDE_DIR-NOTFOUND
LAPACK_CBLAS_H
LAPACK_IMPL Unknown
LAPACK_INCLUDE_DIR
LAPACK_LAPACKE_H
LAPACK_LIBRARIES
Lept_LIBRARY Lept_LIBRARY-NOTFOUND
MKL_INCLUDE_DIRS MKL_ROOT_DIR-NOTFOUND/include
MKL_LAPACKE_INCLUDE_DIR MKL_LAPACKE_INCLUDE_DIR-NOTFOUND
MKL_ROOT_DIR MKL_ROOT_DIR-NOTFOUND
MKL_WITH_OPENMP OFF
MKL_WITH_TBB ON
OGRE_DIR OGRE_DIR-NOTFOUND
OPENCL_FOUND ON
OPENCV_CONFIG_FILE_INCLUDE_DIR /software/opencv-3.4.10/build
OPENCV_DNN_OPENCL ON
OPENCV_DOWNLOAD_PATH /software/opencv-3.4.10/.cache
OPENCV_DUMP_HOOKS_FLOW OFF
OPENCV_ENABLE_ALLOCATOR_STATS ON
OPENCV_ENABLE_ATOMIC_LONG_LONG ON
OPENCV_ENABLE_MEMALIGN ON
OPENCV_ENABLE_MEMORY_SANITIZER OFF
OPENCV_ENABLE_NONFREE ON
OPENCV_EXTRA_MODULES_PATH /software/opencv_contrib/modules
OPENCV_FORCE_3RDPARTY_BUILD OFF
OPENCV_FORCE_PYTHON_LIBS OFF
OPENCV_GENERATE_PKGCONFIG ON
OPENCV_GENERATE_SETUPVARS ON
OPENCV_JAVA_SOURCE_VERSION
OPENCV_JAVA_TARGET_VERSION
OPENCV_MATHJAX_RELPATH https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0
OPENCV_PYTHON3_VERSION OFF
OPENCV_TIMESTAMP 2020-06-26T13:34:43Z
OPENCV_WARNINGS_ARE_ERRORS OFF
OPENEXR_INCLUDE_PATH /usr/include/OpenEXR
OpenCV_HAL_DIR OpenCV_HAL_DIR-NOTFOUND
PROTOBUF_UPDATE_FILES OFF
PYTHON2_EXECUTABLE /usr/bin/python2.7
PYTHON2_INCLUDE_DIR /usr/include/python2.7
PYTHON2_INCLUDE_DIR2
PYTHON2_LIBRARY /usr/lib/x86_64-linux-gnu/libpython2.7.so
PYTHON2_LIBRARY_DEBUG
PYTHON2_NUMPY_INCLUDE_DIRS /home/yue/.local/lib/python2.7/site-packages/numpy/core/include
PYTHON2_PACKAGES_PATH lib/python2.7/dist-packages
PYTHON3_EXECUTABLE /usr/bin/python3
PYTHON3_INCLUDE_DIR /usr/include/python3.6m
PYTHON3_INCLUDE_DIR2
PYTHON3_LIBRARY /usr/lib/x86_64-linux-gnu/libpython3.6m.so
PYTHON3_LIBRARY_DEBUG
PYTHON3_NUMPY_INCLUDE_DIRS /usr/lib/python3/dist-packages/numpy/core/include
PYTHON3_PACKAGES_PATH lib/python3.6/dist-packages
TBB_VER_FILE /software/opencv-3.4.10/build/3rdparty/tbb/oneTBB-2020.1/include/tbb/tbb_stddef.h
Tesseract_DIR Tesseract_DIR-NOTFOUND
Tesseract_INCLUDE_DIR Tesseract_INCLUDE_DIR-NOTFOUND
Tesseract_LIBRARY Tesseract_LIBRARY-NOTFOUND
VTK_DIR /usr/local/lib/cmake/vtk-7.1
WITH_1394 ON
WITH_ARAVIS OFF
WITH_ARITH_DEC ON
WITH_ARITH_ENC ON
WITH_CLP OFF
WITH_CUBLAS ON
WITH_CUDA ON
WITH_CUFFT ON
WITH_EIGEN ON
WITH_FFMPEG ON
WITH_GDAL OFF
WITH_GDCM OFF
WITH_GIGEAPI OFF
WITH_GPHOTO2 OFF
WITH_GSTREAMER ON
WITH_GSTREAMER_0_10 OFF
WITH_GTK ON
WITH_GTK_2_X OFF
WITH_HALIDE OFF
WITH_IMGCODEC_HDR ON
WITH_IMGCODEC_PXM ON
WITH_IMGCODEC_SUNRASTER ON
WITH_INF_ENGINE OFF
WITH_IPP ON
WITH_ITT ON
WITH_JASPER ON
WITH_JPEG ON
WITH_LAPACK ON
WITH_LIBV4L OFF
WITH_MATLAB OFF
WITH_MFX OFF
WITH_NGRAPH OFF
WITH_NVCUVID ON
WITH_OPENCL ON
WITH_OPENCLAMDBLAS ON
WITH_OPENCLAMDFFT ON
WITH_OPENCL_SVM OFF
WITH_OPENEXR ON
WITH_OPENGL ON
WITH_OPENMP OFF
WITH_OPENNI OFF
WITH_OPENNI2 OFF
WITH_OPENVX OFF
WITH_PNG ON
WITH_PROTOBUF ON
WITH_PTHREADS_PF ON
WITH_PVAPI OFF
WITH_QT OFF
WITH_QUIRC ON
WITH_TBB ON
WITH_TESSERACT ON
WITH_TIFF ON
WITH_UNICAP OFF
WITH_V4L ON
WITH_VA OFF
WITH_VA_INTEL OFF
WITH_VTK ON
WITH_WEBP ON
WITH_XIMEA OFF
WITH_XINE OFF
gflags_DIR gflags_DIR-NOTFOUND
opencv_dnn_PERF_CAFFE OFF
opencv_dnn_PERF_CLCAFFE OFF
后记:
到这里基本都编译完毕,就可以进行下一步了,当然了编译过程中会遇到各种各样的问题,大家可以留言哈,有空我就会回的。大家一起加油,后续序列会出如何根据自己的数据集,进行标注并且训练得出自己的权重信息。同时会出如何和ROS结合的版本,欢迎大家继续关注。
参考链接: