Yolo-V4 Installation On Ubuntu 18.04

Dhruvdwivedy
4 min readMar 12, 2021

Hello friends, Installation of YOLOv4 on Ubuntu 18.04.

Pre-Requirements:

  1. Python 3.6
  2. Git
  3. CMake >=3.12
  4. CUDA >= 10.0
  5. CuDNN 7.6.5
  6. OpenCV 4.0.0
  7. OpenCV Contrib (latest)
  8. YOLOv4
  9. Last but not least a good internet connection

Steps are as follows:

Step 1: Install Git

  • sudo apt update
  • sudo apt-get install git

Step 2: Install Nvidia Driver using following commands:-

  • ubuntu-drivers devices
  • then select the recommended drivers
  • Example command => “sudo apt-get install nvidia-driver-460”

Step 3: Install CUDA 10.0

  • link for cuda file(cuda-10.0)
  • After Successful downloading all the files open terminal in that location where your file downloaded and type “sudo sh ‘CUDA filename’”
  • First select “accept” for Eula options
  • then select ’n’ / no for graphic installation
  • then select “y” / yes for all others options
  • press enter for default locations.
  • copy all the lines and paste in bashrc file and save the file if this are not already available in your bashrc file.
  • # NVIDIA CUDA Path
  • export PATH=/usr/local/cuda-10.0/bin:$PATH
  • export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64

Step 4: Install CuDNN

  • To install CuDNN you have to download 3 files are as follows:
  1. cuDNN Runtime Library for Ubuntu18.04 (Deb)
  2. cuDNN Developer Library for Ubuntu18.04 (Deb)
  3. cuDNN Code Samples and User Guide for Ubuntu18.04 (Deb) (Not Mandatory)
  • After Successful downloading all the files open terminal in that location where your file downloaded and type “sudo dpkg -i ‘cuDNN Runtime file name’ “ and press enter.
  • then type same command with the 2nd file name is developer file (dev file).
  • and also same command for the 3rd file name I.e document file(Sample file).
  • After all, type “nvidia-smi” and press enter to be sure you have installed nvidia graphic driver.
  • Now, type “nvcc — version” that will show you the CUDA version that installed in your system.

Step 5: Now time to install OpenCV by build, follow these commands

  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo apt-get install build-essential cmake unzip pkg-config
  • sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
  • sudo apt-get install libjasper-dev
  • sudo add-apt-repository “deb http://security.ubuntu.com/ubuntu xenial-security main”
  • sudo apt update
  • sudo apt install libjasper1 libjasper-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.6-dev
  • wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
  • wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
  • unzip opencv.zip
  • unzip opencv_contrib.zip
  • mv opencv-4.0.0 opencv
  • mv opencv_contrib-4.0.0 opencv_contrib
  • wget https://bootstrap.pypa.io/get-pip.py
  • sudo python3 get-pip.py
  • sudo pip install virtualenv virtualenvwrapper
  • sudo rm -rf ~/get-pip.py ~/.cache/pip
  • echo -e “\n# virtualenv and virtualenvwrapper” >> ~/.bashrc
  • echo “export WORKON_HOME=$HOME/.virtualenvs” >> ~/.bashrc
  • echo “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” >> ~/.bashrc
  • echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc
  • source ~/.bashrc
  • mkvirtualenv cv -p python3
  • workon cv
  • pip install numpy
  • cd opencv
  • mkdir build
  • cd build
  • cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_opencv_python2=OFF -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=/home/antpc/tf/opencv/opencv_contrib/modules -D PYTHON_DEFAULT_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON ..
  • Once CMake finishes, it’s important that you inspect the output. Your output should look similar to mine below:
  • (After Successful configuration and generating be sure it build with “Python3” and second thing is non-free algorithm is “yes”)
  • make -j4 (Here 4 is the no. of cores),(To check your system cores type “nproc” in terminal and press enter)
  • sudo make install
  • sudo ldconfig
  • ls /usr/local/python/cv2/python-3.6(to confirm this file “cv2.cpython-36m-x86_64-linux-gnu.so” )
  • go to that location by using command “cd /usr/local/python/cv2/python-3.6”
  • sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
  • cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
  • ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so
  • Now Test your OpenCV installation
  • workon cv
  • python
  • import cv2
  • cv2.__version__
  • quit()

Step 6: Darknet YOLOv4 installation Steps:

  • Goto this link and download the repo in zip “https://github.com/AlexeyAB/darknet/tree/darknet_yolo_v4_pre"
  • unzip downloaded file “darknet_yolo_v4_pre”
  • open terminal on that folder and activate your virtualenvs by this command==>
  • workon cv
  • sudo apt install libopencv-dev python3-opencv
  • sudo apt install make git g++
  • on the folder open make file and change these ==>
  • GPU=1, CUDNN=1, CUDNN_HALF=1, OPENCV=1, LIBSO=1.
  • make
  • mkdir build_release
  • cd build_release
  • sudo apt remove cmake
  • pip install cmake — upgrade
  • type “which cmake” and add that path in the .bashrc file that are in home location.
  • cmake ..
  • make
  • copy the darknet and libdark.so from the build_release to the darknet folder.
  • You also have to rename ( libdark.so -> libdarknet.so )
  • Confirm your installation by going to the darknet folder with same “cv environment” and type “python” and then “import darknet”.

Thank You✌

--

--