Compiling Darknet on Arch

I and my friend Rohit were working on object detection for a project. So naturally, the first choice was to try YOLO object detection. YOLO is implemented using Darknet.

Darknet is an open source neural network framework written in C and CUDA. It is fast, easy to install, and supports CPU and GPU computation.

You have to compile Darknet to run YOLO. There were few hiccups that I faced while compiling Darknet on Arch with Nvidia GPU. I will detail out the procedure for the same.

For CPU

The procedure of running compiling Darknet and running YOLO on CPU is easy and is listed on its website.

For Nvidia GPU


NOTE

Assuming all the packages are installed in their default location.


  1. Clone the Darknet repo.

    git clone https://github.com/pjreddie/darknet.git
    cd darknet
    mkdir -o obj
  2. Install OpenCV legacy version(opt directory version) from here.

  3. Install CUDA from here. And add installed CUDA binaries to $PATH.

    # add this in ~/.bashrc
    export PATH="$PATH:/opt/cuda/bin"
  4. Open Makefile in darknet directory and set GPU and OPENCV to 1.

    #Makefile
    
    GPU=1
    OPENCV=1
  5. Change first occurance of LDFLAGS and COMMON to the following:

    LDFLAGS= -L/opt/cuda/lib64 -L/opt/opencv2/lib -lm -pthread -lstdc++ 
    COMMON= -Iinclude/ -Isrc/ -I/opt/cuda/include 
  6. In 'ifeq ($(OPENCV), 1)' section change LDFLAGS and COMMON to following and save it.

    LDFLAGS+= -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
    COMMON+= -I/opt/opencv2/include
  7. Now run make

    make -j 8
  8. If you have any errors, try to fix them or ask in comment box. If everything seems to have compiled correctly, try running it!

    LD_LIBRARY_PATH=/opt/opencv2/lib ./darknet

    Download YOLOv3 pre-trained weights.

    wget https://pjreddie.com/media/files/yolov3.weights

    Try running it on input from a webcam.

    LD_LIBRARY_PATH=/opt/opencv2/lib ./darknet detector demo cfg/coco.data cfg/yolov3.cfg yolov3.weights

    We are setting LD_LIBRARY_PATH so that the linker can find OpenCV’s dynamic libraries. Else it will show errors.

  9. If you have less powerful GPU, like mine(I have MX 150), try running Tiny YOLOv3.

    # download weights
    wget https://pjreddie.com/media/files/yolov3-tiny.weights
    
    # run the detector
    LD_LIBRARY_PATH=/opt/opencv2/lib \
    ./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
Running Tiny YOLOv3, at 5 fps on MX 150