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
Conv(c1, c2, k=1, s=1, p=None, g=1, d=1, act=True)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.Conv[Conv]
click ultralytics.nn.modules.conv.Conv href "" "ultralytics.nn.modules.conv.Conv"
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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
forward
forward(x)
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
69 70 71 72 73 74 75 76 77 78 | |
forward_fuse
forward_fuse(x)
Apply convolution and activation without batch normalization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
| Input tensor. | required |
Returns:
| Type | Description |
|---|---|
Tensor
| Output tensor. |
Source code in ultralytics/nn/modules/conv.py
80 81 82 83 84 85 86 87 88 89 | |
ultralytics.nn.modules.conv.Conv2
Conv2(c1, c2, k=3, s=1, p=None, g=1, d=1, act=True)
Bases: Conv
flowchart TD
ultralytics.nn.modules.conv.Conv2[Conv2]
ultralytics.nn.modules.conv.Conv[Conv]
ultralytics.nn.modules.conv.Conv --> ultralytics.nn.modules.conv.Conv2
click ultralytics.nn.modules.conv.Conv2 href "" "ultralytics.nn.modules.conv.Conv2"
click ultralytics.nn.modules.conv.Conv href "" "ultralytics.nn.modules.conv.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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
forward
forward(x)
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
118 119 120 121 122 123 124 125 126 127 | |
forward_fuse
forward_fuse(x)
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
129 130 131 132 133 134 135 136 137 138 | |
fuse_convs
fuse_convs()
Fuse parallel convolutions.
Source code in ultralytics/nn/modules/conv.py
140 141 142 143 144 145 146 147 | |
ultralytics.nn.modules.conv.LightConv
LightConv(c1, c2, k=1, act=nn.ReLU())
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.LightConv[LightConv]
click ultralytics.nn.modules.conv.LightConv href "" "ultralytics.nn.modules.conv.LightConv"
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
160 161 162 163 164 165 166 167 168 169 170 171 | |
forward
forward(x)
Apply 2 convolutions 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
173 174 175 176 177 178 179 180 181 182 | |
ultralytics.nn.modules.conv.DWConv
DWConv(c1, c2, k=1, s=1, d=1, act=True)
Bases: Conv
flowchart TD
ultralytics.nn.modules.conv.DWConv[DWConv]
ultralytics.nn.modules.conv.Conv[Conv]
ultralytics.nn.modules.conv.Conv --> ultralytics.nn.modules.conv.DWConv
click ultralytics.nn.modules.conv.DWConv href "" "ultralytics.nn.modules.conv.DWConv"
click ultralytics.nn.modules.conv.Conv href "" "ultralytics.nn.modules.conv.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
188 189 190 191 192 193 194 195 196 197 198 199 | |
ultralytics.nn.modules.conv.DWConvTranspose2d
DWConvTranspose2d(c1, c2, k=1, s=1, p1=0, p2=0)
Bases: ConvTranspose2d
flowchart TD
ultralytics.nn.modules.conv.DWConvTranspose2d[DWConvTranspose2d]
click ultralytics.nn.modules.conv.DWConvTranspose2d href "" "ultralytics.nn.modules.conv.DWConvTranspose2d"
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
205 206 207 208 209 210 211 212 213 214 215 216 | |
ultralytics.nn.modules.conv.ConvTranspose
ConvTranspose(c1, c2, k=2, s=2, p=0, bn=True, act=True)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.ConvTranspose[ConvTranspose]
click ultralytics.nn.modules.conv.ConvTranspose href "" "ultralytics.nn.modules.conv.ConvTranspose"
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
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
forward
forward(x)
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
248 249 250 251 252 253 254 255 256 257 | |
forward_fuse
forward_fuse(x)
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
259 260 261 262 263 264 265 266 267 268 | |
ultralytics.nn.modules.conv.Focus
Focus(c1, c2, k=1, s=1, p=None, g=1, act=True)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.Focus[Focus]
click ultralytics.nn.modules.conv.Focus href "" "ultralytics.nn.modules.conv.Focus"
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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
forward
forward(x)
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
296 297 298 299 300 301 302 303 304 305 306 307 | |
ultralytics.nn.modules.conv.GhostConv
GhostConv(c1, c2, k=1, s=1, g=1, act=True)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.GhostConv[GhostConv]
click ultralytics.nn.modules.conv.GhostConv href "" "ultralytics.nn.modules.conv.GhostConv"
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. |
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
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
forward
forward(x)
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
340 341 342 343 344 345 346 347 348 349 350 | |
ultralytics.nn.modules.conv.RepConv
RepConv(c1, c2, k=3, s=1, p=1, g=1, d=1, act=True, bn=False, deploy=False)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.RepConv[RepConv]
click ultralytics.nn.modules.conv.RepConv href "" "ultralytics.nn.modules.conv.RepConv"
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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
forward
forward(x)
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
408 409 410 411 412 413 414 415 416 417 418 | |
forward_fuse
forward_fuse(x)
Forward pass for deploy 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
397 398 399 400 401 402 403 404 405 406 | |
fuse_convs
fuse_convs()
Fuse convolutions for inference by creating a single equivalent convolution.
Source code in ultralytics/nn/modules/conv.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
get_equivalent_kernel_bias
get_equivalent_kernel_bias()
Calculate equivalent kernel and bias by fusing convolutions.
Returns:
| Type | Description |
|---|---|
Tensor
| Equivalent kernel |
Tensor
| Equivalent bias |
Source code in ultralytics/nn/modules/conv.py
420 421 422 423 424 425 426 427 428 429 430 | |
ultralytics.nn.modules.conv.ChannelAttention
ChannelAttention(channels: int)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.ChannelAttention[ChannelAttention]
click ultralytics.nn.modules.conv.ChannelAttention href "" "ultralytics.nn.modules.conv.ChannelAttention"
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
526 527 528 529 530 531 532 533 534 535 | |
forward
forward(x: Tensor) -> torch.Tensor
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
537 538 539 540 541 542 543 544 545 546 | |
ultralytics.nn.modules.conv.SpatialAttention
SpatialAttention(kernel_size=7)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.SpatialAttention[SpatialAttention]
click ultralytics.nn.modules.conv.SpatialAttention href "" "ultralytics.nn.modules.conv.SpatialAttention"
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
559 560 561 562 563 564 565 566 567 568 569 | |
forward
forward(x)
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
571 572 573 574 575 576 577 578 579 580 | |
ultralytics.nn.modules.conv.CBAM
CBAM(c1, kernel_size=7)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.CBAM[CBAM]
click ultralytics.nn.modules.conv.CBAM href "" "ultralytics.nn.modules.conv.CBAM"
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
593 594 595 596 597 598 599 600 601 602 | |
forward
forward(x)
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
604 605 606 607 608 609 610 611 612 613 | |
ultralytics.nn.modules.conv.Concat
Concat(dimension=1)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.Concat[Concat]
click ultralytics.nn.modules.conv.Concat href "" "ultralytics.nn.modules.conv.Concat"
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
623 624 625 626 627 628 629 630 | |
forward
forward(x: list[Tensor])
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. |
Source code in ultralytics/nn/modules/conv.py
632 633 634 635 636 637 638 639 640 641 | |
ultralytics.nn.modules.conv.Index
Index(index=0)
Bases: Module
flowchart TD
ultralytics.nn.modules.conv.Index[Index]
click ultralytics.nn.modules.conv.Index href "" "ultralytics.nn.modules.conv.Index"
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
651 652 653 654 655 656 657 658 | |
forward
forward(x: list[Tensor])
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. |
Source code in ultralytics/nn/modules/conv.py
660 661 662 663 664 665 666 667 668 669 | |
ultralytics.nn.modules.conv.autopad
autopad(k, p=None, d=1)
Pad to 'same' shape outputs.
Source code in ultralytics/nn/modules/conv.py
30 31 32 33 34 35 36 | |