Getting your machine ready to use Yolov3 Object Detector on Ubuntu 18.04 | by Pablo Oberhauser | Towards Data Science

At Actuate, our goal is to stay at the forefront of computer vision development. As such, we like to keep up to date with the best work happening in the broader computer vision space. Part of this involves keeping track of the best systems to deploy on, such as darknet.
This post shows how to get your machine ready for object detection using yolov3, and more specifically AlexeyAB’s yolov3 Github repo. This is a high level post on steps needed to start up a virtual machine, install necessary packages, and preliminary testing to make sure you are ready to get started with object detection. This post uses several tutorials I have found to be useful in the installation process for some of these dependencies. The main steps are the following:
Get either your local machine or VM booted and making sure your GPU is Cuda enabled.Cloning necessary GitHub reposInstalling OpenCV and testing darknet with OpenCVInstalling Cuda, Cudnn, and other necessary packages to speed up training process in GPU.
Get either your local machine or VM booted and making sure your GPU is Cuda enabled.
Cloning necessary GitHub repos
Installing OpenCV and testing darknet with OpenCV
Installing Cuda, Cudnn, and other necessary packages to speed up training process in GPU.
If you have a GPU enabled machine, you can skip to step 2. Otherwise, getting a virtual machine with GPUs will be important for the task of object detection to speed things up considerably. For this walk through we’ll be using Microsoft Azure machines, but any local machine with GPUs or any other cloud computing service will obviously work as well.
Once you set up your account in Azure, you can navigate to your Home and then inspect the Virtual Machines service.
Click on the ‘Add +’ button and that will open the ‘Create a virtual machine page’. Make sure you have a resource group set up, otherwise you’ll have to create it as you begin to set up your instance.
Key part here is selecting a size for your VM. On the first page of configuration when starting up a new VM in Azure, you will find the ‘Change Size’ option. Click on that and pick a size where the ‘Family’ column says GPU.
Once the machine has been created you can still configure later, so for example if you want to add disk space or change to another family of machines, that is possible to do down the road. Once you start it, you can ssh into it and you can make sure your machine has a CUDA-enbled GPU with lspci | grep -i nvidia
The output should look somewhat similar to the screenshot below, although the details of the GPU can vary.
If you are new to Azure, this Getting Started Guide could be very useful:
If you have ssh enabled on your github account simply do
git clone git@github.com:AlexeyAB/darknet.git
If this a new vm, you might have to install git before by:
sudo apt install git-all
Once that is done, make sure it worked and your working directory should look somewhat similar to this:
There are a couple of other dependencies you’ll need:
sudo apt install make
If you don't have it already, you’ll need C compiler gcc and g++ . You can check with which gcc and which g++ . If not installed you can do so with the following commands.
sudo apt install gccsudo apt install g++
Now we are ready to test out the darknet repo (without OpenCV and without using GPU for training or predictions. Go to the darknet directory and try $make. You should see bunch of warning messages and printed output to your console, and if there are no errors, you are technically ready to start with object detection. Try the $ ./darknet command and you should see the following output on your console:
usage: ./darknet <function
3. Installing OpenCV 3.4.4 on Ubuntu 18.04
Following these steps, you will install OpenCV3.4.4 but the object detection repo should also work with version 4+.
This is one of the easiest to follow tutorials I have found. Adrian Rosebrock does a really good job at explaining the steps you are copying and pasting so you can understand what these dependencies do. He also has pretty good alternatives for when steps might throw errors back at you.
So go check it out, and hopefully, things go smoothly.
You can check if the installation worked by opening up a python process in your command line and typing
import cv2print(cv2.__version__)
Now to check if darknet will now work with opencv:
Change the Makefile in the /darknet/ directory to show OPENCV=1 and then re- make . As stated in the official documentation, you can test this out with ./darknet imtest data/eagle.jpg although keep in mind that if you are working on a virtual machine you will encounter the “cannot open display” error, but everything should work as it should at this point, and you should see the detection probabilities on the test image.
4. Installing CUDA and CUDNN on Ubuntu 18.04
You’ll probably want to run darknet on GPU so you’ll need CUDA, CUDNN installed as well. “Darknet on the CPU is fast but it’s like 500 times faster on GPU!” — Joseph Redmon
This is an extremely helpful post by Mahedi-61 with a script that performs a good chunk of the installation. Go ahead and work through these steps.
The post also highlights how to remove previous CUDA installations:
sudo apt-get purge nvidia*sudo apt remove nvidia-*sudo rm /etc/apt/sources.list.d/cuda*sudo apt-get autoremove && sudo apt-get autocleansudo rm -rf /usr/local/cuda*
If you want a different Cuda or CUDNN version, just change the download files to the desired version in the above script.
To make sure installation was successful, try
nvidia-smi
And finally, try
nvcc -V
And hopefully, you get
Finally, it's important to make sure that all of this works with darknet and we can then begin testing the object detector.
Change your Makefile in the darknet directory to:
If make command is successful, then you are ready to begin testing.
If you want to test with some real weights
wget https://pjreddie.com/media/files/yolov3.weights./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg -dont_show
Hope this high level recipe to get your machine ready for object detectors with yolov3 from AlexeyAB’s repo.