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. |
The HungarianMatcher uses a cost function that considers classification scores, bounding box coordinates, and optionally mask predictions to perform optimal bipartite matching between predictions and ground truths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cost_gain
|
dict
|
Dictionary of cost coefficients for different components of the matching cost. Should contain keys 'class', 'bbox', 'giou', 'mask', and 'dice'. |
None
|
use_fl
|
bool
|
Whether to use Focal Loss for the classification cost calculation. |
True
|
with_mask
|
bool
|
Whether the model makes mask predictions. |
False
|
num_sample_points
|
int
|
Number of sample points used in mask cost calculation. |
12544
|
alpha
|
float
|
Alpha factor in Focal Loss calculation. |
0.25
|
gamma
|
float
|
Gamma factor in Focal Loss calculation. |
2.0
|
Source code in ultralytics/models/utils/ops.py
forward
Forward pass for HungarianMatcher. Computes costs based on prediction and ground truth 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). |
None
|
gt_mask
|
List[Tensor]
|
List of ground truth masks, each with shape (num_masks, Height, Width). |
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 with positive and negative samples from ground truths.
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 queries. |
100
|
cls_noise_ratio
|
float
|
Noise ratio for class labels. |
0.5
|
box_noise_scale
|
float
|
Noise scale for bounding box coordinates. |
1.0
|
training
|
bool
|
If it's in training mode. |
False
|
Returns:
Name | Type | Description |
---|---|---|
padding_cls |
Optional[Tensor]
|
The modified class embeddings for denoising. |
padding_bbox |
Optional[Tensor]
|
The modified bounding boxes for denoising. |
attn_mask |
Optional[Tensor]
|
The attention mask for denoising. |
dn_meta |
Optional[Dict]
|
Meta information for denoising. |
Source code in ultralytics/models/utils/ops.py
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 260 261 262 263 264 265 266 267 268 |
|