This article introduces the official Darknet implementation of YOLOv4, how to compile on Ubuntu 18.04, and how to use the Python interface.

The main contents are:

  • Prepare the basic environment: Nvidia Driver, CUDA, cuDNN, CMake, Python
  • Compile application environment: OpenCV, Darknet
  • Extrapolation with the pre-training model:darknetPerform, orpython

YOLOv4: How Darknet builds on Docker and trains COCO subsets.

Preparing the basic Environment

Nvidia Driver

Install Nvidia drivers using Graphics Drivers PPA:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
Copy the code

View the recommended Nvidia graphics card drivers:

ubuntu-drivers devices
Copy the code

Install Nvidia driver:

apt-cache search nvidia | grep ^nvidia-driver
sudo apt install nvidia-driver-450
Copy the code

Then, sudo reboot. Run the nvidia-smI command to view the nvidia driver information.

Nvidia CUDA Toolkit

Obtain address:

  • CUDA Toolkit Archive: developer.nvidia.com/cuda-toolki…

CUDA 10.2 is recommended, the latest version currently supported by PyTorch.

Download and install:

Wget sudo http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run Sh cuda_10. 2.89 _440. 33.01 _linux. RunCopy the code

Note: When installing, manually cancel the driver installation option.

Installation output:

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/ cuda - 10.2 / Samples: Installedin /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, the 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/ bin/cuda 10.2 both Please see CUDA_Installation_Guide_Linux. PDFin /usr/local/ cuda - 10.2 / doc/PDFfor 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
Copy the code

Add environment variables:

$ vi ~/.bashrc
export CUDA_HOME=/usr/local/cuda
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
Copy the code

After the terminal is restarted, check:

$ 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
Copy the code

Nvida cuDNN

Obtain address:

  • CuDNN Download: developer.nvidia.com/rdp/cudnn-d…

Select CUDA 10.2 version.

Install deb package:

/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.debCopy the code

Check deb package:

DPKG -c libcudnn8_8. 0.2.39-1 + cuda10.2 _amd64. DebCopy the code

CMake

Download and install:

Curl - O - https://github.com/Kitware/CMake/releases/download/v3.18.2/cmake-3.18.2-Linux-x86_64.sh sh cmake L - *. Sh --prefix=$HOME/Applications/
Copy the code

Add environment variables:

$ vi ~/.bashrc
export PATH=$HOME/ Applications/cmake - 3.18.2 - Linux - x86_64 / bin:$PATH
Copy the code

Apt source cmake is too old, darknet does not compile.

Python

Obtain address:

  • Anaconda: www.anaconda.com/distributio…

Python recommends the Anaconda distribution.

Installation command:

# bash Anaconda3-2020.07 - Linux - x86_64. ShBash Anaconda3-2019.10 - Linux - x86_64. ShCopy the code

Compiling the application Environment

OpenCV 4.4.0

Install dependencies:

apt install -y build-essential git libgtk-3-dev
Copy the code

Compile command:

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 - the depth 1 https://github.com/opencv/opencv.git gitclone- b 4.4.0 - the depth of 1 https://github.com/opencv/opencv_contrib.gitcd 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.7 Margaret spellings \ - DPYTHON3_INCLUDE_DIR = o$CONDA_HOME/ include/python3.7 m \ - 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 installCopy the code

The Python path must correspond to the version you installed.

Operation check:

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
Copy the code

Problem: 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 / set p y", line 96, in <module>
    bootstrap()
  File "/ home/John/opencv - cuda - 4.4.0 / lib/python3.7 / site - packages/cv2 / set p y", line 86, inbootstrap import cv2 ImportError: /home/john/anaconda3/bin/.. /lib/libfontconfig.so.1: undefined symbol: FT_Done_MM_VarCopy the code

Solutions:

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
Copy the code

Problem: libpangoft2-1.0. So. 0

ImportError: /home/john/anaconda3/bin/.. / lib/libpangoft2-1.0. So. 0: undefined symbol: FcWeightToOpenTypeDoubleCopy the code

Solutions:

cd $HOME0 libpangoft2-1.0.so. bak ln -s /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so Libpangoft2 1.0. So. 0Copy the code

Darknet

Compile command:

export OpenCV_DIR=$HOME/ opencv - cuda - 4.4.0 / lib/cmakecd ~/Codes/

git clone https://github.com/AlexeyAB/darknet.git

cd darknet/
./build.sh
Copy the code

Operation check:

$ 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
Copy the code

Extrapolation using a pre-training model

Prepare models and data

Pre-training model yolov4. Weights, download github.com/AlexeyAB/da… .

You can prepare the MS COCO dataset at cocodataset.org/#download. Or find your own picture.

darknetperform

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
Copy the code

Inferred results:

pythonperform

Darknet provides a Python interface in its root directory. Execute as follows:

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:$PYTHONPATHPython 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
Copy the code

Infer the results, as in the previous section.

conclusion

Let’s go coding ~


Share practical tips and knowledge in Coding! Welcome to pay attention and grow together!