Skip to content

Configuration

YOLO settings and hyperparameters play a critical role in the model's performance, speed, and accuracy. These settings and hyperparameters can affect the model's behavior at various stages of the model development process, including training, validation, and prediction.



Watch: Mastering Ultralytics YOLO: Configuration

Ultralytics commands use the following syntax:

Example

yolo TASK MODE ARGS
from ultralytics import YOLO

# Load a YOLO11 model from a pre-trained weights file
model = YOLO("yolo11n.pt")

# Run MODE mode using the custom arguments ARGS (guess TASK)
model.MODE(ARGS)

Where:

Default ARG values are defined on this page from the cfg/defaults.yaml file.

Tasks

YOLO models can be used for a variety of tasks, including detection, segmentation, classification and pose. These tasks differ in the type of output they produce and the specific problem they are designed to solve.

  • Detect: For identifying and localizing objects or regions of interest in an image or video.
  • Segment: For dividing an image or video into regions or pixels that correspond to different objects or classes.
  • Classify: For predicting the class label of an input image.
  • Pose: For identifying objects and estimating their keypoints in an image or video.
  • OBB: Oriented (i.e. rotated) bounding boxes suitable for satellite or medical imagery.
ArgumentDefaultDescription
task'detect'Specifies the YOLO task to be executed. Options include detect for object detection, segment for segmentation, classify for classification, pose for pose estimation and obb for oriented bounding boxes. Each task is tailored to specific types of output and problems within image and video analysis.

Tasks Guide

Modes

YOLO models can be used in different modes depending on the specific problem you are trying to solve. These modes include:

  • Train: For training a YOLO11 model on a custom dataset.
  • Val: For validating a YOLO11 model after it has been trained.
  • Predict: For making predictions using a trained YOLO11 model on new images or videos.
  • Export: For exporting a YOLO11 model to a format that can be used for deployment.
  • Track: For tracking objects in real-time using a YOLO11 model.
  • Benchmark: For benchmarking YOLO11 exports (ONNX, TensorRT, etc.) speed and accuracy.
ArgumentDefaultDescription
mode'train'Specifies the mode in which the YOLO model operates. Options are train for model training, val for validation, predict for inference on new data, export for model conversion to deployment formats, track for object tracking, and benchmark for performance evaluation. Each mode is designed for different stages of the model lifecycle, from development through deployment.

Modes Guide

Train Settings

The training settings for YOLO models encompass various hyperparameters and configurations used during the training process. These settings influence the model's performance, speed, and accuracy. Key training settings include batch size, learning rate, momentum, and weight decay. Additionally, the choice of optimizer, loss function, and training dataset composition can impact the training process. Careful tuning and experimentation with these settings are crucial for optimizing performance.

ArgumentDefaultDescription
modelNoneSpecifies the model file for training. Accepts a path to either a .pt pretrained model or a .yaml configuration file. Essential for defining the model structure or initializing weights.
dataNonePath to the dataset configuration file (e.g., coco8.yaml). This file contains dataset-specific parameters, including paths to training and validation data, class names, and number of classes.
epochs100Total number of training epochs. Each epoch represents a full pass over the entire dataset. Adjusting this value can affect training duration and model performance.
timeNoneMaximum training time in hours. If set, this overrides the epochs argument, allowing training to automatically stop after the specified duration. Useful for time-constrained training scenarios.
patience100Number of epochs to wait without improvement in validation metrics before early stopping the training. Helps prevent overfitting by stopping training when performance plateaus.
batch16Batch size, with three modes: set as an integer (e.g., batch=16), auto mode for 60% GPU memory utilization (batch=-1), or auto mode with specified utilization fraction (batch=0.70).
imgsz640Target image size for training. All images are resized to this dimension before being fed into the model. Affects model accuracy and computational complexity.
saveTrueEnables saving of training checkpoints and final model weights. Useful for resuming training or model deployment.
save_period-1Frequency of saving model checkpoints, specified in epochs. A value of -1 disables this feature. Useful for saving interim models during long training sessions.
cacheFalseEnables caching of dataset images in memory (True/ram), on disk (disk), or disables it (False). Improves training speed by reducing disk I/O at the cost of increased memory usage.
deviceNoneSpecifies the computational device(s) for training: a single GPU (device=0), multiple GPUs (device=0,1), CPU (device=cpu), or MPS for Apple silicon (device=mps).
workers8Number of worker threads for data loading (per RANK if Multi-GPU training). Influences the speed of data preprocessing and feeding into the model, especially useful in multi-GPU setups.
projectNoneName of the project directory where training outputs are saved. Allows for organized storage of different experiments.
nameNoneName of the training run. Used for creating a subdirectory within the project folder, where training logs and outputs are stored.
exist_okFalseIf True, allows overwriting of an existing project/name directory. Useful for iterative experimentation without needing to manually clear previous outputs.
pretrainedTrueDetermines whether to start training from a pretrained model. Can be a boolean value or a string path to a specific model from which to load weights. Enhances training efficiency and model performance.
optimizer'auto'Choice of optimizer for training. Options include SGD, Adam, AdamW, NAdam, RAdam, RMSProp etc., or auto for automatic selection based on model configuration. Affects convergence speed and stability.
seed0Sets the random seed for training, ensuring reproducibility of results across runs with the same configurations.
deterministicTrueForces deterministic algorithm use, ensuring reproducibility but may affect performance and speed due to the restriction on non-deterministic algorithms.
single_clsFalseTreats all classes in multi-class datasets as a single class during training. Useful for binary classification tasks or when focusing on object presence rather than classification.
rectFalseEnables rectangular training, optimizing batch composition for minimal padding. Can improve efficiency and speed but may affect model accuracy.
cos_lrFalseUtilizes a cosine learning rate scheduler, adjusting the learning rate following a cosine curve over epochs. Helps in managing learning rate for better convergence.
close_mosaic10Disables mosaic data augmentation in the last N epochs to stabilize training before completion. Setting to 0 disables this feature.
resumeFalseResumes training from the last saved checkpoint. Automatically loads model weights, optimizer state, and epoch count, continuing training seamlessly.
ampTrueEnables Automatic Mixed Precision (AMP) training, reducing memory usage and possibly speeding up training with minimal impact on accuracy.
fraction1.0Specifies the fraction of the dataset to use for training. Allows for training on a subset of the full dataset, useful for experiments or when resources are limited.
profileFalseEnables profiling of ONNX and TensorRT speeds during training, useful for optimizing model deployment.
freezeNoneFreezes the first N layers of the model or specified layers by index, reducing the number of trainable parameters. Useful for fine-tuning or transfer learning.
lr00.01Initial learning rate (i.e. SGD=1E-2, Adam=1E-3) . Adjusting this value is crucial for the optimization process, influencing how rapidly model weights are updated.
lrf0.01Final learning rate as a fraction of the initial rate = (lr0 * lrf), used in conjunction with schedulers to adjust the learning rate over time.
momentum0.937Momentum factor for SGD or beta1 for Adam optimizers, influencing the incorporation of past gradients in the current update.
weight_decay0.0005L2 regularization term, penalizing large weights to prevent overfitting.
warmup_epochs3.0Number of epochs for learning rate warmup, gradually increasing the learning rate from a low value to the initial learning rate to stabilize training early on.
warmup_momentum0.8Initial momentum for warmup phase, gradually adjusting to the set momentum over the warmup period.
warmup_bias_lr0.1Learning rate for bias parameters during the warmup phase, helping stabilize model training in the initial epochs.
box7.5Weight of the box loss component in the loss function, influencing how much emphasis is placed on accurately predicting bounding box coordinates.
cls0.5Weight of the classification loss in the total loss function, affecting the importance of correct class prediction relative to other components.
dfl1.5Weight of the distribution focal loss, used in certain YOLO versions for fine-grained classification.
pose12.0Weight of the pose loss in models trained for pose estimation, influencing the emphasis on accurately predicting pose keypoints.
kobj2.0Weight of the keypoint objectness loss in pose estimation models, balancing detection confidence with pose accuracy.
label_smoothing0.0Applies label smoothing, softening hard labels to a mix of the target label and a uniform distribution over labels, can improve generalization.
nbs64Nominal batch size for normalization of loss.
overlap_maskTrueDetermines whether object masks should be merged into a single mask for training, or kept separate for each object. In case of overlap, the smaller mask is overlayed on top of the larger mask during merge.
mask_ratio4Downsample ratio for segmentation masks, affecting the resolution of masks used during training.
dropout0.0Dropout rate for regularization in classification tasks, preventing overfitting by randomly omitting units during training.
valTrueEnables validation during training, allowing for periodic evaluation of model performance on a separate dataset.
plotsFalseGenerates and saves plots of training and validation metrics, as well as prediction examples, providing visual insights into model performance and learning progression.

Note on Batch-size Settings

The batch argument can be configured in three ways:

  • Fixed Batch Size: Set an integer value (e.g., batch=16), specifying the number of images per batch directly.
  • Auto Mode (60% GPU Memory): Use batch=-1 to automatically adjust batch size for approximately 60% CUDA memory utilization.
  • Auto Mode with Utilization Fraction: Set a fraction value (e.g., batch=0.70) to adjust batch size based on the specified fraction of GPU memory usage.

Train Guide

Predict Settings

The prediction settings for YOLO models encompass a range of hyperparameters and configurations that influence the model's performance, speed, and accuracy during inference on new data. Careful tuning and experimentation with these settings are essential to achieve optimal performance for a specific task. Key settings include the confidence threshold, Non-Maximum Suppression (NMS) threshold, and the number of classes considered. Additional factors affecting the prediction process are input data size and format, the presence of supplementary features such as masks or multiple labels per box, and the particular task the model is employed for.

Inference arguments:

ArgumentTypeDefaultDescription
sourcestr'ultralytics/assets'Specifies the data source for inference. Can be an image path, video file, directory, URL, or device ID for live feeds. Supports a wide range of formats and sources, enabling flexible application across different types of input.
conffloat0.25Sets the minimum confidence threshold for detections. Objects detected with confidence below this threshold will be disregarded. Adjusting this value can help reduce false positives.
ioufloat0.7Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Lower values result in fewer detections by eliminating overlapping boxes, useful for reducing duplicates.
imgszint or tuple640Defines the image size for inference. Can be a single integer 640 for square resizing or a (height, width) tuple. Proper sizing can improve detection accuracy and processing speed.
halfboolFalseEnables half-precision (FP16) inference, which can speed up model inference on supported GPUs with minimal impact on accuracy.
devicestrNoneSpecifies the device for inference (e.g., cpu, cuda:0 or 0). Allows users to select between CPU, a specific GPU, or other compute devices for model execution.
max_detint300Maximum number of detections allowed per image. Limits the total number of objects the model can detect in a single inference, preventing excessive outputs in dense scenes.
vid_strideint1Frame stride for video inputs. Allows skipping frames in videos to speed up processing at the cost of temporal resolution. A value of 1 processes every frame, higher values skip frames.
stream_bufferboolFalseDetermines whether to queue incoming frames for video streams. If False, old frames get dropped to accomodate new frames (optimized for real-time applications). If `True', queues new frames in a buffer, ensuring no frames get skipped, but will cause latency if inference FPS is lower than stream FPS.
visualizeboolFalseActivates visualization of model features during inference, providing insights into what the model is "seeing". Useful for debugging and model interpretation.
augmentboolFalseEnables test-time augmentation (TTA) for predictions, potentially improving detection robustness at the cost of inference speed.
agnostic_nmsboolFalseEnables class-agnostic Non-Maximum Suppression (NMS), which merges overlapping boxes of different classes. Useful in multi-class detection scenarios where class overlap is common.
classeslist[int]NoneFilters predictions to a set of class IDs. Only detections belonging to the specified classes will be returned. Useful for focusing on relevant objects in multi-class detection tasks.
retina_masksboolFalseReturns high-resolution segmentation masks. The returned masks (masks.data) will match the original image size if enabled. If disabled, they have the image size used during inference.
embedlist[int]NoneSpecifies the layers from which to extract feature vectors or embeddings. Useful for downstream tasks like clustering or similarity search.
projectstrNoneName of the project directory where prediction outputs are saved if save is enabled.
namestrNoneName of the prediction run. Used for creating a subdirectory within the project folder, where prediction outputs are stored if save is enabled.

Visualization arguments:

ArgumentTypeDefaultDescription
showboolFalseIf True, displays the annotated images or videos in a window. Useful for immediate visual feedback during development or testing.
saveboolFalse or TrueEnables saving of the annotated images or videos to file. Useful for documentation, further analysis, or sharing results. Defaults to True when using CLI & False when used in Python.
save_framesboolFalseWhen processing videos, saves individual frames as images. Useful for extracting specific frames or for detailed frame-by-frame analysis.
save_txtboolFalseSaves detection results in a text file, following the format [class] [x_center] [y_center] [width] [height] [confidence]. Useful for integration with other analysis tools.
save_confboolFalseIncludes confidence scores in the saved text files. Enhances the detail available for post-processing and analysis.
save_cropboolFalseSaves cropped images of detections. Useful for dataset augmentation, analysis, or creating focused datasets for specific objects.
show_labelsboolTrueDisplays labels for each detection in the visual output. Provides immediate understanding of detected objects.
show_confboolTrueDisplays the confidence score for each detection alongside the label. Gives insight into the model's certainty for each detection.
show_boxesboolTrueDraws bounding boxes around detected objects. Essential for visual identification and location of objects in images or video frames.
line_widthNone or intNoneSpecifies the line width of bounding boxes. If None, the line width is automatically adjusted based on the image size. Provides visual customization for clarity.

Predict Guide

Validation Settings

The val (validation) settings for YOLO models involve various hyperparameters and configurations used to evaluate the model's performance on a validation dataset. These settings influence the model's performance, speed, and accuracy. Common YOLO validation settings include batch size, validation frequency during training, and performance evaluation metrics. Other factors affecting the validation process include the validation dataset's size and composition, as well as the specific task the model is employed for.

ArgumentTypeDefaultDescription
datastrNoneSpecifies the path to the dataset configuration file (e.g., coco8.yaml). This file includes paths to validation data, class names, and number of classes.
imgszint640Defines the size of input images. All images are resized to this dimension before processing.
batchint16Sets the number of images per batch. Use -1 for AutoBatch, which automatically adjusts based on GPU memory availability.
save_jsonboolFalseIf True, saves the results to a JSON file for further analysis or integration with other tools.
save_hybridboolFalseIf True, saves a hybrid version of labels that combines original annotations with additional model predictions.
conffloat0.001Sets the minimum confidence threshold for detections. Detections with confidence below this threshold are discarded.
ioufloat0.6Sets the Intersection Over Union (IoU) threshold for Non-Maximum Suppression (NMS). Helps in reducing duplicate detections.
max_detint300Limits the maximum number of detections per image. Useful in dense scenes to prevent excessive detections.
halfboolTrueEnables half-precision (FP16) computation, reducing memory usage and potentially increasing speed with minimal impact on accuracy.
devicestrNoneSpecifies the device for validation (cpu, cuda:0, etc.). Allows flexibility in utilizing CPU or GPU resources.
dnnboolFalseIf True, uses the OpenCV DNN module for ONNX model inference, offering an alternative to PyTorch inference methods.
plotsboolFalseWhen set to True, generates and saves plots of predictions versus ground truth for visual evaluation of the model's performance.
rectboolTrueIf True, uses rectangular inference for batching, reducing padding and potentially increasing speed and efficiency.
splitstrvalDetermines the dataset split to use for validation (val, test, or train). Allows flexibility in choosing the data segment for performance evaluation.
projectstrNoneName of the project directory where validation outputs are saved.
namestrNoneName of the validation run. Used for creating a subdirectory within the project folder, where valdiation logs and outputs are stored.

Careful tuning and experimentation with these settings are crucial to ensure optimal performance on the validation dataset and detect and prevent overfitting.

Val Guide

Export Settings

Export settings for YOLO models encompass configurations and options related to saving or exporting the model for use in different environments or platforms. These settings can impact the model's performance, size, and compatibility with various systems. Key export settings include the exported model file format (e.g., ONNX, TensorFlow SavedModel), the target device (e.g., CPU, GPU), and additional features such as masks or multiple labels per box. The export process may also be affected by the model's specific task and the requirements or constraints of the destination environment or platform.

ArgumentTypeDefaultDescription
formatstr'torchscript'Target format for the exported model, such as 'onnx', 'torchscript', 'tensorflow', or others, defining compatibility with various deployment environments.
imgszint or tuple640Desired image size for the model input. Can be an integer for square images or a tuple (height, width) for specific dimensions.
kerasboolFalseEnables export to Keras format for TensorFlow SavedModel, providing compatibility with TensorFlow serving and APIs.
optimizeboolFalseApplies optimization for mobile devices when exporting to TorchScript, potentially reducing model size and improving performance.
halfboolFalseEnables FP16 (half-precision) quantization, reducing model size and potentially speeding up inference on supported hardware.
int8boolFalseActivates INT8 quantization, further compressing the model and speeding up inference with minimal accuracy loss, primarily for edge devices.
dynamicboolFalseAllows dynamic input sizes for ONNX, TensorRT and OpenVINO exports, enhancing flexibility in handling varying image dimensions.
simplifyboolTrueSimplifies the model graph for ONNX exports with onnxslim, potentially improving performance and compatibility.
opsetintNoneSpecifies the ONNX opset version for compatibility with different ONNX parsers and runtimes. If not set, uses the latest supported version.
workspacefloat4.0Sets the maximum workspace size in GiB for TensorRT optimizations, balancing memory usage and performance.
nmsboolFalseAdds Non-Maximum Suppression (NMS) to the CoreML export, essential for accurate and efficient detection post-processing.
batchint1Specifies export model batch inference size or the max number of images the exported model will process concurrently in predict mode.
devicestrNoneSpecifies the device for exporting: GPU (device=0), CPU (device=cpu), MPS for Apple silicon (device=mps) or DLA for NVIDIA Jetson (device=dla:0 or device=dla:1).

It is crucial to thoughtfully configure these settings to ensure the exported model is optimized for the intended use case and functions effectively in the target environment.

Export Guide

Augmentation Settings

Augmentation techniques are essential for improving the robustness and performance of YOLO models by introducing variability into the training data, helping the model generalize better to unseen data. The following table outlines the purpose and effect of each augmentation argument:

ArgumentTypeDefaultRangeDescription
hsv_hfloat0.0150.0 - 1.0Adjusts the hue of the image by a fraction of the color wheel, introducing color variability. Helps the model generalize across different lighting conditions.
hsv_sfloat0.70.0 - 1.0Alters the saturation of the image by a fraction, affecting the intensity of colors. Useful for simulating different environmental conditions.
hsv_vfloat0.40.0 - 1.0Modifies the value (brightness) of the image by a fraction, helping the model to perform well under various lighting conditions.
degreesfloat0.0-180 - +180Rotates the image randomly within the specified degree range, improving the model's ability to recognize objects at various orientations.
translatefloat0.10.0 - 1.0Translates the image horizontally and vertically by a fraction of the image size, aiding in learning to detect partially visible objects.
scalefloat0.5>=0.0Scales the image by a gain factor, simulating objects at different distances from the camera.
shearfloat0.0-180 - +180Shears the image by a specified degree, mimicking the effect of objects being viewed from different angles.
perspectivefloat0.00.0 - 0.001Applies a random perspective transformation to the image, enhancing the model's ability to understand objects in 3D space.
flipudfloat0.00.0 - 1.0Flips the image upside down with the specified probability, increasing the data variability without affecting the object's characteristics.
fliplrfloat0.50.0 - 1.0Flips the image left to right with the specified probability, useful for learning symmetrical objects and increasing dataset diversity.
bgrfloat0.00.0 - 1.0Flips the image channels from RGB to BGR with the specified probability, useful for increasing robustness to incorrect channel ordering.
mosaicfloat1.00.0 - 1.0Combines four training images into one, simulating different scene compositions and object interactions. Highly effective for complex scene understanding.
mixupfloat0.00.0 - 1.0Blends two images and their labels, creating a composite image. Enhances the model's ability to generalize by introducing label noise and visual variability.
copy_pastefloat0.00.0 - 1.0Copies objects from one image and pastes them onto another, useful for increasing object instances and learning object occlusion.
copy_paste_modestrflip-Copy-Paste augmentation method selection among the options of ("flip", "mixup").
auto_augmentstrrandaugment-Automatically applies a predefined augmentation policy (randaugment, autoaugment, augmix), optimizing for classification tasks by diversifying the visual features.
erasingfloat0.40.0 - 0.9Randomly erases a portion of the image during classification training, encouraging the model to focus on less obvious features for recognition.
crop_fractionfloat1.00.1 - 1.0Crops the classification image to a fraction of its size to emphasize central features and adapt to object scales, reducing background distractions.

These settings can be adjusted to meet the specific requirements of the dataset and task at hand. Experimenting with different values can help find the optimal augmentation strategy that leads to the best model performance.

Logging, Checkpoints and Plotting Settings

Logging, checkpoints, plotting, and file management are important considerations when training a YOLO model.

  • Logging: It is often helpful to log various metrics and statistics during training to track the model's progress and diagnose any issues that may arise. This can be done using a logging library such as TensorBoard or by writing log messages to a file.
  • Checkpoints: It is a good practice to save checkpoints of the model at regular intervals during training. This allows you to resume training from a previous point if the training process is interrupted or if you want to experiment with different training configurations.
  • Plotting: Visualizing the model's performance and training progress can be helpful for understanding how the model is behaving and identifying potential issues. This can be done using a plotting library such as matplotlib or by generating plots using a logging library such as TensorBoard.
  • File management: Managing the various files generated during the training process, such as model checkpoints, log files, and plots, can be challenging. It is important to have a clear and organized file structure to keep track of these files and make it easy to access and analyze them as needed.

Effective logging, checkpointing, plotting, and file management can help you keep track of the model's progress and make it easier to debug and optimize the training process.

ArgumentDefaultDescription
project'runs'Specifies the root directory for saving training runs. Each run will be saved in a separate subdirectory within this directory.
name'exp'Defines the name of the experiment. If not specified, YOLO automatically increments this name for each run, e.g., exp, exp2, etc., to avoid overwriting previous experiments.
exist_okFalseDetermines whether to overwrite an existing experiment directory if one with the same name already exists. Setting this to True allows overwriting, while False prevents it.
plotsFalseControls the generation and saving of training and validation plots. Set to True to create plots such as loss curves, precision-recall curves, and sample predictions. Useful for visually tracking model performance over time.
saveFalseEnables the saving of training checkpoints and final model weights. Set to True to periodically save model states, allowing training to be resumed from these checkpoints or models to be deployed.

FAQ

How do I improve the performance of my YOLO model during training?

Improving YOLO model performance involves tuning hyperparameters like batch size, learning rate, momentum, and weight decay. Adjusting augmentation settings, selecting the right optimizer, and employing techniques like early stopping or mixed precision can also help. For detailed guidance on training settings, refer to the Train Guide.

What are the key hyperparameters to consider for YOLO model accuracy?

Key hyperparameters affecting YOLO model accuracy include:

  • Batch Size (batch): Larger batch sizes can stabilize training but may require more memory.
  • Learning Rate (lr0): Controls the step size for weight updates; smaller rates offer fine adjustments but slow convergence.
  • Momentum (momentum): Helps accelerate gradient vectors in the right directions, dampening oscillations.
  • Image Size (imgsz): Larger image sizes can improve accuracy but increase computational load.

Adjust these values based on your dataset and hardware capabilities. Explore more in the Train Settings section.

How do I set the learning rate for training a YOLO model?

The learning rate (lr0) is crucial for optimization. A common starting point is 0.01 for SGD or 0.001 for Adam. It's essential to monitor training metrics and adjust if necessary. Use cosine learning rate schedulers (cos_lr) or warmup techniques (warmup_epochs, warmup_momentum) to dynamically modify the rate during training. Find more details in the Train Guide.

What are the default inference settings for YOLO models?

Default inference settings include:

  • Confidence Threshold (conf=0.25): Minimum confidence for detections.
  • IoU Threshold (iou=0.7): For Non-Maximum Suppression (NMS).
  • Image Size (imgsz=640): Resizes input images prior to inference.
  • Device (device=None): Selects CPU or GPU for inference. For a comprehensive overview, visit the Predict Settings section and the Predict Guide.

Why should I use mixed precision training with YOLO models?

Mixed precision training, enabled with amp=True, helps reduce memory usage and can speed up training by utilizing the advantages of both FP16 and FP32. This is beneficial for modern GPUs, which support mixed precision natively, allowing more models to fit in memory and enable faster computations without significant loss in accuracy. Learn more about this in the Train Guide.

📅 Created 1 year ago ✏️ Updated 1 month ago

Comments