YOLO Plugin¶
yolo ¶
Classes¶
YOLOLoader ¶
Bases: LoaderPlugin
YOLOv5/YOLOv8 format dataset loader.
This loader handles datasets in YOLO format, which consists of: - A YAML configuration file (data.yaml) defining classes and paths - Images organized in directories (typically images/train, images/val, images/test) - Label files in TXT format with normalized coordinates (labels/train, etc.)
The loader supports both YOLOv5 and YOLOv8 format specifications, automatically handling different category naming conventions (dict or list format in YAML).
Label Format
Each line in a label file represents one object:
Attributes¶
description property ¶
Get the loader description.
Returns:
| Type | Description |
|---|---|
str | Description string for YOLOv5/YOLOv8 format. |
supported_extensions property ¶
Get supported file extensions.
Returns:
| Type | Description |
|---|---|
list[str] | List containing [".yaml", ".yml"]. |
Functions¶
load ¶
load(path: str | PathLike[str], name: str | None = None, splits: str | list[str] | None = None, **_kwargs: Any) -> Dataset
Load YOLO format dataset.
Loads a YOLO dataset from the specified directory. The directory should contain a YAML configuration file and subdirectories for images and labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | PathLike[str] | Path to YOLO dataset YAML configuration file. | required |
name | str | None | Optional custom name for the dataset. If None, uses directory name. | None |
splits | str | list[str] | None | Which split(s) to load. Can be: - None: Load all splits (train, val, test) - str: Load single split (e.g., "train") - list[str]: Load specific splits (e.g., ["train", "val"]) | None |
**_kwargs | Any | Additional parameters (currently unused, reserved for future extensions). | {} |
Returns:
| Type | Description |
|---|---|
Dataset | Loaded Dataset instance containing all images, annotations, and |
Dataset | categories. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError | If the YAML configuration file is not found. |
ValueError | If YAML configuration is missing required 'names' field. |
Source code in boxlab/dataset/plugins/yolo.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
YOLOExporter ¶
Bases: ExporterPlugin
YOLOv5/YOLOv8 format dataset exporter.
This exporter converts datasets to YOLO format, creating: - A data.yaml configuration file with class definitions and paths - Image files organized in split subdirectories (images/train, etc.) - Label files in TXT format with normalized coordinates (labels/train, etc.)
The exporter supports: - Train/val/test splits or single dataset export - Custom naming strategies for files - Optional image copying (can export annotations only) - Unified or standard directory structure
Output Structure (standard): output_dir/ ├── data.yaml ├── images/ │ ├── train/ │ ├── val/ │ └── test/ └── labels/ ├── train/ ├── val/ └── test/
Output Structure (unified): output_dir/ ├── data.yaml ├── images/ │ ├── train/ │ ├── val/ │ └── test/ └── annotations/ ├── train/ ├── val/ └── test/
Attributes¶
description property ¶
Get the exporter description.
Returns:
| Type | Description |
|---|---|
str | Description string for YOLOv5/YOLOv8 format. |
default_extension property ¶
Get default file extension for label files.
Returns:
| Type | Description |
|---|---|
str | The string ".txt". |
Functions¶
export ¶
export(dataset: Dataset, output_dir: str | PathLike[str], split_ratio: SplitRatio | None = None, seed: int | None = None, naming_strategy: NamingStrategy | None = None, copy_images: bool = True, unified_structure: bool = False, **_kwargs: Any) -> None
Export dataset to YOLO format.
Creates a YOLO-compatible dataset with proper directory structure, label files, and YAML configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Dataset instance to export. | required |
output_dir | str | PathLike[str] | Output directory path. Will be created if it doesn't exist. | required |
split_ratio | SplitRatio | None | Optional SplitRatio for train/val/test division. If None, exports entire dataset as 'train' split. | None |
seed | int | None | Random seed for reproducible splits. Only used if split_ratio is provided. | None |
naming_strategy | NamingStrategy | None | Strategy for generating output file names. If None, uses OriginalNaming (preserves original filenames). | None |
copy_images | bool | If True, copies image files to output directory. If False, only creates label files. | True |
unified_structure | bool | If True, uses 'annotations' directory instead of labels'. Useful for compatibility with some training frameworks. | False |
**_kwargs | Any | Additional parameters (currently unused, reserved for future extensions). | {} |
Note
Category IDs in label files are 0-indexed (YOLO convention), even though the Dataset uses 1-indexed IDs internally.
Source code in boxlab/dataset/plugins/yolo.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
Functions¶
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true
Overview¶
The YOLO plugin provides support for loading and exporting datasets in YOLOv5/YOLOv8 format. It handles YAML configuration files, normalized bounding box coordinates, and the standard YOLO directory structure.
Format Specification¶
Directory Structure¶
dataset/
├── data.yaml # Configuration file
├── images/
│ ├── train/ # Training images
│ ├── val/ # Validation images
│ └── test/ # Test images
└── labels/
├── train/ # Training labels
├── val/ # Validation labels
└── test/ # Test labels
YAML Configuration¶
# data.yaml
path: /path/to/dataset
train: images/train
val: images/val
test: images/test
nc: 3 # Number of classes
names:
0: person
1: car
2: bicycle
Label Format¶
Each label file (.txt) contains one line per object:
All coordinates are normalized to [0, 1] range:
x_center: Center X coordinate / image widthy_center: Center Y coordinate / image heightwidth: Bounding box width / image widthheight: Bounding box height / image height
YOLOLoader¶
Load datasets from YOLO format.
Basic Usage¶
from boxlab.dataset.plugins.registry import get_loader
loader = get_loader("yolo")
dataset = loader.load("path/to/yolo_dataset")
Load Specific Splits¶
# Load only training data
dataset = loader.load("path/to/yolo_dataset", splits="train")
# Load multiple splits
dataset = loader.load("path/to/yolo_dataset", splits=["train", "val"])
# Load all splits (default)
dataset = loader.load("path/to/yolo_dataset", splits=None)
Custom YAML File¶
Features¶
- Supports both YOLOv5 and YOLOv8 formats
- Handles dict or list category definitions in YAML
- Converts normalized coordinates to absolute pixels
- Validates label file format
- Logs warnings for invalid annotations
- Supports multiple image formats (jpg, png, bmp, tiff, webp)
YOLOExporter¶
Export datasets to YOLO format.
Basic Usage¶
from boxlab.dataset.plugins.registry import get_exporter
exporter = get_exporter("yolo")
exporter.export(dataset, output_dir="output/yolo_format")
Export with Splits¶
from boxlab.dataset.types import SplitRatio
# Define split ratios
split_ratio = SplitRatio(train=0.7, val=0.2, test=0.1)
exporter.export(
dataset,
output_dir="output/yolo_format",
split_ratio=split_ratio,
seed=42 # For reproducibility
)
Export Options¶
from boxlab.dataset.plugins.naming import SequentialNaming
# Custom naming strategy
strategy = SequentialNaming(prefix="img", start=1, digits=6)
# Export with options
exporter.export(
dataset,
output_dir="output/yolo_format",
split_ratio=split_ratio,
seed=42,
naming_strategy=strategy,
copy_images=True, # Copy image files
unified_structure=False # Use standard structure
)
Unified Structure¶
Use unified directory structure (annotations instead of labels):
exporter.export(
dataset,
output_dir="output/yolo_format",
unified_structure=True # Uses 'annotations' directory
)
Output structure:
output/
├── data.yaml
├── images/
│ ├── train/
│ ├── val/
│ └── test/
└── annotations/ # Instead of 'labels'
├── train/
├── val/
└── test/
Features¶
- Generates compliant YAML configuration
- Converts absolute coordinates to normalized format
- Handles filename conflicts automatically
- Supports custom naming strategies
- Optional image copying
- 0-indexed class IDs in output (YOLO convention)
- Preserves annotation precision with 6 decimal places
Coordinate Conversion¶
Loading (Normalized → Absolute)¶
# YOLO label: 0 0.5 0.5 0.3 0.2
# Image size: 640x480
cx_norm, cy_norm = 0.5, 0.5
w_norm, h_norm = 0.3, 0.2
cx = cx_norm * 640 # 320.0
cy = cy_norm * 480 # 240.0
w = w_norm * 640 # 192.0
h = h_norm * 480 # 96.0
Exporting (Absolute → Normalized)¶
# BBox: x_min=224, y_min=144, x_max=416, y_max=336
# Image size: 640x480
cx = (x_min + x_max) / 2 # 320.0
cy = (y_min + y_max) / 2 # 240.0
w = x_max - x_min # 192.0
h = y_max - y_min # 192.0
cx_norm = cx / 640 # 0.5
cy_norm = cy / 480 # 0.5
w_norm = w / 640 # 0.3
h_norm = h / 480 # 0.4
Category ID Handling¶
YOLO uses 0-indexed category IDs, while BoxLab's Dataset uses 1-indexed IDs internally.
During Loading¶
During Export¶
Error Handling¶
The YOLO plugin handles various error conditions:
- Missing YAML: Raises
FileNotFoundError - Invalid YAML: Raises
ValueErrorif 'names' field is missing - Missing directories: Logs warning and skips
- Invalid label format: Logs warning and skips line
- Unknown category: Logs warning and skips annotation
- Image read errors: Logs error and continues
See Also¶
- Plugin System: Plugin architecture overview
- Registry: Plugin registration and discovery
- COCO Plugin: COCO format plugin
- Dataset: Core dataset management