OpenCV
3.3.0
Open Source Computer Vision
|
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 ¶ms) |
Adds new layer to the net. More... | |
int | addLayerToPrev (const String &name, const String &type, LayerParams ¶ms) |
Adds new layer and connects its first input to the first output of previously added layer. 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 | enableFusion (bool fusion) |
Enables or disables layer fusion in the network. More... | |
Mat | forward (const String &outputName=String()) |
Runs forward pass to compute output of layer with name outputName . More... | |
void | forward (std::vector< Mat > &outputBlobs, const String &outputName=String()) |
Runs forward pass to compute output of layer with name outputName . More... | |
void | forward (std::vector< Mat > &outputBlobs, const std::vector< String > &outBlobNames) |
Runs forward pass to compute outputs of layers listed in outBlobNames . More... | |
void | forward (std::vector< std::vector< Mat > > &outputBlobs, const std::vector< String > &outBlobNames) |
Runs forward pass to compute outputs of layers listed in outBlobNames . More... | |
void | forwardOpt (LayerId toLayer) |
Optimized forward. More... | |
void | forwardOpt (const std::vector< LayerId > &toLayers) |
int64 | getFLOPS (const std::vector< MatShape > &netInputShapes) const |
Computes FLOP for whole loaded model with specified input shapes. More... | |
int64 | getFLOPS (const MatShape &netInputShape) const |
int64 | getFLOPS (const int layerId, const std::vector< MatShape > &netInputShapes) const |
int64 | getFLOPS (const int layerId, const MatShape &netInputShape) const |
Ptr< Layer > | getLayer (LayerId layerId) |
Returns pointer to layer with specified id or name which the network use. More... | |
int | getLayerId (const String &layer) |
Converts string name of the layer to the integer identifier. More... | |
std::vector< Ptr< Layer > > | getLayerInputs (LayerId layerId) |
Returns pointers to input layers of specific layer. More... | |
std::vector< String > | getLayerNames () const |
int | getLayersCount (const String &layerType) const |
Returns count of layers of specified type. More... | |
void | getLayerShapes (const MatShape &netInputShape, const int layerId, std::vector< MatShape > *inLayerShapes, std::vector< MatShape > *outLayerShapes) const |
Returns input and output shapes for layer with specified id in loaded model; preliminary inferencing isn't necessary. More... | |
void | getLayerShapes (const std::vector< MatShape > &netInputShapes, const int layerId, std::vector< MatShape > *inLayerShapes, std::vector< MatShape > *outLayerShapes) const |
void | getLayersShapes (const std::vector< MatShape > &netInputShapes, std::vector< int > *layersIds, std::vector< std::vector< MatShape > > *inLayersShapes, std::vector< std::vector< MatShape > > *outLayersShapes) const |
Returns input and output shapes for all layers in loaded model; preliminary inferencing isn't necessary. More... | |
void | getLayersShapes (const MatShape &netInputShape, std::vector< int > *layersIds, std::vector< std::vector< MatShape > > *inLayersShapes, std::vector< std::vector< MatShape > > *outLayersShapes) const |
void | getLayerTypes (std::vector< String > &layersTypes) const |
Returns list of types for layer used in model. More... | |
void | getMemoryConsumption (const std::vector< MatShape > &netInputShapes, size_t &weights, size_t &blobs) const |
Computes bytes number which are requered to store all weights and intermediate blobs for model. More... | |
void | getMemoryConsumption (const MatShape &netInputShape, size_t &weights, size_t &blobs) const |
void | getMemoryConsumption (const int layerId, const std::vector< MatShape > &netInputShapes, size_t &weights, size_t &blobs) const |
void | getMemoryConsumption (const int layerId, const MatShape &netInputShape, size_t &weights, size_t &blobs) const |
void | getMemoryConsumption (const std::vector< MatShape > &netInputShapes, std::vector< int > &layerIds, std::vector< size_t > &weights, std::vector< size_t > &blobs) const |
Computes bytes number which are requered to store all weights and intermediate blobs for each layer. More... | |
void | getMemoryConsumption (const MatShape &netInputShape, std::vector< int > &layerIds, std::vector< size_t > &weights, std::vector< size_t > &blobs) const |
Mat | getParam (LayerId layer, int numParam=0) |
Returns parameter blob of the layer. More... | |
std::vector< int > | getUnconnectedOutLayers () const |
Returns indexes of layers with unconnected outputs. More... | |
void | setHalideScheduler (const String &scheduler) |
Compile Halide layers. More... | |
void | setInput (const Mat &blob, const String &name="") |
Sets the new value for the layer output blob. More... | |
void | setInputsNames (const std::vector< String > &inputBlobNames) |
Sets outputs names of the network input pseudo layer. More... | |
void | setParam (LayerId layer, int numParam, const Mat &blob) |
Sets the new value for the learned param of the layer. More... | |
void | setPreferableBackend (int backendId) |
Ask network to use specific computation backend where it supported. More... | |
void | setPreferableTarget (int targetId) |
Ask network to make computations on specific target device. More... | |
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.
typedef DictValue cv::dnn::Net::LayerId |
Container for strings and integers.
cv::dnn::Net::Net | ( | ) |
Default constructor.
cv::dnn::Net::~Net | ( | ) |
Destructor frees the net only if there aren't references to the net anymore.
int cv::dnn::Net::addLayer | ( | const String & | name, |
const String & | type, | ||
LayerParams & | params | ||
) |
Adds new layer to the net.
name | unique name of the adding layer. |
type | typename of the adding layer (type must be registered in LayerRegister). |
params | parameters which will be used to initialize the creating layer. |
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.
Connects output of the first layer to input of the second layer.
outPin | descriptor of the first layer output. |
inpPin | descriptor of the second layer input. |
Descriptors have the following template <layer_name>[.input_number]
:
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.
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.
outLayerId | identifier of the first layer |
inpLayerId | identifier of the second layer |
outNum | number of the first layer output |
inpNum | number of the second layer input |
void cv::dnn::Net::deleteLayer | ( | LayerId | layer | ) |
Delete layer for the network (not implemented yet)
bool cv::dnn::Net::empty | ( | ) | const |
Returns true if there are no layers in the network.
void cv::dnn::Net::enableFusion | ( | bool | fusion | ) |
Enables or disables layer fusion in the network.
fusion | true to enable the fusion, false to disable. The fusion is enabled by default. |
Runs forward pass to compute output of layer with name outputName
.
outputName | name for layer which output is needed to get |
By default runs forward pass for the whole network.
void cv::dnn::Net::forward | ( | std::vector< Mat > & | outputBlobs, |
const String & | outputName = String() |
||
) |
Runs forward pass to compute output of layer with name outputName
.
outputBlobs | contains all output blobs for specified layer. |
outputName | name for layer which output is needed to get |
If outputName
is empty, runs forward pass for the whole network.
void cv::dnn::Net::forward | ( | std::vector< Mat > & | outputBlobs, |
const std::vector< String > & | outBlobNames | ||
) |
Runs forward pass to compute outputs of layers listed in outBlobNames
.
outputBlobs | contains blobs for first outputs of specified layers. |
outBlobNames | names for layers which outputs are needed to get |
void cv::dnn::Net::forward | ( | std::vector< std::vector< Mat > > & | outputBlobs, |
const std::vector< String > & | outBlobNames | ||
) |
Runs forward pass to compute outputs of layers listed in outBlobNames
.
outputBlobs | contains all output blobs for each layer specified in outBlobNames . |
outBlobNames | names for layers which outputs are needed to get |
void cv::dnn::Net::forwardOpt | ( | LayerId | toLayer | ) |
Optimized forward.
Makes forward only those layers which weren't changed after previous forward().
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.
Computes FLOP for whole loaded model with specified input shapes.
netInputShapes | vector of shapes for all net inputs. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
int64 cv::dnn::Net::getFLOPS | ( | const int | layerId, |
const std::vector< MatShape > & | netInputShapes | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns pointer to layer with specified id or name which the network use.
int cv::dnn::Net::getLayerId | ( | const String & | layer | ) |
Converts string name of the layer to the integer identifier.
Returns pointers to input layers of specific layer.
std::vector<String> cv::dnn::Net::getLayerNames | ( | ) | const |
int cv::dnn::Net::getLayersCount | ( | const String & | layerType | ) | const |
Returns count of layers of specified type.
layerType | type. |
void cv::dnn::Net::getLayerShapes | ( | const MatShape & | netInputShape, |
const int | layerId, | ||
std::vector< MatShape > * | inLayerShapes, | ||
std::vector< MatShape > * | outLayerShapes | ||
) | const |
Returns input and output shapes for layer with specified id in loaded model; preliminary inferencing isn't necessary.
netInputShape | shape input blob in net input layer. |
layerId | id for layer. |
inLayerShapes | output parameter for input layers shapes; order is the same as in layersIds |
outLayerShapes | output parameter for output layers shapes; order is the same as in layersIds |
void cv::dnn::Net::getLayerShapes | ( | const std::vector< MatShape > & | netInputShapes, |
const int | layerId, | ||
std::vector< MatShape > * | inLayerShapes, | ||
std::vector< MatShape > * | outLayerShapes | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::dnn::Net::getLayersShapes | ( | const std::vector< MatShape > & | netInputShapes, |
std::vector< int > * | layersIds, | ||
std::vector< std::vector< MatShape > > * | inLayersShapes, | ||
std::vector< std::vector< MatShape > > * | outLayersShapes | ||
) | const |
Returns input and output shapes for all layers in loaded model; preliminary inferencing isn't necessary.
netInputShapes | shapes for all input blobs in net input layer. |
layersIds | output parameter for layer IDs. |
inLayersShapes | output parameter for input layers shapes; order is the same as in layersIds |
outLayersShapes | output parameter for output layers shapes; order is the same as in layersIds |
void cv::dnn::Net::getLayersShapes | ( | const MatShape & | netInputShape, |
std::vector< int > * | layersIds, | ||
std::vector< std::vector< MatShape > > * | inLayersShapes, | ||
std::vector< std::vector< MatShape > > * | outLayersShapes | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::dnn::Net::getLayerTypes | ( | std::vector< String > & | layersTypes | ) | const |
Returns list of types for layer used in model.
layersTypes | output parameter for returning types. |
void cv::dnn::Net::getMemoryConsumption | ( | const std::vector< MatShape > & | netInputShapes, |
size_t & | weights, | ||
size_t & | blobs | ||
) | const |
Computes bytes number which are requered to store all weights and intermediate blobs for model.
netInputShapes | vector of shapes for all net inputs. |
weights | output parameter to store resulting bytes for weights. |
blobs | output parameter to store resulting bytes for intermediate blobs. |
void cv::dnn::Net::getMemoryConsumption | ( | const MatShape & | netInputShape, |
size_t & | weights, | ||
size_t & | blobs | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::dnn::Net::getMemoryConsumption | ( | const int | layerId, |
const std::vector< MatShape > & | netInputShapes, | ||
size_t & | weights, | ||
size_t & | blobs | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::dnn::Net::getMemoryConsumption | ( | const int | layerId, |
const MatShape & | netInputShape, | ||
size_t & | weights, | ||
size_t & | blobs | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void cv::dnn::Net::getMemoryConsumption | ( | const std::vector< MatShape > & | netInputShapes, |
std::vector< int > & | layerIds, | ||
std::vector< size_t > & | weights, | ||
std::vector< size_t > & | blobs | ||
) | const |
Computes bytes number which are requered to store all weights and intermediate blobs for each layer.
netInputShapes | vector of shapes for all net inputs. |
layerIds | output vector to save layer IDs. |
weights | output parameter to store resulting bytes for weights. |
blobs | output parameter to store resulting bytes for intermediate blobs. |
void cv::dnn::Net::getMemoryConsumption | ( | const MatShape & | netInputShape, |
std::vector< int > & | layerIds, | ||
std::vector< size_t > & | weights, | ||
std::vector< size_t > & | blobs | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns parameter blob of the layer.
layer | name or id of the layer. |
numParam | index of the layer parameter in the Layer::blobs array. |
std::vector<int> cv::dnn::Net::getUnconnectedOutLayers | ( | ) | const |
Returns indexes of layers with unconnected outputs.
void cv::dnn::Net::setHalideScheduler | ( | const String & | scheduler | ) |
Compile Halide layers.
[in] | scheduler | Path to YAML file with scheduling directives. |
Schedule layers that support Halide backend. Then compile them for specific target. For layers that not represented in scheduling file or if no manual scheduling used at all, automatic scheduling will be applied.
Sets the new value for the layer output blob.
name | descriptor of the updating layer output blob. |
blob | new blob. |
blob
must have the same shape, because network reshaping is not implemented yet. void cv::dnn::Net::setInputsNames | ( | 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.
Sets the new value for the learned param of the layer.
layer | name or id of the layer. |
numParam | index of the layer parameter in the Layer::blobs array. |
blob | the new value. |
void cv::dnn::Net::setPreferableBackend | ( | int | backendId | ) |
Ask network to use specific computation backend where it supported.
[in] | backendId | backend identifier. |
void cv::dnn::Net::setPreferableTarget | ( | int | targetId | ) |
Ask network to make computations on specific target device.
[in] | targetId | target identifier. |