共计 6197 个字符,预计需要花费 16 分钟才能阅读完成。
本文将介绍 YOLOv4 官网 Darknet 实现,如何于 Ubuntu 18.04 编译,及应用 Python 接口。
次要内容有:
- 筹备根底环境:Nvidia Driver, CUDA, cuDNN, CMake, Python
- 编译应用环境:OpenCV, Darknet
- 用预训练模型进行推断:
darknet
执行,或python
而 YOLOv4 的介绍或训练,可见前文《YOLOv4: Darknet 如何于 Docker 编译,及训练 COCO 子集》。
筹备根底环境
Nvidia Driver
举荐应用 graphics drivers PPA 装置 Nvidia 驱动:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
查看举荐的 Nvidia 显卡驱动:
ubuntu-drivers devices
装置 Nvidia 驱动:
apt-cache search nvidia | grep ^nvidia-driver
sudo apt install nvidia-driver-450
之后,sudo reboot
重启。运行 nvidia-smi
查看 Nvidia 驱动信息。
Nvidia CUDA Toolkit
获取地址:
- CUDA Toolkit Archive: https://developer.nvidia.com/…
倡议抉择 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.run
sudo sh cuda_10.2.89_440.33.01_linux.run
留神:装置时,请手动勾销驱动装置选项。
装置输入:
===========
= Summary =
===========
Driver: Not Selected
Toolkit: 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 ~/.bashrc
export CUDA_HOME=/usr/local/cuda
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
重启终端后,查看:
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
Nvida cuDNN
获取地址:
- cuDNN Download: https://developer.nvidia.com/…
需抉择 CUDA 10.2 对应的版本。
装置 deb 包:
sudo apt install ./libcudnn8_8.0.2.39-1+cuda10.2_amd64.deb
sudo apt install ./libcudnn8-dev_8.0.2.39-1+cuda10.2_amd64.deb
sudo 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.sh
sh cmake-*.sh --prefix=$HOME/Applications/
增加环境变量:
$ vi ~/.bashrc
export PATH=$HOME/Applications/cmake-3.18.2-Linux-x86_64/bin:$PATH
阐明:apt 源的 cmake 太旧,darknet 编译不过。
Python
获取地址:
- Anaconda: https://www.anaconda.com/dist…
Python 倡议用 Anaconda 发行版。
装置命令:
# bash Anaconda3-2020.07-Linux-x86_64.sh
bash 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.git
git 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_PATH
export PYTHONPATH=$HOME/opencv-cuda-4.4.0/lib/python3.7/site-packages:$PYTHONPATH
python - <<EOF
import cv2
print(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 cv2
ImportError: /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.bak
ln -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.bak
ln -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.0
Not an option: v
用预训练模型进行推断
筹备模型与数据
预训练模型 yolov4.weights,下载地址 https://github.com/AlexeyAB/d…。
能够筹备 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/yolov4
export 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_PATH
export 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
推断后果,如前一大节。
结语
Let’s go coding ~
分享 Coding 中实用的小技巧、小常识!欢送关注,独特成长!