JeVoisBase  1.20
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 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...
 
float predict (cv::Mat const &cvimg, std::vector< jevois::ObjReco > &results)
 Processing function, results are stored internally in the underlying Darknet network object. More...
 
float predict (image &im, std::vector< jevois::ObjReco > &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)
 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

network * net
 
char ** names = nullptr
 
int classes
 

Protected Member Functions

void onParamChange (dknet::netw const &param, dknet::Net const &newval) override
 
void onParamChange (dknet::dataroot const &param, std::string const &newval) override
 
void onParamChange (dknet::datacfg const &param, std::string const &newval) override
 
void onParamChange (dknet::cfgfile const &param, std::string const &newval) override
 
void onParamChange (dknet::weightfile const &param, std::string const &newval) override
 
void onParamChange (dknet::namefile const &param, std::string const &newval) override
 
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
 
const bool 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 96 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 28 of file Darknet.C.

References freeze(), and itsShowDetailParams.

◆ ~Darknet()

Darknet::~Darknet ( )
virtual

Virtual destructor for safe inheritance.

Definition at line 49 of file Darknet.C.

References itsReadyFut, and JEVOIS_WAIT_GET_FUTURE.

Member Function Documentation

◆ getInDims()

void Darknet::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 274 of file Darknet.C.

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

◆ loadNet()

void Darknet::loadNet ( )
protected

◆ onParamChange() [1/6]

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

Definition at line 103 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [2/6]

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

Definition at line 102 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [3/6]

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

Definition at line 101 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [4/6]

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

Definition at line 105 of file Darknet.C.

References itsNeedReload.

◆ onParamChange() [5/6]

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

Definition at line 60 of file Darknet.C.

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

◆ onParamChange() [6/6]

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

Definition at line 104 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 108 of file Darknet.C.

References loadNet().

◆ postUninit()

void Darknet::postUninit ( )
overridevirtual

Un-initialize and free resources.

Reimplemented from jevois::Component.

Definition at line 184 of file Darknet.C.

References classes, itsReadyFut, names, and net.

◆ predict() [1/2]

float Darknet::predict ( cv::Mat const &  cvimg,
std::vector< jevois::ObjReco > &  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 202 of file Darknet.C.

References demo::float, h, demo::image, itsNeedReload, itsReady, LFATAL, loadNet(), demo::results, and demo::w.

◆ predict() [2/2]

float Darknet::predict ( image &  im,
std::vector< jevois::ObjReco > &  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 229 of file Darknet.C.

References demo::float, itsNeedReload, itsReady, loadNet(), names, net, demo::results, and demo::score.

◆ 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 266 of file Darknet.C.

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

Friends And Related Function Documentation

◆ JEVOIS_DECLARE_PARAMETER() [1/3]

JEVOIS_DECLARE_PARAMETER ( threads  ,
int  ,
"Number of parallel computation threads"  ,
,
jevois::Range< int >  1, 1024,
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 ( top  ,
unsigned int  ,
"Max number of top-scoring predictions that score above thresh to return"  ,
,
ParamCateg   
)
related

Parameter.

◆ JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK() [1/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() [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 ( 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() [4/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_DECLARE_PARAMETER_WITH_CALLBACK() [5/5]

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

Parameter.

◆ JEVOIS_DEFINE_ENUM_CLASS()

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

Enum.

Parameter

Member Data Documentation

◆ classes

◆ itsNeedReload

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

Definition at line 158 of file Darknet.H.

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

◆ itsReady

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

Definition at line 156 of file Darknet.H.

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

◆ itsReadyFut

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

Definition at line 155 of file Darknet.H.

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

◆ itsShowDetailParams

const bool Darknet::itsShowDetailParams
protected

Definition at line 157 of file Darknet.H.

Referenced by Darknet(), and onParamChange().

◆ names

char** Darknet::names = nullptr

Definition at line 143 of file Darknet.H.

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

◆ net


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