Object Detection with YOLO26

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

Installation and Environment Setup

Official installation guide: Installing conda - conda documentation

Download link: Download anaconda

You can download Miniconda and install environments as needed.

Create a runtime 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, you need to download it from the official site. Visit https://pytorch.org/get-started/locally/ to find the installation command.

Check your CUDA version with:

1
nvidia-smi

Verify the installation once it’s finished:

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

Annotating Images

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

1
pip install labelImg

Then open the GUI using the command:

1
labelImg

Make sure to select the correct image path and save directory, and switch the current 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 definition file data.yaml:

1
2
3
4
5
6
7
path: ../my_dataset  # Relative path to dataset root
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, the outputs will be in runs/detect.

The weights/ folder contains the model files:

  • best.pt: The weights with the highest accuracy.
  • last.pt: The weights from the final training 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 misidentifying objects as background or other classes.
    • The brighter the diagonal, the more accurate the recognition.
  • Box*_curve.png: Represents Precision, Recall, and their balance curve.
  • labels.jpg: Statistics for all annotation boxes, showing size distribution and class ratios.

Image Visualizations:

  • train_batch*.jpg: Specific images used by the model during training.
  • val_batch*_labels.jpg: Annotated images used for validation.
  • val_batch*_pred.jpg: Prediction results from the model.

Other files:

  • args.yaml: All parameter settings used for this run, useful for reproduction.
  • results.csv: Raw data table for results.png.

Using the Model

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

# Run prediction
results = model.predict(source='new_chicken_video.mp4', save=True)
This post is licensed under CC BY-NC-SA 4.0 by the author.