Русский | English
Этот модуль содержит переписанный с нуля код для обучения моделей архитектуры vzglyad-detect, не использует код Ultralytics под лицензией GPL-3 / AGPL-3.
Весь код написан с использованием стандартных библиотек (torch, opencv-python, numpy) и может свободно использоваться в коммерческих проектах без необходимости открывать исходный код вашего приложения.
Для запуска поместите всё по данной структуре (или скачайте из релизов):
Vzglyad/
├── inference (скачайте https://github.com/AlexeyRF/VZGLYAD-Detect)/
│ ├── example.py
│ ├── example_on_screen.py
│ ├── README.md
│ ├── LICENSE
│ └── vzglyad_vision/
│ ├── __init__.py
│ ├── blocks.py
│ ├── inference.py
│ ├── model.py
│ └── utils.py
└── train/ (эти коды)
├── dataset.py
├── loss.py
├── metrics.py
├── README.md
├── train.py
└── universal_model.yaml
- Своя архитектура (
universal_model.yaml) - Встроенные аугментации
- Target Assignment без GPL: Написана кастомная функция потерь (
loss.py), которая использует пространственное сопоставление (Spatial Target Assignment), GIoU и BCE Loss.
dataset.py- загрузчик данных. Реализует аугментации иletterboxпаддинг.loss.py- реализация функции потерь.train.py- основной скрипт цикла обучения. Загружает архитектуру изyamlсловаря или из.pthмодели.universal_model.yaml- конфиг архитектуры для старта обучения с нуля.
Данные должны быть в стандартном формате:
dataset/
├── images/
│ ├── image1.jpg
│ └── image2.jpg
└── labels/
├── image1.txt
└── image2.txt
Note: Если у вас нет готовой структуры с train val, то скрипт сам разделит их 80/20. соотвтетственно
Вариант А: Обучение с нуля из словаря YAML (универсальная модель)
python train.py --data-dir path/to/dataset --cfg universal_model.yaml --scale n --epochs 100 --batch-size 16Вариант Б: Дообучение базовой модели (.pth)
python train.py --data-dir path/to/dataset --model-path ../inference/your_model.pth --epochs 100Доступные аргументы:
--data-dir(обязательный): путь к папке с датасетом.--cfg: путь к YAML конфигу для старта с нуля.--model-path: путь к базовой модели.pthдля дообучения.--scale: масштаб модели (например,n,s,m,l,x), если используется--cfg.--no-augment: отключить встроенные аугментации.--epochs: количество эпох (по умолчанию 100).--batch-size: размер батча (по умолчанию 16).--img-size: размер входного изображения (по умолчанию 640).--lr: learning rate (по умолчанию 0.001).
Обученная модель сохраняется в папку runs/train/.
This module contains a written-from-scratch training code for the vzglyad-detect architecture, not using Ultralytics code under the GPL-3 / AGPL-3 license.
All code is written using standard libraries (torch, opencv-python, numpy) and can be freely used in commercial projects without the requirement to open-source your application.
To run, place everything in this structure (or download from the releases):
Vzglyad/
├── inference (download https://github.com/AlexeyRF/VZGLYAD-Detect)/
│ ├── example.py
│ ├── example_on_screen.py
│ ├── README.md
│ ├── LICENSE
│ └── vzglyad_vision/
│ ├── __init__.py
│ ├── blocks.py
│ ├── inference.py
│ ├── model.py
│ └── utils.py
└── train/ (this repo)
├── dataset.py
├── loss.py
├── metrics.py
├── README.md
├── train.py
└── universal_model.yaml
- Custom Architecture Layout (
universal_model.yaml) - Built-in Augmentations & Auto-split: Automatically applies advanced data augmentations on the fly. Automatically splits the dataset 80/20 into train/val if explicit split folders are missing.
- GPL-free Target Assignment: A custom loss function (
loss.py) is implemented using Spatial Target Assignment, GIoU, and BCE Loss. - Metrics & Plots: Automatically generates validation metrics (Precision, Recall, mAP@0.5) and dataset/training plots.
dataset.py- Dataloader and Dataset implementation. Handles on-the-fly augmentations, auto-split, andletterboxpadding.loss.py- Loss function implementation.train.py- Main training loop script. Dynamically loads architecture from ayamldictionary or a base.pthmodel.universal_model.yaml- A configuration template for training custom detection models from scratch.
Data must be in the standard format:
dataset/
├── images/
│ ├── image1.jpg
│ └── image2.jpg
└── labels/
├── image1.txt
└── image2.txt
Note: If you don't have separate images/train and images/val folders, the script will automatically split the dataset 80/20.
Option A: Train from scratch using a YAML dictionary (universal model)
python train.py --data-dir path/to/dataset --cfg universal_model.yaml --scale n --epochs 100 --batch-size 16Option B: Fine-tune from a base .pth model
python train.py --data-dir path/to/dataset --model-path ../inference/your_model.pth --epochs 100Available Arguments:
--data-dir(required): Path to the dataset directory.--cfg: Path to a YAML configuration file to train from scratch.--model-path: Path to a base.pthmodel to fine-tune.--scale: Model scale parameter (e.g.,n,s,m,l,x) when using--cfg.--no-augment: Disable built-in data augmentations.--epochs: Number of epochs (default: 100).--batch-size: Batch size (default: 16).--img-size: Input image size (default: 640).--lr: Learning rate (default: 0.001).
The trained model, plots, and dataset statistics are saved in the runs/train/ directory.