JeVois  1.21
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>
24#include <ovxlib/vsi_nn_pub.h> // for data types and quantization types
25
26namespace jevois
27{
28 class StdModule;
29 class RawImage;
30
31 namespace dnn
32 {
33 class PreProcessor;
34 class Network;
35 class PostProcessor;
36
37 namespace pipeline
38 {
39 static jevois::ParameterCategory const ParamCateg("DNN Pipeline Options");
40
41 //! Parameter \relates jevois::dnn::Pipeline
42 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zooroot, std::string, "Path where to find zoo files (.yml). "
43 "If not absolute, it is relative to this module's path",
44 JEVOIS_SHARE_PATH "/dnn", ParamCateg);
45
46 //! Parameter \relates jevois::dnn::Pipeline
47 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(zoo, std::string, "Filename for neural network zoo file (.yml). "
48 "If not absolute, it is relative to zooroot",
49 "models.yml", ParamCateg);
50 //! Enum \relates jevois::dnn::Pipeline
51 JEVOIS_DEFINE_ENUM_CLASS(Filter, (All) (OpenCV) (TPU) (NPU) (VPU) (VPUX) (NPUX) (SPU) (ORT) );
52
53 //! Parameter \relates jevois::dnn::Pipeline
54 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(filter, Filter, "Filter to possibly only show as options in "
55 "the 'pipe' parameter some class of models from the zoo",
56 Filter::All, Filter_Values, ParamCateg);
57
58 //! Parameter \relates jevois::dnn::Pipeline
59 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(pipe, std::string, "Pipeline to use, which should correspond to a "
60 "top-level entry in the zoo file. Network execution types are: OpenCV: "
61 "on CPU, NPU: on JeVois NPU accelerator, TPU: on Coral Edge TPU if "
62 "available, VPU: on MyriadX VPU if available, NPUX: on NPU via TimVX "
63 "OpenCV extension, VPUX: on CPU via ARM-Compute OpenVino emulation "
64 "of VPU, and SPU: on Hailo8 SPU if available.",
65 "", ParamCateg);
66
67 //! Enum \relates jevois::dnn::Pipeline
68 JEVOIS_DEFINE_ENUM_CLASS(PreProc, (Blob) (Python) );
69
70 //! Parameter \relates jevois::dnn::Pipeline
71 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(preproc, PreProc, "Pre-Processor to use, usually set automatically "
72 "by selecting a pipeline from the zoo file",
73 PreProc::Blob, PreProc_Values, ParamCateg);
74 //! Enum \relates jevois::dnn::Pipeline
75#ifdef JEVOIS_PRO
76 JEVOIS_DEFINE_ENUM_CLASS(NetType, (OpenCV) (ORT) (NPU) (TPU) (SPU) (Python) );
77#else
78 JEVOIS_DEFINE_ENUM_CLASS(NetType, (OpenCV) (Python) );
79#endif
80
81 //! Parameter \relates jevois::dnn::Pipeline
82 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(nettype, NetType, "Network runtime framework to use, usually set "
83 "automatically by selecting a pipeline from the zoo file",
84 NetType::OpenCV, NetType_Values, ParamCateg);
85 //! Enum \relates jevois::dnn::Pipeline
86 JEVOIS_DEFINE_ENUM_CLASS(PostProc, (Classify) (Detect) (Segment) (YuNet) (Python) (Stub) );
87
88 //! Parameter \relates jevois::dnn::Pipeline
89 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(postproc, PostProc, "Post-Processor to use, usually set automatically "
90 "by selecting a pipeline from the zoo file",
91 PostProc::Classify, PostProc_Values, ParamCateg);
92 //! Enum \relates jevois::dnn::Pipeline
93 JEVOIS_DEFINE_ENUM_CLASS(Processing, (Sync) (Async) );
94
95 //! Parameter \relates jevois::dnn::Pipeline
96 JEVOIS_DECLARE_PARAMETER(processing, Processing, "Type of processing: Sync runs pre-processing, "
97 "network, and post-processing sequentially for every frame. Use for fast "
98 "networks only, otherwise it will slow down the GUI... Async runs the network in "
99 "a thread and should be used for networks slower than the camera framerate.",
100 Processing::Async, Processing_Values, ParamCateg);
101
102 //! Parameter \relates jevois::dnn::Pipeline
103 JEVOIS_DECLARE_PARAMETER(overlay, bool, "Show some pipeline info as an overlay over output or GUI video",
104 true, ParamCateg);
105
106 //! Parameter \relates jevois::dnn::Pipeline
107 JEVOIS_DECLARE_PARAMETER(paramwarn, bool, "Issue warning if parameters are specified in the zoo file for "
108 "a pipeline, but the pipeline's component is not using them. Warnings appear "
109 "in the console when the pipeline is loaded.",
110 true, ParamCateg);
111
112 //! Parameter \relates jevois::dnn::Pipeline
113 JEVOIS_DECLARE_PARAMETER(statsfile, std::string, "Append pre/net/post timing statistics in HTML table format "
114 "to the specified file if not empty. If path is relative, it is to " JEVOIS_SHARE_PATH,
115 "", ParamCateg);
116
117 //! Parameter \relates jevois::dnn::Pipeline
118 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(benchmark, bool, "Cycle through all networks specified by filter and, "
119 "for each, run it for a while and append pre/net/post timing statistics "
120 "in HTML table format to " JEVOIS_SHARE_PATH "/benchmark.html",
121 false, ParamCateg);
122 }
123
124 //! Neural processing pipeline
125 /*! A neural processing pipeline in JeVois consists of:
126
127 - pre-processing an input image to extract one or more blobs used as input tensors to a deep neural network
128 - processing the input blobs through a deep network to obtain output blobs
129 - post-processing the output blobs to display results and send serial messages
130
131 A pipeline is typically configured by parsing a YAML config file (zoo file) that determines what kind of
132 pre-processing, network, and post-processing to use, and that sets the parameters for those. \ingroup dnn */
134 public jevois::Parameter<pipeline::zooroot, pipeline::zoo, pipeline::filter, pipeline::pipe,
135 pipeline::processing, pipeline::preproc, pipeline::nettype,
136 pipeline::postproc, pipeline::overlay, pipeline::paramwarn,
137 pipeline::statsfile, pipeline::benchmark>
138 {
139 public:
140 //! Constructor
141 Pipeline(std::string const & instance);
142
143 //! Destructor
144 virtual ~Pipeline();
145
146 //! Returns true when all three of preproc, net, and postproc are ready
147 bool ready() const;
148
149 //! Process an input image, send results to serial/image/gui
150 /*! If the network is not ready, no processing will occur. When helper is not null (i.e., using GUI display),
151 hide the information window when idle is true. This function catches all exceptions and reports them. */
152 void process(jevois::RawImage const & inimg, jevois::StdModule * mod,
153 jevois::RawImage * outimg, jevois::OptGUIhelper * helper, bool idle = false);
154
155 //! Freeze/unfreeze parameters that users should not change while running
156 void freeze(bool doit);
157
158 protected:
159 void postInit() override;
160 void preUninit() override;
161 std::shared_ptr<PreProcessor> itsPreProcessor;
162 std::shared_ptr<Network> itsNetwork;
163 std::shared_ptr<PostProcessor> itsPostProcessor;
164 void reloadZoo(std::string const & root, std::string const & filt, std::string const & zoofile);
165
166 void onParamChange(pipeline::zooroot const & param, std::string const & val) override;
167 void onParamChange(pipeline::zoo const & param, std::string const & val) override;
168 void onParamChange(pipeline::filter const & param, pipeline::Filter const & val) override;
169 void onParamChange(pipeline::pipe const & param, std::string const & val) override;
170 void onParamChange(pipeline::nettype const & param, pipeline::NetType const & val) override;
171 void onParamChange(pipeline::preproc const & param, pipeline::PreProc const & val) override;
172 void onParamChange(pipeline::postproc const & param, pipeline::PostProc const & val) override;
173 void onParamChange(pipeline::benchmark const & param, bool const & val) override;
174
175 void showInfo(std::vector<std::string> const & info, jevois::StdModule * mod,
176 jevois::RawImage * outimg, jevois::OptGUIhelper * helper, bool ovl, bool idle);
177 void asyncNetWait();
179#ifdef JEVOIS_PRO
180 // Allow user to peek into outputs. Caller must make sure helper is valid and idle is false
181 void showDataPeekWindow(jevois::GUIhelper * helper, bool refresh);
182#endif
183
184 private:
185 jevois::TimerOne itsTpre, itsTnet, itsTpost;
186 bool itsZooChanged = false;
187 std::future<std::vector<cv::Mat>> itsNetFut;
188 std::array<std::string, 3> itsProcTimes { "PreProc: -", "Network: -", "PstProc: -" };
189 std::array<double, 3> itsProcSecs { 0.0, 0.0, 0.0 };
190 std::vector<cv::Mat> itsBlobs, itsOuts;
191 std::vector<vsi_nn_tensor_attr_t> itsInputAttrs;
192 std::vector<std::string> itsNetInfo, itsAsyncNetInfo;
193 std::string itsAsyncNetworkTime = "Network: -";
194 double itsAsyncNetworkSecs = 0.0;
195 double itsSecsSum = 0.0, itsSecsAvg = 0.0;
196 int itsSecsSumNum = 0;
197 bool itsPipeThrew = false;
198 void scanZoo(std::filesystem::path const & zoofile, std::string const & filt, std::vector<std::string> & pipes,
199 std::string const & indent);
200 bool selectPipe(std::string const & zoofile, std::vector<std::string> const & tok);
201 void setZooParam(std::string const & name, std::string const & value, std::string const & zf,
202 cv::FileNode const & node);
203 int itsOutImgY = 0;
204
205 std::map<std::string, size_t> itsAccelerators;
206 std::vector<double> itsPreStats, itsNetStats, itsPstStats;
207 bool itsStatsWarmup = true;
208#ifdef JEVOIS_PRO
209 bool itsShowDataPeek = false;
210 int itsDataPeekOutIdx = 0;
211 bool itsDataPeekFreeze = false;
212 std::string itsDataPeekStr;
213#endif
214 };
215
216 } // namespace dnn
217} // 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:70
Neural processing pipeline.
Definition Pipeline.H:138
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.
bool checkAsyncNetComplete()
Definition Pipeline.C:644
void showInfo(std::vector< std::string > const &info, jevois::StdModule *mod, jevois::RawImage *outimg, jevois::OptGUIhelper *helper, bool ovl, bool idle)
Definition Pipeline.C:1002
std::shared_ptr< Network > itsNetwork
Definition Pipeline.H:162
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:147
void showDataPeekWindow(jevois::GUIhelper *helper, bool refresh)
Definition Pipeline.C:1037
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:659
void onParamChange(pipeline::zooroot const &param, std::string const &val) override
Definition Pipeline.C:186
virtual ~Pipeline()
Destructor.
Definition Pipeline.C:154
bool ready() const
Returns true when all three of preproc, net, and postproc are ready.
Definition Pipeline.C:638
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.
JEVOIS_DEFINE_ENUM_CLASS(PostProc,(Classify)(Detect)(Segment)(YuNet)(Python)(Stub))
Enum.
void freeze(bool doit)
Freeze/unfreeze parameters that users should not change while running.
Definition Pipeline.C:128
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.
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:163
void reloadZoo(std::string const &root, std::string const &filt, std::string const &zoofile)
std::shared_ptr< PreProcessor > itsPreProcessor
Definition Pipeline.H:161
void postInit() override
Called after all sub-Components are init()ed.
Definition Pipeline.C:140
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.