JeVois  1.17
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  LINFO("Loading " << m << " / " << c << " ...");
58 
59  // Create and load the network:
60  itsNet = cv::dnn::readNet(m, c);
61 
62  switch(backend::get())
63  {
64 #ifdef JEVOIS_PRO
65  case network::Backend::OpenCV: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV); break;
66  case network::Backend::InferenceEngine: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_INFERENCE_ENGINE); break;
67 #else
68  case network::Backend::Default: itsNet.setPreferableBackend(cv::dnn::DNN_BACKEND_DEFAULT); break;
69 #endif
70  }
71 
72  switch(target::get())
73  {
74  case network::Target::CPU: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_CPU); break;
75 #ifdef JEVOIS_PRO
76  case network::Target::OpenCL: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL); break;
77  case network::Target::OpenCL_FP16: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL_FP16); break;
78  case network::Target::Myriad: itsNet.setPreferableTarget(cv::dnn::DNN_TARGET_MYRIAD); break;
79 #endif
80  }
81  LINFO("Backend: " << backend::get() << ", Target: " << target::get());
82 
83  // Get names of the network's output layers:
84  itsOutNames = itsNet.getUnconnectedOutLayersNames();
85  for (auto const & s : itsOutNames) LINFO("Output layer: " << s);
86 }
87 
88 // ####################################################################################################
89 std::vector<cv::Mat> jevois::dnn::NetworkOpenCV::doprocess(std::vector<cv::Mat> const & blobs,
90  std::vector<std::string> & info)
91 {
92  if (blobs.size() != 1) LFATAL("Expecting exactly one input blob");
93 
94  if (itsNet.empty()) LFATAL("Internal inconsistency");
95 
96  itsNet.setInput(blobs[0]);
97  std::vector<cv::Mat> outs;
98  itsNet.forward(outs, itsOutNames);
99 
100  // Show some info:
101  std::vector<cv::dnn::MatShape> inshapes;
102  for (size_t i = 0; i < blobs.size(); ++i)
103  {
104  cv::dnn::MatShape s; cv::MatSize const & ms = blobs[i].size;
105  for (int k = 0; k < ms.dims(); ++k) s.emplace_back(ms[k]);
106  inshapes.emplace_back(s);
107  }
108  info.emplace_back("Forward Network FLOPS: " + std::to_string(itsNet.getFLOPS(inshapes)));
109 
110  return outs;
111 }
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:27
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:260
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.
Definition: Log.H:217
jevois::absolutePath
std::string absolutePath(std::string const &root, std::string const &path)
Compute an absolute path from two paths.
Definition: Utils.C:347
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:89
jevois::to_string
std::string to_string(T const &val)
Convert from type to string.
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:181
jevois::dnn::NetworkOpenCV::~NetworkOpenCV
virtual ~NetworkOpenCV()
Destructor.
Definition: NetworkOpenCV.C:22