JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
NetworkPython.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 
20 #include <jevois/DNN/Utils.H>
21 
22 // ####################################################################################################
23 namespace jevois
24 {
25  namespace dnn
26  {
28  {
29  public:
31  void loadpy(std::string const & pypath);
32  virtual ~NetworkPythonImpl();
33  void freeze(bool doit);
34  void load();
35  std::vector<cv::Mat> doprocess(std::vector<cv::Mat> const & outs, std::vector<std::string> & info);
36  };
37  }
38 }
39 
40 // ####################################################################################################
41 // ####################################################################################################
43 { }
44 
45 // ####################################################################################################
47 {
48  if (jevois::python::hasattr(PythonWrapper::pyinst(), "freeze")) PythonWrapper::pyinst().attr("freeze")(doit);
49 }
50 
51 // ####################################################################################################
52 void jevois::dnn::NetworkPythonImpl::loadpy(std::string const & pypath)
53 {
54  // Load the code and instantiate the python object:
55  PythonWrapper::pythonload(JEVOIS_SHARE_PATH "/" + pypath);
56  LINFO("Loaded " << pypath);
57 
58  // Now that we are fully up and ready, call python module's init() function if implemented:
60 }
61 
62 // ####################################################################################################
64 {
65  PythonWrapper::pyinst().attr("load")();
66 }
67 
68 // ####################################################################################################
69 std::vector<cv::Mat> jevois::dnn::NetworkPythonImpl::doprocess(std::vector<cv::Mat> const & blobs,
70  std::vector<std::string> & info)
71 {
72  boost::python::list bloblst = jevois::python::pyVecToList(blobs);
73  boost::python::object ret = PythonWrapper::pyinst().attr("process")(bloblst);
74 
75  boost::python::tuple rett = boost::python::extract<boost::python::tuple>(ret);
76  if (boost::python::len(rett) != 2) LFATAL("Expected tuple(list of output blobs, list of info strings)");
77 
78  std::vector<cv::Mat> retm = jevois::python::pyListToVec<cv::Mat>(rett[0]);
79  info = jevois::python::pyListToVec<std::string>(rett[1]);
80 
81  return retm;
82 }
83 
84 // ####################################################################################################
85 // ####################################################################################################
86 jevois::dnn::NetworkPython::NetworkPython(std::string const & instance) :
87  jevois::dnn::Network(instance)
88 {
89  itsImpl = addSubComponent<jevois::dnn::NetworkPythonImpl>("pynet");
90 }
91 
92 // ####################################################################################################
94 { }
95 
96 // ####################################################################################################
98 {
99  // First our own params:
100  pynet::freeze(doit);
101 
102  // Then our python params:
103  itsImpl->freeze(doit);
104 
105  jevois::dnn::Network::freeze(doit); // base class parameters
106 }
107 
108 // ####################################################################################################
109 void jevois::dnn::NetworkPython::onParamChange(jevois::dnn::network::pynet const & JEVOIS_UNUSED_PARAM(param),
110  std::string const & newval)
111 {
112  if (newval.empty() == false) itsImpl->loadpy(newval);
113 }
114 
115 // ####################################################################################################
116 std::vector<cv::Mat> jevois::dnn::NetworkPython::doprocess(std::vector<cv::Mat> const & outs,
117  std::vector<std::string> & info)
118 { return itsImpl->doprocess(outs, info); }
119 
120 // ####################################################################################################
122 { itsImpl->load(); }
123 
124 // ####################################################################################################
125 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::inputShapes()
126 { return jevois::dnn::parseTensorSpecs(getParamStringUnique("intensors")); }
127 
128 // ####################################################################################################
129 std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::outputShapes()
130 { return jevois::dnn::parseTensorSpecs(getParamStringUnique("outtensors")); }
131 
jevois::dnn::NetworkPythonImpl::freeze
void freeze(bool doit)
Definition: NetworkPython.C:46
jevois::ParameterRegistry::Component
friend class Component
Allow Component and DynamicParameter to access our registry data, everyone else is locked out.
Definition: ParameterRegistry.H:51
jevois::dnn::NetworkPythonImpl
Definition: NetworkPython.C:27
PythonSupport.H
jevois::python::pyVecToList
boost::python::list pyVecToList(std::vector< T > const &v)
Helper to convert std::vector<T> to python list.
jevois::PythonWrapper::pyinst
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
Definition: PythonWrapper.C:85
jevois::Component
A component of a model hierarchy.
Definition: Component.H:181
Utils.H
jevois::dnn::NetworkPython::outputShapes
std::vector< vsi_nn_tensor_attr_t > outputShapes() override
Get shapes of all output tensors.
Definition: NetworkPython.C:129
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::NetworkPythonImpl::load
void load()
Definition: NetworkPython.C:63
jevois::dnn::parseTensorSpecs
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition: Utils.C:386
jevois::dnn::NetworkPython::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: NetworkPython.C:116
jevois
Definition: Concepts.dox:1
jevois::dnn::NetworkPythonImpl::~NetworkPythonImpl
virtual ~NetworkPythonImpl()
Definition: NetworkPython.C:42
jevois::dnn::NetworkPythonImpl::loadpy
void loadpy(std::string const &pypath)
Definition: NetworkPython.C:52
jevois::python::hasattr
bool hasattr(boost::python::object &o, char const *name)
Check whether a boost::python::object has an attribute.
Definition: PythonSupport.C:108
jevois::dnn::NetworkPython::inputShapes
std::vector< vsi_nn_tensor_attr_t > inputShapes() override
Get shapes of all input tensors.
Definition: NetworkPython.C:125
jevois::dnn::NetworkPythonImpl::doprocess
std::vector< cv::Mat > doprocess(std::vector< cv::Mat > const &outs, std::vector< std::string > &info)
Definition: NetworkPython.C:69
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::dnn::NetworkPython::~NetworkPython
virtual ~NetworkPython()
Destructor.
Definition: NetworkPython.C:93
jevois::PythonWrapper::pythonload
void pythonload(std::string const &path)
Init from path if default constructor was used.
Definition: PythonWrapper.C:34
jevois::dnn::NetworkPython::freeze
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
Definition: NetworkPython.C:97
jevois::dnn::NetworkPython::onParamChange
void onParamChange(network::pynet const &param, std::string const &newval) override
Definition: NetworkPython.C:109
jevois::dnn::NetworkPython::itsImpl
std::shared_ptr< NetworkPythonImpl > itsImpl
Definition: NetworkPython.H:63
jevois::PythonWrapper
Helper class to run python code from C++.
Definition: PythonWrapper.H:41
jevois::dnn::NetworkPython::NetworkPython
NetworkPython(std::string const &instance)
Constructor.
Definition: NetworkPython.C:86
NetworkPython.H
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194
jevois::dnn::NetworkPython::load
void load() override
Load from disk.
Definition: NetworkPython.C:121
jevois::dnn::Network
Abstract class to represent a neural network.
Definition: Network.H:160