Reference for ultralytics/utils/tal.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/tal.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.tal.TaskAlignedAssigner
TaskAlignedAssigner(topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-09)
Bases: Module
A task-aligned assigner for object detection.
This class assigns ground-truth (gt) objects to anchors based on the task-aligned metric, which combines both classification and localization information.
Attributes:
Name | Type | Description |
---|---|---|
topk |
int
|
The number of top candidates to consider. |
num_classes |
int
|
The number of object classes. |
bg_idx |
int
|
Background class index. |
alpha |
float
|
The alpha parameter for the classification component of the task-aligned metric. |
beta |
float
|
The beta parameter for the localization component of the task-aligned metric. |
eps |
float
|
A small value to prevent division by zero. |
Source code in ultralytics/utils/tal.py
30 31 32 33 34 35 36 37 38 |
|
forward
forward(pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt)
Compute the task-aligned assignment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_scores
|
Tensor
|
Predicted classification scores with shape (bs, num_total_anchors, num_classes). |
required |
pd_bboxes
|
Tensor
|
Predicted bounding boxes with shape (bs, num_total_anchors, 4). |
required |
anc_points
|
Tensor
|
Anchor points with shape (num_total_anchors, 2). |
required |
gt_labels
|
Tensor
|
Ground truth labels with shape (bs, n_max_boxes, 1). |
required |
gt_bboxes
|
Tensor
|
Ground truth boxes with shape (bs, n_max_boxes, 4). |
required |
mask_gt
|
Tensor
|
Mask for valid ground truth boxes with shape (bs, n_max_boxes, 1). |
required |
Returns:
Name | Type | Description |
---|---|---|
target_labels |
Tensor
|
Target labels with shape (bs, num_total_anchors). |
target_bboxes |
Tensor
|
Target bounding boxes with shape (bs, num_total_anchors, 4). |
target_scores |
Tensor
|
Target scores with shape (bs, num_total_anchors, num_classes). |
fg_mask |
Tensor
|
Foreground mask with shape (bs, num_total_anchors). |
target_gt_idx |
Tensor
|
Target ground truth indices with shape (bs, num_total_anchors). |
Source code in ultralytics/utils/tal.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
get_box_metrics
get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_gt)
Compute alignment metric given predicted and ground truth bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_scores
|
Tensor
|
Predicted classification scores with shape (bs, num_total_anchors, num_classes). |
required |
pd_bboxes
|
Tensor
|
Predicted bounding boxes with shape (bs, num_total_anchors, 4). |
required |
gt_labels
|
Tensor
|
Ground truth labels with shape (bs, n_max_boxes, 1). |
required |
gt_bboxes
|
Tensor
|
Ground truth boxes with shape (bs, n_max_boxes, 4). |
required |
mask_gt
|
Tensor
|
Mask for valid ground truth boxes with shape (bs, n_max_boxes, h*w). |
required |
Returns:
Name | Type | Description |
---|---|---|
align_metric |
Tensor
|
Alignment metric combining classification and localization. |
overlaps |
Tensor
|
IoU overlaps between predicted and ground truth boxes. |
Source code in ultralytics/utils/tal.py
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 176 177 178 179 180 181 |
|
get_pos_mask
get_pos_mask(pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt)
Get positive mask for each ground truth box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_scores
|
Tensor
|
Predicted classification scores with shape (bs, num_total_anchors, num_classes). |
required |
pd_bboxes
|
Tensor
|
Predicted bounding boxes with shape (bs, num_total_anchors, 4). |
required |
gt_labels
|
Tensor
|
Ground truth labels with shape (bs, n_max_boxes, 1). |
required |
gt_bboxes
|
Tensor
|
Ground truth boxes with shape (bs, n_max_boxes, 4). |
required |
anc_points
|
Tensor
|
Anchor points with shape (num_total_anchors, 2). |
required |
mask_gt
|
Tensor
|
Mask for valid ground truth boxes with shape (bs, n_max_boxes, 1). |
required |
Returns:
Name | Type | Description |
---|---|---|
mask_pos |
Tensor
|
Positive mask with shape (bs, max_num_obj, h*w). |
align_metric |
Tensor
|
Alignment metric with shape (bs, max_num_obj, h*w). |
overlaps |
Tensor
|
Overlaps between predicted and ground truth boxes with shape (bs, max_num_obj, h*w). |
Source code in ultralytics/utils/tal.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
get_targets
get_targets(gt_labels, gt_bboxes, target_gt_idx, fg_mask)
Compute target labels, target bounding boxes, and target scores for the positive anchor points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gt_labels
|
Tensor
|
Ground truth labels of shape (b, max_num_obj, 1), where b is the batch size and max_num_obj is the maximum number of objects. |
required |
gt_bboxes
|
Tensor
|
Ground truth bounding boxes of shape (b, max_num_obj, 4). |
required |
target_gt_idx
|
Tensor
|
Indices of the assigned ground truth objects for positive anchor points, with shape (b, hw), where hw is the total number of anchor points. |
required |
fg_mask
|
Tensor
|
A boolean tensor of shape (b, h*w) indicating the positive (foreground) anchor points. |
required |
Returns:
Name | Type | Description |
---|---|---|
target_labels |
Tensor
|
Shape (b, h*w), containing the target labels for positive anchor points. |
target_bboxes |
Tensor
|
Shape (b, h*w, 4), containing the target bounding boxes for positive anchor points. |
target_scores |
Tensor
|
Shape (b, h*w, num_classes), containing the target scores for positive anchor points. |
Source code in ultralytics/utils/tal.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
iou_calculation
iou_calculation(gt_bboxes, pd_bboxes)
Calculate IoU for horizontal bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gt_bboxes
|
Tensor
|
Ground truth boxes. |
required |
pd_bboxes
|
Tensor
|
Predicted boxes. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
IoU values between each pair of boxes. |
Source code in ultralytics/utils/tal.py
183 184 185 186 187 188 189 190 191 192 193 194 |
|
select_candidates_in_gts
staticmethod
select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-09)
Select positive anchor centers within ground truth bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xy_centers
|
Tensor
|
Anchor center coordinates, shape (h*w, 2). |
required |
gt_bboxes
|
Tensor
|
Ground truth bounding boxes, shape (b, n_boxes, 4). |
required |
eps
|
float
|
Small value for numerical stability. Defaults to 1e-9. |
1e-09
|
Returns:
Type | Description |
---|---|
Tensor
|
Boolean mask of positive anchors, shape (b, n_boxes, h*w). |
Note
b: batch size, n_boxes: number of ground truth boxes, h: height, w: width. Bounding box format: [x_min, y_min, x_max, y_max].
Source code in ultralytics/utils/tal.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
select_highest_overlaps
staticmethod
select_highest_overlaps(mask_pos, overlaps, n_max_boxes)
Select anchor boxes with highest IoU when assigned to multiple ground truths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask_pos
|
Tensor
|
Positive mask, shape (b, n_max_boxes, h*w). |
required |
overlaps
|
Tensor
|
IoU overlaps, shape (b, n_max_boxes, h*w). |
required |
n_max_boxes
|
int
|
Maximum number of ground truth boxes. |
required |
Returns:
Name | Type | Description |
---|---|---|
target_gt_idx |
Tensor
|
Indices of assigned ground truths, shape (b, h*w). |
fg_mask |
Tensor
|
Foreground mask, shape (b, h*w). |
mask_pos |
Tensor
|
Updated positive mask, shape (b, n_max_boxes, h*w). |
Source code in ultralytics/utils/tal.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
select_topk_candidates
select_topk_candidates(metrics, largest=True, topk_mask=None)
Select the top-k candidates based on the given metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics
|
Tensor
|
A tensor of shape (b, max_num_obj, hw), where b is the batch size, max_num_obj is the maximum number of objects, and hw represents the total number of anchor points. |
required |
largest
|
bool
|
If True, select the largest values; otherwise, select the smallest values. |
True
|
topk_mask
|
Tensor
|
An optional boolean tensor of shape (b, max_num_obj, topk), where topk is the number of top candidates to consider. If not provided, the top-k values are automatically computed based on the given metrics. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of shape (b, max_num_obj, h*w) containing the selected top-k candidates. |
Source code in ultralytics/utils/tal.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
ultralytics.utils.tal.RotatedTaskAlignedAssigner
RotatedTaskAlignedAssigner(
topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-09
)
Bases: TaskAlignedAssigner
Assigns ground-truth objects to rotated bounding boxes using a task-aligned metric.
Source code in ultralytics/utils/tal.py
30 31 32 33 34 35 36 37 38 |
|
iou_calculation
iou_calculation(gt_bboxes, pd_bboxes)
Calculate IoU for rotated bounding boxes.
Source code in ultralytics/utils/tal.py
332 333 334 |
|
select_candidates_in_gts
staticmethod
select_candidates_in_gts(xy_centers, gt_bboxes)
Select the positive anchor center in gt for rotated bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xy_centers
|
Tensor
|
Anchor center coordinates with shape (h*w, 2). |
required |
gt_bboxes
|
Tensor
|
Ground truth bounding boxes with shape (b, n_boxes, 5). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Boolean mask of positive anchors with shape (b, n_boxes, h*w). |
Source code in ultralytics/utils/tal.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
ultralytics.utils.tal.make_anchors
make_anchors(feats, strides, grid_cell_offset=0.5)
Generate anchors from features.
Source code in ultralytics/utils/tal.py
364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
ultralytics.utils.tal.dist2bbox
dist2bbox(distance, anchor_points, xywh=True, dim=-1)
Transform distance(ltrb) to box(xywh or xyxy).
Source code in ultralytics/utils/tal.py
379 380 381 382 383 384 385 386 387 388 |
|
ultralytics.utils.tal.bbox2dist
bbox2dist(anchor_points, bbox, reg_max)
Transform bbox(xyxy) to dist(ltrb).
Source code in ultralytics/utils/tal.py
391 392 393 394 |
|
ultralytics.utils.tal.dist2rbox
dist2rbox(pred_dist, pred_angle, anchor_points, dim=-1)
Decode predicted rotated bounding box coordinates from anchor points and distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_dist
|
Tensor
|
Predicted rotated distance with shape (bs, h*w, 4). |
required |
pred_angle
|
Tensor
|
Predicted angle with shape (bs, h*w, 1). |
required |
anchor_points
|
Tensor
|
Anchor points with shape (h*w, 2). |
required |
dim
|
int
|
Dimension along which to split. Defaults to -1. |
-1
|
Returns:
Type | Description |
---|---|
Tensor
|
Predicted rotated bounding boxes with shape (bs, h*w, 4). |
Source code in ultralytics/utils/tal.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|