JeVoisBase  1.6
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Darknet Class Reference

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

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

Inheritance diagram for Darknet:
Collaboration diagram for Darknet:

Public Types

typedef std::pair< float, std::string > predresult
 Typedef for one result: score, category name. More...
 

Public Member Functions

 Darknet (std::string const &instance, bool show_detail_params=false)
 Constructor. More...
 
void postInit () override
 Initialize, configure and load the network in a thread. More...
 
virtual ~Darknet ()
 Virtual destructor for safe inheritance. More...
 
void postUninit () override
 Un-initialize and free resources. More...
 
bool ready () const
 Return true if th enetwork is ready (i.e., not loading) More...
 
float predict (cv::Mat const &cvimg, std::vector< predresult > &results)
 Processing function, results are stored internally in the underlying Darknet network object. More...
 
float predict (image &im, std::vector< predresult > &results)
 Processing function, results are stored internally in the underlying Darknet network object. More...
 
void resizeInDims (int w, int h)
 Resize the network's input image dims. More...
 
void getInDims (int &w, int &h, int &c) const
 Get input width, height, channels. More...
 
- Public Member Functions inherited from jevois::Component
 Component (std::string const &instance)
 
virtual ~Component ()
 
std::shared_ptr< ComponentgetSubComponent (std::string const &instance) const
 
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
 
bool initialized () const
 
std::string const & className () const
 
std::string const & 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)
 
void setPath (std::string const &path)
 
std::string absolutePath (std::string 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
 
bool initialized () const
 
std::string const & className () const
 
std::string const & 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)
 
void setPath (std::string const &path)
 
std::string absolutePath (std::string const &path="")
 
- Public Member Functions inherited from jevois::ParameterRegistry
virtual ~ParameterRegistry ()
 

Public Attributes

network * net
 
char ** names = nullptr
 
int classes
 

Protected Member Functions

void onParamChange (dknet::netw const &param, dknet::Net const &newval)
 
void onParamChange (dknet::dataroot const &param, std::string const &newval)
 
void onParamChange (dknet::datacfg const &param, std::string const &newval)
 
void onParamChange (dknet::cfgfile const &param, std::string const &newval)
 
void onParamChange (dknet::weightfile const &param, std::string const &newval)
 
void onParamChange (dknet::namefile const &param, std::string const &newval)
 
void loadNet ()
 
- 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
 
bool const itsShowDetailParams
 
std::atomic< bool > itsNeedReload
 

Related Functions

(Note that these are not member functions.)

 JEVOIS_DEFINE_ENUM_CLASS (Net,(Reference)(Tiny)) JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(netw
 Enum. 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 "/darknet/single", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (datacfg, std::string, "Data configuration file (if relative, relative to " "dataroot)", "cfg/imagenet1k.data", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (cfgfile, std::string, "Network configuration file (if relative, relative to " "dataroot)", "cfg/tiny.cfg", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (weightfile, std::string, "Network weights file (if relative, relative to " "dataroot)", "weights/tiny.weights", ParamCateg)
 Parameter. More...
 
 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK (namefile, std::string, "Category names file, or empty to fetch it from the " "network " "config file (if relative, relative to dataroot)", "", 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", 6, jevois::Range< int >(1, 1024), ParamCateg)
 Parameter. More...
 

Detailed Description

Identify an object using Darknet deep neural network.

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

See https://pjreddie.com/darknet

Darknet is a great, bare-metal deep learning and deep neural network framework. It is great 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 Tensorflow, MXNet, Theano, Keras, PyTorch, etc. In addition, the port of Darknet to JeVois includes acceleration using the ARM NEON multimedia instructions through the popular NNPACK neural network acceleration package.

Definition at line 95 of file Darknet.H.

Member Typedef Documentation

◆ predresult

typedef std::pair<float, std::string> Darknet::predresult

Typedef for one result: score, category name.

Definition at line 116 of file Darknet.H.

Constructor & Destructor Documentation

◆ Darknet()

Darknet::Darknet ( std::string const &  instance,
bool  show_detail_params = false 
)

Constructor.

if show_detail_params is false, the parameters dataroot, datacfg, cfgfile, weightfile, and namefile are hidden and users can just use the parameter network to set various predefined networks.

Definition at line 23 of file Darknet.C.

References freeze(), and itsShowDetailParams.

◆ ~Darknet()

Darknet::~Darknet ( )
virtual

Virtual destructor for safe inheritance.

Definition at line 44 of file Darknet.C.

Member Function Documentation

◆ getInDims()

void Darknet::getInDims ( int &  w,
int &  h,
int &  c 
) const

Get input width, height, channels.

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

Definition at line 280 of file Darknet.C.

References itsReady, and net.

◆ loadNet()

void Darknet::loadNet ( )
protected

Definition at line 124 of file Darknet.C.

References jevois::Component::absolutePath(), classes, itsNeedReload, itsReady, itsReadyFut, LFATAL, LINFO, names, and net.

Referenced by postInit(), and predict().

◆ onParamChange() [1/6]

void Darknet::onParamChange ( dknet::netw const &  param,
dknet::Net const &  newval 
)
protected

Definition at line 52 of file Darknet.C.

References freeze(), itsShowDetailParams, and unFreeze().

◆ onParamChange() [2/6]

void Darknet::onParamChange ( dknet::dataroot const &  param,
std::string const &  newval 
)
protected

Definition at line 110 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [3/6]

void Darknet::onParamChange ( dknet::datacfg const &  param,
std::string const &  newval 
)
protected

Definition at line 111 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [4/6]

void Darknet::onParamChange ( dknet::cfgfile const &  param,
std::string const &  newval 
)
protected

Definition at line 112 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [5/6]

void Darknet::onParamChange ( dknet::weightfile const &  param,
std::string const &  newval 
)
protected

Definition at line 113 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [6/6]

void Darknet::onParamChange ( dknet::namefile const &  param,
std::string const &  newval 
)
protected

Definition at line 114 of file Darknet.C.

References itsNeedReload.

◆ postInit()

void Darknet::postInit ( )
overridevirtual

Initialize, configure and load the network in a thread.

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

Reimplemented from jevois::Component.

Definition at line 117 of file Darknet.C.

References loadNet().

◆ postUninit()

void Darknet::postUninit ( )
overridevirtual

Un-initialize and free resources.

Reimplemented from jevois::Component.

Definition at line 189 of file Darknet.C.

References classes, itsReadyFut, names, and net.

◆ predict() [1/2]

float Darknet::predict ( cv::Mat const &  cvimg,
std::vector< predresult > &  results 
)

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

This version expects an OpenCV RGB byte image which will be converted to float RGB planar. If the image dims do not match the network's input layer dims, we here resize the network (beware that this only works if the network is fully convolutional). 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 209 of file Darknet.C.

References itsNeedReload, itsReady, LFATAL, and loadNet().

◆ predict() [2/2]

float Darknet::predict ( image &  im,
std::vector< predresult > &  results 
)

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

This version expects a Darknet image input, RGB float planar normalized to [0..1]. If the image dims do not match the network's input layer dims, we here resize the network (beware that this only works if the network is fully convolutional). 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 236 of file Darknet.C.

References itsNeedReload, itsReady, loadNet(), names, and net.

◆ ready()

bool Darknet::ready ( ) const

Return true if th enetwork is ready (i.e., not loading)

Definition at line 205 of file Darknet.C.

References itsNeedReload, and itsReady.

◆ resizeInDims()

void Darknet::resizeInDims ( int  w,
int  h 
)

Resize the network's input image dims.

This will prepare the network to receive inputs of the specified size. It is optional and will be called automatically by predict() if the given image size does not match the current network input size. Note that this only works with fully convolutional networks. Note that the number of channels cannot be changed at this time. Throws std::logic_error if the network is still loading and not ready.

Definition at line 273 of file Darknet.C.

References itsReady, and net.

Friends And Related Function Documentation

◆ JEVOIS_DECLARE_PARAMETER() [1/3]

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

Parameter.

◆ JEVOIS_DECLARE_PARAMETER() [2/3]

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() [3/3]

JEVOIS_DECLARE_PARAMETER ( threads  ,
int  ,
"Number of parallel computation threads"  ,
,
jevois::Range< int >  1, 1024,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [1/5]

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 "/darknet/single"  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [2/5]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( datacfg  ,
std::string  ,
"Data configuration file (if relative, relative to " "dataroot)"  ,
"cfg/imagenet1k.data"  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [3/5]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( cfgfile  ,
std::string  ,
"Network configuration file (if relative, relative to " "dataroot)"  ,
"cfg/tiny.cfg"  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [4/5]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( weightfile  ,
std::string  ,
"Network weights file (if relative, relative to " "dataroot)"  ,
"weights/tiny.weights"  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [5/5]

JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK ( namefile  ,
std::string  ,
"Category names  file,
or empty to fetch it from the " "network " "config file(if relative, relative to dataroot)"  ,
""  ,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DEFINE_ENUM_CLASS()

JEVOIS_DEFINE_ENUM_CLASS ( Net  ,
(Reference)(Tiny)   
)
related

Enum.

Parameter

Member Data Documentation

◆ classes

int Darknet::classes

Definition at line 149 of file Darknet.H.

Referenced by loadNet(), and postUninit().

◆ itsNeedReload

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

Definition at line 163 of file Darknet.H.

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

◆ itsReady

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

Definition at line 161 of file Darknet.H.

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

◆ itsReadyFut

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

Definition at line 160 of file Darknet.H.

Referenced by loadNet(), and postUninit().

◆ itsShowDetailParams

bool const Darknet::itsShowDetailParams
protected

Definition at line 162 of file Darknet.H.

Referenced by Darknet(), and onParamChange().

◆ names

char** Darknet::names = nullptr

Definition at line 148 of file Darknet.H.

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

◆ net

network* Darknet::net

Definition at line 147 of file Darknet.H.

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


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