Darknet Installation - riplaboratory/Kanaloa GitHub Wiki

Installation and Configuration of Darknet

This procedure is for installing Darknet for the purpose of training a neural network in YOLO. Instruction is also provided for running a sample dataset through a training iteration.

Prerequisites

  • Ubuntu 18.06
  • Anaconda (we used version 2019.10 build py37_0)
  • Install and configure OpenCV (instructions here)
  • Download yolo_tl.zip folder available here (note that you need access to the Kanaloa Google Drive).

1. Create a Conda environment

Open a terminal window and navigate to the following directory:

Cd /Desktop/yolo_tl/assets/Ubuntu_18_Environments

Run the following command to create the environment:

conda env create --file yolo.yml

It is normal for it to take a few minutes to create the environment.

2. Install CUDA toolkit version 9.1 (You can skip to step 4 if it is already installed).

Run each of the following commands one at a time in the same terminal window.

sudo rm /etc/apt/sources.list.d/cuda*

sudo apt remove --autoremove nvidia-cuda-toolkit

sudo apt remove --autoremove nvidia-*

sudo apt update

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

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'

sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda_learn.list'

3. Update CUDA

Run the following commands, one at a time

sudo apt update

sudo apt install cuda-10-1

sudo apt auto remove (only if needed)

sudo apt install libcudnn7

After running all the commands, reboot your computer.

4. Clone the Darknet GitHub

Open terminal and navigate to your chosen location for Darknet. We used the Documents folder.

cd ~/Documents

Clone the GitHub by running the following command in terminal:

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

5. Open a terminal window and navigate to the Darknet folder:

cd Documents/darknet

Open the profile. You can also replace subl with your choice of text editor.

sudo subl ~/.profile

At the bottom of the file, paste the following (the updated version at the time of this tutorial was 10.1, replace the cuda-10.1 portion of this code with the appropriate version on your computer cuda-xx.x).

if [ -d "/usr/local/cuda-10.1/bin/" ]; then export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}} Export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}fi

You can then save and exit the text editor, and reboot your computer.

6. In the file explorer, navigate to darknet > Makefile. Open the Makefile in your choice of text editor and change the flags to the following:

GPU=0

CUDNN=0

OPENCV=1

7. Activate a yolo environment

conda activate yolo

8. Create a symbolic link to view the recently updated CUDA

sudo ln -s /usr/local/cuda-10.1 /usr/local/cuda

After doing so, restart the terminal.

Navigate to the downloads folder:

cd Downloads

Run the following commands one at a time, and selecting yes/agree whenever prompted. There will be a lengthy user agreement, just keep hitting enter until you get to the bottom.

sudo apt update

sudo apt upgrade

You can hit tab to autocomplete the cuda version:

sudo chmod +x cuda_10.1.105_418.39_linux.run

sudo sh cuda_10.1.105_418.39_linux.run

9. Compile the Darknet framework

Run the following in terminal:

cd Documents/darknet

Make

Darknet is now installed and compiled.


Training Example

Training can be tested using the sample data from the yolo resources.

1. Select the test image and move to the correct folder.

In the file explorer, move test image 0.jpg from ~/yolo_tl/data into ~/yolo_tl/assets/scripts

2. Edit the config files for the training script.

The file will be edited to have the correct path for the classes, neural net, and image.

In a new terminal tab, navigate to the scripts folder:

cd ~/Desktop/yolo_tl/assets/scripts

Open yolo-3-image.py in sublime (or another text editor).

subl yolo-3-image.py

Edit line 41 so it reads the test image:

image_BGR = cv2.imread('./0.jpg')

Edit line 125 so it reads the classes:

`open(‘classes.names’) as f;

Edit lines 143 and 144 so it reads the neural network:

network = cv2.dnn.readNetFromDarknet('yolov3_custom_test.cfg', '../../backup/yolov3_custom_train_final.weights')

In the terminal, still in the scripts directory:

Python yolo-3-image.py

Training

In a new terminal window, run the following:

cd Documents/darknet

conda activate yolo

./darknet detector demo cfg/custom_data.data cfg/yolov3_custom_test.cfg weights/yolov3_custom_train_final.weights -thresh 0.85 -dont_show data/simulation_camera3.mp4 -out_filename result.avi