Table of Contents

Class Net

Namespace
OpenCvSharp.Dnn
Assembly
OpenCvSharp.dll

DisposableObject + ICvPtrHolder

[SuppressMessage("Microsoft.Design", "CA1724: Type names should not match namespaces")]
public class Net : DisposableCvObject, IDisposable, ICvPtrHolder
Inheritance
Net
Implements
Inherited Members

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

Net()

Default constructor

public Net()

Net(nint)

Constructor

protected Net(nint ptr)

Parameters

ptr nint

Methods

Connect(int, int, int, int)

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

public void Connect(int outLayerId, int outNum, int inpLayerId, int inpNum)

Parameters

outLayerId int

identifier of the first layer

outNum int

identifier of the second layer

inpLayerId int

number of the first layer output

inpNum int

number of the second layer input

Connect(string, string)

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

public void Connect(string outPin, string inpPin)

Parameters

outPin string

descriptor of the first layer output.

inpPin string

descriptor of the second layer input.

DisposeUnmanaged()

releases unmanaged resources

protected override void DisposeUnmanaged()

Dump()

Dump net to String. Call method after setInput(). To see correct backend, target and fusion run after forward().

public string Dump()

Returns

string

String with structure, hyperparameters, backend, target and fusion

DumpToFile(string)

Dump net structure, hyperparameters, backend, target and fusion to dot file

public void DumpToFile(string path)

Parameters

path string

path to output file with .dot extension

Empty()

Returns true if there are no layers in the network.

public bool Empty()

Returns

bool

EnableFusion(bool)

Enables or disables layer fusion in the network.

public void EnableFusion(bool fusion)

Parameters

fusion bool

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.

public void Forward(IEnumerable<Mat> outputBlobs, IEnumerable<string> outBlobNames)

Parameters

outputBlobs IEnumerable<Mat>

contains blobs for first outputs of specified layers.

outBlobNames IEnumerable<string>

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.

public void Forward(IEnumerable<Mat> outputBlobs, string? outputName = null)

Parameters

outputBlobs IEnumerable<Mat>

contains all output blobs for specified layer.

outputName string

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.

public Mat Forward(string? outputName = null)

Parameters

outputName string

name for layer which output is needed to get

Returns

Mat

blob for first output of specified layer.

GetLayerId(string)

Converts string name of the layer to the integer identifier.

public int GetLayerId(string layer)

Parameters

layer string

Returns

int

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

GetLayerNames()

public string?[] GetLayerNames()

Returns

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.

public long GetPerfProfile(out double[] timings)

Parameters

timings double[]

vector for tick timings for all layers.

Returns

long

overall ticks for model inference.

GetUnconnectedOutLayers()

Returns indexes of layers with unconnected outputs.

public int[] GetUnconnectedOutLayers()

Returns

int[]

GetUnconnectedOutLayersNames()

Returns names of layers with unconnected outputs.

public string?[] GetUnconnectedOutLayersNames()

Returns

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.

public static Net? ReadFromModelOptimizer(string xml, string bin)

Parameters

xml string

XML configuration file with network's topology.

bin string

Binary file with trained weights.

Returns

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,

public static Net ReadNet(string model, string config = "", string framework = "")

Parameters

model string

Binary file contains trained weights. The following file * extensions are expected for models from different frameworks: * * *.caffemodel (Caffe, http://caffe.berkeleyvision.org/) * * *.pb (TensorFlow, https://www.tensorflow.org/) * * *.t7 | *.net (Torch, http://torch.ch/) * * *.weights (Darknet, https://pjreddie.com/darknet/) * * *.bin (DLDT, https://software.intel.com/openvino-toolkit)

config string

Text file contains network configuration. It could be a * file with the following extensions: * * *.prototxt (Caffe, http://caffe.berkeleyvision.org/) * * *.pbtxt (TensorFlow, https://www.tensorflow.org/) * * *.cfg (Darknet, https://pjreddie.com/darknet/) * * *.xml (DLDT, https://software.intel.com/openvino-toolkit)

framework string

Explicit framework name tag to determine a format.

Returns

Net

ReadNetFromCaffe(byte[], byte[]?)

Reads a network model stored in Caffe model in memory.

public static Net? ReadNetFromCaffe(byte[] bufferProto, byte[]? bufferModel = null)

Parameters

bufferProto byte[]

buffer containing the content of the .prototxt file

bufferModel byte[]

buffer containing the content of the .caffemodel file

Returns

Net

Remarks

This is shortcut consisting from createCaffeImporter and Net::populateNet calls.

ReadNetFromCaffe(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Reads a network model stored in Caffe model files from memory.

public static Net? ReadNetFromCaffe(ReadOnlySpan<byte> bufferProto, ReadOnlySpan<byte> bufferModel = default)

Parameters

bufferProto ReadOnlySpan<byte>

buffer containing the content of the .prototxt file

bufferModel ReadOnlySpan<byte>

buffer containing the content of the .caffemodel file

Returns

Net

Remarks

This is shortcut consisting from createCaffeImporter and Net::populateNet calls.

ReadNetFromCaffe(string, string?)

Reads a network model stored in Caffe model files.

public static Net? ReadNetFromCaffe(string prototxt, string? caffeModel = null)

Parameters

prototxt string

path to the .prototxt file with text description of the network architecture.

caffeModel string

path to the .caffemodel file with learned network.

Returns

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.

public static Net? ReadNetFromDarknet(byte[] bufferCfg, byte[]? bufferModel = null)

Parameters

bufferCfg byte[]

A buffer contains a content of .cfg file with text description of the network architecture.

bufferModel byte[]

A buffer contains a content of .weights file with learned network.

Returns

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.

public static Net? ReadNetFromDarknet(ReadOnlySpan<byte> bufferCfg, ReadOnlySpan<byte> bufferModel = default)

Parameters

bufferCfg ReadOnlySpan<byte>

A buffer contains a content of .cfg file with text description of the network architecture.

bufferModel ReadOnlySpan<byte>

A buffer contains a content of .weights file with learned network.

Returns

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.

public static Net? ReadNetFromDarknet(string cfgFile, string? darknetModel = null)

Parameters

cfgFile string

path to the .cfg file with text description of the network architecture.

darknetModel string

path to the .weights file with learned network.

Returns

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.

public static Net? ReadNetFromModelOptimizer(string xml, string bin)

Parameters

xml string

XML configuration file with network's topology.

bin string

Binary file with trained weights.

Returns

Net

ReadNetFromONNX(byte[])

Reads a network model ONNX https://onnx.ai/ from memory

public static Net? ReadNetFromONNX(byte[] onnxFileData)

Parameters

onnxFileData byte[]

memory of the first byte of the buffer.

Returns

Net

Network object that ready to do forward, throw an exception in failure cases.

ReadNetFromONNX(ReadOnlySpan<byte>)

Reads a network model ONNX https://onnx.ai/ from memory

public static Net? ReadNetFromONNX(ReadOnlySpan<byte> onnxFileData)

Parameters

onnxFileData ReadOnlySpan<byte>

memory of the first byte of the buffer.

Returns

Net

Network object that ready to do forward, throw an exception in failure cases.

ReadNetFromONNX(string)

Reads a network model ONNX https://onnx.ai/

public static Net? ReadNetFromONNX(string onnxFile)

Parameters

onnxFile string

path to the .onnx file with text description of the network architecture.

Returns

Net

Network object that ready to do forward, throw an exception in failure cases.

ReadNetFromTensorflow(byte[], byte[]?)

Reads a network model stored in Tensorflow model from memory.

public static Net? ReadNetFromTensorflow(byte[] bufferModel, byte[]? bufferConfig = null)

Parameters

bufferModel byte[]

buffer containing the content of the pb file

bufferConfig byte[]

buffer containing the content of the pbtxt file (optional)

Returns

Net

Remarks

This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.

ReadNetFromTensorflow(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Reads a network model stored in Tensorflow model from memory.

public static Net? ReadNetFromTensorflow(ReadOnlySpan<byte> bufferModel, ReadOnlySpan<byte> bufferConfig = default)

Parameters

bufferModel ReadOnlySpan<byte>

buffer containing the content of the pb file

bufferConfig ReadOnlySpan<byte>

buffer containing the content of the pbtxt file (optional)

Returns

Net

Remarks

This is shortcut consisting from createTensorflowImporter and Net::populateNet calls.

ReadNetFromTensorflow(string, string?)

Reads a network model stored in Tensorflow model file.

public static Net? ReadNetFromTensorflow(string model, string? config = null)

Parameters

model string

path to the .pb file with binary protobuf description of the network architecture

config string

path to the .pbtxt file that contains text graph definition in protobuf format.

Returns

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, bool)

Reads a network model stored in Torch model file.

public static Net? ReadNetFromTorch(string model, bool isBinary = true)

Parameters

model string
isBinary bool

Returns

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.

public void SetHalideScheduler(string scheduler)

Parameters

scheduler string

Path to YAML file with scheduling directives.

SetInput(Mat, string)

Sets the new value for the layer output blob

public void SetInput(Mat blob, string name = "")

Parameters

blob Mat

new blob.

name string

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.

public void SetInputsNames(IEnumerable<string> inputBlobNames)

Parameters

inputBlobNames IEnumerable<string>

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.

public void SetPreferableBackend(Backend backendId)

Parameters

backendId Backend

backend identifier.

SetPreferableTarget(Target)

Ask network to make computations on specific target device.

public void SetPreferableTarget(Target targetId)

Parameters

targetId Target

target identifier.