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
SegmentationValidator(
dataloader=None, save_dir=None, args=None, _callbacks=None
)
Bases: DetectionValidator
A class extending the DetectionValidator class for validation based on a segmentation model.
This validator handles the evaluation of segmentation models, processing both bounding box and mask predictions to compute metrics such as mAP for both detection and segmentation tasks.
Attributes:
Name | Type | Description |
---|---|---|
plot_masks |
list
|
List to store masks for plotting. |
process |
callable
|
Function to process masks based on save_json and save_txt flags. |
args |
namespace
|
Arguments for the validator. |
metrics |
SegmentMetrics
|
Metrics calculator for segmentation tasks. |
stats |
dict
|
Dictionary to store statistics during validation. |
Examples:
>>> from ultralytics.models.yolo.segment import SegmentationValidator
>>> args = dict(model="yolo11n-seg.pt", data="coco8-seg.yaml")
>>> validator = SegmentationValidator(args=args)
>>> validator()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader
|
DataLoader
|
Dataloader to use for validation. |
None
|
save_dir
|
Path
|
Directory to save results. |
None
|
args
|
namespace
|
Arguments for the validator. |
None
|
_callbacks
|
list
|
List of callback functions. |
None
|
Source code in ultralytics/models/yolo/segment/val.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
eval_json
eval_json(stats: Dict[str, Any]) -> Dict[str, Any]
Return COCO-style instance segmentation evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
247 248 249 250 251 252 253 254 255 |
|
get_desc
get_desc() -> str
Return a formatted description of evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
init_metrics
init_metrics(model: Module) -> None
Initialize metrics and select mask processing function based on save_json flag.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to validate. |
required |
Source code in ultralytics/models/yolo/segment/val.py
67 68 69 70 71 72 73 74 75 76 77 78 |
|
plot_predictions
plot_predictions(
batch: Dict[str, Any], preds: List[Dict[str, Tensor]], ni: int
) -> None
Plot batch predictions with masks and bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict[str, Any]
|
Batch containing images and annotations. |
required |
preds
|
List[Dict[str, Tensor]]
|
List of predictions from the model. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/segment/val.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
postprocess
postprocess(preds: List[Tensor]) -> List[Dict[str, torch.Tensor]]
Post-process YOLO predictions and return output detections with proto.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
List[Tensor]
|
Raw predictions from the model. |
required |
Returns:
Type | Description |
---|---|
List[Dict[str, Tensor]]
|
List[Dict[str, torch.Tensor]]: Processed detection predictions with masks. |
Source code in ultralytics/models/yolo/segment/val.py
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 |
|
pred_to_json
pred_to_json(predn: Dict[str, Tensor], pbatch: Dict[str, Any]) -> None
Save one JSON result for COCO evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Dict[str, Tensor]
|
Predictions containing bboxes, masks, confidence scores, and classes. |
required |
pbatch
|
Dict[str, Any]
|
Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'. |
required |
Source code in ultralytics/models/yolo/segment/val.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
preprocess
preprocess(batch: Dict[str, Any]) -> Dict[str, Any]
Preprocess batch of images for YOLO segmentation validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict[str, Any]
|
Batch containing images and annotations. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Preprocessed batch. |
Source code in ultralytics/models/yolo/segment/val.py
53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
save_one_txt
save_one_txt(
predn: Tensor, save_conf: bool, shape: Tuple[int, int], file: Path
) -> None
Save YOLO detections to a txt file in normalized coordinates in a specific format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Predictions in the format (x1, y1, x2, y2, conf, class). |
required |
save_conf
|
bool
|
Whether to save confidence scores. |
required |
shape
|
Tuple[int, int]
|
Shape of the original image. |
required |
file
|
Path
|
File path to save the detections. |
required |
Source code in ultralytics/models/yolo/segment/val.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
scale_preds
scale_preds(
predn: Dict[str, Tensor], pbatch: Dict[str, Any]
) -> Dict[str, torch.Tensor]
Scales predictions to the original image size.
Source code in ultralytics/models/yolo/segment/val.py
236 237 238 239 240 241 242 243 244 245 |
|