Object Detection with YOLO26

📢 This article was translated by gemini-3-flash-preview

Installation and Environment Creation

Official installation reference: Installing conda - conda documentation

Download link: Download anaconda

You can download Miniconda and set up environments manually as needed.

Create a running environment:

1
conda create -n env_name python=3.13 -y

Activate the environment:

1
conda activate env_name

Installing PyTorch

For the CPU version, use this command directly:

1
pip install torch torchvision torchaudio

For the GPU version, visit the official site at https://pytorch.org/get-started/locally/ to get the installation command.

Check your CUDA version using:

1
nvidia-smi

After installation, verify if it’s working:

1
2
3
import torch
print(f"GPU available: {torch.cuda.is_available()}")
print(f"Current GPU: {torch.cuda.get_device_name(0)}")

Labeling Images

For small local datasets, you can use labelImg. Install it via pip:

1
pip install labelImg

Then open the GUI using:

1
labelImg

Remember to select your image path and label save directory, and switch the data format to YOLO.

Press W (uppercase) to create a new annotation.

Training the Model

YOLO26 Official Documentation: Ultralytics YOLO26

Install YOLO:

1
pip install ultralytics

Training code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from ultralytics import YOLO

def main():
    # Load a COCO-pretrained YOLO26n model
    model = YOLO("yolo26n.pt")

    # Train the model on the data example dataset for 50 epochs
    results = model.train(data="data.yaml", epochs=50, imgsz=640, device=0)

if __name__ == '__main__':
    main()

Data configuration file data.yaml:

1
2
3
4
5
6
7
path: ../my_dataset  # Relative path to the dataset root directory
train: images/train  # Training set location
val: images/val      # Validation set location

names:
  0: name1            # Class 0: name1
  1: name2            # Class 1: name2

Training Outputs

Once training is complete, outputs are generated in runs/detect.

Inside the weights/ folder are the model files:

  • best.pt: Weights with the highest accuracy.
  • last.pt: Weights from the final epoch.

Charts:

  • results.png: Combines training loss and accuracy metrics (mAP).
    • If the curve is still rising, the model is still learning; if it plateaus, the model has converged.
  • confusion_matrix.png: Shows the probability of the model mistaking objects for background or other classes.
    • The brighter the diagonal, the more accurate the recognition.
  • Box*_curve.png: Represents Precision, Recall, and their balance curves.
  • labels.jpg: Statistics for all annotations, showing box size distribution and class ratios.

Image Annotations:

  • train_batch*.jpg: Specific images used by the model during training.
  • val_batch*_labels.jpg: Labeled images used for validation.
  • val_batch*_pred.jpg: Model prediction results.

Other Files:

  • args.yaml: All parameter settings for this training run, useful for reproduction.
  • results.csv: Raw data for the results.png chart.

Using the Model

1
2
3
4
# Load the model
model = YOLO('runs/detect/train/weights/best.pt')

results = model.predict(source='new_chicken_video.mp4', save=True)

Modifying the Model

In your conda environment, run the following command to find the YOLO installation location:

1
python -c "import ultralytics.nn.tasks as t; print(t.__file__)"

However, this method will fail if you update the library. You can download the official source code locally instead.

Download address: ultralytics/ultralytics

Then, in the project root directory, use pip to load the library in editable mode:

1
pip install -e .

This way, your changes will be synchronized.

This post is licensed under CC BY-NC-SA 4.0 by the author.