JeVoisBase  1.5
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
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 
26 extern "C" {
27 #include <darknet.h>
28 }
29 
30 namespace jevois { class StdModule; }
31 
32 namespace 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 
42  //! Parameter \relates Yolo
43  JEVOIS_DECLARE_PARAMETER(datacfg, std::string, "Data configuration file (if relative, relative to dataroot)",
44  "cfg/voc.data", ParamCateg);
45 
46  //! Parameter \relates Yolo
47  JEVOIS_DECLARE_PARAMETER(cfgfile, std::string, "Network configuration file (if relative, relative to dataroot)",
48  "cfg/tiny-yolo-voc.cfg", ParamCateg);
49 
50  //! Parameter \relates Yolo
51  JEVOIS_DECLARE_PARAMETER(weightfile, std::string, "Network weights file (if relative, relative to dataroot)",
52  "weights/tiny-yolo-voc.weights", ParamCateg);
53 
54  //! Parameter \relates Yolo
55  JEVOIS_DECLARE_PARAMETER(namefile, std::string, "Category names file, or empty to fetch it from the network "
56  "config file (if relative, relative to dataroot)",
57  "", ParamCateg);
58 
59  //! Parameter \relates Yolo
60  JEVOIS_DECLARE_PARAMETER(nms, float, "Non-maximum suppression in percent confidence",
61  40.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
62 
63  //! Parameter \relates Yolo
64  JEVOIS_DECLARE_PARAMETER(thresh, float, "Detection threshold in percent confidence",
65  24.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
66 
67  //! Parameter \relates Yolo
68  JEVOIS_DECLARE_PARAMETER(hierthresh, float, "Hierarchical detection threshold in percent confidence",
69  50.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
70 
71  //! Parameter \relates Yolo
72  JEVOIS_DECLARE_PARAMETER(threads, int, "Number of parallel computation threads",
73  6, jevois::Range<int>(1, 1024), ParamCateg);
74 }
75 
76 //! Detect multiple objects in scenes using the Darknet YOLO deep neural network
77 /*! Darknet is a popular neural network framework, and YOLO is a very interesting network that detects all objects in a
78  scene in one pass. This component detects all instances of any of the objects it knows about (determined by the
79  network structure, labels, dataset used for training, and weights obtained) in the image that is given to is.
80 
81  See https://pjreddie.com/darknet/yolo/
82 
83  Darknet is a great, bare-metal deep learning and deep neural network framework. It is great for embedded systems
84  like the small JeVois camera because it has a very small footprint and fewer dependencies than other deep neural
85  network frameworks like Tensorflow, MXNet, Theano, Keras, PyTorch, etc. In addition, the port of Darknet to JeVois
86  includes acceleration using the ARM NEON multimedia instructions through the popular NNPACK neural network
87  acceleration package.
88 
89  \ingroup Components */
90 class Yolo : public jevois::Component,
91  jevois::Parameter<yolo::dataroot, yolo::datacfg, yolo::cfgfile, yolo::weightfile, yolo::namefile,
92  yolo::nms, yolo::thresh, yolo::hierthresh, yolo::threads>
93 {
94  public:
95  //! Constructor
96  Yolo(std::string const & instance);
97 
98  //! Initialize, configure and load the network in a thread
99  /*! Any call to process() will simply throw until the network is loaded and ready */
100  void postInit() override;
101 
102  //! Virtual destructor for safe inheritance
103  virtual ~Yolo();
104 
105  //! Un-initialize and free resources
106  void postUninit() override;
107 
108  //! Processing function, results are stored internally in the underlying Darknet network object
109  /*! This version expects an OpenCV RGB byte image which will be converted to float RGB planar, and which may be
110  letterboxed if necessary to fit network input dims. Returns the prediction time (neural net forward pass) in
111  milliseconds. Throws std::logic_error if the network is still loading and not ready. */
112  float predict(cv::Mat const & cvimg);
113 
114  //! Processing function, results are stored internally in the underlying Darknet network object
115  /*! This version expects a Darknet image input, RGB float planar normalized to [0..1], with same dims as network
116  input dims. Returns the prediction time (neural net forward pass) in milliseconds. Throws std::logic_error if
117  the network is still loading and not ready. */
118  float predict(image & im);
119 
120  //! Compute the boxes
121  /*! You must have called predict() first for this to not violently crash. */
122  void computeBoxes(int inw, int inh);
123 
124  //! Draw the detections
125  /*! You must have called computeBoxes() first for this to not violently crash. */
126  void drawDetections(jevois::RawImage & outimg, int inw, int inh, int xoff, int yoff);
127 
128  //! Send serial messages about detections
129  /*! You must have called computeBoxes() first for this to not violently crash. The module given should be the owner
130  of this component, we will use it to actually send each serial message using some variant of
131  jevois::Module::sendSerial(). */
132  void sendSerial(jevois::StdModule * mod, int inw, int inh, unsigned long frame);
133 
134  //! Resize the network's input image dims
135  /*! This will prepare the network to receive inputs of the specified size. It is optional and will be called
136  automatically by predict() if the given image size does not match the current network input size. Note that this
137  only works with fully convolutional networks. Note that the number of channels cannot be changed at this
138  time. Throws std::logic_error if the network is still loading and not ready. */
139  void resizeInDims(int w, int h);
140 
141  //! Get input width, height, channels
142  /*! Throws std::logic_error if the network is still loading and not ready. */
143  void getInDims(int & w, int & h, int & c) const;
144 
145  // We leave these in the open in case one wants to access the probs, names, etc but just be careful with them
147  char ** names = nullptr;
148  box * boxes = nullptr;
149  float ** probs = nullptr;
150  int classes;
151 
152  protected:
153  std::future<void> itsReadyFut;
154  std::atomic<bool> itsReady;
155  };
Definition: Yolo.H:32
std::atomic< bool > itsReady
Definition: Yolo.H:154
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
int classes
Definition: Yolo.H:150
Network to load This meta parameter sets parameters cfgfile
Definition: Darknet.H:39
Network to load This meta parameter sets parameters datacfg
Definition: Darknet.H:39
Network to load This meta parameter sets parameters and namefile for the chosen network
Definition: Darknet.H:39
std::future< void > itsReadyFut
Definition: Yolo.H:153
Network to load This meta parameter sets parameters weightfile
Definition: Darknet.H:39
network net
Definition: Yolo.H:146
Detect multiple objects in scenes using the Darknet YOLO deep neural network.
Definition: Yolo.H:90
Network to load This meta parameter sets parameters dataroot
Definition: Darknet.H:39