JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
NetworkOpenCV.C
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 
19 #include <jevois/DNN/Utils.H>
20 
21 // ####################################################################################################
23 { waitBeforeDestroy(); }
24 
25 // ####################################################################################################
27 {
28  dataroot::freeze(doit);
29  config::freeze(doit);
30  model::freeze(doit);
31  backend::freeze(doit);
32  target::freeze(doit);
33  intensors::freeze(doit);
34  outtensors::freeze(doit);
35  jevois::dnn::Network::freeze(doit); // base class parameters
36 }
37 
38 // ####################################################################################################
39 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkOpenCV::inputShapes()
40 {
42 }
43 
44 // ####################################################################################################
45 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkOpenCV::outputShapes()
46 {
48 }
49 
50 // ####################################################################################################
52 {
53  // Need to nuke the network first if it exists or we could run out of RAM:
54  if (itsNet.empty() == false) itsNet = cv::dnn::Net();
55 
56  std::string const m = jevois::absolutePath(dataroot::get(), model::get());
57  std::string const c = jevois::absolutePath(dataroot::get(), config::get());
58 
59  if (config::get().empty()) LINFO("Loading " << m << " ..."); else LINFO("Loading " << m << " / " << c << " ...");
60 
61  // Create and load the network:
62  itsNet = cv::dnn::readNet(m, c);
63 
64  switch(backend::get())
65  {
66 #ifdef JEVOIS_PRO
67  case network::Backend::OpenCV: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV); break;
68  case network::Backend::InferenceEngine: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_INFERENCE_ENGINE); break;
69  case network::Backend::TimVX: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_TIMVX); break;
70 #else
71  case network::Backend::Default: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT); break;
72 #endif
73  }
74 
75  switch(target::get())
76  {
77  case network::Target::CPU: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_CPU); break;
78 #ifdef JEVOIS_PRO
79  case network::Target::OpenCL: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL); break;
80  case network::Target::OpenCL_FP16: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL_FP16); break;
81  case network::Target::Myriad: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_MYRIAD); break;
82  case network::Target::NPU: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_NPU); break;
83 #endif
84  }
85  LINFO("Backend: " << backend::get() << ", Target: " << target::get());
86 
87  // Get names of the network's output layers:
88  itsOutNames = itsNet.getUnconnectedOutLayersNames();
89  int i = 0;
90  for (auto const & s : itsOutNames) LINFO("Output layer " << i++ << ": " << s);
91 }
92 
93 // ####################################################################################################
94 std::vector<cv::Mat> jevois::dnn::NetworkOpenCV::doprocess(std::vector<cv::Mat> const & blobs,
95  std::vector<std::string> & info)
96 {
97  if (blobs.size() != 1) LFATAL("Expecting exactly one input blob");
98 
99  if (itsNet.empty()) LFATAL("Internal inconsistency");
100 
101  itsNet.setInput(blobs[0]);
102  std::vector<cv::Mat> outs;
103  itsNet.forward(outs, itsOutNames);
104 
105  // Show some info:
106  if (itsFLOPS.empty())
107  {
108  std::vector<cv::dnn::MatShape> inshapes;
109  for (size_t i = 0; i < blobs.size(); ++i)
110  {
111  cv::dnn::MatShape s; cv::MatSize const & ms = blobs[i].size;
112  for (int k = 0; k < ms.dims(); ++k) s.emplace_back(ms[k]);
113  inshapes.emplace_back(s);
114  }
115  itsFLOPS = jevois::num2str(itsNet.getFLOPS(inshapes)) + "OPS";
116  }
117 
118  info.emplace_back("Forward Network: " + itsFLOPS);
119 
120  return outs;
121 }
jevois::imu::get
Data collection mode RAW means that the latest available raw data is returned each time get() is called
jevois::dnn::NetworkOpenCV::outputShapes
virtual std::vector< vsi_nn_tensor_attr_t > outputShapes() override
Get shapes of all output tensors.
Definition: NetworkOpenCV.C:45
Utils.H
jevois::dnn::Network::waitBeforeDestroy
void waitBeforeDestroy()
If network is currently loading, wait until that is done before destroying.
Definition: Network.C:45
jevois::dnn::Network::freeze
virtual void freeze(bool doit)
Freeze/unfreeze parameters that users should not change while running.
Definition: Network.C:27
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:386
jevois::absolutePath
std::filesystem::path absolutePath(std::filesystem::path const &root, std::filesystem::path const &path)
Compute an absolute path from two paths.
Definition: Utils.C:385
jevois::dnn::NetworkOpenCV::load
void load() override
Load from disk.
Definition: NetworkOpenCV.C:51
NetworkOpenCV.H
jevois::dnn::NetworkOpenCV::inputShapes
virtual std::vector< vsi_nn_tensor_attr_t > inputShapes() override
Get shapes of all input tensors.
Definition: NetworkOpenCV.C:39
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::dnn::NetworkOpenCV::doprocess
std::vector< cv::Mat > doprocess(std::vector< cv::Mat > const &blobs, std::vector< std::string > &info) override
Process input blobs and obtain output blobs.
Definition: NetworkOpenCV.C:94
jevois::num2str
std::string num2str(double n)
Report a number with variable multipliers (K, M, G, T, P, E, Z, Y), with precision of 2 decimal point...
Definition: Utils.C:511
jevois::dnn::NetworkOpenCV::freeze
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
Definition: NetworkOpenCV.C:26
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194
jevois::dnn::NetworkOpenCV::~NetworkOpenCV
virtual ~NetworkOpenCV()
Destructor.
Definition: NetworkOpenCV.C:22