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
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 |
|
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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
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 |
|
pred_to_json
pred_to_json(predn: Tensor, filename: str) -> 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 |
filename
|
str
|
Image filename. |
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
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 |
|
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|