Reference for ultralytics/utils/tal.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/tal.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.tal.TaskAlignedAssigner
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. |
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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
__init__(topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-09)
Initialize a TaskAlignedAssigner object with customizable hyperparameters.
Source code in ultralytics/utils/tal.py
forward(pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt)
Compute the task-aligned assignment. Reference https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pd_scores |
Tensor
|
shape(bs, num_total_anchors, num_classes) |
required |
pd_bboxes |
Tensor
|
shape(bs, num_total_anchors, 4) |
required |
anc_points |
Tensor
|
shape(num_total_anchors, 2) |
required |
gt_labels |
Tensor
|
shape(bs, n_max_boxes, 1) |
required |
gt_bboxes |
Tensor
|
shape(bs, n_max_boxes, 4) |
required |
mask_gt |
Tensor
|
shape(bs, n_max_boxes, 1) |
required |
Returns:
Name | Type | Description |
---|---|---|
target_labels |
Tensor
|
shape(bs, num_total_anchors) |
target_bboxes |
Tensor
|
shape(bs, num_total_anchors, 4) |
target_scores |
Tensor
|
shape(bs, num_total_anchors, num_classes) |
fg_mask |
Tensor
|
shape(bs, num_total_anchors) |
target_gt_idx |
Tensor
|
shape(bs, num_total_anchors) |
Source code in ultralytics/utils/tal.py
get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes, mask_gt)
Compute alignment metric given predicted and ground truth bounding boxes.
Source code in ultralytics/utils/tal.py
get_pos_mask(pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt)
Get in_gts mask, (b, max_num_obj, h*w).
Source code in ultralytics/utils/tal.py
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:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor]
|
A tuple containing the following tensors: - target_labels (Tensor): Shape (b, hw), containing the target labels for positive anchor points. - target_bboxes (Tensor): Shape (b, hw, 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, where num_classes is the number of object classes. |
Source code in ultralytics/utils/tal.py
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
ultralytics.utils.tal.select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-09)
Select the positive anchor center in gt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xy_centers |
Tensor
|
shape(h*w, 2) |
required |
gt_bboxes |
Tensor
|
shape(b, n_boxes, 4) |
required |
Returns:
Type | Description |
---|---|
Tensor
|
shape(b, n_boxes, h*w) |
Source code in ultralytics/utils/tal.py
ultralytics.utils.tal.select_highest_overlaps(mask_pos, overlaps, n_max_boxes)
If an anchor box is assigned to multiple gts, the one with the highest IoI will be selected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask_pos |
Tensor
|
shape(b, n_max_boxes, h*w) |
required |
overlaps |
Tensor
|
shape(b, n_max_boxes, h*w) |
required |
Returns:
Name | Type | Description |
---|---|---|
target_gt_idx |
Tensor
|
shape(b, h*w) |
fg_mask |
Tensor
|
shape(b, h*w) |
mask_pos |
Tensor
|
shape(b, n_max_boxes, h*w) |
Source code in ultralytics/utils/tal.py
ultralytics.utils.tal.make_anchors(feats, strides, grid_cell_offset=0.5)
Generate anchors from features.
Source code in ultralytics/utils/tal.py
ultralytics.utils.tal.dist2bbox(distance, anchor_points, xywh=True, dim=-1)
Transform distance(ltrb) to box(xywh or xyxy).
Source code in ultralytics/utils/tal.py
ultralytics.utils.tal.bbox2dist(anchor_points, bbox, reg_max)
Transform bbox(xyxy) to dist(ltrb).