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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
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
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
|
get_desc
get_desc() -> str
Return a formatted description of evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
plot_predictions
plot_predictions(batch: Dict[str, Any], preds: List[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[Tensor]
|
List of predictions from the model. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/segment/val.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
plot_val_samples
plot_val_samples(batch: Dict[str, Any], ni: int) -> None
Plot validation samples with bounding box labels and masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict[str, Any]
|
Batch containing images and annotations. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/segment/val.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
postprocess
postprocess(preds: List[Tensor]) -> Tuple[List[torch.Tensor], 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:
Name | Type | Description |
---|---|---|
p |
List[Tensor]
|
Processed detection predictions. |
proto |
Tensor
|
Prototype masks for segmentation. |
Source code in ultralytics/models/yolo/segment/val.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
pred_to_json
pred_to_json(predn: Tensor, filename: str, pred_masks: Tensor) -> None
Save one JSON result for COCO evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Predictions in the format [x1, y1, x2, y2, conf, cls]. |
required |
filename
|
str
|
Image filename. |
required |
pred_masks
|
ndarray
|
Predicted masks. |
required |
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
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
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
55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
save_one_txt
save_one_txt(
predn: Tensor,
pred_masks: 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 |
pred_masks
|
Tensor
|
Predicted masks. |
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
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
update_metrics
update_metrics(
preds: Tuple[List[Tensor], Tensor], batch: Dict[str, Any]
) -> None
Update metrics with the current batch predictions and targets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tuple[List[Tensor], Tensor]
|
List of predictions from the model. |
required |
batch
|
Dict[str, Any]
|
Batch data containing ground truth. |
required |
Source code in ultralytics/models/yolo/segment/val.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 |
|