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