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.
See https://pjreddie.com/darknet/yolo/

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 https://pjreddie.com/darknet/yolo/ for weights download and testing with image based object detection.
I get errors in processing the image recognition samples

$ ./darknet detector test ./cfg/coco.data ./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/coco.data ./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

[net]
# Testing
batch=1
subdivisions=1
# Training
#batch=64
#subdivisions=16
#width=608
#height=608
width=416
height=416
channels=3
[...]

And now it works!
$ ./darknet detector test cfg/coco.data 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


3.run
$./darknet detector demo ./cfg/voc.data ./cfg/yolov3-voc.cfg ./cfg/yolov3-voc.weights
or, for light model
$./darknet detector demo ./cfg/coco.data ./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/voc.data ./cfg/yolo-voc.cfg ./cfg/yolo-voc.weights rtsp://login:pass@192.168.0.228:554 -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.

See https://pjreddie.com/darknet/

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

$cd
$mkdir darknet
$cd darknet
$git clone https://github.com/pjreddie/darknet.git
$cd darknet

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

then running make

$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

and 

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
$cd darknet/darknet
$make


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.