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, pbar=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
|
pbar
|
Any
|
Progress bar for displaying progress. |
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 52 53 |
|
eval_json
eval_json(stats)
Return COCO-style object detection evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
374 375 376 377 378 379 380 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 |
|
finalize_metrics
finalize_metrics(*args, **kwargs)
Finalize evaluation metrics by setting the speed attribute in the metrics object.
This method is called at the end of validation to set the processing speed for the metrics calculations. It transfers the validator's speed measurement to the metrics object for reporting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Variable length argument list. |
()
|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Source code in ultralytics/models/yolo/segment/val.py
217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
get_desc
get_desc()
Return a formatted description of evaluation metrics.
Source code in ultralytics/models/yolo/segment/val.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
init_metrics
init_metrics(model)
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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
plot_predictions
plot_predictions(batch, preds, ni)
Plot batch predictions with masks and bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Batch data containing images. |
required |
preds
|
list
|
Predictions from the model. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/segment/val.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
plot_val_samples
plot_val_samples(batch, ni)
Plot validation samples with bounding box labels and masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Batch data containing images and targets. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/segment/val.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
postprocess
postprocess(preds)
Post-process YOLO predictions and return output detections with proto.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
list
|
Raw predictions from the model. |
required |
Returns:
Name | Type | Description |
---|---|---|
p |
Tensor
|
Processed detection predictions. |
proto |
Tensor
|
Prototype masks for segmentation. |
Source code in ultralytics/models/yolo/segment/val.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
pred_to_json
pred_to_json(predn, filename, pred_masks)
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
336 337 338 339 340 341 342 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 |
|
preprocess
preprocess(batch)
Preprocess batch by converting masks to float and sending to device.
Source code in ultralytics/models/yolo/segment/val.py
55 56 57 58 59 |
|
save_one_txt
save_one_txt(predn, pred_masks, save_conf, shape, file)
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, cls]. |
required |
pred_masks
|
Tensor
|
Predicted masks. |
required |
save_conf
|
bool
|
Whether to save confidence scores. |
required |
shape
|
tuple
|
Original image shape. |
required |
file
|
Path
|
File path to save the detections. |
required |
Source code in ultralytics/models/yolo/segment/val.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
update_metrics
update_metrics(preds, batch)
Update metrics with the current batch predictions and targets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
list
|
Predictions from the model. |
required |
batch
|
dict
|
Batch data containing images and targets. |
required |
Source code in ultralytics/models/yolo/segment/val.py
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 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 |
|