JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
Yolo.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2017 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#pragma once
19
22#include <atomic>
23
24#include <nnpack.h>
25
26extern "C" {
27#include <darknet.h>
28}
29
30namespace jevois { class StdModule; }
31
32namespace yolo
33{
34 static jevois::ParameterCategory const ParamCateg("Darknet YOLO Options");
35
36 //! Parameter \relates Yolo
37 JEVOIS_DECLARE_PARAMETER(dataroot, std::string, "Root path for data, config, and weight files. If empty, use "
38 "the module's path.",
39 JEVOIS_SHARE_PATH "/darknet/yolo", ParamCateg);
40
41 //! Parameter \relates Yolo
42 JEVOIS_DECLARE_PARAMETER(datacfg, std::string, "Data configuration file (if relative, relative to dataroot)",
43 "cfg/coco.data", ParamCateg);
44
45 //! Parameter \relates Yolo
46 JEVOIS_DECLARE_PARAMETER(cfgfile, std::string, "Network configuration file (if relative, relative to dataroot)",
47 "cfg/yolov3-tiny.cfg", ParamCateg);
48
49 //! Parameter \relates Yolo
50 JEVOIS_DECLARE_PARAMETER(weightfile, std::string, "Network weights file (if relative, relative to dataroot)",
51 "weights/yolov3-tiny.weights", ParamCateg);
52
53 //! Parameter \relates Yolo
54 JEVOIS_DECLARE_PARAMETER(namefile, std::string, "Category names file, or empty to fetch it from the network "
55 "config file (if relative, relative to dataroot)",
56 "", ParamCateg);
57
58 //! Parameter \relates Yolo
59 JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent",
60 45.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
61
62 //! Parameter \relates Yolo
63 JEVOIS_DECLARE_PARAMETER(thresh, float, "Detection threshold in percent confidence",
64 24.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
65
66 //! Parameter \relates Yolo
67 JEVOIS_DECLARE_PARAMETER(hierthresh, float, "Hierarchical detection threshold in percent confidence",
68 50.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
69
70 //! Parameter \relates Yolo
71 JEVOIS_DECLARE_PARAMETER(threads, int, "Number of parallel computation threads",
72 6, jevois::Range<int>(1, 1024), ParamCateg);
73}
74
75//! Detect multiple objects in scenes using the Darknet YOLO deep neural network
76/*! Darknet is a popular neural network framework, and YOLO is a very interesting network that detects all objects in a
77 scene in one pass. This component detects all instances of any of the objects it knows about (determined by the
78 network structure, labels, dataset used for training, and weights obtained) in the image that is given to is.
79
80 See https://pjreddie.com/darknet/yolo/
81
82 Darknet is a great, bare-metal deep learning and deep neural network framework. It is great for embedded systems
83 like the small JeVois camera because it has a very small footprint and fewer dependencies than other deep neural
84 network frameworks like Tensorflow, MXNet, Theano, Keras, PyTorch, etc. In addition, the port of Darknet to JeVois
85 includes acceleration using the ARM NEON multimedia instructions through the popular NNPACK neural network
86 acceleration package.
87
88 \ingroup Components */
89class Yolo : public jevois::Component,
90 jevois::Parameter<yolo::dataroot, yolo::datacfg, yolo::cfgfile, yolo::weightfile, yolo::namefile,
91 yolo::nms, yolo::thresh, yolo::hierthresh, yolo::threads>
92{
93 public:
94 //! Constructor
95 Yolo(std::string const & instance);
96
97 //! Initialize, configure and load the network in a thread
98 /*! Any call to process() will simply throw until the network is loaded and ready */
99 void postInit() override;
100
101 //! Virtual destructor for safe inheritance
102 virtual ~Yolo();
103
104 //! Un-initialize and free resources
105 void postUninit() override;
106
107 //! Processing function, results are stored internally in the underlying Darknet network object
108 /*! This version expects an OpenCV RGB byte image which will be converted to float RGB planar, and which may be
109 letterboxed if necessary to fit network input dims. Returns the prediction time (neural net forward pass) in
110 milliseconds. Throws std::logic_error if the network is still loading and not ready. */
111 float predict(cv::Mat const & cvimg);
112
113 //! Processing function, results are stored internally in the underlying Darknet network object
114 /*! This version expects a Darknet image input, RGB float planar normalized to [0..1], with same dims as network
115 input dims. Returns the prediction time (neural net forward pass) in milliseconds. Throws std::logic_error if
116 the network is still loading and not ready. */
117 float predict(image & im);
118
119 //! Compute the boxes
120 /*! You must have called predict() first for this to not violently crash. */
121 void computeBoxes(int inw, int inh);
122
123 //! Draw the detections
124 /*! You must have called computeBoxes() first for this to not violently crash. */
125 void drawDetections(jevois::RawImage & outimg, int inw, int inh, int xoff, int yoff);
126
127 //! Send serial messages about detections
128 /*! You must have called computeBoxes() first for this to not violently crash. The module given should be the owner
129 of this component, we will use it to actually send each serial message using some variant of
130 jevois::Module::sendSerial(). */
131 void sendSerial(jevois::StdModule * mod, int inw, int inh);
132
133 //! Resize the network's input image dims
134 /*! This will prepare the network to receive inputs of the specified size. It is optional and will be called
135 automatically by predict() if the given image size does not match the current network input size. Note that this
136 only works with fully convolutional networks. Note that the number of channels cannot be changed at this
137 time. Throws std::logic_error if the network is still loading and not ready. */
138 void resizeInDims(int w, int h);
139
140 //! Get input width, height, channels
141 /*! Throws std::logic_error if the network is still loading and not ready. */
142 void getInDims(int & w, int & h, int & c) const;
143
144 // We leave these in the open in case one wants to access the probs, names, etc but just be careful with them
145 network * net;
146 char ** names;
148 detection * dets;
150 int * map;
151
152 protected:
153 std::future<void> itsReadyFut;
154 std::atomic<bool> itsReady;
155 };
int h
Detect multiple objects in scenes using the Darknet YOLO deep neural network.
Definition Yolo.H:92
int classes
Definition Yolo.H:149
void drawDetections(jevois::RawImage &outimg, int inw, int inh, int xoff, int yoff)
Draw the detections.
Definition Yolo.C:182
void sendSerial(jevois::StdModule *mod, int inw, int inh)
Send serial messages about detections.
Definition Yolo.C:225
detection * dets
Definition Yolo.H:148
JEVOIS_DECLARE_PARAMETER(cfgfile, std::string, "Network configuration file (if relative, relative to dataroot)", "cfg/yolov3-tiny.cfg", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(thresh, float, "Detection threshold in percent confidence", 24.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(hierthresh, float, "Hierarchical detection threshold in percent confidence", 50.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
void computeBoxes(int inw, int inh)
Compute the boxes.
Definition Yolo.C:168
void resizeInDims(int w, int h)
Resize the network's input image dims.
Definition Yolo.C:258
std::future< void > itsReadyFut
Definition Yolo.H:153
network * net
Definition Yolo.H:145
char ** names
Definition Yolo.H:146
JEVOIS_DECLARE_PARAMETER(threads, int, "Number of parallel computation threads", 6, jevois::Range< int >(1, 1024), ParamCateg)
Parameter.
float predict(cv::Mat const &cvimg)
Processing function, results are stored internally in the underlying Darknet network object.
Definition Yolo.C:121
JEVOIS_DECLARE_PARAMETER(weightfile, std::string, "Network weights file (if relative, relative to dataroot)", "weights/yolov3-tiny.weights", ParamCateg)
Parameter.
std::atomic< bool > itsReady
Definition Yolo.H:154
void postUninit() override
Un-initialize and free resources.
Definition Yolo.C:98
JEVOIS_DECLARE_PARAMETER(namefile, std::string, "Category names file, or empty to fetch it from the network " "config file (if relative, relative to dataroot)", "", ParamCateg)
Parameter.
int nboxes
Definition Yolo.H:147
void getInDims(int &w, int &h, int &c) const
Get input width, height, channels.
Definition Yolo.C:265
JEVOIS_DECLARE_PARAMETER(dataroot, std::string, "Root path for data, config, and weight files. If empty, use " "the module's path.", JEVOIS_SHARE_PATH "/darknet/yolo", ParamCateg)
Parameter.
void postInit() override
Initialize, configure and load the network in a thread.
Definition Yolo.C:46
JEVOIS_DECLARE_PARAMETER(datacfg, std::string, "Data configuration file (if relative, relative to dataroot)", "cfg/coco.data", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression intersection-over-union threshold in percent", 45.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
Parameter.
virtual ~Yolo()
Virtual destructor for safe inheritance.
Definition Yolo.C:38
int * map
Definition Yolo.H:150
Definition Yolo.H:33