JeVois  1.17
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 // ####################################################################################################
28 {
29  // Do not destroy a network that is loading, and do not throw...
30  size_t count = 0;
31  while (itsLoading.load())
32  {
33  std::this_thread::sleep_for(std::chrono::milliseconds(5));
34  try { if (ready()) break; } catch (...) { }
35  if (count++ == 200) { LINFO("Waiting for network load to complete..."); count = 0; }
36  }
37 }
38 
39 // ####################################################################################################
41 {
42  // If we are loaded, we are ready to process:
43  if (itsLoaded.load()) return true;
44 
45  // If we are loading, check whether loading is complete or threw, otherwise return false as we keep loading:
46  if (itsLoading.load())
47  {
48  if (itsLoadFut.valid() && itsLoadFut.wait_for(std::chrono::milliseconds(2)) == std::future_status::ready)
49  {
50  try { itsLoadFut.get(); itsLoaded.store(true); itsLoading.store(false); LINFO("Network loaded."); return true; }
51  catch (...) { itsLoading.store(false); jevois::warnAndRethrowException(); }
52  }
53  return false;
54  }
55 
56  // Otherwise, trigger an async load:
57  itsLoading.store(true);
58  itsLoadFut = jevois::async(std::bind(&jevois::dnn::Network::load, this));
59  LINFO("Loading network...");
60 
61  return false;
62 }
63 
64 // ####################################################################################################
65 std::vector<cv::Mat> jevois::dnn::Network::process(std::vector<cv::Mat> const & blobs,
66  std::vector<std::string> & info)
67 {
68  if (ready() == false) LFATAL("Network is not ready");
69 
70  // Show info about input tensors:
71  info.emplace_back("* Input Tensors");
72  for (size_t i = 0; i < blobs.size(); ++i) info.emplace_back("- " + jevois::dnn::shapestr(blobs[i]));
73 
74  // Run processing on the derived class:
75  info.emplace_back("* Network");
76  std::vector<cv::Mat> outs = doprocess(blobs, info);
77 
78  // Show info about output tensors:
79  info.emplace_back("* Output Tensors");
80  for (size_t i = 0; i < outs.size(); ++i) info.emplace_back("- " + jevois::dnn::shapestr(outs[i]));
81 
82  return outs;
83 }
Async.H
jevois::dnn::Network::ready
bool ready()
Returns true when network is ready to run (loaded and initialized)
Definition: Network.C:40
jevois::dnn::Network::~Network
virtual ~Network()
Destructor.
Definition: Network.C:23
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:193
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:65
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::Network::load
virtual void load()=0
Load from disk.
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:105
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition: Log.H:217
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.
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:181