JeVois  1.18
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 }
36 
37 // ####################################################################################################
38 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkOpenCV::inputShapes()
39 {
41 }
42 
43 // ####################################################################################################
44 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkOpenCV::outputShapes()
45 {
47 }
48 
49 // ####################################################################################################
51 {
52  // Need to nuke the network first if it exists or we could run out of RAM:
53  if (itsNet.empty() == false) itsNet = cv::dnn::Net();
54 
55  std::string const m = jevois::absolutePath(dataroot::get(), model::get());
56  std::string const c = jevois::absolutePath(dataroot::get(), config::get());
57 
58  if (config::get().empty()) LINFO("Loading " << m << " ..."); else LINFO("Loading " << m << " / " << c << " ...");
59 
60  // Create and load the network:
61  itsNet = cv::dnn::readNet(m, c);
62 
63  switch(backend::get())
64  {
65 #ifdef JEVOIS_PRO
66  case network::Backend::OpenCV: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV); break;
67  case network::Backend::InferenceEngine: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_INFERENCE_ENGINE); break;
68  case network::Backend::TimVX: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_TIMVX); break;
69 #else
70  case network::Backend::Default: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT); break;
71 #endif
72  }
73 
74  switch(target::get())
75  {
76  case network::Target::CPU: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_CPU); break;
77 #ifdef JEVOIS_PRO
78  case network::Target::OpenCL: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL); break;
79  case network::Target::OpenCL_FP16: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL_FP16); break;
80  case network::Target::Myriad: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_MYRIAD); break;
81  case network::Target::NPU: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_NPU); break;
82 #endif
83  }
84  LINFO("Backend: " << backend::get() << ", Target: " << target::get());
85 
86  // Get names of the network's output layers:
87  itsOutNames = itsNet.getUnconnectedOutLayersNames();
88  int i = 0;
89  for (auto const & s : itsOutNames) LINFO("Output layer " << i++ << ": " << s);
90 }
91 
92 // ####################################################################################################
93 std::vector<cv::Mat> jevois::dnn::NetworkOpenCV::doprocess(std::vector<cv::Mat> const & blobs,
94  std::vector<std::string> & info)
95 {
96  if (blobs.size() != 1) LFATAL("Expecting exactly one input blob");
97 
98  if (itsNet.empty()) LFATAL("Internal inconsistency");
99 
100  itsNet.setInput(blobs[0]);
101  std::vector<cv::Mat> outs;
102  itsNet.forward(outs, itsOutNames);
103 
104  // Show some info:
105  if (itsFLOPS.empty())
106  {
107  std::vector<cv::dnn::MatShape> inshapes;
108  for (size_t i = 0; i < blobs.size(); ++i)
109  {
110  cv::dnn::MatShape s; cv::MatSize const & ms = blobs[i].size;
111  for (int k = 0; k < ms.dims(); ++k) s.emplace_back(ms[k]);
112  inshapes.emplace_back(s);
113  }
114  itsFLOPS = jevois::num2str(itsNet.getFLOPS(inshapes)) + "OPS";
115  }
116 
117  info.emplace_back("Forward Network: " + itsFLOPS);
118 
119  return outs;
120 }
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:44
Utils.H
jevois::dnn::Network::waitBeforeDestroy
void waitBeforeDestroy()
If network is currently loading, wait until that is done before destroying.
Definition: Network.C:37
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:308
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:365
jevois::dnn::NetworkOpenCV::load
void load() override
Load from disk.
Definition: NetworkOpenCV.C:50
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:38
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:93
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:486
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