Detection¶
Loss¶
yolo.tasks.detection.loss
¶
logger = logging.getLogger('yolo')
module-attribute
¶
Config
dataclass
¶
Source code in yolo/config/config.py
LossConfig
dataclass
¶
BoxMatcher
¶
Source code in yolo/tasks/detection/postprocess.py
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 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 327 328 329 330 331 332 333 334 335 336 | |
get_valid_matrix(target_bbox)
¶
Get a boolean mask that indicates whether each target bounding box overlaps with each anchor and is able to correctly predict it with the available reg_max value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_bbox
|
Tensor
|
The bounding box of each target, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Boolean mask of shape |
Tensor
|
target overlaps an anchor and the anchor can predict the target within |
Source code in yolo/tasks/detection/postprocess.py
get_cls_matrix(predict_cls, target_cls)
¶
Get the (predicted class' probabilities) corresponding to the target classes across all anchors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_cls
|
Tensor
|
Predicted class probabilities, shape |
required |
target_cls
|
Tensor
|
Ground-truth class indices, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Class probabilities gathered for each target, shape |
Source code in yolo/tasks/detection/postprocess.py
get_iou_matrix(predict_bbox, target_bbox)
¶
Get the IoU between each target bounding box and each predicted bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_bbox
|
Tensor
|
Predicted boxes in |
required |
target_bbox
|
Tensor
|
Ground-truth boxes in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
IoU scores of shape |
Source code in yolo/tasks/detection/postprocess.py
filter_topk(target_matrix, grid_mask, topk=10)
¶
Filter the top-k suitability of targets for each anchor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_matrix
|
Tensor
|
Suitability scores, shape |
required |
grid_mask
|
Tensor
|
Validity mask, shape |
required |
topk
|
int
|
Number of top scores to retain per anchor. |
10
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[Tensor, Tensor]: |
Tensor
|
|
Source code in yolo/tasks/detection/postprocess.py
ensure_one_anchor(target_matrix, topk_mask)
¶
Ensures each valid target gets at least one anchor matched based on the unmasked target matrix, which enables an otherwise invalid match. This enables too small or too large targets to be learned as well, even if they can't be predicted perfectly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_matrix
|
Tensor
|
Suitability scores, shape |
required |
topk_mask
|
Tensor
|
Boolean top-k mask, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Updated top-k mask of shape |
Source code in yolo/tasks/detection/postprocess.py
filter_duplicates(iou_mat, topk_mask)
¶
Filter the maximum suitability target index of each anchor based on IoU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iou_mat
|
Tensor
|
IoU scores, shape |
required |
topk_mask
|
Tensor
|
Boolean top-k mask, shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[Tensor, Tensor, Tensor]: |
Tensor
|
|
Source code in yolo/tasks/detection/postprocess.py
__call__(target, predict)
¶
Matches each target to the most suitable anchor. 1. For each anchor prediction, find the highest suitability targets. 2. Match target to the best anchor. 3. Noramlize the class probilities of targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The ground truth class and bounding box information as tensor of size [batch x targets x 5]. |
required |
predict
|
Tuple[Tensor]
|
Tuple of predicted class and bounding box tensors. Class tensor is of size [batch x anchors x class] Bounding box tensor is of size [batch x anchors x 4]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
anchor_matched_targets |
Tensor
|
Tensor of size [batch x anchors x (class + 4)]. A tensor assigning each target/gt to the best fitting anchor. The class probabilities are normalized. |
valid_mask |
Tensor
|
Bool tensor of shape [batch x anchors]. True if a anchor has a target/gt assigned to it. |
Source code in yolo/tasks/detection/postprocess.py
Vec2Box
¶
Source code in yolo/tasks/detection/postprocess.py
update(image_size)
¶
image_size: W, H
Source code in yolo/tasks/detection/postprocess.py
BCELoss
¶
Bases: Module
Source code in yolo/tasks/detection/loss.py
BoxLoss
¶
Bases: Module
Source code in yolo/tasks/detection/loss.py
DFLoss
¶
Bases: Module
Source code in yolo/tasks/detection/loss.py
YOLOLoss
¶
Source code in yolo/tasks/detection/loss.py
separate_anchor(anchors)
¶
separate anchor and bbouding box
Source code in yolo/tasks/detection/loss.py
DualLoss
¶
Source code in yolo/tasks/detection/loss.py
calculate_iou(bbox1, bbox2, metrics='iou')
¶
Source code in yolo/tasks/detection/postprocess.py
create_loss_function(cfg, vec2box)
¶
Source code in yolo/tasks/detection/loss.py
Post-processing¶
yolo.tasks.detection.postprocess
¶
logger = logging.getLogger('yolo')
module-attribute
¶
AnchorConfig
dataclass
¶
MatcherConfig
dataclass
¶
NMSConfig
dataclass
¶
YOLO
¶
Bases: Module
A preliminary YOLO (You Only Look Once) model class still under development.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
ModelConfig
|
Configuration for the YOLO model. Expected to define the layers, parameters, and any other relevant configuration details. |
required |
Source code in yolo/model/builder.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 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 | |
save_load_weights(weights)
¶
Update the model's weights with the provided weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Union[Path, OrderedDict]
|
A OrderedDict containing the new weights. |
required |
Source code in yolo/model/builder.py
BoxMatcher
¶
Source code in yolo/tasks/detection/postprocess.py
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 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 327 328 329 330 331 332 333 334 335 336 | |
get_valid_matrix(target_bbox)
¶
Get a boolean mask that indicates whether each target bounding box overlaps with each anchor and is able to correctly predict it with the available reg_max value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_bbox
|
Tensor
|
The bounding box of each target, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Boolean mask of shape |
Tensor
|
target overlaps an anchor and the anchor can predict the target within |
Source code in yolo/tasks/detection/postprocess.py
get_cls_matrix(predict_cls, target_cls)
¶
Get the (predicted class' probabilities) corresponding to the target classes across all anchors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_cls
|
Tensor
|
Predicted class probabilities, shape |
required |
target_cls
|
Tensor
|
Ground-truth class indices, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Class probabilities gathered for each target, shape |
Source code in yolo/tasks/detection/postprocess.py
get_iou_matrix(predict_bbox, target_bbox)
¶
Get the IoU between each target bounding box and each predicted bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_bbox
|
Tensor
|
Predicted boxes in |
required |
target_bbox
|
Tensor
|
Ground-truth boxes in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
IoU scores of shape |
Source code in yolo/tasks/detection/postprocess.py
filter_topk(target_matrix, grid_mask, topk=10)
¶
Filter the top-k suitability of targets for each anchor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_matrix
|
Tensor
|
Suitability scores, shape |
required |
grid_mask
|
Tensor
|
Validity mask, shape |
required |
topk
|
int
|
Number of top scores to retain per anchor. |
10
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[Tensor, Tensor]: |
Tensor
|
|
Source code in yolo/tasks/detection/postprocess.py
ensure_one_anchor(target_matrix, topk_mask)
¶
Ensures each valid target gets at least one anchor matched based on the unmasked target matrix, which enables an otherwise invalid match. This enables too small or too large targets to be learned as well, even if they can't be predicted perfectly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_matrix
|
Tensor
|
Suitability scores, shape |
required |
topk_mask
|
Tensor
|
Boolean top-k mask, shape |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
Updated top-k mask of shape |
Source code in yolo/tasks/detection/postprocess.py
filter_duplicates(iou_mat, topk_mask)
¶
Filter the maximum suitability target index of each anchor based on IoU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iou_mat
|
Tensor
|
IoU scores, shape |
required |
topk_mask
|
Tensor
|
Boolean top-k mask, shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[Tensor, Tensor, Tensor]: |
Tensor
|
|
Source code in yolo/tasks/detection/postprocess.py
__call__(target, predict)
¶
Matches each target to the most suitable anchor. 1. For each anchor prediction, find the highest suitability targets. 2. Match target to the best anchor. 3. Noramlize the class probilities of targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The ground truth class and bounding box information as tensor of size [batch x targets x 5]. |
required |
predict
|
Tuple[Tensor]
|
Tuple of predicted class and bounding box tensors. Class tensor is of size [batch x anchors x class] Bounding box tensor is of size [batch x anchors x 4]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
anchor_matched_targets |
Tensor
|
Tensor of size [batch x anchors x (class + 4)]. A tensor assigning each target/gt to the best fitting anchor. The class probabilities are normalized. |
valid_mask |
Tensor
|
Bool tensor of shape [batch x anchors]. True if a anchor has a target/gt assigned to it. |
Source code in yolo/tasks/detection/postprocess.py
Vec2Box
¶
Source code in yolo/tasks/detection/postprocess.py
update(image_size)
¶
image_size: W, H
Source code in yolo/tasks/detection/postprocess.py
Anc2Box
¶
Source code in yolo/tasks/detection/postprocess.py
calculate_iou(bbox1, bbox2, metrics='iou')
¶
Source code in yolo/tasks/detection/postprocess.py
transform_bbox(bbox, indicator='xywh -> xyxy')
¶
Source code in yolo/tasks/detection/postprocess.py
generate_anchors(image_size, strides)
¶
Find the anchor maps for each w, h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_size
|
List[int]
|
The image size |
required |
strides
|
List[int]
|
The stride for each prediction layer, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple[Tensor, Tensor]: |
Tensor
|
has shape |