Reference for ultralytics/models/utils/ops.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/utils/ops.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.utils.ops.HungarianMatcher
HungarianMatcher(
cost_gain=None,
use_fl=True,
with_mask=False,
num_sample_points=12544,
alpha=0.25,
gamma=2.0,
)
Bases: Module
A module implementing the HungarianMatcher, which is a differentiable module to solve the assignment problem in an end-to-end fashion.
HungarianMatcher performs optimal assignment over the predicted and ground truth bounding boxes using a cost function that considers classification scores, bounding box coordinates, and optionally, mask predictions.
Attributes:
Name | Type | Description |
---|---|---|
cost_gain |
dict
|
Dictionary of cost coefficients: 'class', 'bbox', 'giou', 'mask', and 'dice'. |
use_fl |
bool
|
Indicates whether to use Focal Loss for the classification cost calculation. |
with_mask |
bool
|
Indicates whether the model makes mask predictions. |
num_sample_points |
int
|
The number of sample points used in mask cost calculation. |
alpha |
float
|
The alpha factor in Focal Loss calculation. |
gamma |
float
|
The gamma factor in Focal Loss calculation. |
Methods:
Name | Description |
---|---|
forward |
Computes the assignment between predictions and ground truths for a batch. |
_cost_mask |
Computes the mask cost and dice cost if masks are predicted. |
Source code in ultralytics/models/utils/ops.py
forward
Forward pass for HungarianMatcher. This function computes costs based on prediction and ground truth (classification cost, L1 cost between boxes and GIoU cost between boxes) and finds the optimal matching between predictions and ground truth based on these costs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_bboxes
|
Tensor
|
Predicted bounding boxes with shape [batch_size, num_queries, 4]. |
required |
pred_scores
|
Tensor
|
Predicted scores with shape [batch_size, num_queries, num_classes]. |
required |
gt_cls
|
Tensor
|
Ground truth classes with shape [num_gts, ]. |
required |
gt_bboxes
|
Tensor
|
Ground truth bounding boxes with shape [num_gts, 4]. |
required |
gt_groups
|
List[int]
|
List of length equal to batch size, containing the number of ground truths for each image. |
required |
masks
|
Tensor
|
Predicted masks with shape [batch_size, num_queries, height, width]. Defaults to None. |
None
|
gt_mask
|
List[Tensor]
|
List of ground truth masks, each with shape [num_masks, Height, Width]. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
List[Tuple[Tensor, Tensor]]
|
A list of size batch_size, each element is a tuple (index_i, index_j), where: - index_i is the tensor of indices of the selected predictions (in order) - index_j is the tensor of indices of the corresponding selected ground truth targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes) |
Source code in ultralytics/models/utils/ops.py
ultralytics.models.utils.ops.get_cdn_group
get_cdn_group(
batch,
num_classes,
num_queries,
class_embed,
num_dn=100,
cls_noise_ratio=0.5,
box_noise_scale=1.0,
training=False,
)
Get contrastive denoising training group. This function creates a contrastive denoising training group with positive and negative samples from the ground truths (gt). It applies noise to the class labels and bounding box coordinates, and returns the modified labels, bounding boxes, attention mask and meta information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
A dict that includes 'gt_cls' (torch.Tensor with shape [num_gts, ]), 'gt_bboxes' (torch.Tensor with shape [num_gts, 4]), 'gt_groups' (List(int)) which is a list of batch size length indicating the number of gts of each image. |
required |
num_classes
|
int
|
Number of classes. |
required |
num_queries
|
int
|
Number of queries. |
required |
class_embed
|
Tensor
|
Embedding weights to map class labels to embedding space. |
required |
num_dn
|
int
|
Number of denoising. Defaults to 100. |
100
|
cls_noise_ratio
|
float
|
Noise ratio for class labels. Defaults to 0.5. |
0.5
|
box_noise_scale
|
float
|
Noise scale for bounding box coordinates. Defaults to 1.0. |
1.0
|
training
|
bool
|
If it's in training mode. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tuple[Optional[Tensor], Optional[Tensor], Optional[Tensor], Optional[Dict]]
|
The modified class embeddings, bounding boxes, attention mask and meta information for denoising. If not in training mode or 'num_dn' is less than or equal to 0, the function returns None for all elements in the tuple. |
Source code in ultralytics/models/utils/ops.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|