YOLO Training Pipeline: Difference between revisions

From LogicalDOC Community Wiki
Jump to navigationJump to search
Giuseppe (talk | contribs)
Giuseppe (talk | contribs)
No edit summary
 
(64 intermediate revisions by the same user not shown)
Line 2: Line 2:


This guide describes how to prepare a dataset and train a custom YOLO model using annotations created with Label Studio.
This guide describes how to prepare a dataset and train a custom YOLO model using annotations created with Label Studio.
{{Advice| This guide describes an example workflow for training a custom YOLO model and preparing it for use with LogicalDOC.
<b><u>Please be aware that this procedure is not coverded by the standard support contract</u></b>. LogicalDOC cannot provide assistance with issues related to dataset preparation, training failures, model quality, GPU configuration, or third-party tools such as Label Studio, Ultralytics YOLO, or ONNX Runtime.
If you require professional assistance, please contact  <b>sales@logicaldoc.com</b> to request a quotation for consulting services.}}
{{Warning|
<b>Licensing Notice:</b> The examples in this guide use the Ultralytics implementation of YOLO. Ultralytics YOLO is distributed under its own licensing terms. <b><u>Before using YOLO in a commercial environment, ensure that your intended use complies with the applicable Ultralytics license.</u></b> For the latest licensing information, refer to the official Ultralytics website: https://www.ultralytics.com/license
}}


=== Prerequisites ===
=== Prerequisites ===
Line 12: Line 21:
* Ultralytics YOLO is installed
* Ultralytics YOLO is installed


=== Extract the Dataset ===


After annotating the documents in Label Studio (see [[Label Studio Guide]]), extract the downloaded archive to a local directory.
=== Expected Training Directory Structure ===
 
Before starting the training process, the training directory should be organized as follows:
 
<pre>
training/
├── dataset/
│  ├── images/
│  │  ├── train/
│  │  └── val/
│  └── labels/
│      ├── train/
│      └── val/
├── data.yaml
├── synset.txt
├── notes.json
├── train.py
├── train.sh
└── convert_pt_to_onnx.py
</pre>
 
Where:
 
* '''dataset/images/train''' contains the images used for training.
* '''dataset/images/val''' contains the images used for validation.
* '''dataset/labels/train''' contains the annotation files corresponding to the training images.
* '''dataset/labels/val''' contains the annotation files corresponding to the validation images.
* '''data.yaml''' defines the dataset configuration used by Ultralytics YOLO.
* '''synset.txt''' contains the list of class names used during annotation.
* '''notes.json''' is generated by Label Studio and contains additional project metadata.
* '''train.py''' defines the YOLO training configuration and launches the training process.
* '''train.sh''' (optional) automates dataset preparation, training, and model conversion.
* '''convert_pt_to_onnx.py''' converts the trained PyTorch model (`best.pt`) into the ONNX format required by LogicalDOC.
 


The extracted archive contains:


* labels/          (YOLO annotation files)
The following sections describe how to create this directory structure starting from the dataset exported by Label Studio.
* classes.txt      (class definitions)
* notes.json      (Label Studio metadata)
* images/          (may be empty depending on export settings)


== Populate the Training Directory ==


[[File:yolo-downloaded-archive.png|thumb|800px|center|YOLO archive content]]
=== Extract the Dataset ===


After annotating the documents in Label Studio (see [[Label Studio Guide]]), export the dataset and extract the downloaded archive to a local directory.


The extracted archive contains:


== Prepare the folder to be correctly used for the training ==
* <code>labels/</code> – YOLO annotation files (`.txt`)
* <code>classes.txt</code> – class definitions
* <code>notes.json</code> – Label Studio metadata
* <code>images/</code> – image files (may be empty depending on the selected export format)


[[File:Yolo-downloaded-archive.png|thumb|800px|center|Content of the dataset exported by Label Studio]]


=== Dataset Structure ===
=== Prepare the Dataset Structure ===


The training pipeline expects the following directory structure:
The training pipeline expects the following dataset structure:


<pre>
<pre>
Line 43: Line 87:
│  ├── train/
│  ├── train/
│  └── val/
│  └── val/
├── data.yaml
├── synset.txt
├── synset.txt
└── train.py
├── notes.json
└── data.yaml
</pre>
</pre>


Where:
The following steps describe how to transform the exported dataset into the expected structure.


* images/train contains the training images
=== Populate the Dataset ===
* images/val contains the validation images
* labels/train contains the training annotations
* labels/val contains the validation annotations
* data.yaml contains the dataset configuration


* Move all annotation files (<code>.txt</code>) from <code>labels/</code> to <code>labels/train/</code>.


'''IMPORTANT'''
* Copy the corresponding images into <code>images/train/</code>.


Move the <code>.txt</code> file from <code>labels</code> directory into <code>labels/train</code> directory.
* Rename <code>classes.txt</code> to <code>synset.txt</code>. Yolo explicitly requires a <code>synset.txt</code> file


And create the other required folders.
* Create the directories:


<pre>
images/train/
images/val/


labels/train/
labels/val/
</pre>


'''IMPORTANT'''
'''IMPORTANT'''


The conversion process generates only the annotation files and does not move the images.
Depending on the selected export format, Label Studio may export only the annotation (<code>.txt</code>) files. In this case, the corresponding images must be copied manually from the original image directory into <code>images/train/</code>.


Copy the corresponding images into the <code>images/train</code> directory.
'''IMPORTANT'''


Do not worry about populating the <code>images/val</code> or <code>labels/val</code> directories.
Verify that every image has a corresponding annotation file with the same filename.
 
Those will be automatically populated moving the 30% of files from <code>images/train</code> and <code>labels/train</code> via a script
 
Verify that every annotation file has a matching image.
Each image must have a corresponding annotation file with the same filename.


Example:
Example:
Line 85: Line 127:
</pre>
</pre>


If the automated training script (<code>train.sh</code>) is used, the <code>images/val/</code> and <code>labels/val/</code> directories should initially remain empty.


in addition to that specific structure for the <code>dataset/</code> directory, other important files are required for the training (<code>data.yaml</code>, <code>synset.txt</code>, <code>train.py</code>).
During execution, the script automatically moves the configured percentage of images and annotation files from the training set into the validation set.
 
<pre>
dataset/
├── images/
│  ├── train/
│  └── val/
├── labels/
│  ├── train/
│  └── val/
├── data.yaml
├── synset.txt
└── train.py
</pre>
 
 
Rename <code>classes.txt</code> to <code>synset.txt</code>, as the training pipeline expects class definitions to be provided through a file named <code>synset.txt</code>.


=== Create the data.yaml File ===
=== Create the data.yaml File ===


YOLO uses a configuration file named data.yaml to locate the dataset and identify the available classes.
YOLO uses a configuration file named `data.yaml` to locate the dataset and identify the available classes.


The file specifies:
The file specifies:
Line 117: Line 144:
Example:
Example:


<pre>
<syntaxhighlight lang="yaml">
path: dataset
path: dataset
train: images/train
train: images/train
val: images/val
val: images/val
nc: 4    # number of classes


names:
names:
  0: invoice_number
0: invoice_number
  1: date
1: date
  2: seller_name
2: seller_name
  3: total
3: total </syntaxhighlight>
</pre>
 
The class definitions must match those defined during annotation.
 
=== Create the Python Training Script ===
 
Create a Python script (<code>train.py</code>) that uses the Ultralytics YOLO framework to train the model.


The class definitions must match those used during annotation.
Training always starts from a pretrained YOLO model, such as:


* <code>yolo11n.pt</code>
* <code>yolo11s.pt</code>
* <code>yolo11m.pt</code>
* <code>yolo11l.pt</code>


=== Create the python trining script ===
The pretrained model does not need to be downloaded manually. Ultralytics automatically downloads it when the training script is executed.


In this example multiple parameters have been defined:
A minimal training script is shown below:


<pre>
<syntaxhighlight lang="python">
from ultralytics import YOLO
from ultralytics import YOLO


def main():
model = YOLO("yolo11n.pt")


    # load pretrained model
model.train(
    model = YOLO("yolo11m.pt")
data="data.yaml",
epochs=100
) </syntaxhighlight>


    print(model)
A more advanced script may explicitly configure additional training parameters such as the optimizer, learning rate, batch size, image size, and device selection.


    # train
The parameters most commonly customized are:
    model.train(
        data="data.yaml",
        epochs=150,
        batch=15,
        workers=12,
        lr0=0.002,
        momentum=0.9,
        weight_decay=0.0005,
        warmup_epochs=3,
        warmup_momentum=0.8,
        warmup_bias_lr=0.1,
        optimizer="AdamW",
        patience=30,  # number of epochs without sensible improvements stops the training
        imgsz=846,
        name="target",
        device=0,
        amp=True,
        save=True,
        save_period=10,
        cache="disk"
        # augment=False,
        # close_mosaic=20
    )


if __name__ == "__main__":
* <code>epochs</code> – number of training epochs
    main()
* <code>batch</code> – number of images processed per batch
</pre>
* <code>imgsz</code> – input image size
* <code>device</code> – CPU or GPU used for training
* <code>project</code> – output directory
* <code>name</code> – training run name
 
For a complete list of supported parameters, refer to the official Ultralytics documentation:
 
https://docs.ultralytics.com/modes/train/#train-settings


== Start the Training ==
== Start the Training ==


Create a Python training script using the Ultralytics YOLO framework.
=== Start the Training ===
 
Once the dataset has been prepared and the required configuration files have been created, the training process can be started.
 
During training, Ultralytics YOLO:


A pretrained YOLO model such as:
* Loads the dataset.
* Trains the model.
* Evaluates the model using the validation dataset.
* Saves the generated model weights and training metrics.


* yolo11n.pt
The training can be started in one of the following ways.
* yolo11s.pt
* yolo11m.pt
* yolo11l.pt


is required as the starting point for training.
==== Manual Training ====


The model does not need to be downloaded manually. Ultralytics automatically downloads it when the training script is executed.
If the validation dataset has already been prepared manually, execute the training script directly:


Execute the training script.
<pre>
<pre>
python train.py
</pre>
==== Automated Training ====
To automate the dataset preparation, model training, and ONNX conversion, add the following scripts to the training directory:
* <code>train.sh</code>
* <code>convert_pt_to_onnx.py</code>
The <code>train.sh</code> script automates the following tasks:
# Removes images that do not have a corresponding annotation file.
# Moves the configured percentage of images and annotations from the training dataset to the validation dataset.
# Executes <code>train.py</code> to train the model.
# Executes <code>convert_pt_to_onnx.py</code> to convert the generated <code>best.pt</code> model into the ONNX format required by LogicalDOC.
An example implementation of <code>convert_pt_to_onnx.py</code> is available in the [[YOLO to ONNX Conversion]] page.
An example implementation of <code>train.sh</code> is shown below:
<syntaxhighlight lang="Shell">
#!/bin/bash
IMAGES_TRAINING_DIR=dataset/images/train
IMAGES_VALIDATION_DIR=dataset/images/val
LABELS_TRAINING_DIR=dataset/labels/train
LABELS_VALIDATION_DIR=dataset/labels/val
VALIDATION_RATE=0.0
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOG=logs/training-$TIMESTAMP.log
# Remove .npy files perhaps left by last elaboration
rm -rf dataset/images/train/*.npy
rm -rf dataset/images/val/*.npy
# -----------------------------------------------------------------------------
# Clean the dataset
# -----------------------------------------------------------------------------
echo "$TIMESTAMP: Looking for unlabeled images in: $IMAGES_TRAINING_DIR" >> $LOG
for file in $IMAGES_TRAINING_DIR/*.*; do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension
  if [ ! -f "$LABELS_TRAINING_DIR/$BASE_FILENAME.txt" ]; then
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
    echo "$TIMESTAMP: Image $file non annotated, delete it" >> $LOG
    rm -rf $file
  fi
done
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Looking for unlabeled images in: $IMAGES_VALIDATION_DIR" >> $LOG
for file in $IMAGES_VALIDATION_DIR/*.*; do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension


</pre>
  if [ ! -f "$LABELS_VALIDATION_DIR/$BASE_FILENAME.txt" ]; then
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
    echo "$TIMESTAMP: Image $file non annotated, delete it" >> $LOG
    rm -rf $file
  fi
done
 
# -----------------------------------------------------------------------------
# Prepare the validation dataset
# -----------------------------------------------------------------------------
 
echo "$TIMESTAMP: Take $VALIDATION_RATE percent of training images and move them to validation" >> $LOG
total=$(ls -1 $IMAGES_TRAINING_DIR | wc -l)
take=$(echo "$total * $VALIDATION_RATE" | bc | cut -d. -f1)
for file in $(ls -1 $IMAGES_TRAINING_DIR | shuf | head -n "$take"); do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension
 
  TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
  echo "$TIMESTAMP: Relocating file: $file" >> $LOG
  mv $IMAGES_TRAINING_DIR/$file  $IMAGES_VALIDATION_DIR
  mv $LABELS_TRAINING_DIR/$BASE_FILENAME.txt $LABELS_VALIDATION_DIR
done


During training, Ultralytics YOLO:
# -----------------------------------------------------------------------------
# Train the YOLO model
# -----------------------------------------------------------------------------


* Loads the dataset
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
* Trains the model
echo "$TIMESTAMP: Launching the training" >> $LOG
* Evaluates the model on the validation set
python train.py >> $LOG  2>&1
* Saves the best-performing weights
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Training completed" >> $LOG


The number of iterations depends on:
# -----------------------------------------------------------------------------
# Convert the trained model to ONNX
# -----------------------------------------------------------------------------


* Dataset size
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
* Number of epochs
echo "$TIMESTAMP: Launching the conversion" >> $LOG
* Batch size
python convert_pt_to_onnx.py >> $LOG 2>&1
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Conversion completed" >> $LOG


The batch size can be specified through the training parameters. Using -1 allows Ultralytics to select the default value automatically.
</syntaxhighlight>


=== Training Output ===
== Training Output ==


At the end of the training process, Ultralytics creates a runs directory containing the training artifacts.
At the end of the training process, Ultralytics creates a <code>runs/</code> directory containing the training artifacts.


Typical outputs include:
Typical outputs include:
Line 222: Line 340:
* Model weights
* Model weights


The most important file is:
Among the generated files, the most important are the following model checkpoints:


<pre>
<pre>
best.pt
best.pt
last.pt
</pre>
</pre>


This file contains the trained model and should be used for subsequent validation, conversion, or deployment.
These files serve different purposes.
 
The <code>best.pt</code> file contains the model that achieved the highest validation performance during training.
 
After each epoch, Ultralytics evaluates the model on the validation dataset and computes a fitness score based on metrics such as mAP. Whenever the model achieves a better validation score than any previous epoch, the <code>best.pt</code> file is updated.
 
The <code>last.pt</code> file always contains the model from the final training epoch, regardless of its validation performance.
 
It is primarily intended for resuming training, while <code>best.pt</code> is the recommended model for validation, conversion to ONNX, and deployment in LogicalDOC.

Latest revision as of 07:43, 26 June 2026

YOLO Training Pipeline

This guide describes how to prepare a dataset and train a custom YOLO model using annotations created with Label Studio.


This guide describes an example workflow for training a custom YOLO model and preparing it for use with LogicalDOC.

Please be aware that this procedure is not coverded by the standard support contract. LogicalDOC cannot provide assistance with issues related to dataset preparation, training failures, model quality, GPU configuration, or third-party tools such as Label Studio, Ultralytics YOLO, or ONNX Runtime.

If you require professional assistance, please contact sales@logicaldoc.com to request a quotation for consulting services.



Licensing Notice: The examples in this guide use the Ultralytics implementation of YOLO. Ultralytics YOLO is distributed under its own licensing terms. Before using YOLO in a commercial environment, ensure that your intended use complies with the applicable Ultralytics license. For the latest licensing information, refer to the official Ultralytics website: https://www.ultralytics.com/license



Prerequisites

Before starting, ensure that:

  • Label Studio has been installed and configured
  • A dataset has been annotated and exported
  • Python 3.10 or later is available
  • Ultralytics YOLO is installed


Expected Training Directory Structure

Before starting the training process, the training directory should be organized as follows:

training/
├── dataset/
│   ├── images/
│   │   ├── train/
│   │   └── val/
│   └── labels/
│       ├── train/
│       └── val/
├── data.yaml
├── synset.txt
├── notes.json
├── train.py
├── train.sh
└── convert_pt_to_onnx.py

Where:

  • dataset/images/train contains the images used for training.
  • dataset/images/val contains the images used for validation.
  • dataset/labels/train contains the annotation files corresponding to the training images.
  • dataset/labels/val contains the annotation files corresponding to the validation images.
  • data.yaml defines the dataset configuration used by Ultralytics YOLO.
  • synset.txt contains the list of class names used during annotation.
  • notes.json is generated by Label Studio and contains additional project metadata.
  • train.py defines the YOLO training configuration and launches the training process.
  • train.sh (optional) automates dataset preparation, training, and model conversion.
  • convert_pt_to_onnx.py converts the trained PyTorch model (`best.pt`) into the ONNX format required by LogicalDOC.


The following sections describe how to create this directory structure starting from the dataset exported by Label Studio.

Populate the Training Directory

Extract the Dataset

After annotating the documents in Label Studio (see Label Studio Guide), export the dataset and extract the downloaded archive to a local directory.

The extracted archive contains:

  • labels/ – YOLO annotation files (`.txt`)
  • classes.txt – class definitions
  • notes.json – Label Studio metadata
  • images/ – image files (may be empty depending on the selected export format)
Content of the dataset exported by Label Studio

Prepare the Dataset Structure

The training pipeline expects the following dataset structure:

dataset/
├── images/
│   ├── train/
│   └── val/
├── labels/
│   ├── train/
│   └── val/
├── synset.txt
├── notes.json
└── data.yaml

The following steps describe how to transform the exported dataset into the expected structure.

Populate the Dataset

  • Move all annotation files (.txt) from labels/ to labels/train/.
  • Copy the corresponding images into images/train/.
  • Rename classes.txt to synset.txt. Yolo explicitly requires a synset.txt file
  • Create the directories:
images/train/
images/val/

labels/train/
labels/val/

IMPORTANT

Depending on the selected export format, Label Studio may export only the annotation (.txt) files. In this case, the corresponding images must be copied manually from the original image directory into images/train/.

IMPORTANT

Verify that every image has a corresponding annotation file with the same filename.

Example:

images/train/invoice001.jpg
labels/train/invoice001.txt

If the automated training script (train.sh) is used, the images/val/ and labels/val/ directories should initially remain empty.

During execution, the script automatically moves the configured percentage of images and annotation files from the training set into the validation set.

Create the data.yaml File

YOLO uses a configuration file named `data.yaml` to locate the dataset and identify the available classes.

The file specifies:

  • Training image directory
  • Validation image directory
  • Number of classes
  • Class names

Example:

path: dataset

train: images/train
val: images/val

nc: 4     # number of classes

names:
0: invoice_number
1: date
2: seller_name
3: total

The class definitions must match those defined during annotation.

Create the Python Training Script

Create a Python script (train.py) that uses the Ultralytics YOLO framework to train the model.

Training always starts from a pretrained YOLO model, such as:

  • yolo11n.pt
  • yolo11s.pt
  • yolo11m.pt
  • yolo11l.pt

The pretrained model does not need to be downloaded manually. Ultralytics automatically downloads it when the training script is executed.

A minimal training script is shown below:

from ultralytics import YOLO

model = YOLO("yolo11n.pt")

model.train(
data="data.yaml",
epochs=100
)

A more advanced script may explicitly configure additional training parameters such as the optimizer, learning rate, batch size, image size, and device selection.

The parameters most commonly customized are:

  • epochs – number of training epochs
  • batch – number of images processed per batch
  • imgsz – input image size
  • device – CPU or GPU used for training
  • project – output directory
  • name – training run name

For a complete list of supported parameters, refer to the official Ultralytics documentation:

https://docs.ultralytics.com/modes/train/#train-settings

Start the Training

Start the Training

Once the dataset has been prepared and the required configuration files have been created, the training process can be started.

During training, Ultralytics YOLO:

  • Loads the dataset.
  • Trains the model.
  • Evaluates the model using the validation dataset.
  • Saves the generated model weights and training metrics.

The training can be started in one of the following ways.

Manual Training

If the validation dataset has already been prepared manually, execute the training script directly:

python train.py

Automated Training

To automate the dataset preparation, model training, and ONNX conversion, add the following scripts to the training directory:

  • train.sh
  • convert_pt_to_onnx.py

The train.sh script automates the following tasks:

  1. Removes images that do not have a corresponding annotation file.
  1. Moves the configured percentage of images and annotations from the training dataset to the validation dataset.
  1. Executes train.py to train the model.
  1. Executes convert_pt_to_onnx.py to convert the generated best.pt model into the ONNX format required by LogicalDOC.

An example implementation of convert_pt_to_onnx.py is available in the YOLO to ONNX Conversion page.

An example implementation of train.sh is shown below:

#!/bin/bash

IMAGES_TRAINING_DIR=dataset/images/train
IMAGES_VALIDATION_DIR=dataset/images/val
LABELS_TRAINING_DIR=dataset/labels/train
LABELS_VALIDATION_DIR=dataset/labels/val
VALIDATION_RATE=0.0

TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOG=logs/training-$TIMESTAMP.log

# Remove .npy files perhaps left by last elaboration
rm -rf dataset/images/train/*.npy
rm -rf dataset/images/val/*.npy

# ----------------------------------------------------------------------------- 
# Clean the dataset
# ----------------------------------------------------------------------------- 

echo "$TIMESTAMP: Looking for unlabeled images in: $IMAGES_TRAINING_DIR" >> $LOG
for file in $IMAGES_TRAINING_DIR/*.*; do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension

  if [ ! -f "$LABELS_TRAINING_DIR/$BASE_FILENAME.txt" ]; then
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
    echo "$TIMESTAMP: Image $file non annotated, delete it" >> $LOG
    rm -rf $file
  fi
done

TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Looking for unlabeled images in: $IMAGES_VALIDATION_DIR" >> $LOG
for file in $IMAGES_VALIDATION_DIR/*.*; do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension

  if [ ! -f "$LABELS_VALIDATION_DIR/$BASE_FILENAME.txt" ]; then
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
    echo "$TIMESTAMP: Image $file non annotated, delete it" >> $LOG
    rm -rf $file
  fi
done

# ----------------------------------------------------------------------------- 
# Prepare the validation dataset
# ----------------------------------------------------------------------------- 

echo "$TIMESTAMP: Take $VALIDATION_RATE percent of training images and move them to validation" >> $LOG
total=$(ls -1 $IMAGES_TRAINING_DIR | wc -l)
take=$(echo "$total * $VALIDATION_RATE" | bc | cut -d. -f1)
for file in $(ls -1 $IMAGES_TRAINING_DIR | shuf | head -n "$take"); do
  BASE_FILENAME="$(basename "$file")"
  BASE_FILENAME="${BASE_FILENAME%.*}"        # strip the extension

  TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
  echo "$TIMESTAMP: Relocating file: $file" >> $LOG
  mv $IMAGES_TRAINING_DIR/$file  $IMAGES_VALIDATION_DIR
  mv $LABELS_TRAINING_DIR/$BASE_FILENAME.txt $LABELS_VALIDATION_DIR
done

# ----------------------------------------------------------------------------- 
# Train the YOLO model
# ----------------------------------------------------------------------------- 

TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Launching the training" >> $LOG
python train.py >> $LOG  2>&1
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Training completed" >> $LOG

# ----------------------------------------------------------------------------- 
# Convert the trained model to ONNX
# ----------------------------------------------------------------------------- 

TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Launching the conversion" >> $LOG
python convert_pt_to_onnx.py >> $LOG 2>&1
TIMESTAMP=$(date +"%Y%m%d_%H%M%S.%3N")
echo "$TIMESTAMP: Conversion completed" >> $LOG

Training Output

At the end of the training process, Ultralytics creates a runs/ directory containing the training artifacts.

Typical outputs include:

  • Training logs
  • Validation metrics
  • Loss curves
  • Confusion matrix
  • Model weights

Among the generated files, the most important are the following model checkpoints:

best.pt
last.pt

These files serve different purposes.

The best.pt file contains the model that achieved the highest validation performance during training.

After each epoch, Ultralytics evaluates the model on the validation dataset and computes a fitness score based on metrics such as mAP. Whenever the model achieves a better validation score than any previous epoch, the best.pt file is updated.

The last.pt file always contains the model from the final training epoch, regardless of its validation performance.

It is primarily intended for resuming training, while best.pt is the recommended model for validation, conversion to ONNX, and deployment in LogicalDOC.