Reference for ultralytics/nn/modules/block.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/modules/block.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.nn.modules.block.DFL
DFL(c1: int = 16)
Bases: Module
Integral module of Distribution Focal Loss (DFL).
Proposed in Generalized Focal Loss https://ieeexplore.ieee.org/document/9792391
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Number of input channels. |
16
|
Source code in ultralytics/nn/modules/block.py
64 65 66 67 68 69 70 71 72 73 74 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply the DFL module to input tensor and return transformed output.
Source code in ultralytics/nn/modules/block.py
76 77 78 79 | |
ultralytics.nn.modules.block.Proto
Proto(c1: int, c_: int = 256, c2: int = 32)
Bases: Module
Ultralytics YOLO models mask Proto module for segmentation models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c_
|
int
|
Intermediate channels. |
256
|
c2
|
int
|
Output channels (number of protos). |
32
|
Source code in ultralytics/nn/modules/block.py
86 87 88 89 90 91 92 93 94 95 96 97 98 | |
forward
forward(x: Tensor) -> torch.Tensor
Perform a forward pass through layers using an upsampled input image.
Source code in ultralytics/nn/modules/block.py
100 101 102 | |
ultralytics.nn.modules.block.HGStem
HGStem(c1: int, cm: int, c2: int)
Bases: Module
StemBlock of PPHGNetV2 with 5 convolutions and one maxpool2d.
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
cm
|
int
|
Middle channels. |
required |
c2
|
int
|
Output channels. |
required |
Source code in ultralytics/nn/modules/block.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of a PPHGNetV2 backbone layer.
Source code in ultralytics/nn/modules/block.py
127 128 129 130 131 132 133 134 135 136 137 138 | |
ultralytics.nn.modules.block.HGBlock
HGBlock(
c1: int,
cm: int,
c2: int,
k: int = 3,
n: int = 6,
lightconv: bool = False,
shortcut: bool = False,
act: Module = nn.ReLU(),
)
Bases: Module
HG_Block of PPHGNetV2 with 2 convolutions and LightConv.
https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
cm
|
int
|
Middle channels. |
required |
c2
|
int
|
Output channels. |
required |
k
|
int
|
Kernel size. |
3
|
n
|
int
|
Number of LightConv or Conv blocks. |
6
|
lightconv
|
bool
|
Whether to use LightConv. |
False
|
shortcut
|
bool
|
Whether to use shortcut connection. |
False
|
act
|
Module
|
Activation function. |
ReLU()
|
Source code in ultralytics/nn/modules/block.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of a PPHGNetV2 backbone layer.
Source code in ultralytics/nn/modules/block.py
177 178 179 180 181 182 | |
ultralytics.nn.modules.block.SPP
SPP(c1: int, c2: int, k: tuple[int, ...] = (5, 9, 13))
Bases: Module
Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
k
|
tuple
|
Kernel sizes for max pooling. |
(5, 9, 13)
|
Source code in ultralytics/nn/modules/block.py
188 189 190 191 192 193 194 195 196 197 198 199 200 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of the SPP layer, performing spatial pyramid pooling.
Source code in ultralytics/nn/modules/block.py
202 203 204 205 | |
ultralytics.nn.modules.block.SPPF
SPPF(c1: int, c2: int, k: int = 5)
Bases: Module
Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
k
|
int
|
Kernel size. |
5
|
Notes
This module is equivalent to SPP(k=(5, 9, 13)).
Source code in ultralytics/nn/modules/block.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply sequential pooling operations to input and return concatenated feature maps.
Source code in ultralytics/nn/modules/block.py
228 229 230 231 232 | |
ultralytics.nn.modules.block.C1
C1(c1: int, c2: int, n: int = 1)
Bases: Module
CSP Bottleneck with 1 convolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of convolutions. |
1
|
Source code in ultralytics/nn/modules/block.py
238 239 240 241 242 243 244 245 246 247 248 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply convolution and residual connection to input tensor.
Source code in ultralytics/nn/modules/block.py
250 251 252 253 | |
ultralytics.nn.modules.block.C2
C2(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: Module
CSP Bottleneck with 2 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through the CSP bottleneck with 2 convolutions.
Source code in ultralytics/nn/modules/block.py
277 278 279 280 | |
ultralytics.nn.modules.block.C2f
C2f(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = False,
g: int = 1,
e: float = 0.5,
)
Bases: Module
Faster Implementation of CSP Bottleneck with 2 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
False
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through C2f layer.
Source code in ultralytics/nn/modules/block.py
303 304 305 306 307 | |
forward_split
forward_split(x: Tensor) -> torch.Tensor
Forward pass using split() instead of chunk().
Source code in ultralytics/nn/modules/block.py
309 310 311 312 313 314 | |
ultralytics.nn.modules.block.C3
C3(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: Module
CSP Bottleneck with 3 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through the CSP bottleneck with 3 convolutions.
Source code in ultralytics/nn/modules/block.py
338 339 340 | |
ultralytics.nn.modules.block.C3x
C3x(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: C3
C3 module with cross-convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
ultralytics.nn.modules.block.RepC3
RepC3(c1: int, c2: int, n: int = 3, e: float = 1.0)
Bases: Module
Rep C3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of RepConv blocks. |
3
|
e
|
float
|
Expansion ratio. |
1.0
|
Source code in ultralytics/nn/modules/block.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of RepC3 module.
Source code in ultralytics/nn/modules/block.py
381 382 383 | |
ultralytics.nn.modules.block.C3TR
C3TR(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: C3
C3 module with TransformerBlock().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Transformer blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
ultralytics.nn.modules.block.C3Ghost
C3Ghost(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: C3
C3 module with GhostBottleneck().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Ghost bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
ultralytics.nn.modules.block.GhostBottleneck
GhostBottleneck(c1: int, c2: int, k: int = 3, s: int = 1)
Bases: Module
Ghost Bottleneck https://github.com/huawei-noah/Efficient-AI-Backbones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
k
|
int
|
Kernel size. |
3
|
s
|
int
|
Stride. |
1
|
Source code in ultralytics/nn/modules/block.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply skip connection and concatenation to input tensor.
Source code in ultralytics/nn/modules/block.py
447 448 449 | |
ultralytics.nn.modules.block.Bottleneck
Bottleneck(
c1: int,
c2: int,
shortcut: bool = True,
g: int = 1,
k: tuple[int, int] = (3, 3),
e: float = 0.5,
)
Bases: Module
Standard bottleneck.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
shortcut
|
bool
|
Whether to use shortcut connection. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
k
|
tuple
|
Kernel sizes for convolutions. |
(3, 3)
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply bottleneck with optional shortcut connection.
Source code in ultralytics/nn/modules/block.py
474 475 476 | |
ultralytics.nn.modules.block.BottleneckCSP
BottleneckCSP(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: Module
CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply CSP bottleneck with 3 convolutions.
Source code in ultralytics/nn/modules/block.py
503 504 505 506 507 | |
ultralytics.nn.modules.block.ResNetBlock
ResNetBlock(c1: int, c2: int, s: int = 1, e: int = 4)
Bases: Module
ResNet block with standard convolution layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
s
|
int
|
Stride. |
1
|
e
|
int
|
Expansion ratio. |
4
|
Source code in ultralytics/nn/modules/block.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through the ResNet block.
Source code in ultralytics/nn/modules/block.py
529 530 531 | |
ultralytics.nn.modules.block.ResNetLayer
ResNetLayer(
c1: int, c2: int, s: int = 1, is_first: bool = False, n: int = 1, e: int = 4
)
Bases: Module
ResNet layer with multiple ResNet blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
s
|
int
|
Stride. |
1
|
is_first
|
bool
|
Whether this is the first layer. |
False
|
n
|
int
|
Number of ResNet blocks. |
1
|
e
|
int
|
Expansion ratio. |
4
|
Source code in ultralytics/nn/modules/block.py
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through the ResNet layer.
Source code in ultralytics/nn/modules/block.py
560 561 562 | |
ultralytics.nn.modules.block.MaxSigmoidAttnBlock
MaxSigmoidAttnBlock(
c1: int,
c2: int,
nh: int = 1,
ec: int = 128,
gc: int = 512,
scale: bool = False,
)
Bases: Module
Max Sigmoid attention block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
nh
|
int
|
Number of heads. |
1
|
ec
|
int
|
Embedding channels. |
128
|
gc
|
int
|
Guide channels. |
512
|
scale
|
bool
|
Whether to use learnable scale parameter. |
False
|
Source code in ultralytics/nn/modules/block.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
forward
forward(x: Tensor, guide: Tensor) -> torch.Tensor
Forward pass of MaxSigmoidAttnBlock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
guide
|
Tensor
|
Guide tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after attention. |
Source code in ultralytics/nn/modules/block.py
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
ultralytics.nn.modules.block.C2fAttn
C2fAttn(
c1: int,
c2: int,
n: int = 1,
ec: int = 128,
nh: int = 1,
gc: int = 512,
shortcut: bool = False,
g: int = 1,
e: float = 0.5,
)
Bases: Module
C2f module with an additional attn module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
ec
|
int
|
Embedding channels for attention. |
128
|
nh
|
int
|
Number of heads for attention. |
1
|
gc
|
int
|
Guide channels for attention. |
512
|
shortcut
|
bool
|
Whether to use shortcut connections. |
False
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
forward
forward(x: Tensor, guide: Tensor) -> torch.Tensor
Forward pass through C2f layer with attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
guide
|
Tensor
|
Guide tensor for attention. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after processing. |
Source code in ultralytics/nn/modules/block.py
652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
forward_split
forward_split(x: Tensor, guide: Tensor) -> torch.Tensor
Forward pass using split() instead of chunk().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
guide
|
Tensor
|
Guide tensor for attention. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after processing. |
Source code in ultralytics/nn/modules/block.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 | |
ultralytics.nn.modules.block.ImagePoolingAttn
ImagePoolingAttn(
ec: int = 256,
ch: tuple[int, ...] = (),
ct: int = 512,
nh: int = 8,
k: int = 3,
scale: bool = False,
)
Bases: Module
ImagePoolingAttn: Enhance the text embeddings with image-aware information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ec
|
int
|
Embedding channels. |
256
|
ch
|
tuple
|
Channel dimensions for feature maps. |
()
|
ct
|
int
|
Channel dimension for text embeddings. |
512
|
nh
|
int
|
Number of attention heads. |
8
|
k
|
int
|
Kernel size for pooling. |
3
|
scale
|
bool
|
Whether to use learnable scale parameter. |
False
|
Source code in ultralytics/nn/modules/block.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
forward
forward(x: list[Tensor], text: Tensor) -> torch.Tensor
Forward pass of ImagePoolingAttn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
list[Tensor]
|
List of input feature maps. |
required |
text
|
Tensor
|
Text embeddings. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Enhanced text embeddings. |
Source code in ultralytics/nn/modules/block.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
ultralytics.nn.modules.block.ContrastiveHead
ContrastiveHead()
Bases: Module
Implements contrastive learning head for region-text similarity in vision-language models.
Source code in ultralytics/nn/modules/block.py
751 752 753 754 755 756 | |
forward
forward(x: Tensor, w: Tensor) -> torch.Tensor
Forward function of contrastive learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Image features. |
required |
w
|
Tensor
|
Text features. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Similarity scores. |
Source code in ultralytics/nn/modules/block.py
758 759 760 761 762 763 764 765 766 767 768 769 770 771 | |
ultralytics.nn.modules.block.BNContrastiveHead
BNContrastiveHead(embed_dims: int)
Bases: Module
Batch Norm Contrastive Head using batch norm instead of l2-normalization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embed_dims
|
int
|
Embed dimensions of text and image features. |
required |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embed_dims
|
int
|
Embedding dimensions for features. |
required |
Source code in ultralytics/nn/modules/block.py
781 782 783 784 785 786 787 788 789 790 791 792 | |
forward
forward(x: Tensor, w: Tensor) -> torch.Tensor
Forward function of contrastive learning with batch normalization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Image features. |
required |
w
|
Tensor
|
Text features. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Similarity scores. |
Source code in ultralytics/nn/modules/block.py
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
forward_fuse
forward_fuse(x: Tensor, w: Tensor) -> torch.Tensor
Passes input out unchanged.
Source code in ultralytics/nn/modules/block.py
801 802 803 | |
fuse
fuse()
Fuse the batch normalization layer in the BNContrastiveHead module.
Source code in ultralytics/nn/modules/block.py
794 795 796 797 798 799 | |
ultralytics.nn.modules.block.RepBottleneck
RepBottleneck(
c1: int,
c2: int,
shortcut: bool = True,
g: int = 1,
k: tuple[int, int] = (3, 3),
e: float = 0.5,
)
Bases: Bottleneck
Rep bottleneck.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
shortcut
|
bool
|
Whether to use shortcut connection. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
k
|
tuple
|
Kernel sizes for convolutions. |
(3, 3)
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 | |
ultralytics.nn.modules.block.RepCSP
RepCSP(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
)
Bases: C3
Repeatable Cross Stage Partial Network (RepCSP) module for efficient feature extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of RepBottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 | |
ultralytics.nn.modules.block.RepNCSPELAN4
RepNCSPELAN4(c1: int, c2: int, c3: int, c4: int, n: int = 1)
Bases: Module
CSP-ELAN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
c3
|
int
|
Intermediate channels. |
required |
c4
|
int
|
Intermediate channels for RepCSP. |
required |
n
|
int
|
Number of RepCSP blocks. |
1
|
Source code in ultralytics/nn/modules/block.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through RepNCSPELAN4 layer.
Source code in ultralytics/nn/modules/block.py
882 883 884 885 886 | |
forward_split
forward_split(x: Tensor) -> torch.Tensor
Forward pass using split() instead of chunk().
Source code in ultralytics/nn/modules/block.py
888 889 890 891 892 | |
ultralytics.nn.modules.block.ELAN1
ELAN1(c1: int, c2: int, c3: int, c4: int)
Bases: RepNCSPELAN4
ELAN1 module with 4 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
c3
|
int
|
Intermediate channels. |
required |
c4
|
int
|
Intermediate channels for convolutions. |
required |
Source code in ultralytics/nn/modules/block.py
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 | |
ultralytics.nn.modules.block.AConv
AConv(c1: int, c2: int)
Bases: Module
AConv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
Source code in ultralytics/nn/modules/block.py
918 919 920 921 922 923 924 925 926 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through AConv layer.
Source code in ultralytics/nn/modules/block.py
928 929 930 931 | |
ultralytics.nn.modules.block.ADown
ADown(c1: int, c2: int)
Bases: Module
ADown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
Source code in ultralytics/nn/modules/block.py
937 938 939 940 941 942 943 944 945 946 947 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through ADown layer.
Source code in ultralytics/nn/modules/block.py
949 950 951 952 953 954 955 956 | |
ultralytics.nn.modules.block.SPPELAN
SPPELAN(c1: int, c2: int, c3: int, k: int = 5)
Bases: Module
SPP-ELAN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
c3
|
int
|
Intermediate channels. |
required |
k
|
int
|
Kernel size for max pooling. |
5
|
Source code in ultralytics/nn/modules/block.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through SPPELAN layer.
Source code in ultralytics/nn/modules/block.py
979 980 981 982 983 | |
ultralytics.nn.modules.block.CBLinear
CBLinear(
c1: int,
c2s: list[int],
k: int = 1,
s: int = 1,
p: int | None = None,
g: int = 1,
)
Bases: Module
CBLinear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2s
|
list[int]
|
List of output channel sizes. |
required |
k
|
int
|
Kernel size. |
1
|
s
|
int
|
Stride. |
1
|
p
|
int | None
|
Padding. |
None
|
g
|
int
|
Groups. |
1
|
Source code in ultralytics/nn/modules/block.py
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 | |
forward
forward(x: Tensor) -> list[torch.Tensor]
Forward pass through CBLinear layer.
Source code in ultralytics/nn/modules/block.py
1004 1005 1006 | |
ultralytics.nn.modules.block.CBFuse
CBFuse(idx: list[int])
Bases: Module
CBFuse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
list[int]
|
Indices for feature selection. |
required |
Source code in ultralytics/nn/modules/block.py
1012 1013 1014 1015 1016 1017 1018 1019 | |
forward
forward(xs: list[Tensor]) -> torch.Tensor
Forward pass through CBFuse layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xs
|
list[Tensor]
|
List of input tensors. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Fused output tensor. |
Source code in ultralytics/nn/modules/block.py
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 | |
ultralytics.nn.modules.block.C3f
C3f(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = False,
g: int = 1,
e: float = 0.5,
)
Bases: Module
Faster Implementation of CSP Bottleneck with 2 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
False
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through C3f layer.
Source code in ultralytics/nn/modules/block.py
1056 1057 1058 1059 1060 | |
ultralytics.nn.modules.block.C3k2
C3k2(
c1: int,
c2: int,
n: int = 1,
c3k: bool = False,
e: float = 0.5,
g: int = 1,
shortcut: bool = True,
)
Bases: C2f
Faster Implementation of CSP Bottleneck with 2 convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of blocks. |
1
|
c3k
|
bool
|
Whether to use C3k blocks. |
False
|
e
|
float
|
Expansion ratio. |
0.5
|
g
|
int
|
Groups for convolutions. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
Source code in ultralytics/nn/modules/block.py
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
ultralytics.nn.modules.block.C3k
C3k(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = True,
g: int = 1,
e: float = 0.5,
k: int = 3,
)
Bases: C3
C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of Bottleneck blocks. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
k
|
int
|
Kernel size. |
3
|
Source code in ultralytics/nn/modules/block.py
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 | |
ultralytics.nn.modules.block.RepVGGDW
RepVGGDW(ed: int)
Bases: Module
RepVGGDW is a class that represents a depth wise separable convolutional block in RepVGG architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ed
|
int
|
Input and output channels. |
required |
Source code in ultralytics/nn/modules/block.py
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 | |
forward
forward(x: Tensor) -> torch.Tensor
Perform a forward pass of the RepVGGDW block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after applying the depth wise separable convolution. |
Source code in ultralytics/nn/modules/block.py
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
forward_fuse
forward_fuse(x: Tensor) -> torch.Tensor
Perform a forward pass of the RepVGGDW block without fusing the convolutions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after applying the depth wise separable convolution. |
Source code in ultralytics/nn/modules/block.py
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 | |
fuse
fuse()
Fuse the convolutional layers in the RepVGGDW block.
This method fuses the convolutional layers and updates the weights and biases accordingly.
Source code in ultralytics/nn/modules/block.py
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | |
ultralytics.nn.modules.block.CIB
CIB(c1: int, c2: int, shortcut: bool = True, e: float = 0.5, lk: bool = False)
Bases: Module
Conditional Identity Block (CIB) module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
shortcut
|
bool
|
Whether to add a shortcut connection. Defaults to True. |
True
|
e
|
float
|
Scaling factor for the hidden channels. Defaults to 0.5. |
0.5
|
lk
|
bool
|
Whether to use RepVGGDW for the third convolutional layer. Defaults to False. |
False
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
shortcut
|
bool
|
Whether to use shortcut connection. |
True
|
e
|
float
|
Expansion ratio. |
0.5
|
lk
|
bool
|
Whether to use RepVGGDW. |
False
|
Source code in ultralytics/nn/modules/block.py
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of the CIB module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor. |
Source code in ultralytics/nn/modules/block.py
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 | |
ultralytics.nn.modules.block.C2fCIB
C2fCIB(
c1: int,
c2: int,
n: int = 1,
shortcut: bool = False,
lk: bool = False,
g: int = 1,
e: float = 0.5,
)
Bases: C2f
C2fCIB class represents a convolutional block with C2f and CIB modules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
n
|
int
|
Number of CIB modules to stack. Defaults to 1. |
1
|
shortcut
|
bool
|
Whether to use shortcut connection. Defaults to False. |
False
|
lk
|
bool
|
Whether to use local key connection. Defaults to False. |
False
|
g
|
int
|
Number of groups for grouped convolution. Defaults to 1. |
1
|
e
|
float
|
Expansion ratio for CIB modules. Defaults to 0.5. |
0.5
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of CIB modules. |
1
|
shortcut
|
bool
|
Whether to use shortcut connection. |
False
|
lk
|
bool
|
Whether to use local key connection. |
False
|
g
|
int
|
Groups for convolutions. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 | |
ultralytics.nn.modules.block.Attention
Attention(dim: int, num_heads: int = 8, attn_ratio: float = 0.5)
Bases: Module
Attention module that performs self-attention on the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The input tensor dimension. |
required |
num_heads
|
int
|
The number of attention heads. |
8
|
attn_ratio
|
float
|
The ratio of the attention key dimension to the head dimension. |
0.5
|
Attributes:
| Name | Type | Description |
|---|---|---|
num_heads |
int
|
The number of attention heads. |
head_dim |
int
|
The dimension of each attention head. |
key_dim |
int
|
The dimension of the attention key. |
scale |
float
|
The scaling factor for the attention scores. |
qkv |
Conv
|
Convolutional layer for computing the query, key, and value. |
proj |
Conv
|
Convolutional layer for projecting the attended values. |
pe |
Conv
|
Convolutional layer for positional encoding. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Input dimension. |
required |
num_heads
|
int
|
Number of attention heads. |
8
|
attn_ratio
|
float
|
Attention ratio for key dimension. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass of the Attention module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The output tensor after self-attention. |
Source code in ultralytics/nn/modules/block.py
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 | |
ultralytics.nn.modules.block.PSABlock
PSABlock(
c: int, attn_ratio: float = 0.5, num_heads: int = 4, shortcut: bool = True
)
Bases: Module
PSABlock class implementing a Position-Sensitive Attention block for neural networks.
This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers with optional shortcut connections.
Attributes:
| Name | Type | Description |
|---|---|---|
attn |
Attention
|
Multi-head attention module. |
ffn |
Sequential
|
Feed-forward neural network module. |
add |
bool
|
Flag indicating whether to add shortcut connections. |
Methods:
| Name | Description |
|---|---|
forward |
Performs a forward pass through the PSABlock, applying attention and feed-forward layers. |
Examples:
Create a PSABlock and perform a forward pass
>>> psablock = PSABlock(c=128, attn_ratio=0.5, num_heads=4, shortcut=True)
>>> input_tensor = torch.randn(1, 128, 32, 32)
>>> output_tensor = psablock(input_tensor)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
int
|
Input and output channels. |
required |
attn_ratio
|
float
|
Attention ratio for key dimension. |
0.5
|
num_heads
|
int
|
Number of attention heads. |
4
|
shortcut
|
bool
|
Whether to use shortcut connections. |
True
|
Source code in ultralytics/nn/modules/block.py
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 | |
forward
forward(x: Tensor) -> torch.Tensor
Execute a forward pass through PSABlock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after attention and feed-forward processing. |
Source code in ultralytics/nn/modules/block.py
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 | |
ultralytics.nn.modules.block.PSA
PSA(c1: int, c2: int, e: float = 0.5)
Bases: Module
PSA class for implementing Position-Sensitive Attention in neural networks.
This class encapsulates the functionality for applying position-sensitive attention and feed-forward networks to input tensors, enhancing feature extraction and processing capabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
c |
int
|
Number of hidden channels after applying the initial convolution. |
cv1 |
Conv
|
1x1 convolution layer to reduce the number of input channels to 2*c. |
cv2 |
Conv
|
1x1 convolution layer to reduce the number of output channels to c. |
attn |
Attention
|
Attention module for position-sensitive attention. |
ffn |
Sequential
|
Feed-forward network for further processing. |
Methods:
| Name | Description |
|---|---|
forward |
Applies position-sensitive attention and feed-forward network to the input tensor. |
Examples:
Create a PSA module and apply it to an input tensor
>>> psa = PSA(c1=128, c2=128, e=0.5)
>>> input_tensor = torch.randn(1, 128, 64, 64)
>>> output_tensor = psa.forward(input_tensor)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 | |
forward
forward(x: Tensor) -> torch.Tensor
Execute forward pass in PSA module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after attention and feed-forward processing. |
Source code in ultralytics/nn/modules/block.py
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 | |
ultralytics.nn.modules.block.C2PSA
C2PSA(c1: int, c2: int, n: int = 1, e: float = 0.5)
Bases: Module
C2PSA module with attention mechanism for enhanced feature extraction and processing.
This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.
Attributes:
| Name | Type | Description |
|---|---|---|
c |
int
|
Number of hidden channels. |
cv1 |
Conv
|
1x1 convolution layer to reduce the number of input channels to 2*c. |
cv2 |
Conv
|
1x1 convolution layer to reduce the number of output channels to c. |
m |
Sequential
|
Sequential container of PSABlock modules for attention and feed-forward operations. |
Methods:
| Name | Description |
|---|---|
forward |
Performs a forward pass through the C2PSA module, applying attention and feed-forward operations. |
Examples:
>>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
>>> input_tensor = torch.randn(1, 256, 64, 64)
>>> output_tensor = c2psa(input_tensor)
Notes
This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of PSABlock modules. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 | |
forward
forward(x: Tensor) -> torch.Tensor
Process the input tensor through a series of PSA blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after processing. |
Source code in ultralytics/nn/modules/block.py
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 | |
ultralytics.nn.modules.block.C2fPSA
C2fPSA(c1: int, c2: int, n: int = 1, e: float = 0.5)
Bases: C2f
C2fPSA module with enhanced feature extraction using PSA blocks.
This class extends the C2f module by incorporating PSA blocks for improved attention mechanisms and feature extraction.
Attributes:
| Name | Type | Description |
|---|---|---|
c |
int
|
Number of hidden channels. |
cv1 |
Conv
|
1x1 convolution layer to reduce the number of input channels to 2*c. |
cv2 |
Conv
|
1x1 convolution layer to reduce the number of output channels to c. |
m |
ModuleList
|
List of PSA blocks for feature extraction. |
Methods:
| Name | Description |
|---|---|
forward |
Performs a forward pass through the C2fPSA module. |
forward_split |
Performs a forward pass using split() instead of chunk(). |
Examples:
>>> import torch
>>> from ultralytics.models.common import C2fPSA
>>> model = C2fPSA(c1=64, c2=64, n=3, e=0.5)
>>> x = torch.randn(1, 64, 128, 128)
>>> output = model(x)
>>> print(output.shape)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
n
|
int
|
Number of PSABlock modules. |
1
|
e
|
float
|
Expansion ratio. |
0.5
|
Source code in ultralytics/nn/modules/block.py
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 | |
ultralytics.nn.modules.block.SCDown
SCDown(c1: int, c2: int, k: int, s: int)
Bases: Module
SCDown module for downsampling with separable convolutions.
This module performs downsampling using a combination of pointwise and depthwise convolutions, which helps in efficiently reducing the spatial dimensions of the input tensor while maintaining the channel information.
Attributes:
| Name | Type | Description |
|---|---|---|
cv1 |
Conv
|
Pointwise convolution layer that reduces the number of channels. |
cv2 |
Conv
|
Depthwise convolution layer that performs spatial downsampling. |
Methods:
| Name | Description |
|---|---|
forward |
Applies the SCDown module to the input tensor. |
Examples:
>>> import torch
>>> from ultralytics import SCDown
>>> model = SCDown(c1=64, c2=128, k=3, s=2)
>>> x = torch.randn(1, 64, 128, 128)
>>> y = model(x)
>>> print(y.shape)
torch.Size([1, 128, 64, 64])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Input channels. |
required |
c2
|
int
|
Output channels. |
required |
k
|
int
|
Kernel size. |
required |
s
|
int
|
Stride. |
required |
Source code in ultralytics/nn/modules/block.py
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply convolution and downsampling to the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Downsampled output tensor. |
Source code in ultralytics/nn/modules/block.py
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 | |
ultralytics.nn.modules.block.TorchVision
TorchVision(
model: str,
weights: str = "DEFAULT",
unwrap: bool = True,
truncate: int = 2,
split: bool = False,
)
Bases: Module
TorchVision module to allow loading any torchvision model.
This class provides a way to load a model from the torchvision library, optionally load pre-trained weights, and customize the model by truncating or unwrapping layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the torchvision model to load. |
required |
weights
|
str
|
Pre-trained weights to load. Default is "DEFAULT". |
'DEFAULT'
|
unwrap
|
bool
|
Unwraps the model to a sequential containing all but the last |
True
|
truncate
|
int
|
Number of layers to truncate from the end if |
2
|
split
|
bool
|
Returns output from intermediate child modules as list. Default is False. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
m |
Module
|
The loaded torchvision model, possibly truncated and unwrapped. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Name of the torchvision model to load. |
required |
weights
|
str
|
Pre-trained weights to load. |
'DEFAULT'
|
unwrap
|
bool
|
Whether to unwrap the model. |
True
|
truncate
|
int
|
Number of layers to truncate. |
2
|
split
|
bool
|
Whether to split the output. |
False
|
Source code in ultralytics/nn/modules/block.py
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor | list[Tensor]
|
Output tensor or list of tensors. |
Source code in ultralytics/nn/modules/block.py
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 | |
ultralytics.nn.modules.block.AAttn
AAttn(dim: int, num_heads: int, area: int = 1)
Bases: Module
Area-attention module for YOLO models, providing efficient attention mechanisms.
This module implements an area-based attention mechanism that processes input features in a spatially-aware manner, making it particularly effective for object detection tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
area |
int
|
Number of areas the feature map is divided. |
num_heads |
int
|
Number of heads into which the attention mechanism is divided. |
head_dim |
int
|
Dimension of each attention head. |
qkv |
Conv
|
Convolution layer for computing query, key and value tensors. |
proj |
Conv
|
Projection convolution layer. |
pe |
Conv
|
Position encoding convolution layer. |
Methods:
| Name | Description |
|---|---|
forward |
Applies area-attention to input tensor. |
Examples:
>>> attn = AAttn(dim=256, num_heads=8, area=4)
>>> x = torch.randn(1, 256, 32, 32)
>>> output = attn(x)
>>> print(output.shape)
torch.Size([1, 256, 32, 32])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Number of hidden channels. |
required |
num_heads
|
int
|
Number of heads into which the attention mechanism is divided. |
required |
area
|
int
|
Number of areas the feature map is divided. |
1
|
Source code in ultralytics/nn/modules/block.py
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 | |
forward
forward(x: Tensor) -> torch.Tensor
Process the input tensor through the area-attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after area-attention. |
Source code in ultralytics/nn/modules/block.py
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 | |
ultralytics.nn.modules.block.ABlock
ABlock(dim: int, num_heads: int, mlp_ratio: float = 1.2, area: int = 1)
Bases: Module
Area-attention block module for efficient feature extraction in YOLO models.
This module implements an area-attention mechanism combined with a feed-forward network for processing feature maps. It uses a novel area-based attention approach that is more efficient than traditional self-attention while maintaining effectiveness.
Attributes:
| Name | Type | Description |
|---|---|---|
attn |
AAttn
|
Area-attention module for processing spatial features. |
mlp |
Sequential
|
Multi-layer perceptron for feature transformation. |
Methods:
| Name | Description |
|---|---|
_init_weights |
Initializes module weights using truncated normal distribution. |
forward |
Applies area-attention and feed-forward processing to input tensor. |
Examples:
>>> block = ABlock(dim=256, num_heads=8, mlp_ratio=1.2, area=1)
>>> x = torch.randn(1, 256, 32, 32)
>>> output = block(x)
>>> print(output.shape)
torch.Size([1, 256, 32, 32])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Number of input channels. |
required |
num_heads
|
int
|
Number of heads into which the attention mechanism is divided. |
required |
mlp_ratio
|
float
|
Expansion ratio for MLP hidden dimension. |
1.2
|
area
|
int
|
Number of areas the feature map is divided. |
1
|
Source code in ultralytics/nn/modules/block.py
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through ABlock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after area-attention and feed-forward processing. |
Source code in ultralytics/nn/modules/block.py
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 | |
ultralytics.nn.modules.block.A2C2f
A2C2f(
c1: int,
c2: int,
n: int = 1,
a2: bool = True,
area: int = 1,
residual: bool = False,
mlp_ratio: float = 2.0,
e: float = 0.5,
g: int = 1,
shortcut: bool = True,
)
Bases: Module
Area-Attention C2f module for enhanced feature extraction with area-based attention mechanisms.
This module extends the C2f architecture by incorporating area-attention and ABlock layers for improved feature processing. It supports both area-attention and standard convolution modes.
Attributes:
| Name | Type | Description |
|---|---|---|
cv1 |
Conv
|
Initial 1x1 convolution layer that reduces input channels to hidden channels. |
cv2 |
Conv
|
Final 1x1 convolution layer that processes concatenated features. |
gamma |
Parameter | None
|
Learnable parameter for residual scaling when using area attention. |
m |
ModuleList
|
List of either ABlock or C3k modules for feature processing. |
Methods:
| Name | Description |
|---|---|
forward |
Processes input through area-attention or standard convolution pathway. |
Examples:
>>> m = A2C2f(512, 512, n=1, a2=True, area=1)
>>> x = torch.randn(1, 512, 32, 32)
>>> output = m(x)
>>> print(output.shape)
torch.Size([1, 512, 32, 32])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c1
|
int
|
Number of input channels. |
required |
c2
|
int
|
Number of output channels. |
required |
n
|
int
|
Number of ABlock or C3k modules to stack. |
1
|
a2
|
bool
|
Whether to use area attention blocks. If False, uses C3k blocks instead. |
True
|
area
|
int
|
Number of areas the feature map is divided. |
1
|
residual
|
bool
|
Whether to use residual connections with learnable gamma parameter. |
False
|
mlp_ratio
|
float
|
Expansion ratio for MLP hidden dimension. |
2.0
|
e
|
float
|
Channel expansion ratio for hidden channels. |
0.5
|
g
|
int
|
Number of groups for grouped convolutions. |
1
|
shortcut
|
bool
|
Whether to use shortcut connections in C3k blocks. |
True
|
Source code in ultralytics/nn/modules/block.py
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 | |
forward
forward(x: Tensor) -> torch.Tensor
Forward pass through A2C2f layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after processing. |
Source code in ultralytics/nn/modules/block.py
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 | |
ultralytics.nn.modules.block.SwiGLUFFN
SwiGLUFFN(gc: int, ec: int, e: int = 4)
Bases: Module
SwiGLU Feed-Forward Network for transformer-based architectures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gc
|
int
|
Guide channels. |
required |
ec
|
int
|
Embedding channels. |
required |
e
|
int
|
Expansion factor. |
4
|
Source code in ultralytics/nn/modules/block.py
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply SwiGLU transformation to input features.
Source code in ultralytics/nn/modules/block.py
1860 1861 1862 1863 1864 1865 | |
ultralytics.nn.modules.block.Residual
Residual(m: Module)
Bases: Module
Residual connection wrapper for neural network modules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
Module
|
Module to wrap with residual connection. |
required |
Source code in ultralytics/nn/modules/block.py
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 | |
forward
forward(x: Tensor) -> torch.Tensor
Apply residual connection to input features.
Source code in ultralytics/nn/modules/block.py
1884 1885 1886 | |
ultralytics.nn.modules.block.SAVPE
SAVPE(ch: list[int], c3: int, embed: int)
Bases: Module
Spatial-Aware Visual Prompt Embedding module for feature enhancement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ch
|
list[int]
|
List of input channel dimensions. |
required |
c3
|
int
|
Intermediate channels. |
required |
embed
|
int
|
Embedding dimension. |
required |
Source code in ultralytics/nn/modules/block.py
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 | |
forward
forward(x: list[Tensor], vp: Tensor) -> torch.Tensor
Process input features and visual prompts to generate enhanced embeddings.
Source code in ultralytics/nn/modules/block.py
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 | |