Skip to main content

Overview

The Net class is the core of OpenCV’s DNN module. It represents a neural network loaded from various frameworks and provides methods for inference.

Loading Models

readNet (Auto-detect)

Automatically detects model format:

Framework-Specific Loaders

ONNX

TensorFlow

Caffe

Darknet (YOLO)

Backend and Target

setPreferableBackend

Available backends:
  • DNN_BACKEND_DEFAULT: Default OpenCV implementation
  • DNN_BACKEND_OPENCV: Pure OpenCV implementation
  • DNN_BACKEND_CUDA: NVIDIA CUDA
  • DNN_BACKEND_INFERENCE_ENGINE: Intel OpenVINO
  • DNN_BACKEND_VKCOM: Vulkan

setPreferableTarget

Available targets:
  • DNN_TARGET_CPU: CPU
  • DNN_TARGET_OPENCL: OpenCL (GPU)
  • DNN_TARGET_OPENCL_FP16: OpenCL with FP16
  • DNN_TARGET_CUDA: CUDA
  • DNN_TARGET_CUDA_FP16: CUDA with FP16
  • DNN_TARGET_MYRIAD: Intel Myriad
  • DNN_TARGET_FPGA: FPGA

Backend/Target Compatibility

Setting Inputs

setInput

Parameters:
  • blob: 4D blob (NCHW format)
  • name: Input layer name (optional if single input)
  • scalefactor: Multiplicative scaling factor
  • mean: Mean values to subtract

Forward Pass

forward (Single Output)

forward (Multiple Outputs)

forwardAsync (Asynchronous)

Network Information

empty

Check if network is loaded:

getLayerNames

Get all layer names:

getLayerId

Get layer ID by name:

getLayer

Get layer object:

getUnconnectedOutLayersNames

Get output layer names:

Network Modification

setInputsNames

setInputShape

getParam

Get layer parameters (weights):

setParam

Set layer parameters:

Performance Analysis

getPerfProfile

Get layer-wise timing:

getFLOPS

Compute FLOPs:

getMemoryConsumption

Get memory usage:

Network Optimization

enableFusion

Enable/disable layer fusion:

enableWinograd

Enable Winograd convolution optimization:

Debugging

dump

Get network structure:

dumpToFile

Save structure to file:

dumpToPbtxt

Save as protobuf text (viewable in Netron):

Complete Example

See Also