Reference for ultralytics/models/yolo/pose/val.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/pose/val.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.pose.val.PoseValidator
Bases: DetectionValidator
A class extending the DetectionValidator class for validation based on a pose model.
This validator is specifically designed for pose estimation tasks, handling keypoints and implementing specialized metrics for pose evaluation.
Attributes:
Name | Type | Description |
---|---|---|
sigma |
ndarray
|
Sigma values for OKS calculation, either OKS_SIGMA or ones divided by number of keypoints. |
kpt_shape |
List[int]
|
Shape of the keypoints, typically [17, 3] for COCO format. |
args |
dict
|
Arguments for the validator including task set to "pose". |
metrics |
PoseMetrics
|
Metrics object for pose evaluation. |
Methods:
Name | Description |
---|---|
preprocess |
Preprocesses batch data for pose validation. |
get_desc |
Returns description of evaluation metrics. |
init_metrics |
Initializes pose metrics for the model. |
_prepare_batch |
Prepares a batch for processing. |
_prepare_pred |
Prepares and scales predictions for evaluation. |
update_metrics |
Updates metrics with new predictions. |
_process_batch |
Processes batch to compute IoU between detections and ground truth. |
plot_val_samples |
Plots validation samples with ground truth annotations. |
plot_predictions |
Plots model predictions. |
save_one_txt |
Saves detections to a text file. |
pred_to_json |
Converts predictions to COCO JSON format. |
eval_json |
Evaluates model using COCO JSON format. |
Examples:
>>> from ultralytics.models.yolo.pose import PoseValidator
>>> args = dict(model="yolo11n-pose.pt", data="coco8-pose.yaml")
>>> validator = PoseValidator(args=args)
>>> validator()
This validator is specifically designed for pose estimation tasks, handling keypoints and implementing specialized metrics for pose evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader
|
DataLoader
|
Dataloader to be used for validation. |
None
|
save_dir
|
Path | str
|
Directory to save results. |
None
|
pbar
|
Any
|
Progress bar for displaying progress. |
None
|
args
|
dict
|
Arguments for the validator including task set to "pose". |
None
|
_callbacks
|
list
|
List of callback functions to be executed during validation. |
None
|
Examples:
>>> from ultralytics.models.yolo.pose import PoseValidator
>>> args = dict(model="yolov8n-pose.pt", data="coco8-pose.yaml")
>>> validator = PoseValidator(args=args)
>>> validator()
Notes
This class extends DetectionValidator with pose-specific functionality. It initializes with sigma values for OKS calculation and sets up PoseMetrics for evaluation. A warning is displayed when using Apple MPS due to a known bug with pose models.
Source code in ultralytics/models/yolo/pose/val.py
eval_json
Evaluate object detection model using COCO JSON format.
Source code in ultralytics/models/yolo/pose/val.py
get_desc
Return description of evaluation metrics in string format.
Source code in ultralytics/models/yolo/pose/val.py
init_metrics
Initialize pose estimation metrics for YOLO model.
Source code in ultralytics/models/yolo/pose/val.py
plot_predictions
Plot and save model predictions with bounding boxes and keypoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Dictionary containing batch data including images, file paths, and other metadata. |
required |
preds
|
List[Tensor]
|
List of prediction tensors from the model, each containing bounding boxes, confidence scores, class predictions, and keypoints. |
required |
ni
|
int
|
Batch index used for naming the output file. |
required |
The function extracts keypoints from predictions, converts predictions to target format, and plots them on the input images. The resulting visualization is saved to the specified save directory.
Source code in ultralytics/models/yolo/pose/val.py
plot_val_samples
Plot and save validation set samples with ground truth bounding boxes and keypoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Dictionary containing batch data with keys: - img (torch.Tensor): Batch of images - batch_idx (torch.Tensor): Batch indices for each image - cls (torch.Tensor): Class labels - bboxes (torch.Tensor): Bounding box coordinates - keypoints (torch.Tensor): Keypoint coordinates - im_file (list): List of image file paths |
required |
ni
|
int
|
Batch index used for naming the output file |
required |
Source code in ultralytics/models/yolo/pose/val.py
pred_to_json
Convert YOLO predictions to COCO JSON format.
This method takes prediction tensors and a filename, converts the bounding boxes from YOLO format to COCO format, and appends the results to the internal JSON dictionary (self.jdict).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Prediction tensor containing bounding boxes, confidence scores, class IDs, and keypoints, with shape (N, 6+K) where N is the number of predictions and K is the flattened keypoints dimension. |
required |
filename
|
str | Path
|
Path to the image file for which predictions are being processed. |
required |
Notes
The method extracts the image ID from the filename stem (either as an integer if numeric, or as a string), converts bounding boxes from xyxy to xywh format, and adjusts coordinates from center to top-left corner before saving to the JSON dictionary.
Source code in ultralytics/models/yolo/pose/val.py
preprocess
Preprocess batch by converting keypoints data to float and moving it to the device.
save_one_txt
Save YOLO pose detections to a text file in normalized coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Prediction boxes and scores with shape (N, 6) for (x1, y1, x2, y2, conf, cls). |
required |
pred_kpts
|
Tensor
|
Predicted keypoints with shape (N, K, D) where K is the number of keypoints and D is the dimension (typically 3 for x, y, visibility). |
required |
save_conf
|
bool
|
Whether to save confidence scores. |
required |
shape
|
tuple
|
Original image shape (height, width). |
required |
file
|
Path
|
Output file path to save detections. |
required |
Notes
The output format is: class_id x_center y_center width height confidence keypoints where keypoints are normalized (x, y, visibility) values for each point.
Source code in ultralytics/models/yolo/pose/val.py
update_metrics
Update metrics with new predictions and ground truth data.
This method processes each prediction, compares it with ground truth, and updates various statistics for performance evaluation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
List[Tensor]
|
List of prediction tensors from the model. |
required |
batch
|
dict
|
Batch data containing images and ground truth annotations. |
required |