Reference for ultralytics/nn/modules/head.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/nn/modules/head.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.nn.modules.head.Detect
Bases: Module
YOLO Detect head for detection models.
Source code in ultralytics/nn/modules/head.py
bias_init
Initialize Detect() biases, WARNING: requires stride availability.
Source code in ultralytics/nn/modules/head.py
decode_bboxes
forward
Concatenates and returns predicted bounding boxes and class probabilities.
Source code in ultralytics/nn/modules/head.py
forward_end2end
Performs forward pass of the v10Detect module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
List[Tensor]
|
Input feature maps from different levels. |
required |
Returns:
Type | Description |
---|---|
dict | tuple
|
If in training mode, returns a dictionary containing the outputs of both one2many and one2one detections. If not in training mode, returns processed detections or a tuple with processed detections and raw outputs. |
Source code in ultralytics/nn/modules/head.py
postprocess
staticmethod
Post-processes YOLO model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor
|
Raw predictions with shape (batch_size, num_anchors, 4 + nc) with last dimension format [x, y, w, h, class_probs]. |
required |
max_det
|
int
|
Maximum detections per image. |
required |
nc
|
int
|
Number of classes. Default: 80. |
80
|
Returns:
Type | Description |
---|---|
Tensor
|
Processed predictions with shape (batch_size, min(max_det, num_anchors), 6) and last dimension format [x, y, w, h, max_class_prob, class_index]. |
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.Segment
Bases: Detect
YOLO Segment head for segmentation models.
Source code in ultralytics/nn/modules/head.py
forward
Return model outputs and mask coefficients if training, otherwise return outputs and mask coefficients.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.OBB
Bases: Detect
YOLO OBB detection head for detection with rotation models.
Source code in ultralytics/nn/modules/head.py
decode_bboxes
forward
Concatenates and returns predicted bounding boxes and class probabilities.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.Pose
Bases: Detect
YOLO Pose head for keypoints models.
Source code in ultralytics/nn/modules/head.py
forward
Perform forward pass through YOLO model and return predictions.
Source code in ultralytics/nn/modules/head.py
kpts_decode
Decodes keypoints.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.Classify
Bases: Module
YOLO classification head, i.e. x(b,c1,20,20) to x(b,c2).
Source code in ultralytics/nn/modules/head.py
forward
Performs a forward pass of the YOLO model on input image data.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.WorldDetect
Bases: Detect
Head for integrating YOLO detection models with semantic understanding from text embeddings.
Source code in ultralytics/nn/modules/head.py
bias_init
Initialize Detect() biases, WARNING: requires stride availability.
Source code in ultralytics/nn/modules/head.py
forward
Concatenates and returns predicted bounding boxes and class probabilities.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.LRPCHead
Bases: Module
Lightweight Region Proposal and Classification Head for efficient object detection.
Source code in ultralytics/nn/modules/head.py
conv2linear
Convert a 1x1 convolutional layer to a linear layer.
Source code in ultralytics/nn/modules/head.py
forward
Process classification and localization features to generate detection proposals.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.YOLOEDetect
Bases: Detect
Head for integrating YOLO detection models with semantic understanding from text embeddings.
Source code in ultralytics/nn/modules/head.py
bias_init
Initialize biases for detection heads.
Source code in ultralytics/nn/modules/head.py
forward
Process features with class prompt embeddings to generate detections.
Source code in ultralytics/nn/modules/head.py
forward_lrpc
Process features with fused text embeddings to generate detections for prompt-free model.
Source code in ultralytics/nn/modules/head.py
fuse
Fuse text features with model weights for efficient inference.
Source code in ultralytics/nn/modules/head.py
get_tpe
get_vpe
Get visual prompt embeddings with spatial awareness.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.YOLOESegment
Bases: YOLOEDetect
YOLO segmentation head with text embedding capabilities.
Source code in ultralytics/nn/modules/head.py
forward
Return model outputs and mask coefficients if training, otherwise return outputs and mask coefficients.
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.RTDETRDecoder
RTDETRDecoder(
nc=80,
ch=(512, 1024, 2048),
hd=256,
nq=300,
ndp=4,
nh=8,
ndl=6,
d_ffn=1024,
dropout=0.0,
act=nn.ReLU(),
eval_idx=-1,
nd=100,
label_noise_ratio=0.5,
box_noise_scale=1.0,
learnt_init_query=False,
)
Bases: Module
Real-Time Deformable Transformer Decoder (RTDETRDecoder) module for object detection.
This decoder module utilizes Transformer architecture along with deformable convolutions to predict bounding boxes and class labels for objects in an image. It integrates features from multiple layers and runs through a series of Transformer decoder layers to output the final predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nc
|
int
|
Number of classes. Default is 80. |
80
|
ch
|
tuple
|
Channels in the backbone feature maps. Default is (512, 1024, 2048). |
(512, 1024, 2048)
|
hd
|
int
|
Dimension of hidden layers. Default is 256. |
256
|
nq
|
int
|
Number of query points. Default is 300. |
300
|
ndp
|
int
|
Number of decoder points. Default is 4. |
4
|
nh
|
int
|
Number of heads in multi-head attention. Default is 8. |
8
|
ndl
|
int
|
Number of decoder layers. Default is 6. |
6
|
d_ffn
|
int
|
Dimension of the feed-forward networks. Default is 1024. |
1024
|
dropout
|
float
|
Dropout rate. Default is 0.0. |
0.0
|
act
|
Module
|
Activation function. Default is nn.ReLU. |
ReLU()
|
eval_idx
|
int
|
Evaluation index. Default is -1. |
-1
|
nd
|
int
|
Number of denoising. Default is 100. |
100
|
label_noise_ratio
|
float
|
Label noise ratio. Default is 0.5. |
0.5
|
box_noise_scale
|
float
|
Box noise scale. Default is 1.0. |
1.0
|
learnt_init_query
|
bool
|
Whether to learn initial query embeddings. Default is False. |
False
|
Source code in ultralytics/nn/modules/head.py
581 582 583 584 585 586 587 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 615 616 617 618 619 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 651 652 653 654 655 656 657 658 |
|
forward
Runs the forward pass of the module, returning bounding box and classification scores for the input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
List[Tensor]
|
List of feature maps from the backbone. |
required |
batch
|
dict
|
Batch information for training. |
None
|
Returns:
Type | Description |
---|---|
tuple | Tensor
|
During training, returns a tuple of bounding boxes, scores, and other metadata. During inference, returns a tensor of shape (bs, 300, 4+nc) containing bounding boxes and class scores. |
Source code in ultralytics/nn/modules/head.py
ultralytics.nn.modules.head.v10Detect
Bases: Detect
v10 Detection head from https://arxiv.org/pdf/2405.14458.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nc
|
int
|
Number of classes. |
80
|
ch
|
tuple
|
Tuple of channel sizes. |
()
|
Attributes:
Name | Type | Description |
---|---|---|
max_det |
int
|
Maximum number of detections. |
Methods:
Name | Description |
---|---|
forward |
Performs forward pass of the v10Detect module. |
bias_init |
Initializes biases of the Detect module. |