Skip to content

cacybernetic/YOLO

From Scratch

A comprehensive starter template for building object detection model trainers from scratch. This pre-configured, ready-to-use project provides best practices and essential tools to develop, train, and deploy YOLO-based object detection models efficiently. Designed for researchers and developers who want to understand and customize every aspect of the detection pipeline.

Table of Contents

  • Description: contents the project description.
  • Features: contents the descriptions of each features implemented and available on this software.
  • Installation: contents the process of the installation for two plateforms.
    • For Linux: here is the process of installation of this software on linux.
    • For Windows: here is the process of installation of this software on Windows.
  • Usage: all details of the use cases usefull to get starting this software.
  • Tests: all details to run unittest.
  • To contribute: usefull information for the person who want to contribute to this project.
  • Licence: description of the license of this software.
  • Contact: developers contacts.

Description

This is an open source YOLO model implementation from scratch in PyTorch. The project provides a complete framework for object detection, allowing users to train, fine-tune, and deploy YOLO models on custom datasets. It is designed to be accessible, flexible, and production-ready.

Features

  • Training YOLO model from scratch on your own dataset formatted into YOLO format (cx, cy, w, h).
  • Fine-tuning of pre-trained models on another dataset.
  • Export pre-trained models for production environments.
  • Test your model by running predictions on image files.
  • Run inference in real-time via webcam.

Installation

To install the project, make sure you have Python 3.8 or later version and pip installed on your machine. And then run the following command lines to install directly this program.

# From main branch:
pip install git+https://github.com/cacybernetic/YOLO

# Or From develop branch:
pip install git+https://github.com/cacybernetic/YOLO@dev

or run the following command line to get project repository, install all dependencies manually and install this program in developer mode.

For Linux

git clone https://github.com/cacybernetic/YOLO;
cd YOLO;
sudo rm -r .git;
git init;  # To create a new instance of git repository

OS dependences

Ubuntu

Open your terminal and run following command lines to add the deadsnakes PPA to your system:

sudo apt update;
sudo apt install software-properties-common -y;
sudo add-apt-repository ppa:deadsnakes/ppa -y

Refresh your package list to include the deadsnakes PPA and then install Python 3.10:

sudo apt update;
sudo apt install python3.10;
python3.10 --version

NOTE: Do not change the default Python version of Ubuntu, as it may break system tools that depend on it.

Debian or Kali

In first, install the following dependences on your computer.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

And then, we can run the following command to install pyenv directly via APT on your computer.

sudo apt install pyenv

Or run the following command lines, to clone and install pyenv from its souce code.

git clone https://github.com/pyenv/pyenv.git ~/.pyenv;
 
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc;
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc;
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc;
echo 'eval "$(pyenv init -)"' >> ~/.bashrc;
source ~/.bashrc;

Now, runing the following command line, we can use pyenv to install the version of Python what we want to install.

pyenv install 3.10.18;  # Here, we install Python 3.10.18.
sudo ln -s $HOME/.pyenv/versions/3.10.18/bin/python3 /usr/local/bin/python3.10

Project dependences

  1. sudo apt install cmake python3-venv Install Cmake and Virtual env;
  2. python3 -m venv .venv create a virtual env into directory named env;
  3. source .venv/bin/activate activate the virtual environment named .venv;
  4. make install install the requirements of this package;
  5. make dev_install or pip install -e . install the package in dev mode in virtual environment;
  6. Run make test or pytest to execute the unit test scripts located at tests directory.

NOTE: If you run this program in a linux server, execute in first the following command line:

sudo apt-get install libgl1-mesa-glx libglib2.0-0

For Windows

git clone https://github.com/cacybernetic/YOLO
cd YOLO

And then, delete the hidden directory named .git located at the root of the directory project.

And then,

  1. Install python for windows;
  2. Open your command prompt;
  3. Run python -m venv .venv to create a virtual env into directory named .venv;
  4. Run .venv\Scripts\activate to activate the virtual environment;
  5. Run pip install -r requirements.txt to install the requirements of this package or project;
  6. Run pip install -e . install the package in dev mode in virtual environment;
  7. pytest run the unit test scripts located at tests directory.

Usage

  1. Train a YOLO model on your dataset:
yolo train train_data="datasets/train" test_data="datasets/val" shuffle=True epochs=10 batchs=4 optimizer=AdamW lr0=1e-4 weight_decay=5e-4 workers=2 gradient_accumulations=48 amp=False class_names="class1, class2" backbone_drop=0.2 neck_drop=0.3 head_drop=0.4 device=cuda

Dropout Integration Strategy:

  • dropout_backbone: Applied to deep residual blocks in the backbone (≥ 4th block)
  • dropout_neck: Applied before each prediction head (between features and detection)
  • dropout_head: Applied in ScalePrediction before final convolutions

Recommendations:

  • For medium datasets: dropout_backbone=0.1, dropout_neck=0.2, dropout_head=0.3
  • For small datasets: dropout_backbone=0.2, dropout_neck=0.3, dropout_head=0.4
  • For large datasets: dropout_backbone=0.0, dropout_neck=0.1, dropout_head=0.2

Export To ONNX

To export a YOLO model weight into ONNX format, you must install in first the following modules.

pip install onnx>=1.16.0 onnxsim>=0.4.33

And then, according your device, do the following installations:

  • On CPU device, install the ONNX package version for CPU running pip install onnxruntime.
  • On GPU device (like CUDA), install the ONNX package version for GPU running pip install onnxruntime-gpu.

NOTE: onnxruntime or onnxruntime-gpu allows to run the inference of ONNX model. The module onnxruntime allows to run on CPU device, and onnxruntime-gpu allows to run on GPU device.

Finally, run the following command line to perform exportation.

yolo export saved_model output=export_folder
  • saved_model is the name of folder containing the model weights saved as .pth or .pt format accompanied by its config file.
  • output=export_folder is the path to the directory in which you want to save the ONNX files that represents your exported model for production.

Tests

To execute the unittest, make sure you have pytest package installed, and then run the following command line:

make test 

or

pytest

To contribute

Contributions are welcome! Please follow these steps:

  1. Create a new branch for your feature (git checkout -b feature/my-feature);
  2. Commit your changes (git commit -m 'Adding a new feature');
  3. Push toward the branch (git push origin feature/my-feature);
  4. Create a new Pull Request or Merge Request.

Licence

This project is licensed under the MIT License. See the file LICENSE for more details, contact me please.

Contact

For your question or suggestion, contact me please:

About

You Only Look One From Scratch Implementation.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors