JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
TensorFlow Class Reference

Identify an object using TensorFlow deep neural network. More...

#include <jevoisbase/Components/ObjectDetection/TensorFlow.H>

Inheritance diagram for TensorFlow:
Collaboration diagram for TensorFlow:

Classes

struct  JeVoisReporter
 

Public Member Functions

 TensorFlow (std::string const &instance)
 Constructor. More...
 
void postInit () override
 Initialize, configure and load the network in a thread. More...
 
virtual ~TensorFlow ()
 Virtual destructor for safe inheritance. More...
 
void postUninit () override
 Un-initialize and free resources. More...
 
float predict (cv::Mat const &cvimg, std::vector< jevois::ObjReco > &results)
 Processing function, results are stored internally in the underlying TensorFlow network object. More...
 
void getInDims (int &w, int &h, int &c)
 Get input width, height, channels. More...
 
- Public Member Functions inherited from jevois::Component
 Component (std::string const &instance)
 
virtual ~Component ()
 
std::shared_ptr< Comp > addSubComponent (std::string const &instance, Args &&...args)
 
void removeSubComponent (std::shared_ptr< Comp > &component)
 
void removeSubComponent (std::string const &instance, bool warnIfNotFound=true)
 
std::shared_ptr< Comp > getSubComponent (std::string const &instance) const
 
bool isTopLevel () const
 
Engineengine () const
 
bool initialized () const
 
const std::string & className () const
 
const std::string & instanceName () const
 
std::vector< std::string > setParamVal (std::string const &paramdescriptor, T const &val)
 
void setParamValUnique (std::string const &paramdescriptor, T const &val)
 
std::vector< std::pair< std::string, T > > getParamVal (std::string const &paramdescriptor) const
 
getParamValUnique (std::string const &paramdescriptor) const
 
std::vector< std::string > setParamString (std::string const &paramdescriptor, std::string const &val)
 
void setParamStringUnique (std::string const &paramdescriptor, std::string const &val)
 
std::vector< std::pair< std::string, std::string > > getParamString (std::string const &paramdescriptor) const
 
std::string getParamStringUnique (std::string const &paramdescriptor) const
 
void freezeParam (std::string const &paramdescriptor)
 
void unFreezeParam (std::string const &paramdescriptor)
 
void freezeAllParams ()
 
void unFreezeAllParams ()
 
std::string descriptor () const
 
void setParamsFromFile (std::string const &filename)
 
std::istream & setParamsFromStream (std::istream &is, std::string const &absfile)
 
virtual void paramInfo (std::shared_ptr< UserInterface > s, std::map< std::string, std::string > &categs, bool skipFrozen, std::string const &cname="", std::string const &pfx="")
 
void foreachParam (std::function< void(std::string const &compname, ParameterBase *p)> func, std::string const &cname="")
 
std::shared_ptr< DynamicParameter< T > > addDynamicParameter (std::string const &name, std::string const &description, T const &defaultValue, ParameterCategory const &category)
 
std::shared_ptr< DynamicParameter< T > > addDynamicParameter (std::string const &name, std::string const &description, T const &defaultValue, ValidValuesSpec< T > const &validValuesSpec, ParameterCategory const &category)
 
void setDynamicParameterCallback (std::string const &name, std::function< void(T const &)> cb, bool callnow=true)
 
void removeDynamicParameter (std::string const &name)
 
void setPath (std::string const &path)
 
std::filesystem::path absolutePath (std::filesystem::path const &path="")
 
std::shared_ptr< Comp > addSubComponent (std::string const &instance, Args &&...args)
 
void removeSubComponent (std::shared_ptr< Comp > &component)
 
void removeSubComponent (std::string const &instance, bool warnIfNotFound=true)
 
std::shared_ptr< Comp > getSubComponent (std::string const &instance) const
 
bool isTopLevel () const
 
Engineengine () const
 
bool initialized () const
 
const std::string & className () const
 
const std::string & instanceName () const
 
std::vector< std::string > setParamVal (std::string const &paramdescriptor, T const &val)
 
void setParamValUnique (std::string const &paramdescriptor, T const &val)
 
std::vector< std::pair< std::string, T > > getParamVal (std::string const &paramdescriptor) const
 
getParamValUnique (std::string const &paramdescriptor) const
 
std::vector< std::string > setParamString (std::string const &paramdescriptor, std::string const &val)
 
void setParamStringUnique (std::string const &paramdescriptor, std::string const &val)
 
std::vector< std::pair< std::string, std::string > > getParamString (std::string const &paramdescriptor) const
 
std::string getParamStringUnique (std::string const &paramdescriptor) const
 
void freezeParam (std::string const &paramdescriptor)
 
void unFreezeParam (std::string const &paramdescriptor)
 
void freezeAllParams ()
 
void unFreezeAllParams ()
 
std::string descriptor () const
 
void setParamsFromFile (std::string const &filename)
 
std::istream & setParamsFromStream (std::istream &is, std::string const &absfile)
 
virtual void paramInfo (std::shared_ptr< UserInterface > s, std::map< std::string, std::string > &categs, bool skipFrozen, std::string const &cname="", std::string const &pfx="")
 
void foreachParam (std::function< void(std::string const &compname, ParameterBase *p)> func, std::string const &cname="")
 
std::shared_ptr< DynamicParameter< T > > addDynamicParameter (std::string const &name, std::string const &description, T const &defaultValue, ParameterCategory const &category)
 
std::shared_ptr< DynamicParameter< T > > addDynamicParameter (std::string const &name, std::string const &description, T const &defaultValue, ValidValuesSpec< T > const &validValuesSpec, ParameterCategory const &category)
 
void setDynamicParameterCallback (std::string const &name, std::function< void(T const &)> cb, bool callnow=true)
 
void removeDynamicParameter (std::string const &name)
 
void setPath (std::string const &path)
 
std::filesystem::path absolutePath (std::filesystem::path const &path="")
 
- Public Member Functions inherited from jevois::ParameterRegistry
virtual ~ParameterRegistry ()
 

Public Attributes

std::vector< std::string > labels
 
size_t numlabels
 
std::unique_ptr< tflite::FlatBufferModel > model
 
std::unique_ptr< tflite::Interpreter > interpreter
 

Protected Member Functions

void onParamChange (tflow::netdir const &param, std::string const &newval) override
 
void onParamChange (tflow::dataroot const &param, std::string const &newval) override
 
void loadNet ()
 
void readLabelsFile (std::string const &fname)
 
template<class T >
void get_top_n (T *prediction, int prediction_size, std::vector< jevois::ObjReco > &top_results, bool input_floating)
 
- Protected Member Functions inherited from jevois::Component
virtual void preInit ()
 
virtual void preUninit ()
 
virtual void preInit ()
 
virtual void preUninit ()
 
- Protected Member Functions inherited from jevois::ParameterRegistry
void addParameter (ParameterBase *const param)
 
void removeParameter (ParameterBase *const param)
 
void callbackInitCall ()
 

Protected Attributes

std::future< void > itsReadyFut
 
std::atomic< bool > itsReady
 
std::atomic< bool > itsNeedReload
 
JeVoisReporter itsErrorReporter
 

Related Functions

(Note that these are not member functions.)

 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (netdir, std::string, "Network to load. This should be the name of a " "directory within JEVOIS:/share/tensorflow/ which should contain two files: " "model.tflite and labels.txt", "mobilenet_v1_224_android_quant_2017_11_08", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (dataroot, std::string, "Root path for data, config, and weight files. " "If empty, use the module's path.", JEVOIS_SHARE_PATH "/tensorflow", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER (top, unsigned int, "Max number of top-scoring predictions that score above thresh to return", 5, ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER (thresh, float, "Threshold (in percent confidence) above which predictions will be reported", 20.0F, jevois::Range< float >(0.0F, 100.0F), ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER (threads, int, "Number of parallel computation threads, or 0 for auto", 4, jevois::Range< int >(0, 1024), ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER (scorescale, float, "Scaling factors applied to recognition scores, useful for InceptionV3", 1.0F, ParamCateg)
 Parameter. More...
 

Detailed Description

Identify an object using TensorFlow deep neural network.

TensorFlow is a popular neural network framework. This component identifies the object in the given image crop. It returns the top scoring candidates.

See https://www.tensorflow.org

TensorFlow is a great deep learning and deep neural network framework. TensorFlow Lite (used here) is optimized for embedded systems like the small JeVois camera because it has a very small footprint and fewer dependencies than other deep neural network frameworks like MXNet, Theano, Keras, PyTorch, etc. In addition, the port of TensorFlow to JeVois includes acceleration using the ARM NEON multimedia instructions.

Definition at line 73 of file TensorFlow.H.

Constructor & Destructor Documentation

◆ TensorFlow()

TensorFlow::TensorFlow ( std::string const &  instance)

Constructor.

Definition at line 51 of file TensorFlow.C.

◆ ~TensorFlow()

TensorFlow::~TensorFlow ( )
virtual

Virtual destructor for safe inheritance.

Definition at line 56 of file TensorFlow.C.

References itsReadyFut, and JEVOIS_WAIT_GET_FUTURE.

Member Function Documentation

◆ get_top_n()

template<class T >
void TensorFlow::get_top_n ( T *  prediction,
int  prediction_size,
std::vector< jevois::ObjReco > &  top_results,
bool  input_floating 
)
protected

Definition at line 97 of file TensorFlow.C.

References labels, and value.

◆ getInDims()

void TensorFlow::getInDims ( int &  w,
int &  h,
int &  c 
)

Get input width, height, channels.

Throws std::logic_error if the network is still loading and not ready.

Definition at line 296 of file TensorFlow.C.

References h, interpreter, itsNeedReload, itsReady, loadNet(), and demo::w.

◆ loadNet()

void TensorFlow::loadNet ( )
protected

◆ onParamChange() [1/2]

void TensorFlow::onParamChange ( tflow::dataroot const &  param,
std::string const &  newval 
)
overrideprotected

Definition at line 67 of file TensorFlow.C.

References itsNeedReload.

◆ onParamChange() [2/2]

void TensorFlow::onParamChange ( tflow::netdir const &  param,
std::string const &  newval 
)
overrideprotected

Definition at line 63 of file TensorFlow.C.

References itsNeedReload.

◆ postInit()

void TensorFlow::postInit ( )
overridevirtual

Initialize, configure and load the network in a thread.

Any call to predict() will simply throw until the network is loaded and ready

Reimplemented from jevois::Component.

Definition at line 89 of file TensorFlow.C.

References loadNet().

◆ postUninit()

void TensorFlow::postUninit ( )
overridevirtual

Un-initialize and free resources.

Reimplemented from jevois::Component.

Definition at line 214 of file TensorFlow.C.

References itsReadyFut.

◆ predict()

float TensorFlow::predict ( cv::Mat const &  cvimg,
std::vector< jevois::ObjReco > &  results 
)

Processing function, results are stored internally in the underlying TensorFlow network object.

Expects an OpenCV byte RGB (CV_8UC3) or Gray (CV_8UC1) image, which may be further converted to float if that is what the network wants as input. The image dims and number of channels must match the network's input dims and channels. Returns the prediction time (neural net forward pass) in milliseconds. Throws std::logic_error if the network is still loading and not ready.

Definition at line 220 of file TensorFlow.C.

References interpreter, itsNeedReload, itsReady, LFATAL, loadNet(), numlabels, and demo::results.

◆ readLabelsFile()

void TensorFlow::readLabelsFile ( std::string const &  fname)
protected

Definition at line 71 of file TensorFlow.C.

References file, labels, LFATAL, LINFO, and numlabels.

Referenced by loadNet().

Friends And Related Function Documentation

◆ JEVOIS_DECLARE_PARAMETER() [1/4]

JEVOIS_DECLARE_PARAMETER ( scorescale  ,
float  ,
"Scaling factors applied to recognition  scores,
useful for InceptionV3"  ,
1.  0F,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER() [2/4]

JEVOIS_DECLARE_PARAMETER ( threads  ,
int  ,
"Number of parallel computation  threads,
or 0 for auto"  ,
,
jevois::Range< int >  0, 1024,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER() [3/4]

JEVOIS_DECLARE_PARAMETER ( thresh  ,
float  ,
"Threshold (in percent confidence) above which predictions will be reported"  ,
20.  0F,
jevois::Range< float >  0.0F, 100.0F,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER() [4/4]

JEVOIS_DECLARE_PARAMETER ( top  ,
unsigned int  ,
"Max number of top-scoring predictions that score above thresh to return"  ,
,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [1/2]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( dataroot  ,
std::string  ,
"Root path for  data,
config  ,
and weight files. " "If  empty,
use the module 's path."  ,
JEVOIS_SHARE_PATH "/tensorflow"  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [2/2]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( netdir  ,
std::string  ,
"Network to load. This should be the name of a " "directory within JEVOIS:/share/tensorflow/ which should contain two files: " "model.tflite and labels.txt"  ,
"mobilenet_v1_224_android_quant_2017_11_08"  ,
ParamCateg   
)
related

Parameter.

Member Data Documentation

◆ interpreter

◆ itsErrorReporter

JeVoisReporter TensorFlow::itsErrorReporter
protected

Definition at line 127 of file TensorFlow.H.

Referenced by loadNet().

◆ itsNeedReload

std::atomic<bool> TensorFlow::itsNeedReload
protected

Definition at line 120 of file TensorFlow.H.

Referenced by getInDims(), loadNet(), onParamChange(), and predict().

◆ itsReady

std::atomic<bool> TensorFlow::itsReady
protected

Definition at line 119 of file TensorFlow.H.

Referenced by getInDims(), loadNet(), and predict().

◆ itsReadyFut

std::future<void> TensorFlow::itsReadyFut
protected

Definition at line 118 of file TensorFlow.H.

Referenced by loadNet(), postUninit(), and ~TensorFlow().

◆ labels

◆ model

◆ numlabels

size_t TensorFlow::numlabels

Definition at line 104 of file TensorFlow.H.

Referenced by loadNet(), predict(), and readLabelsFile().


The documentation for this class was generated from the following files: