Reference for ultralytics/models/yolo/detect/val.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/detect/val.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.detect.val.DetectionValidator
Bases: BaseValidator
A class extending the BaseValidator class for validation based on a detection model.
This class implements validation functionality specific to object detection tasks, including metrics calculation, prediction processing, and visualization of results.
Attributes:
Name | Type | Description |
---|---|---|
nt_per_class |
ndarray
|
Number of targets per class. |
nt_per_image |
ndarray
|
Number of targets per image. |
is_coco |
bool
|
Whether the dataset is COCO. |
is_lvis |
bool
|
Whether the dataset is LVIS. |
class_map |
list
|
Mapping from model class indices to dataset class indices. |
metrics |
DetMetrics
|
Object detection metrics calculator. |
iouv |
Tensor
|
IoU thresholds for mAP calculation. |
niou |
int
|
Number of IoU thresholds. |
lb |
list
|
List for storing ground truth labels for hybrid saving. |
jdict |
list
|
List for storing JSON detection results. |
stats |
dict
|
Dictionary for storing statistics during validation. |
Examples:
>>> from ultralytics.models.yolo.detect import DetectionValidator
>>> args = dict(model="yolo11n.pt", data="coco8.yaml")
>>> validator = DetectionValidator(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
|
dict
|
Arguments for the validator. |
None
|
_callbacks
|
list
|
List of callback functions. |
None
|
Source code in ultralytics/models/yolo/detect/val.py
build_dataset
Build YOLO Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the folder containing images. |
required |
mode
|
str
|
|
'val'
|
batch
|
int
|
Size of batches, this is for |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
YOLO dataset. |
Source code in ultralytics/models/yolo/detect/val.py
eval_json
Evaluate YOLO output in JSON format and return performance statistics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stats
|
dict
|
Current statistics dictionary. |
required |
Returns:
Type | Description |
---|---|
dict
|
Updated statistics dictionary with COCO/LVIS evaluation results. |
Source code in ultralytics/models/yolo/detect/val.py
finalize_metrics
Set final values for metrics speed and confusion matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Variable length argument list. |
()
|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Source code in ultralytics/models/yolo/detect/val.py
get_dataloader
Construct and return dataloader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_path
|
str
|
Path to the dataset. |
required |
batch_size
|
int
|
Size of each batch. |
required |
Returns:
Type | Description |
---|---|
DataLoader
|
Dataloader for validation. |
Source code in ultralytics/models/yolo/detect/val.py
get_desc
Return a formatted string summarizing class metrics of YOLO model.
get_stats
Calculate and return metrics statistics.
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing metrics results. |
Source code in ultralytics/models/yolo/detect/val.py
init_metrics
Initialize evaluation metrics for YOLO detection validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to validate. |
required |
Source code in ultralytics/models/yolo/detect/val.py
plot_predictions
Plot predicted bounding boxes on input images and save the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
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/detect/val.py
plot_val_samples
Plot validation image samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Batch containing images and annotations. |
required |
ni
|
int
|
Batch index. |
required |
Source code in ultralytics/models/yolo/detect/val.py
postprocess
Apply Non-maximum suppression to prediction outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor
|
Raw predictions from the model. |
required |
Returns:
Type | Description |
---|---|
List[Tensor]
|
Processed predictions after NMS. |
Source code in ultralytics/models/yolo/detect/val.py
pred_to_json
Serialize YOLO predictions to COCO json format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Predictions in the format (x1, y1, x2, y2, conf, class). |
required |
filename
|
str
|
Image filename. |
required |
Source code in ultralytics/models/yolo/detect/val.py
preprocess
Preprocess batch of images for YOLO validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Batch containing images and annotations. |
required |
Returns:
Type | Description |
---|---|
dict
|
Preprocessed batch. |
Source code in ultralytics/models/yolo/detect/val.py
print_results
Print training/validation set metrics per class.
Source code in ultralytics/models/yolo/detect/val.py
save_one_txt
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
|
Shape of the original image. |
required |
file
|
Path
|
File path to save the detections. |
required |
Source code in ultralytics/models/yolo/detect/val.py
update_metrics
Update metrics with new predictions and ground truth.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
List[Tensor]
|
List of predictions from the model. |
required |
batch
|
dict
|
Batch data containing ground truth. |
required |