Reference for ultralytics/models/yolo/segment/val.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/segment/val.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.segment.val.SegmentationValidator
Bases: DetectionValidator
A class extending the DetectionValidator class for validation based on a segmentation model.
Example
Source code in ultralytics/models/yolo/segment/val.py
_prepare_batch
Prepares a batch for training or inference by processing images and targets.
Source code in ultralytics/models/yolo/segment/val.py
_prepare_pred
Prepares a batch for training or inference by processing images and targets.
Source code in ultralytics/models/yolo/segment/val.py
_process_batch
_process_batch(
detections,
gt_bboxes,
gt_cls,
pred_masks=None,
gt_masks=None,
overlap=False,
masks=False,
)
Compute correct prediction matrix for a batch based on bounding boxes and optional masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detections
|
Tensor
|
Tensor of shape (N, 6) representing detected bounding boxes and associated confidence scores and class indices. Each row is of the format [x1, y1, x2, y2, conf, class]. |
required |
gt_bboxes
|
Tensor
|
Tensor of shape (M, 4) representing ground truth bounding box coordinates. Each row is of the format [x1, y1, x2, y2]. |
required |
gt_cls
|
Tensor
|
Tensor of shape (M,) representing ground truth class indices. |
required |
pred_masks
|
Tensor | None
|
Tensor representing predicted masks, if available. The shape should match the ground truth masks. |
None
|
gt_masks
|
Tensor | None
|
Tensor of shape (M, H, W) representing ground truth masks, if available. |
None
|
overlap
|
bool
|
Flag indicating if overlapping masks should be considered. |
False
|
masks
|
bool
|
Flag indicating if the batch contains mask data. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
A correct prediction matrix of shape (N, 10), where 10 represents different IoU levels. |
Note
- If
masks
is True, the function computes IoU between predicted and ground truth masks. - If
overlap
is True andmasks
is True, overlapping masks are taken into account when computing IoU.
Example
Source code in ultralytics/models/yolo/segment/val.py
eval_json
Return COCO-style object detection evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
finalize_metrics
Sets speed and confusion matrix for evaluation metrics.
get_desc
Return a formatted description of evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
init_metrics
Initialize metrics and select mask processing function based on save_json flag.
Source code in ultralytics/models/yolo/segment/val.py
plot_predictions
Plots batch predictions with masks and bounding boxes.
Source code in ultralytics/models/yolo/segment/val.py
plot_val_samples
Plots validation samples with bounding box labels.
Source code in ultralytics/models/yolo/segment/val.py
postprocess
Post-processes YOLO predictions and returns output detections with proto.
Source code in ultralytics/models/yolo/segment/val.py
pred_to_json
Save one JSON result.
Examples:
>>> result = {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
Source code in ultralytics/models/yolo/segment/val.py
preprocess
Preprocesses batch by converting masks to float and sending to device.
save_one_txt
Save YOLO detections to a txt file in normalized coordinates in a specific format.
Source code in ultralytics/models/yolo/segment/val.py
update_metrics
Metrics.