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
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
50 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 79 |
|
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
81 82 83 84 85 86 87 88 89 90 91 |
|
ultralytics.nn.modules.conv.Conv2
Conv2(c1, c2, k=3, s=1, p=None, g=1, d=1, act=True)
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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
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
122 123 124 125 126 127 128 129 130 131 132 |
|
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
134 135 136 137 138 139 140 141 142 143 144 |
|
fuse_convs
fuse_convs()
Fuse parallel convolutions.
Source code in ultralytics/nn/modules/conv.py
146 147 148 149 150 151 152 153 |
|
ultralytics.nn.modules.conv.LightConv
LightConv(c1, c2, k=1, act=nn.ReLU())
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
167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
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
181 182 183 184 185 186 187 188 189 190 191 |
|
ultralytics.nn.modules.conv.DWConv
DWConv(c1, c2, k=1, s=1, d=1, act=True)
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
197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
ultralytics.nn.modules.conv.DWConvTranspose2d
DWConvTranspose2d(c1, c2, k=1, s=1, p1=0, p2=0)
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
215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
ultralytics.nn.modules.conv.ConvTranspose
ConvTranspose(c1, c2, k=2, s=2, p=0, bn=True, act=True)
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
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
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
261 262 263 264 265 266 267 268 269 270 271 |
|
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
273 274 275 276 277 278 279 280 281 282 283 |
|
ultralytics.nn.modules.conv.Focus
Focus(c1, c2, k=1, s=1, p=None, g=1, act=True)
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
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
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
313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
ultralytics.nn.modules.conv.GhostConv
GhostConv(c1, c2, k=1, s=1, g=1, act=True)
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. |
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
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
|
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
360 361 362 363 364 365 366 367 368 369 370 371 |
|
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
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
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
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
432 433 434 435 436 437 438 439 440 441 442 443 |
|
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
420 421 422 423 424 425 426 427 428 429 430 |
|
fuse_convs
fuse_convs()
Fuse convolutions for inference by creating a single equivalent convolution.
Source code in ultralytics/nn/modules/conv.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
|
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
445 446 447 448 449 450 451 452 453 454 455 456 |
|
ultralytics.nn.modules.conv.ChannelAttention
ChannelAttention(channels: int)
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
555 556 557 558 559 560 561 562 563 564 565 |
|
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
567 568 569 570 571 572 573 574 575 576 577 |
|
ultralytics.nn.modules.conv.SpatialAttention
SpatialAttention(kernel_size=7)
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
591 592 593 594 595 596 597 598 599 600 601 602 |
|
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
604 605 606 607 608 609 610 611 612 613 614 |
|
ultralytics.nn.modules.conv.CBAM
CBAM(c1, kernel_size=7)
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
628 629 630 631 632 633 634 635 636 637 638 |
|
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
640 641 642 643 644 645 646 647 648 649 650 |
|
ultralytics.nn.modules.conv.Concat
Concat(dimension=1)
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
661 662 663 664 665 666 667 668 669 |
|
forward
forward(x)
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
671 672 673 674 675 676 677 678 679 680 681 |
|
ultralytics.nn.modules.conv.Index
Index(index=0)
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
692 693 694 695 696 697 698 699 700 |
|
forward
forward(x)
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
702 703 704 705 706 707 708 709 710 711 712 |
|
ultralytics.nn.modules.conv.autopad
autopad(k, p=None, d=1)
Pad to 'same' shape outputs.
Source code in ultralytics/nn/modules/conv.py
28 29 30 31 32 33 34 |
|