Skip to content

YOLO26

A wrapper implementation for YOLOv26 (Ultralytics YOLO v8.4.0+) object detection within the ECOS AI framework. This module provides an easy-to-use interface for training, inference, and visualization of object detection tasks, segmentation,...

Overview

The YOLO26 module extends the BaseModel class and integrates the latest Ultralytics YOLO implementation for object detection tasks. It supports automatic model downloading, training, prediction, and result visualization with customizable parameters.

Installation

Please install ecos_core

Requirements

pip install -r requirements.txt

Usage

Configuration

Configuration is managed through a JSON file (opt.json). Here's the example structure:

{
    "data": "/path/to/data.yaml",
    "weight": "yolo26s.pt",
    "weights_url": "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo26s.pt",
    "batch_size": 8,
    "epochs": 1500,
    "device": 0,
    "imgsz": 640,
    "task": "detect",
    "type": "pytorch",
    "conf": 0.25,
    "iou": 0.7,
    "font_scale": 2,
    "text_thickness": 2,
    "padding_rect": 5,
    "visualization": true
}

With: - data: Path to data.yaml file containing dataset configuration - weight: Model weight filename - weights_url: URL to download pre-trained weights - batch_size: Number of samples per training batch - epochs: Number of training epochs - device: Device to use (0,1,2,3 for GPU or "cpu") - imgsz: Target image size for training. - task: Task type. Default: "detect" - type: Framework type - conf: Confidence threshold for predictions - iou: IoU threshold for NMS - font_scale: Font scale for visualization text - text_thichness: Text thickness for visualization - padding_rect: Padding for text background rectangles - visualization: Enable/disable result visualization

Training

Basic Training Example:

import sys
import os
import json
import argparse
sys.path.insert(0, os.path.abspath(
    os.path.join(os.path.dirname(__file__), './../../')
))
from ecos_core.yolo26.yolo26 import Model
from easydict import EasyDict

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--opt',
                        type=str, default="./opt.json", help='opt path')
    parser.add_argument('--weight-path',
                        type=str, default=None, help='weight path')
    args = parser.parse_args()
    opt = args.opt
    weight_path = args.weight_path
    with open(opt, "r") as f:
        opts = EasyDict(json.load(f))
    model = Model(opts)
    print(weight_path)
    model.build_model(weight_path)
    model.fit()

Training script:

python train.py --opt ./opt.json --weight-path ./yolo26s.pt

Inference

Basic inference example:

import sys
import os
import json
import argparse
sys.path.insert(0, os.path.abspath(
    os.path.join(os.path.dirname(__file__), './../../')
))
from ecos_core.yolo26.yolo26 import Model
from easydict import EasyDict

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--opt',
                        type=str, default="./opt.json", help='opt path')
    parser.add_argument('--weight-path',
                        type=str, default=None, help='weight path')
    parser.add_argument('--input-path', required=True,
                        type=str, default="./broken/images/test", help='input path')
    args = parser.parse_args()
    opt = args.opt
    weight_path = args.weight_path
    with open(opt, "r") as f:
        opts = EasyDict(json.load(f))
    model = Model(opts)
    print(weight_path)
    model.build_model(weight_path)
    save_path = model.process_predictions(
        net_output=results,
        raw_image_path="path/to/image.jpg",
        raw_image_mat=None,
        image_transform=None,
        save_image_path="output/annotated_image.jpg"
    )

APIs

Model

Bases: BaseModel

build_model(custom_weight=None)

Build model in YOLO26 model

Parameters:

Name Type Description Default
custom_weight str

custom weight path. Defaults to None.

None

Returns:

Name Type Description
model Model

YOLO model

fit()

Train the model

Returns:

Name Type Description
results

training results

get_transform()

Get image data transform function for preprocessing

Returns:

Name Type Description
transform func

transform function. This function will take input as image path and output - raw_image: image numpy mat - image_transform: image tensor (pytorch) Example: torchvision.transforms.transforms: data transforms function

predict(source=None, classes=None)

Run inference on images or folders.

Parameters:

Name Type Description Default
source str

image path or folder path. Defaults to None.

None
classes list

list of class ids to filter. Defaults to None.

None

Returns:

Name Type Description
results

prediction results

process_predictions(net_output, raw_image_path, raw_image_mat, image_transform, save_image_path)

Process predictions and generate annotated images.

Parameters:

Name Type Description Default
net_output

output of detection net

required
raw_image_path str

raw image path

required
raw_image_mat Array

raw image numpy mat type

required
image_transform func

image_transform function

required
save_image_path str

save image path

required

Returns:

Name Type Description
save_image_path

inform that the annotation image has been written successfully.

reload_param(opt_path='./opt.json')

Reload parameters

Parameters:

Name Type Description Default
opt_path str

Opt path. Defaults to "./opt.json".

'./opt.json'

update_opt(option, output_path='./opt.json')

Update config

Parameters:

Name Type Description Default
option Namespace / Dict

option to update

required
output_path str

path to save opt json. Defaults to "./opt.json".

'./opt.json'