JeVoisBase  1.6
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Darknet.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 <jevois/Types/Enum.H>
23 #include <atomic>
24 
25 #include <nnpack.h>
26 
27 extern "C" {
28 #include <darknet.h>
29 }
30 
31 namespace dknet
32 {
33  static jevois::ParameterCategory const ParamCateg("Darknet Options");
34 
35  //! Enum \relates Darknet
36  JEVOIS_DEFINE_ENUM_CLASS(Net, (Reference) (Tiny) ) // STILL NOT READY (MobileNet) (CompMobileNet) )
37 
38  //! Parameter \relates Darknet
39  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(netw, Net, "Network to load. This meta-parameter sets parameters "
40  "dataroot, datacfg, cfgfile, weightfile, and namefile for the chosen network.",
41  Net::Tiny, Net_Values, ParamCateg);
42 
43  //! Parameter \relates Darknet
44  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(dataroot, std::string, "Root path for data, config, and weight files. "
45  "If empty, use the module's path.",
46  JEVOIS_SHARE_PATH "/darknet/single", ParamCateg);
47 
48  //! Parameter \relates Darknet
49  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(datacfg, std::string, "Data configuration file (if relative, relative to "
50  "dataroot)",
51  "cfg/imagenet1k.data", ParamCateg);
52 
53  //! Parameter \relates Darknet
54  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(cfgfile, std::string, "Network configuration file (if relative, relative to "
55  "dataroot)",
56  "cfg/tiny.cfg", ParamCateg);
57 
58  //! Parameter \relates Darknet
59  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(weightfile, std::string, "Network weights file (if relative, relative to "
60  "dataroot)",
61  "weights/tiny.weights", ParamCateg);
62 
63  //! Parameter \relates Darknet
64  JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(namefile, std::string, "Category names file, or empty to fetch it from the "
65  "network "
66  "config file (if relative, relative to dataroot)",
67  "", ParamCateg);
68 
69  //! Parameter \relates Darknet
70  JEVOIS_DECLARE_PARAMETER(top, unsigned int, "Max number of top-scoring predictions that score above thresh to return",
71  5, ParamCateg);
72 
73  //! Parameter \relates Darknet
74  JEVOIS_DECLARE_PARAMETER(thresh, float, "Threshold (in percent confidence) above which predictions will be reported",
75  20.0F, jevois::Range<float>(0.0F, 100.0F), ParamCateg);
76 
77  //! Parameter \relates Darknet
78  JEVOIS_DECLARE_PARAMETER(threads, int, "Number of parallel computation threads",
79  6, jevois::Range<int>(1, 1024), ParamCateg);
80 }
81 
82 //! Identify an object using Darknet deep neural network
83 /*! Darknet is a popular neural network framework. This component identifies the object in the given image crop. It
84  returns the top scoring candidates.
85 
86  See https://pjreddie.com/darknet
87 
88  Darknet is a great, bare-metal deep learning and deep neural network framework. It is great for embedded systems
89  like the small JeVois camera because it has a very small footprint and fewer dependencies than other deep neural
90  network frameworks like Tensorflow, MXNet, Theano, Keras, PyTorch, etc. In addition, the port of Darknet to JeVois
91  includes acceleration using the ARM NEON multimedia instructions through the popular NNPACK neural network
92  acceleration package.
93 
94  \ingroup Components */
95 class Darknet : public jevois::Component,
96  jevois::Parameter<dknet::netw, dknet::dataroot, dknet::datacfg, dknet::cfgfile,
97  dknet::weightfile, dknet::namefile, dknet::top, dknet::thresh, dknet::threads>
98 {
99  public:
100  //! Constructor
101  /*! if show_detail_params is false, the parameters dataroot, datacfg, cfgfile, weightfile, and namefile are hidden
102  and users can just use the parameter network to set various predefined networks. */
103  Darknet(std::string const & instance, bool show_detail_params = false);
104 
105  //! Initialize, configure and load the network in a thread
106  /*! Any call to process() will simply throw until the network is loaded and ready */
107  void postInit() override;
108 
109  //! Virtual destructor for safe inheritance
110  virtual ~Darknet();
111 
112  //! Un-initialize and free resources
113  void postUninit() override;
114 
115  //! Typedef for one result: score, category name
116  typedef std::pair<float, std::string> predresult;
117 
118  //! Return true if th enetwork is ready (i.e., not loading)
119  bool ready() const;
120 
121  //! Processing function, results are stored internally in the underlying Darknet network object
122  /*! This version expects an OpenCV RGB byte image which will be converted to float RGB planar. If the image dims do
123  not match the network's input layer dims, we here resize the network (beware that this only works if the network
124  is fully convolutional). Returns the prediction time (neural net forward pass) in milliseconds. Throws
125  std::logic_error if the network is still loading and not ready. */
126  float predict(cv::Mat const & cvimg, std::vector<predresult> & results);
127 
128  //! Processing function, results are stored internally in the underlying Darknet network object
129  /*! This version expects a Darknet image input, RGB float planar normalized to [0..1]. If the image dims do not
130  match the network's input layer dims, we here resize the network (beware that this only works if the network is
131  fully convolutional). Returns the prediction time (neural net forward pass) in milliseconds. Throws
132  std::logic_error if the network is still loading and not ready. */
133  float predict(image & im, std::vector<predresult> & results);
134 
135  //! Resize the network's input image dims
136  /*! This will prepare the network to receive inputs of the specified size. It is optional and will be called
137  automatically by predict() if the given image size does not match the current network input size. Note that this
138  only works with fully convolutional networks. Note that the number of channels cannot be changed at this
139  time. Throws std::logic_error if the network is still loading and not ready. */
140  void resizeInDims(int w, int h);
141 
142  //! Get input width, height, channels
143  /*! Throws std::logic_error if the network is still loading and not ready. */
144  void getInDims(int & w, int & h, int & c) const;
145 
146  // We leave these in the open in case one wants to access the probs, names, etc but just be careful with them
147  network * net;
148  char ** names = nullptr;
149  int classes;
150 
151  protected:
152  void onParamChange(dknet::netw const & param, dknet::Net const & newval);
153  void onParamChange(dknet::dataroot const & param, std::string const & newval);
154  void onParamChange(dknet::datacfg const & param, std::string const & newval);
155  void onParamChange(dknet::cfgfile const & param, std::string const & newval);
156  void onParamChange(dknet::weightfile const & param, std::string const & newval);
157  void onParamChange(dknet::namefile const & param, std::string const & newval);
158  void loadNet();
159 
160  std::future<void> itsReadyFut;
161  std::atomic<bool> itsReady;
163  std::atomic<bool> itsNeedReload;
164  };
Network to load This meta parameter sets parameters and namefile for the chosen Net_Values
Definition: Darknet.H:39
int classes
Definition: Darknet.H:149
std::atomic< bool > itsReady
Definition: Darknet.H:161
bool const itsShowDetailParams
Definition: Darknet.H:162
STL namespace.
Identify an object using Darknet deep neural network.
Definition: Darknet.H:95
network * net
Definition: Darknet.H:147
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
std::pair< float, std::string > predresult
Typedef for one result: score, category name.
Definition: Darknet.H:116
Network to load This meta parameter sets parameters and namefile for the chosen network
Definition: Darknet.H:39
Net
Definition: Darknet.H:39
std::atomic< bool > itsNeedReload
Definition: Darknet.H:163
void onParamChange(manager::loglevel const &param, manager::LogLevel const &newval)
Network to load This meta parameter sets parameters weightfile
Definition: Darknet.H:39
Definition: Darknet.H:31
std::future< void > itsReadyFut
Definition: Darknet.H:160
Network to load This meta parameter sets parameters dataroot
Definition: Darknet.H:39