JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Pipeline.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2021 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/Debug/Timer.H>
23#include <jevois/Types/Enum.H>
27
28#include <ovxlib/vsi_nn_pub.h> // for data types and quantization types
29
30namespace jevois
31{
32 class StdModule;
33 class RawImage;
34
35 namespace dnn
36 {
37 class PreProcessor;
38 class Network;
39 class PostProcessor;
40
41 namespace pipeline
42 {
43 static jevois::ParameterCategory const ParamCateg("DNN Pipeline Options");
44
45 //! Parameter \relates jevois::dnn::Pipeline
46 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zooroot, std::string, "Path where to find zoo files (.yml). "
47 "If not absolute, it is relative to this module's path",
48 JEVOIS_SHARE_PATH "/dnn", ParamCateg);
49
50 //! Parameter \relates jevois::dnn::Pipeline
51 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zoo, std::string, "Filename for neural network zoo file (.yml). "
52 "If not absolute, it is relative to zooroot",
53 "models.yml", ParamCateg);
54 //! Enum \relates jevois::dnn::Pipeline
55 JEVOIS_DEFINE_ENUM_CLASS(Filter, (All) (OpenCV) (TPU) (NPU) (VPU) (VPUX) (NPUX) (SPU) (ORT) );
56
57 //! Parameter \relates jevois::dnn::Pipeline
58 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(filter, Filter, "Filter to possibly only show as options in "
59 "the 'pipe' parameter some class of models from the zoo",
60 Filter::All, Filter_Values, ParamCateg);
61
62 //! Parameter \relates jevois::dnn::Pipeline
63 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pipe, std::string, "Pipeline to use, which should correspond to a "
64 "top-level entry in the zoo file. Network execution types are: OpenCV: "
65 "on CPU, NPU: on JeVois NPU accelerator, TPU: on Coral Edge TPU if "
66 "available, VPU: on MyriadX VPU if available, NPUX: on NPU via TimVX "
67 "OpenCV extension, VPUX: on CPU via ARM-Compute OpenVino emulation "
68 "of VPU, and SPU: on Hailo8 SPU if available.",
69 "", ParamCateg);
70
71 //! Enum \relates jevois::dnn::Pipeline
72 JEVOIS_DEFINE_ENUM_CLASS(PreProc, (Blob) (Python) );
73
74 //! Parameter \relates jevois::dnn::Pipeline
75 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(preproc, PreProc, "Pre-Processor to use, usually set automatically "
76 "by selecting a pipeline from the zoo file",
77 PreProc::Blob, PreProc_Values, ParamCateg);
78 //! Enum \relates jevois::dnn::Pipeline
79#ifdef JEVOIS_PRO
80 JEVOIS_DEFINE_ENUM_CLASS(NetType, (OpenCV) (ORT) (NPU) (TPU) (SPU) (Python) );
81#else
82 JEVOIS_DEFINE_ENUM_CLASS(NetType, (OpenCV) (Python) );
83#endif
84
85 //! Parameter \relates jevois::dnn::Pipeline
86 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(nettype, NetType, "Network runtime framework to use, usually set "
87 "automatically by selecting a pipeline from the zoo file",
88 NetType::OpenCV, NetType_Values, ParamCateg);
89 //! Enum \relates jevois::dnn::Pipeline
90 JEVOIS_DEFINE_ENUM_CLASS(PostProc, (Classify) (Detect) (DetectOBB) (Segment) (YuNet) (Pose) (Python) (Stub) );
91
92 //! Parameter \relates jevois::dnn::Pipeline
93 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(postproc, PostProc, "Post-Processor to use, usually set automatically "
94 "by selecting a pipeline from the zoo file",
95 PostProc::Classify, PostProc_Values, ParamCateg);
96 //! Enum \relates jevois::dnn::Pipeline
97 JEVOIS_DEFINE_ENUM_CLASS(Processing, (Sync) (Async) );
98
99 //! Parameter \relates jevois::dnn::Pipeline
100 JEVOIS_DECLARE_PARAMETER(processing, Processing, "Type of processing: Sync runs pre-processing, "
101 "network, and post-processing sequentially for every frame. Use for fast "
102 "networks only, otherwise it will slow down the GUI... Async runs the network in "
103 "a thread and should be used for networks slower than the camera framerate.",
104 Processing::Async, Processing_Values, ParamCateg);
105
106 //! Parameter \relates jevois::dnn::Pipeline
107 JEVOIS_DECLARE_PARAMETER(overlay, bool, "Show some pipeline info as an overlay over output or GUI video",
108 true, ParamCateg);
109
110 //! Parameter \relates jevois::dnn::Pipeline
111 JEVOIS_DECLARE_PARAMETER(paramwarn, bool, "Issue warning if parameters are specified in the zoo file for "
112 "a pipeline, but the pipeline's component is not using them. Warnings appear "
113 "in the console when the pipeline is loaded.",
114 true, ParamCateg);
115
116 //! Parameter \relates jevois::dnn::Pipeline
117 JEVOIS_DECLARE_PARAMETER(statsfile, std::string, "Append pre/net/post timing statistics in HTML table format "
118 "to the specified file if not empty. If path is relative, it is to " JEVOIS_SHARE_PATH,
119 "", ParamCateg);
120
121 //! Parameter \relates jevois::dnn::Pipeline
122 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(benchmark, bool, "Cycle through all networks specified by filter and, "
123 "for each, run it for a while and append pre/net/post timing statistics "
124 "in HTML table format to " JEVOIS_SHARE_PATH "/benchmark.html",
125 false, ParamCateg);
126
127 //! Parameter \relates jevois::dnn::Pipeline
128 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(extramodels, bool, "If true, show all available models, including older "
129 "ones, in the pipe list; otherwise, only those not marked 'extramodel' "
130 "in their model zoo definition",
131 false, ParamCateg);
132 }
133
134 //! Neural processing pipeline
135 /*! A neural processing pipeline in JeVois consists of:
136
137 - pre-processing an input image to extract one or more blobs used as input tensors to a deep neural network
138 - processing the input blobs through a deep network to obtain output blobs
139 - post-processing the output blobs to display results and send serial messages
140
141 A pipeline is typically configured by parsing a YAML config file (zoo file) that determines what kind of
142 pre-processing, network, and post-processing to use, and that sets the parameters for those. \ingroup dnn */
144 public jevois::Parameter<pipeline::zooroot, pipeline::zoo, pipeline::filter, pipeline::pipe,
145 pipeline::processing, pipeline::preproc, pipeline::nettype,
146 pipeline::postproc, pipeline::overlay, pipeline::paramwarn,
147 pipeline::statsfile, pipeline::benchmark, pipeline::extramodels>
148 {
149 public:
150 //! Constructor
151 Pipeline(std::string const & instance);
152
153 //! Destructor
154 virtual ~Pipeline();
155
156 //! Returns true when all three of preproc, net, and postproc are ready
157 bool ready() const;
158
159 //! Process an input image, send results to serial/image/gui
160 /*! If the network is not ready, no processing will occur. When helper is not null (i.e., using GUI display),
161 hide the information window when idle is true. This function catches all exceptions and reports them. */
162 void process(jevois::RawImage const & inimg, jevois::StdModule * mod,
163 jevois::RawImage * outimg, jevois::OptGUIhelper * helper, bool idle = false);
164
165 //! Freeze/unfreeze parameters that users should not change while running
166 void freeze(bool doit);
167
168 //! Get the latest recognition results, use with caution, not thread-safe
169 /*! This returns a reference to our internal vector of recognitions. That vector will get overwritten every time
170 process() is called. It is ok to use this after you have called process() on the current frame, but do not
171 hold this ref past the end of the current video frame. If you need to keep a persistent copy of the data,
172 make a deep copy of the vector. Throws if the post-processor is not of type Classify. */
173 std::vector<ObjReco> const & latestRecognitions() const;
174
175 //! Get the latest detection results, use with caution, not thread-safe
176 /*! This returns a reference to our internal vector of detections. That vector will get overwritten every time
177 process() is called. It is ok to use this after you have called process() on the current frame, but do not
178 hold this ref past the end of the current video frame. If you need to keep a persistent copy of the data,
179 make a deep copy of the vector. Throws if the post-processor is not of type Detect or Pose. */
180 std::vector<ObjDetect> const & latestDetections() const;
181
182 //! Get the latest oriented bounded box (OBB) detection results, use with caution, not thread-safe
183 /*! This returns a reference to our internal vector of detections. That vector will get overwritten every time
184 process() is called. It is ok to use this after you have called process() on the current frame, but do not
185 hold this ref past the end of the current video frame. If you need to keep a persistent copy of the data,
186 make a deep copy of the vector. Throws if the post-processor is not of type Detect or Pose. */
187 std::vector<ObjDetectOBB> const & latestDetectionsOBB() const;
188
189 //! Get the latest skeletons, use with caution, not thread-safe
190 /*! This returns a reference to our internal vector of skeletons. That vector will get overwritten every time
191 process() is called. It is ok to use this after you have called process() on the current frame, but do not
192 hold this ref past the end of the current video frame. If you need to keep a persistent copy of the data,
193 make a deep copy of the vector. Throws if the post-processor is not of type Pose. */
194 std::vector<PoseSkeleton> const & latestSkeletons() const;
195
196 protected:
197 void postInit() override;
198 void preUninit() override;
199 std::shared_ptr<PreProcessor> itsPreProcessor;
200 std::shared_ptr<Network> itsNetwork;
201 std::shared_ptr<PostProcessor> itsPostProcessor;
202 void reloadZoo(std::string const & root, std::string const & filt, std::string const & zoofile);
203
204 void onParamChange(pipeline::zooroot const & param, std::string const & val) override;
205 void onParamChange(pipeline::zoo const & param, std::string const & val) override;
206 void onParamChange(pipeline::filter const & param, pipeline::Filter const & val) override;
207 void onParamChange(pipeline::pipe const & param, std::string const & val) override;
208 void onParamChange(pipeline::nettype const & param, pipeline::NetType const & val) override;
209 void onParamChange(pipeline::preproc const & param, pipeline::PreProc const & val) override;
210 void onParamChange(pipeline::postproc const & param, pipeline::PostProc const & val) override;
211 void onParamChange(pipeline::benchmark const & param, bool const & val) override;
212 void onParamChange(pipeline::extramodels const & param, bool const & val) override;
213
214 void showInfo(std::vector<std::string> const & info, jevois::StdModule * mod,
215 jevois::RawImage * outimg, jevois::OptGUIhelper * helper, bool ovl, bool idle);
216 void asyncNetWait();
218#ifdef JEVOIS_PRO
219 // Allow user to peek into outputs. Caller must make sure helper is valid and idle is false
220 void showDataPeekWindow(jevois::GUIhelper * helper, bool refresh);
221#endif
222
223 private:
224 jevois::TimerOne itsTpre, itsTnet, itsTpost;
225 bool itsZooChanged = false;
226 std::future<std::vector<cv::Mat>> itsNetFut;
227 std::array<std::string, 3> itsProcTimes { "PreProc: -", "Network: -", "PstProc: -" };
228 std::array<double, 3> itsProcSecs { 0.0, 0.0, 0.0 };
229 std::vector<cv::Mat> itsBlobs, itsOuts;
230 std::vector<vsi_nn_tensor_attr_t> itsInputAttrs;
231 std::vector<std::string> itsNetInfo, itsAsyncNetInfo;
232 std::string itsAsyncNetworkTime = "Network: -";
233 double itsAsyncNetworkSecs = 0.0;
234 double itsSecsSum = 0.0, itsSecsAvg = 0.0;
235 int itsSecsSumNum = 0;
236 bool itsPipeThrew = false;
237 void scanZoo(std::filesystem::path const & zoofile, std::string const & filt, std::vector<std::string> & pipes,
238 std::string const & indent);
239 bool selectPipe(std::string const & zoofile, std::vector<std::string> const & tok);
240 void setZooParam(std::string const & name, std::string const & value, std::string const & zf,
241 cv::FileNode const & node);
242 int itsOutImgY = 0;
243
244 std::map<std::string, size_t> itsAccelerators;
245 std::vector<double> itsPreStats, itsNetStats, itsPstStats;
246 bool itsStatsWarmup = true;
247#ifdef JEVOIS_PRO
248 bool itsShowDataPeek = false;
249 int itsDataPeekOutIdx = 0;
250 bool itsDataPeekFreeze = false;
251 std::string itsDataPeekStr;
252#endif
253 };
254
255 } // namespace dnn
256} // namespace jevois
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
A component of a model hierarchy.
Definition Component.H:182
Helper class to assist modules in creating graphical and GUI elements.
Definition GUIhelper.H:133
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
Base class for a module that supports standardized serial messages.
Definition Module.H:234
Simple one-shot timer class.
Definition Timer.H:72
Neural processing pipeline.
Definition Pipeline.H:148
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(preproc, PreProc, "Pre-Processor to use, usually set automatically " "by selecting a pipeline from the zoo file", PreProc::Blob, PreProc_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(nettype, NetType, "Network runtime framework to use, usually set " "automatically by selecting a pipeline from the zoo file", NetType::OpenCV, NetType_Values, ParamCateg)
Parameter.
std::vector< ObjDetectOBB > const & latestDetectionsOBB() const
Get the latest oriented bounded box (OBB) detection results, use with caution, not thread-safe.
Definition Pipeline.C:184
JEVOIS_DEFINE_ENUM_CLASS(PostProc,(Classify)(Detect)(DetectOBB)(Segment)(YuNet)(Pose)(Python)(Stub))
Enum.
bool checkAsyncNetComplete()
Definition Pipeline.C:707
void showInfo(std::vector< std::string > const &info, jevois::StdModule *mod, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool ovl, bool idle)
Definition Pipeline.C:1078
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(extramodels, bool, "If true, show all available models, including older " "ones, in the pipe list otherwise, only those not marked 'extramodel' " "in their model zoo definition", false, ParamCateg)
Parameter.
std::shared_ptr< Network > itsNetwork
Definition Pipeline.H:200
JEVOIS_DECLARE_PARAMETER(statsfile, std::string, "Append pre/net/post timing statistics in HTML table format " "to the specified file if not empty. If path is relative, it is to " JEVOIS_SHARE_PATH, "", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(processing, Processing, "Type of processing: Sync runs pre-processing, " "network, and post-processing sequentially for every frame. Use for fast " "networks only, otherwise it will slow down the GUI... Async runs the network in " "a thread and should be used for networks slower than the camera framerate.", Processing::Async, Processing_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(filter, Filter, "Filter to possibly only show as options in " "the 'pipe' parameter some class of models from the zoo", Filter::All, Filter_Values, ParamCateg)
Parameter.
void preUninit() override
Called before all sub-Components are uninit()ed.
Definition Pipeline.C:149
void showDataPeekWindow(jevois::GUIhelper *helper, bool refresh)
Definition Pipeline.C:1113
std::vector< ObjReco > const & latestRecognitions() const
Get the latest recognition results, use with caution, not thread-safe.
Definition Pipeline.C:163
void process(jevois::RawImage const &inimg, jevois::StdModule *mod, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool idle=false)
Process an input image, send results to serial/image/gui.
Definition Pipeline.C:722
void onParamChange(pipeline::zooroot const &param, std::string const &val) override
Definition Pipeline.C:227
virtual ~Pipeline()
Destructor.
Definition Pipeline.C:156
bool ready() const
Returns true when all three of preproc, net, and postproc are ready.
Definition Pipeline.C:701
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pipe, std::string, "Pipeline to use, which should correspond to a " "top-level entry in the zoo file. Network execution types are: OpenCV: " "on CPU, NPU: on JeVois NPU accelerator, TPU: on Coral Edge TPU if " "available, VPU: on MyriadX VPU if available, NPUX: on NPU via TimVX " "OpenCV extension, VPUX: on CPU via ARM-Compute OpenVino emulation " "of VPU, and SPU: on Hailo8 SPU if available.", "", ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(Processing,(Sync)(Async))
Enum.
JEVOIS_DEFINE_ENUM_CLASS(Filter,(All)(OpenCV)(TPU)(NPU)(VPU)(VPUX)(NPUX)(SPU)(ORT))
Enum.
void freeze(bool doit)
Freeze/unfreeze parameters that users should not change while running.
Definition Pipeline.C:130
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zoo, std::string, "Filename for neural network zoo file (.yml). " "If not absolute, it is relative to zooroot", "models.yml", ParamCateg)
Parameter.
std::vector< PoseSkeleton > const & latestSkeletons() const
Get the latest skeletons, use with caution, not thread-safe.
Definition Pipeline.C:193
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(postproc, PostProc, "Post-Processor to use, usually set automatically " "by selecting a pipeline from the zoo file", PostProc::Classify, PostProc_Values, ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(PreProc,(Blob)(Python))
Enum.
std::shared_ptr< PostProcessor > itsPostProcessor
Definition Pipeline.H:201
std::vector< ObjDetect > const & latestDetections() const
Get the latest detection results, use with caution, not thread-safe.
Definition Pipeline.C:172
void reloadZoo(std::string const &root, std::string const &filt, std::string const &zoofile)
std::shared_ptr< PreProcessor > itsPreProcessor
Definition Pipeline.H:199
void postInit() override
Called after all sub-Components are init()ed.
Definition Pipeline.C:142
JEVOIS_DEFINE_ENUM_CLASS(NetType,(OpenCV)(ORT)(NPU)(TPU)(SPU)(Python))
Enum.
JEVOIS_DECLARE_PARAMETER(paramwarn, bool, "Issue warning if parameters are specified in the zoo file for " "a pipeline, but the pipeline's component is not using them. Warnings appear " "in the console when the pipeline is loaded.", true, ParamCateg)
Parameter.
void onParamChange(pipeline::filter const &param, pipeline::Filter const &val) override
JEVOIS_DECLARE_PARAMETER(overlay, bool, "Show some pipeline info as an overlay over output or GUI video", true, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(benchmark, bool, "Cycle through all networks specified by filter and, " "for each, run it for a while and append pre/net/post timing statistics " "in HTML table format to " JEVOIS_SHARE_PATH "/benchmark.html", false, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zooroot, std::string, "Path where to find zoo files (.yml). " "If not absolute, it is relative to this module's path", JEVOIS_SHARE_PATH "/dnn", ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(imx290)(os08a10)(ar0234))
Enum for different sensor models.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2
A category to which multiple ParameterDef definitions can belong.