JeVois  1.22
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/Core/Engine.H>
21#include <jevois/DNN/Utils.H>
22
23// ####################################################################################################
24namespace jevois
25{
26 namespace dnn
27 {
29 {
30 public:
32 void loadpy(std::string const & pypath);
33 virtual ~NetworkPythonImpl();
34 void freeze(bool doit);
35 void load();
36 std::vector<cv::Mat> doprocess(std::vector<cv::Mat> const & outs, std::vector<std::string> & info);
37 };
38 }
39}
40
41// ####################################################################################################
42// ####################################################################################################
47
48// ####################################################################################################
50{
51 if (jevois::python::hasattr(PythonWrapper::pyinst(), "freeze")) PythonWrapper::pyinst().attr("freeze")(doit);
52}
53
54// ####################################################################################################
55void jevois::dnn::NetworkPythonImpl::loadpy(std::string const & pypath)
56{
57 // Load the code and instantiate the python object:
59 LINFO("Loaded " << pypath);
60
61 // Now that we are fully up and ready, call python module's init() function if implemented:
63}
64
65// ####################################################################################################
67{
69 else LFATAL("No load() method provided. It is required, please add it to your Python network processor.");
70}
71
72// ####################################################################################################
73std::vector<cv::Mat> jevois::dnn::NetworkPythonImpl::doprocess(std::vector<cv::Mat> const & blobs,
74 std::vector<std::string> & info)
75{
76 if (jevois::python::hasattr(PythonWrapper::pyinst(), "process") == false)
77 LFATAL("No process() method provided. It is required, please add it to your Python network processor.");
78 boost::python::list bloblst = jevois::python::pyVecToList(blobs);
79 boost::python::object ret = PythonWrapper::pyinst().attr("process")(bloblst);
80
81 boost::python::tuple rett = boost::python::extract<boost::python::tuple>(ret);
82 if (boost::python::len(rett) != 2) LFATAL("Expected tuple(list of output blobs, list of info strings)");
83
84 std::vector<cv::Mat> retm = jevois::python::pyListToVec<cv::Mat>(rett[0]);
85 info = jevois::python::pyListToVec<std::string>(rett[1]);
86
87 return retm;
88}
89
90// ####################################################################################################
91// ####################################################################################################
92jevois::dnn::NetworkPython::NetworkPython(std::string const & instance) :
93 jevois::dnn::Network(instance)
94{
95 itsImpl = addSubComponent<jevois::dnn::NetworkPythonImpl>("pynet");
96}
97
98// ####################################################################################################
101
102// ####################################################################################################
104{
105 // First our own params:
106 pynet::freeze(doit);
107
108 // Then our python params:
109 itsImpl->freeze(doit);
110
111 jevois::dnn::Network::freeze(doit); // base class parameters
112}
113
114// ####################################################################################################
115void jevois::dnn::NetworkPython::onParamChange(jevois::dnn::network::pynet const &, std::string const & newval)
116{
117 if (newval.empty() == false) itsImpl->loadpy(newval);
118}
119
120// ####################################################################################################
121std::vector<cv::Mat> jevois::dnn::NetworkPython::doprocess(std::vector<cv::Mat> const & outs,
122 std::vector<std::string> & info)
123{ return itsImpl->doprocess(outs, info); }
124
125// ####################################################################################################
127{ itsImpl->load(); }
128
129// ####################################################################################################
130std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::inputShapes()
131{ return jevois::dnn::parseTensorSpecs(getParamStringUnique("intensors")); }
132
133// ####################################################################################################
134std::vector<vsi_nn_tensor_attr_t> jevois::dnn::NetworkPython::outputShapes()
135{ return jevois::dnn::parseTensorSpecs(getParamStringUnique("outtensors")); }
136
#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
void unRegisterPythonComponent(Component *comp)
Unregister a component as linked to some python code, used by dynamic params created in python.
Definition Engine.C:3016
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:212
virtual void freeze(bool doit)
Freeze/unfreeze parameters that users should not change while running.
Definition Network.C:32
#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:411
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