Skip to main content

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 channels
  • kernel_size: Kernel dimensions
  • stride: Stride
  • pad: Padding
  • dilation: Dilation
  • group: Number of groups
Example network:

Deconvolution

Type: Deconvolution / ConvolutionTranspose Transposed convolution for upsampling.

Depthwise Convolution

Implemented as regular convolution with group = num_input.

Pooling Layers

MaxPooling

Type: Pooling with pool: MAX Parameters:
  • kernel_size: Pool window size
  • stride: Stride
  • pad: Padding
Example:

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 slice
  • slice_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 addition
  • PROD: Element-wise multiplication
  • MAX: Element-wise maximum
Parameters:
  • operation: Operation type
  • coeff: 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 factor
  • interpolation: 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