Skip to main content

YOLOv4 Ubuntu 使用

本文将介绍 YOLOv4 官方 Darknet 实现,如何于 Ubuntu 18.04 编译,及使用 Python 接口。

主要内容有:

  • 准备基础环境: Nvidia Driver, CUDA, cuDNN, CMake, Python
  • 编译应用环境: OpenCV, Darknet
  • 用预训练模型进行推断: darknet 执行,或 python

而 YOLOv4 的介绍或训练,可见《YOLOv4 Docker 使用》。

准备基础环境#

Nvidia Driver#

推荐使用 graphics drivers PPA 安装 Nvidia 驱动:

sudo add-apt-repository ppa:graphics-drivers/ppasudo apt update

查看推荐的 Nvidia 显卡驱动:

ubuntu-drivers devices

安装 Nvidia 驱动:

apt-cache search nvidia | grep ^nvidia-driversudo apt install nvidia-driver-450

之后, sudo reboot 重启。运行 nvidia-smi 查看 Nvidia 驱动信息。

Nvidia CUDA Toolkit#

获取地址:

 建议选择 CUDA 10.2 ,为目前 PyTorch 可支持的最新版本。

下载安装:

wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.runsudo sh cuda_10.2.89_440.33.01_linux.run

注意:安装时,请手动取消驱动安装选项。

安装输出:

============ Summary ============
Driver:   Not SelectedToolkit:  Installed in /usr/local/cuda-10.2/Samples:  Installed in /home/john/cuda-10.2/, but missing recommended libraries
Please make sure that -   PATH includes /usr/local/cuda-10.2/bin -   LD_LIBRARY_PATH includes /usr/local/cuda-10.2/lib64, or, add /usr/local/cuda-10.2/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-10.2/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.2/doc/pdf for detailed information on setting up CUDA.***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 440.00 is required for CUDA 10.2 functionality to work.To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:    sudo <CudaInstaller>.run --silent --driver
Logfile is /var/log/cuda-installer.log

添加环境变量:

$ vi ~/.bashrcexport CUDA_HOME=/usr/local/cudaexport PATH=$CUDA_HOME/bin:$PATHexport LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH

重启终端后,检查:

$ nvcc --versionnvcc: NVIDIA (R) Cuda compiler driverCopyright (c) 2005-2019 NVIDIA CorporationBuilt on Wed_Oct_23_19:24:38_PDT_2019Cuda compilation tools, release 10.2, V10.2.89

Nvida cuDNN#

获取地址:

需选择 CUDA 10.2 对应的版本。

安装 deb 包:

sudo apt install ./libcudnn8_8.0.2.39-1+cuda10.2_amd64.debsudo apt install ./libcudnn8-dev_8.0.2.39-1+cuda10.2_amd64.debsudo apt install ./libcudnn8-doc_8.0.2.39-1+cuda10.2_amd64.deb

查看 deb 包:

dpkg -c libcudnn8_8.0.2.39-1+cuda10.2_amd64.deb

CMake#

下载安装:

curl -O -L https://github.com/Kitware/CMake/releases/download/v3.18.2/cmake-3.18.2-Linux-x86_64.shsh cmake-*.sh --prefix=$HOME/Applications/

添加环境变量:

$ vi ~/.bashrcexport PATH=$HOME/Applications/cmake-3.18.2-Linux-x86_64/bin:$PATH

说明: apt 源的 cmake 太旧, darknet 编译不过。

Python#

获取地址:

Python 建议用 Anaconda 发行版。

安装命令:

# bash Anaconda3-2020.07-Linux-x86_64.shbash Anaconda3-2019.10-Linux-x86_64.sh

编译应用环境#

OpenCV 4.4.0#

安装依赖:

apt install -y build-essential git libgtk-3-dev

编译命令:

conda deactivate
# export CONDA_HOME="/home/john/anaconda3/envs/clenv"export CONDA_HOME=`conda info -s | grep -Po "sys.prefix:\s*\K[/\w]*"`
cd ~/Codes/
git clone -b 4.4.0 --depth 1 https://github.com/opencv/opencv.gitgit clone -b 4.4.0 --depth 1 https://github.com/opencv/opencv_contrib.git
cd opencv/mkdir _build && cd _build/
cmake -DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=$HOME/opencv-cuda-4.4.0 \-DOPENCV_EXTRA_MODULES_PATH=$HOME/Codes/opencv_contrib/modules \\-DPYTHON_EXECUTABLE=$CONDA_HOME/bin/python3.7 \-DPYTHON3_EXECUTABLE=$CONDA_HOME/bin/python3.7 \-DPYTHON3_LIBRARY=$CONDA_HOME/lib/libpython3.7m.so \-DPYTHON3_INCLUDE_DIR=$CONDA_HOME/include/python3.7m \-DPYTHON3_NUMPY_INCLUDE_DIRS=$CONDA_HOME/lib/python3.7/site-packages/numpy/core/include \-DBUILD_opencv_python2=OFF \-DBUILD_opencv_python3=ON \\-DWITH_CUDA=ON \\-DBUILD_DOCS=OFF \-DBUILD_EXAMPLES=OFF \-DBUILD_TESTS=OFF \..
make -j$(nproc)make install

其中 Python 路径请对应自己安装的版本。

运行检查:

conda activate
export LD_LIBRARY_PATH=$HOME/opencv-cuda-4.4.0/lib:$LD_LIBRARY_PATHexport PYTHONPATH=$HOME/opencv-cuda-4.4.0/lib/python3.7/site-packages:$PYTHONPATH
python - <<EOFimport cv2print(cv2.__version__)EOF

问题: libfontconfig.so.1#

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/home/john/opencv-cuda-4.4.0/lib/python3.7/site-packages/cv2/__init__.py", line 96, in <module>    bootstrap()  File "/home/john/opencv-cuda-4.4.0/lib/python3.7/site-packages/cv2/__init__.py", line 86, in bootstrap    import cv2ImportError: /home/john/anaconda3/bin/../lib/libfontconfig.so.1: undefined symbol: FT_Done_MM_Var

解决办法:

cd $HOME/anaconda3/lib/mv libfontconfig.so.1 libfontconfig.so.1.bakln -s /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 libfontconfig.so.1

问题: libpangoft2-1.0.so.0#

ImportError: /home/john/anaconda3/bin/../lib/libpangoft2-1.0.so.0: undefined symbol: FcWeightToOpenTypeDouble

解决办法:

cd $HOME/anaconda3/lib/mv libpangoft2-1.0.so.0 libpangoft2-1.0.so.0.bakln -s /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 libpangoft2-1.0.so.0

Darknet#

编译命令:

export OpenCV_DIR=$HOME/opencv-cuda-4.4.0/lib/cmake
cd ~/Codes/
git clone https://github.com/AlexeyAB/darknet.git
cd darknet/./build.sh

运行检查:

$ export LD_LIBRARY_PATH=$HOME/opencv-cuda-4.4.0/lib:$LD_LIBRARY_PATH
$ ./darknet v CUDA-version: 10020 (10020), cuDNN: 8.0.2, CUDNN_HALF=1, GPU count: 1 CUDNN_HALF=1 OpenCV version: 4.4.0Not an option: v

用预训练模型进行推断#

准备模型与数据#

预训练模型 yolov4.weights ,下载地址 https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights

可以准备 MS COCO 数据集,下载地址 http://cocodataset.org/#download 。或者自己找个图片。

darknet 执行#

cd ~/Codes/darknet/
export LD_LIBRARY_PATH=$HOME/opencv-cuda-4.4.0/lib:$LD_LIBRARY_PATH
export MY_MODEL_DIR=~/Codes/devel/models/yolov4export MY_COCO_DIR=~/Codes/devel/datasets/coco2017
./darknet detector test cfg/coco.data cfg/yolov4.cfg \$MY_MODEL_DIR/yolov4.weights \-thresh 0.25 -ext_output -show \$MY_COCO_DIR/test2017/000000000001.jpg

推断结果:

python 执行#

Darknet 于其根目录,提供有 Python 接口。如下执行:

cd ~/Codes/darknet/
export LD_LIBRARY_PATH=$HOME/opencv-cuda-4.4.0/lib:$LD_LIBRARY_PATHexport PYTHONPATH=$HOME/opencv-cuda-4.4.0/lib/python3.7/site-packages:$PYTHONPATH
python darknet_images.py -h
python darknet_images.py \--batch_size 1 \--thresh 0.1 \--ext_output \--config_file cfg/yolov4.cfg \--data_file cfg/coco.data \--weights $MY_MODEL_DIR/yolov4.weights \--input $MY_COCO_DIR/test2017/000000000001.jpg

推断结果,如前一小节。