Overview
OpenCV DNN module supports numerous layer types from different frameworks. This document covers common layers and how to implement custom ones.Base Layer Class
Layer Interface
Convolution Layers
Convolution
Type:Convolution
Parameters:
num_output: Number of output channelskernel_size: Kernel dimensionsstride: Stridepad: Paddingdilation: Dilationgroup: Number of groups
Deconvolution
Type:Deconvolution / ConvolutionTranspose
Transposed convolution for upsampling.
Depthwise Convolution
Implemented as regular convolution withgroup = num_input.
Pooling Layers
MaxPooling
Type:Pooling with pool: MAX
Parameters:
kernel_size: Pool window sizestride: Stridepad: Padding
AveragePooling
Type:Pooling with pool: AVE
GlobalPooling
Type:Pooling with global_pooling: true
Reduces spatial dimensions to 1x1.
Activation Layers
ReLU
Type:ReLU
LeakyReLU
Type:ReLU with negative_slope
Parameters:
negative_slope: Slope for negative values (e.g., 0.1)
PReLU
Type:PReLU
Parametric ReLU with learned slopes.
ELU
Type:ELU
Parameters:
alpha: Scale factor
Sigmoid
Type:Sigmoid
TanH
Type:TanH
Swish / SiLU
Type:Swish
Mish
Type:Mish
Normalization Layers
BatchNormalization
Type:BatchNorm
Parameters:
eps: Epsilon for numerical stability- Learned parameters: gamma, beta, mean, variance
LayerNormalization
Type:LayerNorm
Normalizes across channel dimension.
InstanceNormalization
Type:InstanceNorm
Normalizes each sample independently.
GroupNormalization
Type:GroupNorm
Parameters:
num_groups: Number of groups
Fully Connected Layers
InnerProduct / Dense
Type:InnerProduct
Parameters:
num_output: Output dimension
Reshape Layers
Reshape
Type:Reshape
Parameters:
dim: New dimensions (can use -1 for auto)
Flatten
Type:Flatten
Reshapes to 2D (batch_size, features).
Permute
Type:Permute
Transposes dimensions.
Parameters:
order: New axis order (e.g., [0, 2, 3, 1])
Slice
Type:Slice
Slices along an axis.
Parameters:
axis: Axis to sliceslice_point: Split points
Concat
Type:Concat
Concatenates along an axis.
Parameters:
axis: Concatenation axis
Attention Layers
Attention (Generic)
Type:Attention
Multi-head self-attention mechanism.
ScaledDotProductAttention
Implemented for transformer models.Dropout
Type:Dropout
Parameters:
dropout_ratio: Probability of dropping (0-1)
Dropout is typically disabled during inference (automatically handled).
Element-wise Operations
Eltwise
Type:Eltwise
Operations:
SUM: Element-wise additionPROD: Element-wise multiplicationMAX: Element-wise maximum
operation: Operation typecoeff: Optional coefficients for SUM
Scale
Type:Scale
Scales and shifts: output = scale * input + bias
Shift
Type:Shift
Adds a bias term.
Upsampling Layers
Resize
Type:Resize / Upsample
Parameters:
zoom_factor: Scale factorinterpolation: NEAREST, BILINEAR
UpsamplingNearest
Type:ResizeNearest
Nearest neighbor upsampling.
UpsamplingBilinear
Type:ResizeBilinear
Bilinear upsampling.
Utility Layers
Split
Type:Split
Duplicates input to multiple outputs.
Crop
Type:Crop
Crops spatial dimensions.
Padding
Type:Padding
Adds padding to input.
Exp
Type:Exp
Element-wise exponential.
Log
Type:Log
Element-wise logarithm.
Power
Type:Power
Raises to power: output = (shift + scale * input) ^ power
Abs
Type:AbsVal
Absolute value.
BNLL
Type:BNLL
Binomial normal log likelihood.
Custom Layers
Implementing Custom Layer
Registering Custom Layer
Using Custom Layer
The layer will be automatically used when loading models containing that layer type.Layer Parameters
LayerParams Class
Accessing Parameters
Backend Support
CPU Implementation
Default implementation for all layers.OpenCL Implementation
Many layers have optimized OpenCL kernels.CUDA Implementation
Adding Backend Support
Best Practices
Check Blob Sizes
Validate parameter blob dimensions in constructor
Implement getMemoryShapes
Define output shapes for memory allocation
Support Multiple Backends
Provide CUDA/OpenCL implementations when possible
Test Thoroughly
Compare outputs with reference implementation
See Also
- Net Class - Network loading and inference
- DNN Module - DNN module overview
- DNN Inference - Inference utilities
