After you completed the steps before, you can now train the ML model.
The data.yaml file represents an essential element in the process of
configuring the training environment for the ML model. It centralizes information related to
the dataset structure and the parameters necessary for the training script to run correctly.
Essentially, the file defines the paths to the training and validation images, the number of
classes, and
their names, thus facilitating data access and organization in a standardized manner.
data.yaml (Simple) [Recommended For Starting]
path: AI
train: train/images
val: val/images
nc: 3
names: ['YellowSample', 'BlueSample', 'RedSample']
train and val: These entries indicate the path to the folders containing the images for
training and validation. Thus, the script knows exactly where to retrieve the data from.
nc: Specifies the number of classes in the dataset. This information is vital for
the correct configuration of the last layer of the neural network.
names: A list of labels (class names) corresponding to the numeric identifiers
used in the generated label files, for example, with the labelImg application.
Or the [BETA] version \/.
data.yaml (Advanced) [BETA] [Not really stable] [!Use only for large
datasets!]
path: AI
train: train/images
val: val/images
nc: 3
lr0: 0.001
lrf: 0.1
warmup_epochs: 5
degrees: 2.5
perspective: 0.0
scale: 0.01
fliplr: 0.05
copy_paste: 0.0
box: 0.07
cls: 0.4
dfl: 1.5
names: ['YellowSample', 'BlueSample', 'RedSample']
The ml_training.py file is the central component that orchestrates
the entire training and validation process of the YOLOv8n model, using the Ultralytics library and
PyTorch infrastructure. Here is a technical description of each section and the parameters used:
1. Module import and basic settings:
At the beginning, the YOLO class is imported from the Ultralytics library, essential
for
manipulating and training YOLO networks, as well as PyTorch, which handles tensor operations
and GPU execution. The variable indicating the path to the configuration file (data.yaml)
contains all the details about the dataset (path to training and validation images, number of
classes and their names). Additionally, setting the device to "cuda" ensures that
training will be performed on the GPU, significantly accelerating calculations.
2. Model initialization:
The model is instantiated using a pre-trained weights file (yolov8n.pt).
This approach offers a robust starting point, as the network benefits from pre-extracted knowledge,
accelerating the convergence process and improving initial performance.
3. Training configuration:
The training process is triggered with a series of critical hyperparameters, each
playing an essential role in optimizing model performance:
Dataset: The data.yaml file is used to locate the images and related labels,
ensuring consistency between training and validation data.
Epochs: Set to 150, these represent the total number of complete cycles
through the entire training dataset. Each epoch gives the model the opportunity to
adjust weights based on all available data, contributing to establishing
convergence.
Image size (imgsz): A fixed value of 640 indicates that all
images will be resized to 640x640 pixels, ensuring uniformity in the training process
and facilitating network input management.
Automatic Mixed Precision (amp): Activating this feature allows the combined use
of 16 and 32-bit precision, reducing memory consumption and accelerating
training, without significantly compromising model accuracy.
Batch size: With a value of 12, this parameter defines the number of images
processed simultaneously before updating model parameters. An optimal batch size helps
stabilize gradients, offering a balance between computational performance and
optimization stability.
Single Class vs. Multi-Class: The single_cls parameter is set to false, indicating
that the model is prepared to distinguish between multiple object classes, which
implies greater learning complexity.
Patience: The value of 100 epochs specifies an early stopping strategy, interrupting
training if no improvements are recorded on the validation set over an extended period,
thus contributing to avoiding overfitting.
Optimizer and related hyperparameters: Choosing the Adam optimizer, together with
settings for momentum (0.9) and weight decay (0.0005), controls how
model weights are adjusted. These settings are essential to ensure efficient convergence
and to prevent the accumulation of excessively large weight values.
Augmentation specific parameter (close_mosaic): This setting adjusts the application
mode
of mosaic augmentation, a technique that combines multiple images to increase
data diversity. Closing this technique after a certain number of epochs allows
the model to focus on learning fine details once it has benefited from an initially
diversified dataset.
ml_training.py
from ultralytics import YOLO
import torch
data_yaml = 'data.yaml'
device = 'cuda'
def main():
model = YOLO("yolov8n.pt")
model.train(data='data.yaml', epochs=150, imgsz=640,amp=True, device=device, batch=12, single_cls=False,patience=100, optimizer='Adam', momentum=0.9, weight_decay=0.0005, close_mosaic=25)
model.val(data=data_yaml)
if __name__ == '__main__':
main()
Examples