- Version: 20261019
- Source Code Copyright: GNU General Public License v3.0 (GPL-3.0) — WongKinYiu/yolor
- Redistribution Restriction: This source code is integrated by LEADERG INC. Please use it privately. Please DO NOT distribute it to internet.
Console/terminal pipeline for training and running a YOLOR object detector, supporting Windows 11, Ubuntu Linux 24.04 and macOS 14+ (Apple Silicon). The vendored YOLOR engine (WongKinYiu, GPL-3.0, including the LEADERG --show-rate extension) lives under src/yolor/ and is imported directly from source. Two example datasets ship with trained checkpoints: tumor (brain-tumor detection, 1 class) and plate (steel-plate surface defects, 4 classes), both annotated with Pascal VOC XML. Training and inference run on the GPU (CUDA on Windows/Linux, MPS on Apple Silicon) and fall back to the CPU automatically.
[Folder Structure]
- CLAUDE.md / AGENTS.md: Program specification (identical content).
- CHANGELOG.md: Change log.
- copyright.txt: Copyright notice.
- Image-Object-Detection-YOLOR-GPL-Python.html: This document (usage page).
- 2105.04206.pdf: YOLOR reference paper.
- data/{dataset}/: label.names, yolor_csp_x.cfg, hyp.scratch.1280.yaml, voc.yaml + train.txt/val.txt (generated by step 05), train/ and val/ with images/ + annotations/ (Pascal VOC XML) + labels/ (YOLO txt), and test/images/.
- model/{dataset}/: yolor_csp_x.pt (pretrained), best.pt (trained), training checkpoints/plots after retraining, predict/ detection outputs.
- tool/tools.py: Shared helpers: delete_ipynb_checkpoints, project_root, select_device / resolve_device (CUDA → MPS → CPU).
- src/yolor/: Vendored YOLOR engine (train.py, detect.py, test.py, models/, utils/).
- src/annotation/: Bundled aiohttp annotation web server (cross-platform).
- src/: Wrapper entry points, launcher scripts and requirements.txt.
- venv/: Python virtual environment, created by 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh.
[Prerequisites]
- Python 3.14, installed by src\01_python_install_windows.bat (winget), src/01_python_install_linux.sh (apt, deadsnakes PPA when needed; uses sudo) or src/01_python_install_mac.sh (Homebrew).
- The venv scripts install PyTorch 2.13.0 + torchvision 0.28.0 first, then src/requirements.txt (the engine dependencies such as opencv-python, numpy, scipy, matplotlib, seaborn, pandas, PyYAML, tqdm, thop, pycocotools, plus aiohttp / aiohttp-jinja2 / jinja2 for the annotation server).
- Windows / Linux: the CUDA 13.0 (cu130) build. GPU use needs an NVIDIA Turing or newer GPU (GTX 16 / RTX 20 series and later) with a driver supporting CUDA 13.0 or newer (check nvidia-smi); without one the same build runs on the CPU.
- macOS: the PyPI build, using the Apple GPU through MPS. Apple Silicon (arm64) with macOS 14+ only; Intel Macs are not supported by PyTorch 2.13.0.
- Annotation (step 04) uses the bundled web server, no external binary. Webcam inference (step 09) requires a display and an attached camera.
[Launcher Scripts]
Every step exists as a Windows batch file (.bat) and a shell script (.sh, shared by Linux and macOS) with identical behavior. All launchers switch to the project root first, so they can be run from any directory.
1. 01_python_install_windows.bat / 01_python_install_linux.sh / 01_python_install_mac.sh
Install Python 3.14 (winget / apt + deadsnakes / Homebrew). Skips when it is already installed.
2. 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh
Delete the old venv/, create a fresh one with Python 3.14, install PyTorch 2.13.0 + torchvision 0.28.0 (CUDA 13.0 build on Windows/Linux, PyPI/MPS build on macOS), then src/requirements.txt. Aborts loudly if pip fails; prints whether CUDA (or MPS) is available.
3. 03_venv_activate.bat / 03_venv_activate.sh
Activate the venv. On Linux/macOS, source it: source src/03_venv_activate.sh. If the project folder was renamed after the venv was created, re-create the venv with 02_venv_create_windows/linux/mac first; the other launchers do not need activation.
4. 04_annotation.bat / 04_annotation.sh
Launch the Pascal VOC XML annotation web server (default dataset tumor, split train, port 8801), then open http://localhost:8801.
5. 05_convert_yolo_format.bat / 05_convert_yolo_format.sh
Pascal VOC XML → YOLO labels + train.txt/val.txt + voc.yaml.
6. 06_train.bat / 06_train.sh
Train the detector. Auto-runs the converter if voc.yaml is missing. Extra args are forwarded to train.py.
7. 07_inference.bat / 07_inference.sh
Run inference on a single image. Extra args are forwarded to inference.py.
8. 08_inference_folder.bat / 08_inference_folder.sh
Run inference on a folder (default data/{dataset}/test/images) with --show-rate statistics.
9. 09_inference_webcam.bat / 09_inference_webcam.sh
Run inference on webcam 0; requires a display and camera.
10. 99_venv_clean.bat / 99_venv_clean.sh
Delete the venv/ folder.
11. 99_venv_deactivate.bat / 99_venv_deactivate.sh
Deactivate the venv in the current shell. On Linux/macOS, source it: source src/99_venv_deactivate.sh (bash or zsh).
Extra arguments are forwarded to the Python entry point, e.g. src\07_inference.bat --device cpu.
[Python Entry Points]
src/annotation.py
--dataset tumor --port 8801 --split train. Starts the bundled annotation server (Pascal VOC XML) on the chosen dataset split.
src/convert_yolo_format.py
--dataset tumor. Writes labels/{image}.txt (class_id x_center y_center width height, normalized 0..1, difficult objects skipped), train.txt/val.txt image lists, and voc.yaml. Validates classes against image_annotation_classes.txt when present.
src/train.py
--dataset tumor --batch-size 8 --image-size 512 --epochs 1000 --workers 1 --device auto --name yolor_csp_x; --weights defaults to empty = train from scratch (pass model/{dataset}/yolor_csp_x.pt for transfer learning). Training artifacts (last.pt, best.pt and variants, checkpoints, plots) land directly in model/{dataset}/ and overwrite the existing best.pt. Progress is printed to the console only.
src/inference.py
--dataset tumor --source data/{dataset}/test/images --image-size 512 --device auto --threshold 0.6 --weights model/{dataset}/best.pt [--show] [--show-rate]. A numeric --source selects a webcam (auto-enables --show). Retraining overwrites model/{dataset}/best.pt in place, so inference automatically uses the newest model. Annotated results are saved to model/{dataset}/predict/.
--device: auto (default) picks CUDA → MPS → CPU; cuda, cuda:N, a CUDA index list (0, 0,1), mps or cpu forces a device (a missing CUDA/MPS device falls back to the CPU with a warning). The selected device is printed at startup.
[Quick Start]
Windows 11
src\01_python_install_windows.bat
src\02_venv_create_windows.bat
src\05_convert_yolo_format.bat --dataset tumor
src\06_train.bat --dataset tumor --epochs 1000
src\07_inference.bat --dataset tumor --source data\tumor\test\images\tumor-1.jpg
src\08_inference_folder.bat --dataset tumor
Ubuntu 24.04
bash src/01_python_install_linux.sh
bash src/02_venv_create_linux.sh
bash src/05_convert_yolo_format.sh --dataset tumor
bash src/06_train.sh --dataset tumor --epochs 1000
bash src/07_inference.sh --dataset tumor --source data/tumor/test/images/tumor-1.jpg
bash src/08_inference_folder.sh --dataset tumor
macOS 14+ (Apple Silicon)
bash src/01_python_install_mac.sh
bash src/02_venv_create_mac.sh
bash src/05_convert_yolo_format.sh --dataset tumor
bash src/06_train.sh --dataset tumor --epochs 1000
bash src/07_inference.sh --dataset tumor --source data/tumor/test/images/tumor-1.jpg
bash src/08_inference_folder.sh --dataset tumor
For the plate example, pass --dataset plate to every step. Inference works out of the box with the shipped best.pt. Training the YOLOR-CSP-X model realistically requires a GPU.
[Dataset Format (Detection)]
- label.names: one class name per line (tumor: 1 class; plate: 4 classes). Example sizes: tumor 10 train / 3 val / 10 test; plate 40 train / 8 val / 8 test.
- Images in train/images/ and val/images/; Pascal VOC XML in the matching annotations/ folders.
- Step 05 writes YOLO txt to train/labels/ and val/labels/; the engine resolves labels by replacing images with labels in each path.
- yolor_csp_x.cfg (network) and hyp.scratch.1280.yaml (hyperparameters) are per-dataset copies.
[GPU]
- Device order: CUDA → MPS → CPU, overridable with --device.
- Windows / Linux: the venv scripts install the CUDA 13.0 (cu130) build directly. It needs a Turing-or-newer NVIDIA GPU and a driver supporting CUDA 13.0+. Pascal/Volta GPUs (GTX 10xx, V100) are not supported by cu130; on those, reinstall torch from the cu126 index inside the venv: pip install torch==2.13.0 torchvision==0.28.0 --index-url https://download.pytorch.org/whl/cu126 --force-reinstall
- On CUDA, training uses mixed precision, DataParallel when several GPUs are visible and pinned DataLoader memory; inference runs in FP16.
- macOS (Apple Silicon): the model runs on the Apple GPU through MPS in FP32; ops MPS lacks fall back to the CPU (PYTORCH_ENABLE_MPS_FALLBACK=1 is set by the entry scripts).
- Without a usable GPU everything runs on the CPU (slow for training). Checkpoints are loaded on the CPU first, so they load on any device; the engine's checkpoint loading was patched for PyTorch ≥ 2.6 (weights_only).
[References]
- YOLOR paper (Wang et al. 2021, shipped as 2105.04206.pdf): https://arxiv.org/abs/2105.04206
- YOLOR source: https://github.com/WongKinYiu/yolor
- Pascal VOC dataset: http://host.robots.ox.ac.uk/pascal/VOC/
- PyTorch installation: https://pytorch.org/get-started/locally/
- PyTorch MPS backend: https://docs.pytorch.org/docs/stable/notes/mps.html
This SDK is built in AppForAI - AI Dev Tools.