Class Net
This class allows to create and manipulate comprehensive artificial neural networks.
Implements
Inherited Members
Namespace: OpenCvSharp.Dnn
Assembly: OpenCvSharp.dll
Syntax
public class Net : DisposableCvObject, ICvPtrHolder
Remarks
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.
Constructors
| Improve this Doc View SourceNet()
Default constructor.
Declaration
public Net()
Net(IntPtr)
Declaration
protected Net(IntPtr ptr)
Parameters
Type | Name | Description |
---|---|---|
IntPtr | ptr |
Methods
| Improve this Doc View SourceConnect(Int32, Int32, Int32, Int32)
Connects #@p outNum output of the first layer to #@p inNum input of the second layer.
Declaration
public void Connect(int outLayerId, int outNum, int inpLayerId, int inpNum)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | outLayerId | identifier of the first layer |
System.Int32 | outNum | identifier of the second layer |
System.Int32 | inpLayerId | number of the first layer output |
System.Int32 | inpNum | number of the second layer input |
Connect(String, String)
Connects output of the first layer to input of the second layer.
Declaration
public void Connect(string outPin, string inpPin)
Parameters
Type | Name | Description |
---|---|---|
System.String | outPin | descriptor of the first layer output. |
System.String | inpPin | descriptor of the second layer input. |
DisposeUnmanaged()
Declaration
protected override void DisposeUnmanaged()
Overrides
| Improve this Doc View SourceDump()
Dump net to String. Call method after setInput(). To see correct backend, target and fusion run after forward().
Declaration
public string Dump()
Returns
Type | Description |
---|---|
System.String | String with structure, hyperparameters, backend, target and fusion |
DumpToFile(String)
Dump net structure, hyperparameters, backend, target and fusion to dot file
Declaration
public void DumpToFile(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | path to output file with .dot extension |
Empty()
Returns true if there are no layers in the network.
Declaration
public bool Empty()
Returns
Type | Description |
---|---|
System.Boolean |
EnableFusion(Boolean)
Enables or disables layer fusion in the network.
Declaration
public void EnableFusion(bool fusion)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | fusion | true to enable the fusion, false to disable. The fusion is enabled by default. |
Forward(IEnumerable<Mat>, IEnumerable<String>)
Runs forward pass to compute outputs of layers listed in @p outBlobNames.
Declaration
public void Forward(IEnumerable<Mat> outputBlobs, IEnumerable<string> outBlobNames)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Mat> | outputBlobs | contains blobs for first outputs of specified layers. |
IEnumerable<System.String> | outBlobNames | names for layers which outputs are needed to get |
Forward(IEnumerable<Mat>, String)
Runs forward pass to compute output of layer with name @p outputName.
Declaration
public void Forward(IEnumerable<Mat> outputBlobs, string outputName = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Mat> | outputBlobs | contains all output blobs for specified layer. |
System.String | outputName | name for layer which output is needed to get. If outputName is empty, runs forward pass for the whole network. |
Forward(String)
Runs forward pass to compute output of layer with name @p outputName. By default runs forward pass for the whole network.
Declaration
public Mat Forward(string outputName = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | outputName | name for layer which output is needed to get |
Returns
Type | Description |
---|---|
Mat | blob for first output of specified layer. |
GetLayerId(String)
Converts string name of the layer to the integer identifier.
Declaration
public int GetLayerId(string layer)
Parameters
Type | Name | Description |
---|---|---|
System.String | layer |
Returns
Type | Description |
---|---|
System.Int32 | id of the layer, or -1 if the layer wasn't found. |
GetLayerNames()
Declaration
public string[] GetLayerNames()
Returns
Type | Description |
---|---|
System.String[] |
GetPerfProfile(out Double[])
Returns overall time for inference and timings (in ticks) for layers. Indexes in returned vector correspond to layers ids.Some layers can be fused with others, in this case zero ticks count will be return for that skipped layers.
Declaration
public long GetPerfProfile(out double[] timings)
Parameters
Type | Name | Description |
---|---|---|
System.Double[] | timings | vector for tick timings for all layers. |
Returns
Type | Description |
---|---|
System.Int64 | overall ticks for model inference. |
GetUnconnectedOutLayers()
Returns indexes of layers with unconnected outputs.
Declaration
public int[] GetUnconnectedOutLayers()
Returns
Type | Description |
---|---|
System.Int32[] |
GetUnconnectedOutLayersNames()
Returns names of layers with unconnected outputs.
Declaration
public string[] GetUnconnectedOutLayersNames()
Returns
Type | Description |
---|---|
System.String[] |
ReadFromModelOptimizer(String, String)
Create a network from Intel's Model Optimizer intermediate representation (IR). Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine backend.
Declaration
public static Net ReadFromModelOptimizer(string xml, string bin)
Parameters
Type | Name | Description |
---|---|---|
System.String | xml | XML configuration file with network's topology. |
System.String | bin | Binary file with trained weights. |
Returns
Type | Description |
---|---|
Net |
ReadNet(String, String, String)
Read deep learning network represented in one of the supported formats.
This function automatically detects an origin framework of trained model and calls an appropriate function such @ref readNetFromCaffe, @ref readNetFromTensorflow,
Declaration
public static Net ReadNet(string model, string config = "", string framework = "")
Parameters
Type | Name | Description |
---|---|---|
System.String | model | Binary file contains trained weights. The following file
|
System.String | config | Text file contains network configuration. It could be a
|
System.String | framework | Explicit framework name tag to determine a format. |
Returns
Type | Description |
---|---|
Net |
ReadNetFromCaffe(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>)
Reads a network model stored in Caffe model files from memory.
Declaration
public static Net ReadNetFromCaffe(ReadOnlySpan<byte> bufferProto, ReadOnlySpan<byte> bufferModel = null)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Byte> | bufferProto | buffer containing the content of the .prototxt file |
ReadOnlySpan<System.Byte> | bufferModel | buffer containing the content of the .caffemodel file |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
ReadNetFromCaffe(Byte[], Byte[])
Reads a network model stored in Caffe model in memory.
Declaration
public static Net ReadNetFromCaffe(byte[] bufferProto, byte[] bufferModel = null)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bufferProto | buffer containing the content of the .prototxt file |
System.Byte[] | bufferModel | buffer containing the content of the .caffemodel file |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
ReadNetFromCaffe(String, String)
Reads a network model stored in Caffe model files.
Declaration
public static Net ReadNetFromCaffe(string prototxt, string caffeModel = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | prototxt | path to the .prototxt file with text description of the network architecture. |
System.String | caffeModel | path to the .caffemodel file with learned network. |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
ReadNetFromDarknet(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>)
Reads a network model stored in Caffe model files from memory.
Declaration
public static Net ReadNetFromDarknet(ReadOnlySpan<byte> bufferCfg, ReadOnlySpan<byte> bufferModel = null)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Byte> | bufferCfg | A buffer contains a content of .cfg file with text description of the network architecture. |
ReadOnlySpan<System.Byte> | bufferModel | A buffer contains a content of .weights file with learned network. |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
ReadNetFromDarknet(Byte[], Byte[])
Reads a network model stored in Caffe model files from memory.
Declaration
public static Net ReadNetFromDarknet(byte[] bufferCfg, byte[] bufferModel = null)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bufferCfg | A buffer contains a content of .cfg file with text description of the network architecture. |
System.Byte[] | bufferModel | A buffer contains a content of .weights file with learned network. |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createCaffeImporter and Net::populateNet calls.
ReadNetFromDarknet(String, String)
Reads a network model stored in Darknet (https://pjreddie.com/darknet/) model files.
Declaration
public static Net ReadNetFromDarknet(string cfgFile, string darknetModel = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | cfgFile | path to the .cfg file with text description of the network architecture. |
System.String | darknetModel | path to the .weights file with learned network. |
Returns
Type | Description |
---|---|
Net | Network object that ready to do forward, throw an exception in failure cases. |
Remarks
This is shortcut consisting from DarknetImporter and Net::populateNet calls.
ReadNetFromModelOptimizer(String, String)
Load a network from Intel's Model Optimizer intermediate representation. Networks imported from Intel's Model Optimizer are launched in Intel's Inference Engine backend.
Declaration
public static Net ReadNetFromModelOptimizer(string xml, string bin)
Parameters
Type | Name | Description |
---|---|---|
System.String | xml | XML configuration file with network's topology. |
System.String | bin | Binary file with trained weights. |
Returns
Type | Description |
---|---|
Net |
ReadNetFromONNX(ReadOnlySpan<Byte>)
Reads a network model ONNX https://onnx.ai/ from memory
Declaration
public static Net ReadNetFromONNX(ReadOnlySpan<byte> onnxFileData)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Byte> | onnxFileData | memory of the first byte of the buffer. |
Returns
Type | Description |
---|---|
Net | Network object that ready to do forward, throw an exception in failure cases. |
ReadNetFromONNX(Byte[])
Reads a network model ONNX https://onnx.ai/ from memory
Declaration
public static Net ReadNetFromONNX(byte[] onnxFileData)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | onnxFileData | memory of the first byte of the buffer. |
Returns
Type | Description |
---|---|
Net | Network object that ready to do forward, throw an exception in failure cases. |
ReadNetFromONNX(String)
Reads a network model ONNX https://onnx.ai/
Declaration
public static Net ReadNetFromONNX(string onnxFile)
Parameters
Type | Name | Description |
---|---|---|
System.String | onnxFile | path to the .onnx file with text description of the network architecture. |
Returns
Type | Description |
---|---|
Net | Network object that ready to do forward, throw an exception in failure cases. |
ReadNetFromTensorflow(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>)
Reads a network model stored in Tensorflow model from memory.
Declaration
public static Net ReadNetFromTensorflow(ReadOnlySpan<byte> bufferModel, ReadOnlySpan<byte> bufferConfig = null)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<System.Byte> | bufferModel | buffer containing the content of the pb file |
ReadOnlySpan<System.Byte> | bufferConfig | buffer containing the content of the pbtxt file (optional) |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.
ReadNetFromTensorflow(Byte[], Byte[])
Reads a network model stored in Tensorflow model from memory.
Declaration
public static Net ReadNetFromTensorflow(byte[] bufferModel, byte[] bufferConfig = null)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | bufferModel | buffer containing the content of the pb file |
System.Byte[] | bufferConfig | buffer containing the content of the pbtxt file (optional) |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.
ReadNetFromTensorflow(String, String)
Reads a network model stored in Tensorflow model file.
Declaration
public static Net ReadNetFromTensorflow(string model, string config = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | model | path to the .pb file with binary protobuf description of the network architecture |
System.String | config | path to the .pbtxt file that contains text graph definition in protobuf format. |
Returns
Type | Description |
---|---|
Net | Resulting Net object is built by text graph using weights from a binary one that let us make it more flexible. |
Remarks
This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.
ReadNetFromTorch(String, Boolean)
Reads a network model stored in Torch model file.
Declaration
public static Net ReadNetFromTorch(string model, bool isBinary = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | model | |
System.Boolean | isBinary |
Returns
Type | Description |
---|---|
Net |
Remarks
This is shortcut consisting from createTorchImporter and Net::populateNet calls.
SetHalideScheduler(String)
Compile Halide layers. 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.
Declaration
public void SetHalideScheduler(string scheduler)
Parameters
Type | Name | Description |
---|---|---|
System.String | scheduler | Path to YAML file with scheduling directives. |
SetInput(Mat, String)
Sets the new value for the layer output blob
Declaration
public void SetInput(Mat blob, string name = "")
Parameters
Type | Name | Description |
---|---|---|
Mat | blob | new blob. |
System.String | name | descriptor of the updating layer output blob. |
Remarks
connect(String, String) to know format of the descriptor. If updating blob is not empty then @p blob must have the same shape, because network reshaping is not implemented yet.
SetInputsNames(IEnumerable<String>)
Sets outputs names of the network input pseudo layer.
Declaration
public void SetInputsNames(IEnumerable<string> inputBlobNames)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<System.String> | inputBlobNames |
Remarks
- 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.
SetPreferableBackend(Backend)
Ask network to use specific computation backend where it supported.
Declaration
public void SetPreferableBackend(Backend backendId)
Parameters
Type | Name | Description |
---|---|---|
Backend | backendId | backend identifier. |
SetPreferableTarget(Target)
Ask network to make computations on specific target device.
Declaration
public void SetPreferableTarget(Target targetId)
Parameters
Type | Name | Description |
---|---|---|
Target | targetId | target identifier. |