JeVois  1.18
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Network.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 
18 #include <jevois/DNN/Network.H>
19 #include <jevois/DNN/Utils.H>
20 #include <jevois/Util/Async.H>
21 
22 // ####################################################################################################
24 { }
25 
26 // ####################################################################################################
27 void jevois::dnn::Network::onParamChange(network::outreshape const & JEVOIS_UNUSED_PARAM(param),
28  std::string const & val)
29 {
30  itsReshape.clear();
31  if (val.empty()) return;
32 
33  itsReshape = jevois::dnn::parseTensorSpecs(val);
34 }
35 
36 // ####################################################################################################
38 {
39  // Do not destroy a network that is loading, and do not throw...
40  size_t count = 0;
41  while (itsLoading.load())
42  {
43  std::this_thread::sleep_for(std::chrono::milliseconds(5));
44  try { if (ready()) break; } catch (...) { }
45  if (count++ == 200) { LINFO("Waiting for network load to complete..."); count = 0; }
46  }
47 }
48 
49 // ####################################################################################################
51 {
52  // If we are loaded, we are ready to process:
53  if (itsLoaded.load()) return true;
54 
55  // If we are loading, check whether loading is complete or threw, otherwise return false as we keep loading:
56  if (itsLoading.load())
57  {
58  if (itsLoadFut.valid() && itsLoadFut.wait_for(std::chrono::milliseconds(2)) == std::future_status::ready)
59  {
60  try { itsLoadFut.get(); itsLoaded.store(true); itsLoading.store(false); LINFO("Network loaded."); return true; }
61  catch (...) { itsLoading.store(false); jevois::warnAndRethrowException(); }
62  }
63  return false;
64  }
65 
66  // Otherwise, trigger an async load:
67  itsLoading.store(true);
68  itsLoadFut = jevois::async(std::bind(&jevois::dnn::Network::load, this));
69  LINFO("Loading network...");
70 
71  return false;
72 }
73 
74 // ####################################################################################################
75 std::vector<cv::Mat> jevois::dnn::Network::process(std::vector<cv::Mat> const & blobs,
76  std::vector<std::string> & info)
77 {
78  if (ready() == false) LFATAL("Network is not ready");
79 
80  // Show info about input tensors:
81  info.emplace_back("* Input Tensors");
82  for (size_t i = 0; i < blobs.size(); ++i) info.emplace_back("- " + jevois::dnn::shapestr(blobs[i]));
83 
84  // Run processing on the derived class:
85  info.emplace_back("* Network");
86  std::string const c = comment::get();
87  if (c.empty() == false) info.emplace_back(c);
88 
89  std::vector<cv::Mat> outs = doprocess(blobs, info);
90 
91  // Show info about output tensors:
92  info.emplace_back("* Output Tensors");
93  for (size_t i = 0; i < outs.size(); ++i) info.emplace_back("- " + jevois::dnn::shapestr(outs[i]));
94 
95  // Possibly reshape the tensors:
96  if (itsReshape.empty() == false)
97  {
98  if (itsReshape.size() != outs.size())
99  LFATAL("Received " << outs.size() << " but outreshape has " << itsReshape.size() << " entries");
100 
101  info.emplace_back("* Reshaped Output Tensors");
102  for (size_t i = 0; i < outs.size(); ++i)
103  {
104  outs[i] = outs[i].reshape(1, jevois::dnn::attrdims(itsReshape[i]));
105  info.emplace_back("- " + jevois::dnn::shapestr(outs[i]));
106  }
107  }
108 
109  return outs;
110 }
jevois::imu::get
Data collection mode RAW means that the latest available raw data is returned each time get() is called
jevois::async
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
Async execution using a thread pool.
Async.H
jevois::dnn::Network::ready
bool ready()
Returns true when network is ready to run (loaded and initialized)
Definition: Network.C:50
jevois::dnn::Network::~Network
virtual ~Network()
Destructor.
Definition: Network.C:23
jevois::dnn::Network::onParamChange
void onParamChange(network::outreshape const &param, std::string const &val) override
Definition: Network.C:27
Network.H
jevois::warnAndRethrowException
void warnAndRethrowException(std::string const &prefix="")
Convenience function to catch an exception, issue some LERROR (depending on type),...
Definition: Log.C:203
jevois::dnn::Network::process
std::vector< cv::Mat > process(std::vector< cv::Mat > const &blobs, std::vector< std::string > &info)
Process input blobs and obtain output blobs.
Definition: Network.C:75
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::Network::load
virtual void load()=0
Load from disk.
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:308
jevois::dnn::shapestr
std::string shapestr(cv::Mat const &m)
Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE.
Definition: Utils.C:104
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::dnn::attrdims
std::vector< int > attrdims(vsi_nn_tensor_attr_t const &attr)
Get a tensor dims as a vector of int, useful to construct a matching cv::Mat.
Definition: Utils.C:399
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194