OpenCV  3.2.0
Open Source Computer Vision
Public Types | Public Member Functions | List of all members
cv::dnn::Net Class Reference

This class allows to create and manipulate comprehensive artificial neural networks. More...

#include "dnn.hpp"

Public Types

typedef DictValue LayerId
 Container for strings and integers. More...
 

Public Member Functions

 Net ()
 Default constructor. More...
 
 ~Net ()
 Destructor frees the net only if there aren't references to the net anymore. More...
 
int addLayer (const String &name, const String &type, LayerParams &params)
 Adds new layer to the net. More...
 
int addLayerToPrev (const String &name, const String &type, LayerParams &params)
 Adds new layer and connects its first input to the first output of previously added layer. More...
 
void allocate ()
 Initializes and allocates all layers. More...
 
void connect (String outPin, String inpPin)
 Connects output of the first layer to input of the second layer. More...
 
void connect (int outLayerId, int outNum, int inpLayerId, int inpNum)
 Connects #outNum output of the first layer to #inNum input of the second layer. More...
 
void deleteLayer (LayerId layer)
 Delete layer for the network (not implemented yet) More...
 
bool empty () const
 
void forward (LayerId toLayer=String())
 Runs forward pass to compute output of layer toLayer. More...
 
void forward (LayerId startLayer, LayerId toLayer)
 Runs forward pass to compute output of layer toLayer, but computations start from startLayer. More...
 
void forward (const std::vector< LayerId > &startLayers, const std::vector< LayerId > &toLayers)
 
void forwardOpt (LayerId toLayer)
 Optimized forward. More...
 
void forwardOpt (const std::vector< LayerId > &toLayers)
 
Blob getBlob (String outputName)
 Returns the layer output blob. More...
 
Ptr< LayergetLayer (LayerId layerId)
 Returns pointer to layer with specified name which the network use. More...
 
int getLayerId (const String &layer)
 Converts string name of the layer to the integer identifier. More...
 
std::vector< StringgetLayerNames () const
 
Blob getParam (LayerId layer, int numParam=0)
 Returns parameter blob of the layer. More...
 
void setBlob (String outputName, const Blob &blob)
 Sets the new value for the layer output blob. More...
 
void setNetInputs (const std::vector< String > &inputBlobNames)
 Sets outputs names of the network input pseudo layer. More...
 
void setParam (LayerId layer, int numParam, const Blob &blob)
 Sets the new value for the learned param of the layer. More...
 

Detailed Description

This class allows to create and manipulate comprehensive artificial neural networks.

Neural network is presented as directed acyclic graph (DAG), where vertices are Layer instances, and edges specify relationships between layers inputs and outputs.

Each network layer has unique integer id and unique string name inside its network. LayerId can store either layer name or layer id.

This class supports reference counting of its instances, i. e. copies point to the same instance.

Member Typedef Documentation

§ LayerId

Container for strings and integers.

Constructor & Destructor Documentation

§ Net()

cv::dnn::Net::Net ( )

Default constructor.

§ ~Net()

cv::dnn::Net::~Net ( )

Destructor frees the net only if there aren't references to the net anymore.

Member Function Documentation

§ addLayer()

int cv::dnn::Net::addLayer ( const String name,
const String type,
LayerParams params 
)

Adds new layer to the net.

Parameters
nameunique name of the adding layer.
typetypename of the adding layer (type must be registered in LayerRegister).
paramsparameters which will be used to initialize the creating layer.
Returns
unique identifier of created layer, or -1 if a failure will happen.

§ addLayerToPrev()

int cv::dnn::Net::addLayerToPrev ( const String name,
const String type,
LayerParams params 
)

Adds new layer and connects its first input to the first output of previously added layer.

See also
addLayer()

§ allocate()

void cv::dnn::Net::allocate ( )

Initializes and allocates all layers.

§ connect() [1/2]

void cv::dnn::Net::connect ( String  outPin,
String  inpPin 
)

Connects output of the first layer to input of the second layer.

Parameters
outPindescriptor of the first layer output.
inpPindescriptor of the second layer input.

Descriptors have the following template <layer_name>[.input_number]:

  • the first part of the template layer_name is sting name of the added layer. If this part is empty then the network input pseudo layer will be used;
  • the second optional part of the template input_number is either number of the layer input, either label one. If this part is omitted then the first layer input will be used.

    See also
    setNetInputs(), Layer::inputNameToIndex(), Layer::outputNameToIndex()

§ connect() [2/2]

void cv::dnn::Net::connect ( int  outLayerId,
int  outNum,
int  inpLayerId,
int  inpNum 
)

Connects #outNum output of the first layer to #inNum input of the second layer.

Parameters
outLayerIdidentifier of the first layer
inpLayerIdidentifier of the second layer
outNumnumber of the first layer output
inpNumnumber of the second layer input

§ deleteLayer()

void cv::dnn::Net::deleteLayer ( LayerId  layer)

Delete layer for the network (not implemented yet)

§ empty()

bool cv::dnn::Net::empty ( ) const

Returns true if there are no layers in the network.

§ forward() [1/3]

void cv::dnn::Net::forward ( LayerId  toLayer = String())

Runs forward pass to compute output of layer toLayer.

By default runs forward pass for the whole network.

§ forward() [2/3]

void cv::dnn::Net::forward ( LayerId  startLayer,
LayerId  toLayer 
)

Runs forward pass to compute output of layer toLayer, but computations start from startLayer.

§ forward() [3/3]

void cv::dnn::Net::forward ( const std::vector< LayerId > &  startLayers,
const std::vector< LayerId > &  toLayers 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

§ forwardOpt() [1/2]

void cv::dnn::Net::forwardOpt ( LayerId  toLayer)

Optimized forward.

Warning
Not implemented yet.

Makes forward only those layers which weren't changed after previous forward().

§ forwardOpt() [2/2]

void cv::dnn::Net::forwardOpt ( const std::vector< LayerId > &  toLayers)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

§ getBlob()

Blob cv::dnn::Net::getBlob ( String  outputName)

Returns the layer output blob.

Parameters
outputNamethe descriptor of the returning layer output blob.
See also
connect(String, String)

§ getLayer()

Ptr<Layer> cv::dnn::Net::getLayer ( LayerId  layerId)

Returns pointer to layer with specified name which the network use.

§ getLayerId()

int cv::dnn::Net::getLayerId ( const String layer)

Converts string name of the layer to the integer identifier.

Returns
id of the layer, or -1 if the layer wasn't found.

§ getLayerNames()

std::vector<String> cv::dnn::Net::getLayerNames ( ) const

§ getParam()

Blob cv::dnn::Net::getParam ( LayerId  layer,
int  numParam = 0 
)

Returns parameter blob of the layer.

Parameters
layername or id of the layer.
numParamindex of the layer parameter in the Layer::blobs array.
See also
Layer::blobs

§ setBlob()

void cv::dnn::Net::setBlob ( String  outputName,
const Blob blob 
)

Sets the new value for the layer output blob.

Parameters
outputNamedescriptor of the updating layer output blob.
blobnew blob.
See also
connect(String, String) to know format of the descriptor.
Note
If updating blob is not empty then blob must have the same shape, because network reshaping is not implemented yet.

§ setNetInputs()

void cv::dnn::Net::setNetInputs ( const std::vector< String > &  inputBlobNames)

Sets outputs names of the network input pseudo layer.

Each net always has special own the network input pseudo layer with id=0. This layer stores the user blobs only and don't make any computations. In fact, this layer provides the only way to pass user data into the network. As any other layer, this layer can label its outputs and this function provides an easy way to do this.

§ setParam()

void cv::dnn::Net::setParam ( LayerId  layer,
int  numParam,
const Blob blob 
)

Sets the new value for the learned param of the layer.

Parameters
layername or id of the layer.
numParamindex of the layer parameter in the Layer::blobs array.
blobthe new value.
See also
Layer::blobs
Note
If shape of the new blob differs from the previous shape, then the following forward pass may fail.

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