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
|
tensor
|
Input tensor. |
required |
Returns:
Type | Description |
---|---|
(dict, tensor)
|
If not in training mode, returns a dictionary containing the outputs of both one2many and one2one detections. If in training mode, returns a dictionary containing the outputs of one2many and one2one detections separately. |
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.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
|
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
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
forward
Runs the forward pass of the module, returning bounding box and classification scores for the input.
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. |