JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
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// ####################################################################################################
23namespace 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// ####################################################################################################
44
45// ####################################################################################################
47{
48 if (jevois::python::hasattr(PythonWrapper::pyinst(), "freeze")) PythonWrapper::pyinst().attr("freeze")(doit);
49}
50
51// ####################################################################################################
52void jevois::dnn::NetworkPythonImpl::loadpy(std::string const & pypath)
53{
54 // Load the code and instantiate the python object:
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// ####################################################################################################
67
68// ####################################################################################################
69std::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// ####################################################################################################
86jevois::dnn::NetworkPython::NetworkPython(std::string const & instance) :
87 jevois::dnn::Network(instance)
88{
89 itsImpl = addSubComponent<jevois::dnn::NetworkPythonImpl>("pynet");
90}
91
92// ####################################################################################################
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// ####################################################################################################
109void jevois::dnn::NetworkPython::onParamChange(jevois::dnn::network::pynet const &, std::string const & newval)
110{
111 if (newval.empty() == false) itsImpl->loadpy(newval);
112}
113
114// ####################################################################################################
115std::vector<cv::Mat> jevois::dnn::NetworkPython::doprocess(std::vector<cv::Mat> const & outs,
116 std::vector<std::string> & info)
117{ return itsImpl->doprocess(outs, info); }
118
119// ####################################################################################################
121{ itsImpl->load(); }
122
123// ####################################################################################################
124std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::inputShapes()
125{ return jevois::dnn::parseTensorSpecs(getParamStringUnique("intensors")); }
126
127// ####################################################################################################
128std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::outputShapes()
129{ return jevois::dnn::parseTensorSpecs(getParamStringUnique("outtensors")); }
130
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
A component of a model hierarchy.
Definition Component.H:182
friend class Component
Allow Component and DynamicParameter to access our registry data, everyone else is locked out.
Helper class to run python code from C++.
void pythonload(std::string const &path)
Init from path if default constructor was used.
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
std::vector< cv::Mat > doprocess(std::vector< cv::Mat > const &outs, std::vector< std::string > &info)
void loadpy(std::string const &pypath)
std::vector< cv::Mat > doprocess(std::vector< cv::Mat > const &blobs, std::vector< std::string > &info) override
Process input blobs and obtain output blobs.
virtual ~NetworkPython()
Destructor.
void load() override
Load from disk.
std::vector< vsi_nn_tensor_attr_t > inputShapes() override
Get shapes of all input tensors.
void onParamChange(network::pynet const &param, std::string const &newval) override
std::shared_ptr< NetworkPythonImpl > itsImpl
std::vector< vsi_nn_tensor_attr_t > outputShapes() override
Get shapes of all output tensors.
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
NetworkPython(std::string const &instance)
Constructor.
Abstract class to represent a neural network.
Definition Network.H:162
virtual void freeze(bool doit)
Freeze/unfreeze parameters that users should not change while running.
Definition Network.C:27
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition Utils.C:387
boost::python::list pyVecToList(std::vector< T > const &v)
Helper to convert std::vector<T> to python list.
bool hasattr(boost::python::object &o, char const *name)
Check whether a boost::python::object has an attribute.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2