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
ptrnint
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
outLayerIdintidentifier of the first layer
outNumintidentifier of the second layer
inpLayerIdintnumber of the first layer output
inpNumintnumber 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
outPinstringdescriptor of the first layer output.
inpPinstringdescriptor 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
pathstringpath to output file with .dot extension
Empty()
Returns true if there are no layers in the network.
public bool Empty()
Returns
EnableFusion(bool)
Enables or disables layer fusion in the network.
public void EnableFusion(bool fusion)
Parameters
fusionbooltrue 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
outputBlobsIEnumerable<Mat>contains blobs for first outputs of specified layers.
outBlobNamesIEnumerable<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
outputBlobsIEnumerable<Mat>contains all output blobs for specified layer.
outputNamestringname 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
outputNamestringname 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
layerstring
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
timingsdouble[]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
xmlstringXML configuration file with network's topology.
binstringBinary file with trained weights.
Returns
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
modelstringBinary 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)configstringText 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)frameworkstringExplicit framework name tag to determine a format.
Returns
ReadNetFromCaffe(byte[], byte[]?)
Reads a network model stored in Caffe model in memory.
public static Net? ReadNetFromCaffe(byte[] bufferProto, byte[]? bufferModel = null)
Parameters
bufferProtobyte[]buffer containing the content of the .prototxt file
bufferModelbyte[]buffer containing the content of the .caffemodel file
Returns
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
bufferProtoReadOnlySpan<byte>buffer containing the content of the .prototxt file
bufferModelReadOnlySpan<byte>buffer containing the content of the .caffemodel file
Returns
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
prototxtstringpath to the .prototxt file with text description of the network architecture.
caffeModelstringpath to the .caffemodel file with learned network.
Returns
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
bufferCfgbyte[]A buffer contains a content of .cfg file with text description of the network architecture.
bufferModelbyte[]A buffer contains a content of .weights file with learned network.
Returns
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
bufferCfgReadOnlySpan<byte>A buffer contains a content of .cfg file with text description of the network architecture.
bufferModelReadOnlySpan<byte>A buffer contains a content of .weights file with learned network.
Returns
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
cfgFilestringpath to the .cfg file with text description of the network architecture.
darknetModelstringpath 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
xmlstringXML configuration file with network's topology.
binstringBinary file with trained weights.
Returns
ReadNetFromONNX(byte[])
Reads a network model ONNX https://onnx.ai/ from memory
public static Net? ReadNetFromONNX(byte[] onnxFileData)
Parameters
onnxFileDatabyte[]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
onnxFileDataReadOnlySpan<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
onnxFilestringpath 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
bufferModelbyte[]buffer containing the content of the pb file
bufferConfigbyte[]buffer containing the content of the pbtxt file (optional)
Returns
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
bufferModelReadOnlySpan<byte>buffer containing the content of the pb file
bufferConfigReadOnlySpan<byte>buffer containing the content of the pbtxt file (optional)
Returns
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
modelstringpath to the .pb file with binary protobuf description of the network architecture
configstringpath 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
Returns
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
schedulerstringPath 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
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
inputBlobNamesIEnumerable<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
backendIdBackendbackend identifier.
SetPreferableTarget(Target)
Ask network to make computations on specific target device.
public void SetPreferableTarget(Target targetId)
Parameters
targetIdTargettarget identifier.