Reference for ultralytics/nn/modules/conv.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/modules/conv.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.nn.modules.conv.Conv
Bases: Module
Standard convolution module with batch normalization and activation.
Attributes:
Name | Type | Description |
---|---|---|
conv |
Conv2d
|
Convolutional layer. |
bn |
BatchNorm2d
|
Batch normalization layer. |
act |
Module
|
Activation function layer. |
default_act |
Module
|
Default activation function (SiLU). |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
p
|
int
|
Padding. |
None
|
g
|
int
|
Groups. |
1
|
d
|
int
|
Dilation. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply convolution, batch normalization and activation to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
forward_fuse
Apply convolution and activation without batch normalization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
ultralytics.nn.modules.conv.Conv2
Bases: Conv
Simplified RepConv module with Conv fusing.
Attributes:
Name | Type | Description |
---|---|---|
conv |
Conv2d
|
Main 3x3 convolutional layer. |
cv2 |
Conv2d
|
Additional 1x1 convolutional layer. |
bn |
BatchNorm2d
|
Batch normalization layer. |
act |
Module
|
Activation function layer. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
3
|
s
|
int
|
Stride. |
1
|
p
|
int
|
Padding. |
None
|
g
|
int
|
Groups. |
1
|
d
|
int
|
Dilation. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply convolution, batch normalization and activation to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
forward_fuse
Apply fused convolution, batch normalization and activation to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
fuse_convs
Fuse parallel convolutions.
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.LightConv
Bases: Module
Light convolution module with 1x1 and depthwise convolutions.
This implementation is based on the PaddleDetection HGNetV2 backbone.
Attributes:
Name | Type | Description |
---|---|---|
conv1 |
Conv
|
1x1 convolution layer. |
conv2 |
DWConv
|
Depthwise convolution layer. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size for depthwise convolution. |
1
|
act
|
Module
|
Activation function. |
ReLU()
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply 2 convolutions to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
ultralytics.nn.modules.conv.DWConv
Bases: Conv
Depth-wise convolution module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
d
|
int
|
Dilation. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.DWConvTranspose2d
Bases: ConvTranspose2d
Depth-wise transpose convolution module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
p1
|
int
|
Padding. |
0
|
p2
|
int
|
Output padding. |
0
|
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.ConvTranspose
Bases: Module
Convolution transpose module with optional batch normalization and activation.
Attributes:
Name | Type | Description |
---|---|---|
conv_transpose |
ConvTranspose2d
|
Transposed convolution layer. |
bn |
BatchNorm2d | Identity
|
Batch normalization layer. |
act |
Module
|
Activation function layer. |
default_act |
Module
|
Default activation function (SiLU). |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
2
|
s
|
int
|
Stride. |
2
|
p
|
int
|
Padding. |
0
|
bn
|
bool
|
Use batch normalization. |
True
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply transposed convolution, batch normalization and activation to input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
forward_fuse
Apply activation and convolution transpose operation to input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.Focus
Bases: Module
Focus module for concentrating feature information.
Slices input tensor into 4 parts and concatenates them in the channel dimension.
Attributes:
Name | Type | Description |
---|---|---|
conv |
Conv
|
Convolution layer. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
p
|
int
|
Padding. |
None
|
g
|
int
|
Groups. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply Focus operation and convolution to input tensor.
Input shape is (b,c,w,h) and output shape is (b,4c,w/2,h/2).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.GhostConv
Bases: Module
Ghost Convolution module.
Generates more features with fewer parameters by using cheap operations.
Attributes:
Name | Type | Description |
---|---|---|
cv1 |
Conv
|
Primary convolution. |
cv2 |
Conv
|
Cheap operation convolution. |
References
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
g
|
int
|
Groups. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply Ghost Convolution to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor with concatenated features. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.RepConv
Bases: Module
RepConv module with training and deploy modes.
This module is used in RT-DETR and can fuse convolutions during inference for efficiency.
Attributes:
Name | Type | Description |
---|---|---|
conv1 |
Conv
|
3x3 convolution. |
conv2 |
Conv
|
1x1 convolution. |
bn |
BatchNorm2d
|
Batch normalization for identity branch. |
act |
Module
|
Activation function. |
default_act |
Module
|
Default activation function (SiLU). |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
k
|
int
|
Kernel size. |
3
|
s
|
int
|
Stride. |
1
|
p
|
int
|
Padding. |
1
|
g
|
int
|
Groups. |
1
|
d
|
int
|
Dilation. |
1
|
act
|
bool | Module
|
Activation function. |
True
|
bn
|
bool
|
Use batch normalization for identity branch. |
False
|
deploy
|
bool
|
Deploy mode for inference. |
False
|
Source code in ultralytics/nn/modules/conv.py
_fuse_bn_tensor
Fuse batch normalization with convolution weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
branch
|
Conv | BatchNorm2d | None
|
Branch to fuse. |
required |
Returns:
Type | Description |
---|---|
tuple
|
Tuple containing: - Fused kernel (torch.Tensor) - Fused bias (torch.Tensor) |
Source code in ultralytics/nn/modules/conv.py
_pad_1x1_to_3x3_tensor
staticmethod
Pad a 1x1 kernel to 3x3 size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel1x1
|
Tensor
|
1x1 convolution kernel. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Padded 3x3 kernel. |
Source code in ultralytics/nn/modules/conv.py
forward
Forward pass for training mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/conv.py
forward_fuse
Forward pass for deploy mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor. |
fuse_convs
Fuse convolutions for inference by creating a single equivalent convolution.
Source code in ultralytics/nn/modules/conv.py
get_equivalent_kernel_bias
Calculate equivalent kernel and bias by fusing convolutions.
Returns:
Type | Description |
---|---|
tuple
|
Tuple containing: - Equivalent kernel (torch.Tensor) - Equivalent bias (torch.Tensor) |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.ChannelAttention
Bases: Module
Channel-attention module for feature recalibration.
Applies attention weights to channels based on global average pooling.
Attributes:
Name | Type | Description |
---|---|---|
pool |
AdaptiveAvgPool2d
|
Global average pooling. |
fc |
Conv2d
|
Fully connected layer implemented as 1x1 convolution. |
act |
Sigmoid
|
Sigmoid activation for attention weights. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channels
|
int
|
Number of input channels. |
required |
Source code in ultralytics/nn/modules/conv.py
forward
Apply channel attention to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Channel-attended output tensor. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.SpatialAttention
Bases: Module
Spatial-attention module for feature recalibration.
Applies attention weights to spatial dimensions based on channel statistics.
Attributes:
Name | Type | Description |
---|---|---|
cv1 |
Conv2d
|
Convolution layer for spatial attention. |
act |
Sigmoid
|
Sigmoid activation for attention weights. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel_size
|
int
|
Size of the convolutional kernel (3 or 7). |
7
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply spatial attention to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Spatial-attended output tensor. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.CBAM
Bases: Module
Convolutional Block Attention Module.
Combines channel and spatial attention mechanisms for comprehensive feature refinement.
Attributes:
Name | Type | Description |
---|---|---|
channel_attention |
ChannelAttention
|
Channel attention module. |
spatial_attention |
SpatialAttention
|
Spatial attention module. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
kernel_size
|
int
|
Size of the convolutional kernel for spatial attention. |
7
|
Source code in ultralytics/nn/modules/conv.py
forward
Apply channel and spatial attention sequentially to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Attended output tensor. |
Source code in ultralytics/nn/modules/conv.py
ultralytics.nn.modules.conv.Concat
Bases: Module
Concatenate a list of tensors along specified dimension.
Attributes:
Name | Type | Description |
---|---|---|
d |
int
|
Dimension along which to concatenate tensors. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dimension
|
int
|
Dimension along which to concatenate tensors. |
1
|
Source code in ultralytics/nn/modules/conv.py
forward
Concatenate input tensors along specified dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
List[Tensor]
|
List of input tensors. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Concatenated tensor. |
ultralytics.nn.modules.conv.Index
Bases: Module
Returns a particular index of the input.
Attributes:
Name | Type | Description |
---|---|---|
index |
int
|
Index to select from input. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
Index to select from input. |
0
|
Source code in ultralytics/nn/modules/conv.py
forward
Select and return a particular index from input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
List[Tensor]
|
List of input tensors. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Selected tensor. |
ultralytics.nn.modules.conv.autopad
Pad to 'same' shape outputs.