Net

Objective-C

@interface Net : NSObject

Swift

class Net : NSObject

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 of Dnn

Methods

  • Declaration

    Objective-C

    - (instancetype)init;

    Swift

    init()
  • Runs forward pass to compute output of layer with name @p outputName.

    Declaration

    Objective-C

    - (nonnull Mat *)forward:(nonnull NSString *)outputName;

    Swift

    func forward(outputName: String) -> Mat

    Parameters

    outputName

    name for layer which output is needed to get

    Return Value

    blob for first output of specified layer. By default runs forward pass for the whole network.

  • Runs forward pass to compute output of layer with name @p outputName.

    Declaration

    Objective-C

    - (nonnull Mat *)forward;

    Swift

    func forward() -> Mat

    Return Value

    blob for first output of specified layer. By default runs forward pass for the whole network.

  • Returns parameter blob of the layer.

    See

    Layer::blobs

    Declaration

    Objective-C

    - (nonnull Mat *)getParam:(nonnull DictValue *)layer numParam:(int)numParam;

    Swift

    func getParam(layer: DictValue, numParam: Int32) -> Mat

    Parameters

    layer

    name or id of the layer.

    numParam

    index of the layer parameter in the Layer::blobs array.

  • Returns parameter blob of the layer.

    See

    Layer::blobs

    Declaration

    Objective-C

    - (nonnull Mat *)getParam:(nonnull DictValue *)layer;

    Swift

    func getParam(layer: DictValue) -> Mat

    Parameters

    layer

    name or id of the layer.

  • Create a network from Intel’s Model Optimizer intermediate representation (IR).

    Declaration

    Objective-C

    + (nonnull Net *)readFromModelOptimizer:(nonnull NSString *)xml
                                        bin:(nonnull NSString *)bin;

    Swift

    class func readFromModelOptimizer(xml: String, bin: String) -> Net

    Parameters

    xml

    XML configuration file with network’s topology.

    bin

    Binary file with trained weights. Networks imported from Intel’s Model Optimizer are launched in Intel’s Inference Engine backend.

  • Create a network from Intel’s Model Optimizer in-memory buffers with intermediate representation (IR).

    Declaration

    Objective-C

    + (nonnull Net *)readFromModelOptimizer:(nonnull ByteVector *)bufferModelConfig
                              bufferWeights:(nonnull ByteVector *)bufferWeights;

    Swift

    class func readFromModelOptimizer(bufferModelConfig: ByteVector, bufferWeights: ByteVector) -> Net

    Parameters

    bufferModelConfig

    buffer with model’s configuration.

    bufferWeights

    buffer with model’s trained weights.

    Return Value

    Net object.

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

    Declaration

    Objective-C

    - (nonnull Layer *)getLayer:(nonnull DictValue *)layerId;

    Swift

    func getLayer(layerId: DictValue) -> Layer
  • Dump net to String

    Declaration

    Objective-C

    - (nonnull NSString *)dump;

    Swift

    func dump() -> String

    Return Value

    String with structure, hyperparameters, backend, target and fusion Call method after setInput(). To see correct backend, target and fusion run after forward().

  • Returns true if there are no layers in the network.

    Declaration

    Objective-C

    - (BOOL)empty;

    Swift

    func empty() -> Bool
  • Converts string name of the layer to the integer identifier.

    Declaration

    Objective-C

    - (int)getLayerId:(nonnull NSString *)layer;

    Swift

    func getLayerId(layer: String) -> Int32

    Return Value

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

  • Returns count of layers of specified type.

    Declaration

    Objective-C

    - (int)getLayersCount:(nonnull NSString *)layerType;

    Swift

    func getLayersCount(layerType: String) -> Int32

    Parameters

    layerType

    type.

    Return Value

    count of layers

  • Declaration

    Objective-C

    - (long)getFLOPSWithNetInputShape:(IntVector*)netInputShape NS_SWIFT_NAME(getFLOPS(netInputShape:));

    Swift

    func getFLOPS(netInputShape: IntVector) -> Int
  • Declaration

    Objective-C

    - (long)getFLOPSWithLayerId:(int)layerId netInputShape:(IntVector*)netInputShape NS_SWIFT_NAME(getFLOPS(layerId:netInputShape:));

    Swift

    func getFLOPS(layerId: Int32, netInputShape: IntVector) -> Int
  • Declaration

    Objective-C

    - (long)getFLOPSWithLayerId:(int)layerId netInputShapes:(NSArray<IntVector*>*)netInputShapes NS_SWIFT_NAME(getFLOPS(layerId:netInputShapes:));

    Swift

    func getFLOPS(layerId: Int32, netInputShapes: [IntVector]) -> Int
  • Computes FLOP for whole loaded model with specified input shapes.

    Declaration

    Objective-C

    - (long)getFLOPSWithNetInputShapes:
        (nonnull NSArray<IntVector *> *)netInputShapes;

    Swift

    func getFLOPS(netInputShapes: [IntVector]) -> Int

    Parameters

    netInputShapes

    vector of shapes for all net inputs.

    Return Value

    computed FLOP.

  • 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

    Objective-C

    - (long)getPerfProfile:(nonnull DoubleVector *)timings;

    Swift

    func getPerfProfile(timings: DoubleVector) -> Int

    Parameters

    timings

    vector for tick timings for all layers.

    Return Value

    overall ticks for model inference.

  • Declaration

    Objective-C

    - (NSArray<NSString*>*)getLayerNames NS_SWIFT_NAME(getLayerNames());

    Swift

    func getLayerNames() -> [String]
  • Returns names of layers with unconnected outputs.

    Declaration

    Objective-C

    - (nonnull NSArray<NSString *> *)getUnconnectedOutLayersNames;

    Swift

    func getUnconnectedOutLayersNames() -> [String]
  • Returns indexes of layers with unconnected outputs.

    Declaration

    Objective-C

    - (nonnull IntVector *)getUnconnectedOutLayers;

    Swift

    func getUnconnectedOutLayers() -> IntVector
  • Connects output of the first layer to input of the second layer.

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

    • the first part of the template layer_name is string 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

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

    Declaration

    Objective-C

    - (void)connect:(nonnull NSString *)outPin inpPin:(nonnull NSString *)inpPin;

    Swift

    func connect(outPin: String, inpPin: String)

    Parameters

    outPin

    descriptor of the first layer output.

    inpPin

    descriptor of the second layer input.

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

    See

    dump()

    Declaration

    Objective-C

    - (void)dumpToFile:(nonnull NSString *)path;

    Swift

    func dumpToFile(path: String)

    Parameters

    path

    path to output file with .dot extension

  • Enables or disables layer fusion in the network.

    Declaration

    Objective-C

    - (void)enableFusion:(BOOL)fusion;

    Swift

    func enableFusion(fusion: Bool)

    Parameters

    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 @p outputName.

    Declaration

    Objective-C

    - (void)forwardOutputBlobs:(nonnull NSMutableArray<Mat *> *)outputBlobs
                    outputName:(nonnull NSString *)outputName;

    Swift

    func forward(outputBlobs: NSMutableArray, outputName: String)

    Parameters

    outputBlobs

    contains all output blobs for specified layer.

    outputName

    name for layer which output is needed to get If @p outputName is empty, runs forward pass for the whole network.

  • Runs forward pass to compute output of layer with name @p outputName.

    Declaration

    Objective-C

    - (void)forwardOutputBlobs:(nonnull NSMutableArray<Mat *> *)outputBlobs;

    Swift

    func forward(outputBlobs: NSMutableArray)

    Parameters

    outputBlobs

    contains all output blobs for specified layer. If @p outputName is empty, runs forward pass for the whole network.

  • Runs forward pass to compute outputs of layers listed in @p outBlobNames.

    Declaration

    Objective-C

    - (void)forwardOutputBlobs:(nonnull NSMutableArray<Mat *> *)outputBlobs
                  outBlobNames:(nonnull NSArray<NSString *> *)outBlobNames;

    Swift

    func forward(outputBlobs: NSMutableArray, outBlobNames: [String])

    Parameters

    outputBlobs

    contains blobs for first outputs of specified layers.

    outBlobNames

    names for layers which outputs are needed to get

  • Runs forward pass to compute outputs of layers listed in @p outBlobNames.

    Declaration

    Objective-C

    - (void)forwardAndRetrieve:
                (nonnull NSMutableArray<NSMutableArray<Mat *> *> *)outputBlobs
                  outBlobNames:(nonnull NSArray<NSString *> *)outBlobNames;

    Swift

    func forward(outputBlobs: NSMutableArray, outBlobNames: [String])

    Parameters

    outputBlobs

    contains all output blobs for each layer specified in @p outBlobNames.

    outBlobNames

    names for layers which outputs are needed to get

  • Returns list of types for layer used in model.

    Declaration

    Objective-C

    - (void)getLayerTypes:(nonnull NSMutableArray<NSString *> *)layersTypes;

    Swift

    func getLayerTypes(layersTypes: NSMutableArray)

    Parameters

    layersTypes

    output parameter for returning types.

  • Declaration

    Objective-C

    - (void)getLayersShapesWithNetInputShape:(IntVector*)netInputShape layersIds:(IntVector*)layersIds inLayersShapes:(NSMutableArray<NSMutableArray<IntVector*>*>*)inLayersShapes outLayersShapes:(NSMutableArray<NSMutableArray<IntVector*>*>*)outLayersShapes NS_SWIFT_NAME(getLayersShapes(netInputShape:layersIds:inLayersShapes:outLayersShapes:));

    Swift

    func getLayersShapes(netInputShape: IntVector, layersIds: IntVector, inLayersShapes: NSMutableArray, outLayersShapes: NSMutableArray)
  • Returns input and output shapes for all layers in loaded model; preliminary inferencing isn’t necessary.

    Declaration

    Objective-C

    - (void)
        getLayersShapesWithNetInputShapes:
            (nonnull NSArray<IntVector *> *)netInputShapes
                                layersIds:(nonnull IntVector *)layersIds
                           inLayersShapes:
                               (nonnull
                                    NSMutableArray<NSMutableArray<IntVector *> *> *)
                                   inLayersShapes
                          outLayersShapes:
                              (nonnull NSMutableArray<NSMutableArray<IntVector *> *>
                                   *)outLayersShapes;

    Swift

    func getLayersShapes(netInputShapes: [IntVector], layersIds: IntVector, inLayersShapes: NSMutableArray, outLayersShapes: NSMutableArray)

    Parameters

    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

  • Declaration

    Objective-C

    - (void)getMemoryConsumption:(IntVector*)netInputShape weights:(size_t)weights blobs:(size_t)blobs NS_SWIFT_NAME(getMemoryConsumption(netInputShape:weights:blobs:));

    Swift

    func getMemoryConsumption(netInputShape: IntVector, weights: Int, blobs: Int)
  • Declaration

    Objective-C

    - (void)getMemoryConsumption:(int)layerId netInputShape:(IntVector*)netInputShape weights:(size_t)weights blobs:(size_t)blobs NS_SWIFT_NAME(getMemoryConsumption(layerId:netInputShape:weights:blobs:));

    Swift

    func getMemoryConsumption(layerId: Int32, netInputShape: IntVector, weights: Int, blobs: Int)
  • Declaration

    Objective-C

    - (void)getMemoryConsumption:(int)layerId netInputShapes:(NSArray<IntVector*>*)netInputShapes weights:(size_t)weights blobs:(size_t)blobs NS_SWIFT_NAME(getMemoryConsumption(layerId:netInputShapes:weights:blobs:));

    Swift

    func getMemoryConsumption(layerId: Int32, netInputShapes: [IntVector], weights: Int, blobs: Int)
  • 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

    Objective-C

    - (void)setHalideScheduler:(nonnull NSString *)scheduler;

    Swift

    func setHalideScheduler(scheduler: String)

    Parameters

    scheduler

    Path to YAML file with scheduling directives.

  • Sets the new input value for the network

    See

    connect(String, String) to know format of the descriptor.

    If scale or mean values are specified, a final input blob is computed as:

    input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)

    Declaration

    Objective-C

    - (void)setInput:(nonnull Mat *)blob
                name:(nonnull NSString *)name
         scalefactor:(double)scalefactor
                mean:(nonnull Scalar *)mean;

    Swift

    func setInput(blob: Mat, name: String, scalefactor: Double, mean: Scalar)

    Parameters

    blob

    A new blob. Should have CV_32F or CV_8U depth.

    name

    A name of input layer.

    scalefactor

    An optional normalization scale.

    mean

    An optional mean subtraction values.

  • Sets the new input value for the network

    See

    connect(String, String) to know format of the descriptor.

    If scale or mean values are specified, a final input blob is computed as:

    input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)

    Declaration

    Objective-C

    - (void)setInput:(nonnull Mat *)blob
                name:(nonnull NSString *)name
         scalefactor:(double)scalefactor;

    Swift

    func setInput(blob: Mat, name: String, scalefactor: Double)

    Parameters

    blob

    A new blob. Should have CV_32F or CV_8U depth.

    name

    A name of input layer.

    scalefactor

    An optional normalization scale.

  • Sets the new input value for the network

    See

    connect(String, String) to know format of the descriptor.

    If scale or mean values are specified, a final input blob is computed as:

    input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)

    Declaration

    Objective-C

    - (void)setInput:(nonnull Mat *)blob name:(nonnull NSString *)name;

    Swift

    func setInput(blob: Mat, name: String)

    Parameters

    blob

    A new blob. Should have CV_32F or CV_8U depth.

    name

    A name of input layer.

  • Sets the new input value for the network

    See

    connect(String, String) to know format of the descriptor.

    If scale or mean values are specified, a final input blob is computed as:

    input(n,c,h,w) = scalefactor \times (blob(n,c,h,w) - mean_c)

    Declaration

    Objective-C

    - (void)setInput:(nonnull Mat *)blob;

    Swift

    func setInput(blob: Mat)

    Parameters

    blob

    A new blob. Should have CV_32F or CV_8U depth.

  • Specify shape of network input.

    Declaration

    Objective-C

    - (void)setInputShape:(nonnull NSString *)inputName
                    shape:(nonnull IntVector *)shape;

    Swift

    func setInputShape(inputName: String, shape: IntVector)
  • 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.

    Declaration

    Objective-C

    - (void)setInputsNames:(nonnull NSArray<NSString *> *)inputBlobNames;

    Swift

    func setInputsNames(inputBlobNames: [String])
  • Sets the new value for the learned param of the layer.

    See

    Layer::blobs

    Note

    If shape of the new blob differs from the previous shape, then the following forward pass may fail.

    Declaration

    Objective-C

    - (void)setParam:(nonnull DictValue *)layer
            numParam:(int)numParam
                blob:(nonnull Mat *)blob;

    Swift

    func setParam(layer: DictValue, numParam: Int32, blob: Mat)

    Parameters

    layer

    name or id of the layer.

    numParam

    index of the layer parameter in the Layer::blobs array.

    blob

    the new value.

  • Ask network to use specific computation backend where it supported.

    See

    Backend

    If OpenCV is compiled with Intel’s Inference Engine library, DNN_BACKEND_DEFAULT means DNN_BACKEND_INFERENCE_ENGINE. Otherwise it equals to DNN_BACKEND_OPENCV.

    Declaration

    Objective-C

    - (void)setPreferableBackend:(int)backendId;

    Swift

    func setPreferableBackend(backendId: Int32)

    Parameters

    backendId

    backend identifier.

  • Ask network to make computations on specific target device.

    See

    Target

    List of supported combinations backend / target: | | DNN_BACKEND_OPENCV | DNN_BACKEND_INFERENCE_ENGINE | DNN_BACKEND_HALIDE | DNN_BACKEND_CUDA | |————————|——————–|——————————|——————–|——————-| | DNN_TARGET_CPU | + | + | + | | | DNN_TARGET_OPENCL | + | + | + | | | DNN_TARGET_OPENCL_FP16 | + | + | | | | DNN_TARGET_MYRIAD | | + | | | | DNN_TARGET_FPGA | | + | | | | DNN_TARGET_CUDA | | | | + | | DNN_TARGET_CUDA_FP16 | | | | + |

    Declaration

    Objective-C

    - (void)setPreferableTarget:(int)targetId;

    Swift

    func setPreferableTarget(targetId: Int32)

    Parameters

    targetId

    target identifier.