Get Started: Train a YOLO Detectorο
In this walkthrough, we show how to train, evaluate, and benchmark supervised learning models on EnvoDat. Further, we show how to perform inference on new data and provide a walk-through on key steps in model selection and fine-tuning.
Prerequisitesο
Python 3 is installed on your system
Basic knowledge of Python and command-line operations
GPU for faster training (optional but recommended)
Environment Setupο
Clone the Envodat repository:
git clone https://github.com/LinusNEP/EnvoDat.git
cd EnvoDat
Create a virtual environment with Python 3.8+ and install the necessary Python requirements. If Python 3 is not installed, download and install it from Pythonβs official site.
sudo apt-get install python3-venv
python3 -m venv EnvoDatEnv
source EnvoDatEnv/bin/activate
After the above step, you should see (EnvoDatEnv) at the start of your command prompt, indicating that the virtual environment is active. Once activated, install the necessary packages, e.g., ultralytics for YOLO and other relevant Python libraries:
pip install torch ultralytics
pip install matplotlib opencv-python
pip install pandas matplotlib
Prepare the EnvoData Datasetο
Option A (recommended): fetch a training-ready scene from Hugging Faceο
The annotated scenes are published on the Hugging Face Hub already in YOLOβs directory structure, together with a ready-made config. Hence, a single command replaces steps 1 and 2 of Option B:
pip install envodat
envodat --root ./data download mu-hall
The scene then sits in ./data/yolo/mu-hall/, and its config is ./data/yolo/mu-hall/data.yaml. Use that path wherever this guide writes /.../dataset/envodata-mu-hall.yaml, and continue with Training the YOLO model on EnvoDat. Alternatively, envodat train mu-hall downloads and trains in one go. For other model families, see the Examples.
Option B: download manually and reorganiseο
Download the annotated data at EnvoDat annotations. Depending on your task, download only the format that satisfies your requirements, e.g., YOLOv*, COCO, OpenAI-CLIP, VOC, etc. We organised the EnvoDat dataset in the hierarchical structure shown below:
EnvoDat/
βββ Indoors/
β βββ mu-hall/
β β βββ annotations/
β β βββ ...
β β
β βββ ...
βββ Outdoors/
β βββ ...
βββ SubTunnels/
β βββ ...
βββ README.md
For this demo, we show how to train YOLOv8 model on the MU-Hall data. Create a folder named dataset, and reorganise the downloaded annotation data in YOLOβs required directory structure as shown in the example below:
dataset/
βββ test/
β βββ images/
β βββ labels/
βββ train/
β βββ images/
β βββ labels/
βββ valid/
β βββ images/
β βββ labels/
βββ envodata-mu-hall.yaml
In each of the scenes, provide a YOLOv* configuration file named envodata-*.yaml which specifies the dataset paths and class names. If it does not exist, create one in the following form:
train: ../train/images
val: ../valid/images
test: ../test/images
nc: <number of classes>
names: ["class_name1", "class_name2", ..., "class_nameN"]
Training the YOLO model on EnvoDatο
Download the pre-trained model checkpoints (e.g.,
yolov8s.ptfor YOLOv8 small oryolo11n.ptfor YOLO11 nano):
Example models |
Pretrained checkpoints download |
|---|---|
YOLOv8 |
|
YOLOv9 |
|
YOLOv10 |
|
YOLO11 |
Train the model with the following command:
# For YOLOv8
yolo train model=yolov8n.pt data=/.../dataset/envodata-mu-hall.yaml epochs=100 imgsz=640
# For YOLO11
yolo train model=yolo11x.pt data=/.../dataset/envodata-mu-hall.yaml epochs=100 imgsz=640
Adjust the model and hyperparameters as needed (e.g., model, epochs, batch size) for optimal results:
yolo train model=yolo11x.pt \
data=/.../dataset/envodata-mu-hall.yaml \
epochs=150 imgsz=640 batch=16 \
lr0=0.01 lrf=0.1 momentum=0.937 weight_decay=0.0005 \
save_period=10 patience=50 device=0
Argument |
Meaning |
|---|---|
|
Number of training epochs |
|
Batch size |
|
Initial learning rate, and final learning rate as a fraction of |
|
Momentum for the SGD optimiser |
|
Regularisation strength |
|
Save a checkpoint every n epochs |
|
Stop after this many epochs without improvement |
|
|
Modelsβ Performance Evaluation
After training, evaluate the performance on the validation data:
from ultralytics import YOLO
model = YOLO('runs/detect/train/weights/best.pt') # the weights from your training run
validation_metrics = model.val(split='val') # This automatically uses the validation split defined in envodata-*.yaml
You could also run the evaluation on the test set to see how the model performs on unseen data:
test_metrics = model.val(split='test') # This uses the test split from the envodata-*.yaml
Once you are done training, you can save the trained model weights for inference or for further fine-tuning:
model.export(format='onnx') # Export to ONNX format (alternatively use 'torchscript', 'engine' etc.)
Run inference on multiple unseen imagesο
Prepare new images: Ensure all the new images you want to run inference on are in a single directory.
You can use the following command to run inference on all the new images in the directory.
yolo predict model=best.pt source=path/to/unseen_images
Alternatively, you can simply run the following Python script:
python scripts/unseenImgInference.py \
--model runs/detect/train/weights/best.pt \
--source path/to/unseen_images \
--project runs/detect \
--name exp \
--conf 0.4
Metrics and Analysisο
After training the model, the training results results.csv will be generated. You can analyse the validation losses, validation metrics (Precision, Recall, mAP), and learning rate over time using the following python script:
python scripts/metrics_eval.py