Sunday, September 8, 2019

Deep learning in Ubuntu: Darknet YOLOv3

This post describes setting up object detection features of Darknet neural network framework, coupled with camera input, using the YOLOv3 pretrained network, designed by Joseph Redmon and Ali Farhadi, 2018.

The prerequisites are an ubuntu system, equipped with a GPU, with appropriate drivers, CUDA, and Opencv, as well as the Darknet tools.
All these are described in the previous posts.

1.Follow instructions from for weights download and testing with image based object detection.
I get errors in processing the image recognition samples

$ ./darknet detector test ./cfg/ ./cfg/yolov3.cfg ./cfg/yolov3.weights ./data/dog.jpg
layer     filters    size              input                output
    0 conv     32  3 x 3 / 1   608 x 608 x   3   ->   608 x 608 x  32  0.639 BFLOPs
    1 conv     64  3 x 3 / 2   608 x 608 x  32   ->   304 x 304 x  64  3.407 BFLOPs
    2 conv     32  1 x 1 / 1   304 x 304 x  64   ->   304 x 304 x  32  0.379 BFLOPs
    3 CUDA Error: out of memory
darknet: ./src/cuda.c:36: check_error: Assertion `0' failed.
Aborted (core dumped)

I retry with tiny model (i download the tiny network weights) and now it works
$ ./darknet detector test ./cfg/ ./cfg/yolov3-tiny.cfg ./cfg/yolov3-tiny.weights ./data/dog.jpg
Loading weights from cfg/yolov3-tiny.weights...Done!
data/dog.jpg: Predicted in 0.192830 seconds.
dog: 56%
car: 52%
truck: 56%
car: 62%
bicycle: 58%

It seems that the error comes from the network configurations in cfg/yolov3.cfg

Comparing the yolov3.cfg and the yolov3-tiny.cfg i managed to find parameter to have the network run correctly with the larger weight-set

I edited the first lines of yolov3.cfg as follows

# Testing
# Training

And now it works!
$ ./darknet detector test cfg/ cfg/yolov3.cfg cfg/yolov3.weights data/dog.jpg
Loading weights from cfg/yolov3.weights...Done!
data/dog.jpg: Predicted in 0.327576 seconds.
dog: 99%
truck: 93%
bicycle: 99%
(base) mgua@mgtp53s:~/darknet/darknet$ 

2.install mplayer to have the libraries required to access the camera feed
#apt update
#apt install mplayer
$./darknet detector demo ./cfg/ ./cfg/yolov3-voc.cfg ./cfg/yolov3-voc.weights
or, for light model
$./darknet detector demo ./cfg/ ./cfg/yolov3-tiny.cfg ./cfg/yolov3-tiny.weights

On my laptop computer, with GPU Nvidia Quadro P520, OpenCV and CUDA I get about 6 FPS (frames per second) with the full weights set and 16 FPS with the tiny model.

The following syntax should allow to use YOLO getting input from a network camera:

$./darknet detector demo ./cfg/ ./cfg/yolo-voc.cfg ./cfg/yolo-voc.weights rtsp://login:pass@ -i 0

Deep learning in Ubuntu: installing darknet

This document describes how I added the darknet components to my Ubuntu deep learning machines.

Darknet is an open source environment for neural networks, written in C, by Joseph Redmon, aka pjreddie.
Darknet is very fast.


This document describes the installation of darknet on top of an ubuntu 18.04 linux machine,
equipped with a NVIDIA GPU board, where CUDA and OPENCV have been installed
(see these: NVIDIA and CUDA Setup and OPENCV setup).
The installation process is relying on Darknet setup instructions, but for my environment required some additional steps.

working in user mode, I create a darknet folder in my home

$mkdir darknet
$cd darknet
$git clone
$cd darknet

Edit the Makefile, adjusting options GPU=1 and OPENCV=1

then running make


it chokes giving many errors like

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

Examining the Makefile, you can see that it uses the commands

pkg-config --libs opencv


pkg-config --cflags opencv

1) install apt-file and update its library

#apt install apt-file
#apt-file update

2) look which package provides the file opecv.pc

#apt-file search opencv.pc
libopencv-dev: /usr/lib/x86_64-linux-gnu/pkgconfig/opencv.pc

3) install libopecv-dev

#apt install libopencv-dev

4) re-executing make, which now does not give any more errors...

$cd darknet/darknet

5) and finally now you can see the eagles...

$./darknet imtest data/eagle.jpg

A potentially useful tool for showing pictures from command line is feh. It allows zoom and it is very handy.