Darknet Object Detection Framework and YOLO
Darknet is an open-source neural network framework primarily written in C and C++, with some CUDA code for GPU acceleration. It's known for its speed and efficiency, making it a popular choice for real-time object detection applications.
YOLO (You Only Look Once) is a powerful real-time object detection system designed to run within the Darknet framework. It excels in speed and accuracy, consistently outperforming other object detection systems in benchmarks.
Dive deeper into the Darknet/YOLO world:
Hank.ai's Contributions: Discover how Hank.ai is actively supporting the Darknet/YOLO community.
Official Website: Explore the official Darknet/YOLO website for comprehensive information.
Frequently Asked Questions: Find answers to common questions about Darknet/YOLO.
Discord Server: Join the vibrant Darknet/YOLO Discord community for discussions and assistance.
Papers
The evolution of YOLO is documented in several research papers:
1. YOLOv7: Pushing the Boundaries of Deep Learning for Object Detection
2. Scaled-YOLOv4: Scaling Up YOLOv4 for Industrial Applications
3. YOLOv4: Optimal Speed and Accuracy of Object Detection
4. YOLOv3: An Incremental Improvement
General Information
The Darknet/YOLO framework continues to be at the forefront of object detection, boasting both speed and accuracy. This framework is entirely free and open source, allowing developers to seamlessly incorporate it into their projects without any licensing or fee restrictions.
Darknet V3, codenamed "Jazz," released in October 2024, achieves remarkable performance with a NVIDIA RTX 3090 GPU. It can process the LEGO dataset videos at up to 1000 FPS, meaning each frame is analyzed in just 1 millisecond or less.
Stay connected:
Discord: Join the Darknet/YOLO Discord server for support and discussions: https://discord.gg/zSq8rtW.
Darknet/YOLO is adaptable and runs on various platforms, from Raspberry Pi and cloud servers to desktops and high-end training systems. While the CPU version operates on a wide range of devices, the GPU version requires a CUDA-capable GPU from NVIDIA.
Darknet Version
The Darknet framework has evolved significantly since its inception, with several key versions:
1. Version 0.x: This original version, developed by Joseph Redmon between 2013-2017, lacked a specific version number.
2. Version 1.x: Maintained by Alexey Bochkovskiy from 2017-2021, this version also didn't have a version number.
3. Version 2.x "OAK": Sponsored by Hank.ai and maintained by Stéphane Charette starting in 2023, this marked the first version with a version command. This version ran until late 2024.
4. Version 3.x "JAZZ": The latest version, released in October 2024, introduces significant improvements and a new API.
Building Darknet
Building Darknet requires a C++17 compiler or newer, OpenCV, and the CMake build system. While the process may appear complex, you don't need to be a C++ expert to build, install, and run Darknet/YOLO.
Building Options:
1. Google Colab: The Google Colab instructions are identical to the Linux instructions.
2. Linux CMake Method: This method provides a detailed guide for Linux users.
3. Windows CMake Method: Follow these instructions for building Darknet on Windows.
Note: If you are using old tutorials, their build steps may differ from the new, unified approach outlined below.
Software Developers: Visit https://darknetcv.ai/ for insights into the inner workings of the Darknet/YOLO object detection framework.
Google Colab
The Google Colab instructions closely mirror the Linux CMake Method. There are several Jupyter notebooks within the colab subdirectory that demonstrate specific tasks like training a new network.
Linux CMake Method
Follow these instructions for building Darknet on Linux:
1. Prerequisites: Install the necessary tools:
`bash
sudo apt-get install build-essential git libopencv-dev cmake
`
2. Clone Darknet: Download the Darknet repository:
`bash
mkdir ~/srccd ~/src
git clone https://github.com/hank-ai/darknet
cd darknet
`
3. Create Build Directory: Create a directory for the build process:
`bash
mkdir build
cd build
`
4. Configure with CMake: Generate the build files:
`bash
cmake -DCMAKEBUILDTYPE=Release ..
`
5. Build Darknet: Build the Darknet executables:
`bash
make -j4
`
6. Package for Installation: Create a DEB package for easy installation (optional):
`bash
make package
sudo dpkg -i darknet-VERSION.deb
`
7. CUDA/cuDNN (Optional): For GPU acceleration, install CUDA or CUDA+cuDNN:
* CUDA: https://developer.nvidia.com/cuda-downloads
* cuDNN: https://developer.nvidia.com/rdp/cudnn-download
8. Verify Installation: After installation, check the Darknet version:
`bash
darknet version
`
Windows CMake Method
These instructions are for building Darknet on a clean Windows 11 22H2 installation.
1. Install Prerequisites:
`bash
winget install Git.Git winget install Kitware.CMake winget install nsis.nsis winget install Microsoft.VisualStudio.2022.Community
`
2. Visual Studio C++ Support:
* Open the Visual Studio Installer and select Modify.
* Enable Desktop Development With C++ and click Modify.
3. Developer Command Prompt for VS 2022: Open the Developer Command Prompt for VS 2022.
4. Install Microsoft VCPKG:
`bash
cd c:
mkdir c:src
cd c:src
git clone https://github.com/microsoft/vcpkg
cd vcpkg
bootstrap-vcpkg.bat
.vcpkg.exe integrate install
.vcpkg.exe integrate powershell
.vcpkg.exe install opencv[contrib,dnn,freetype,jpeg,openmp,png,webp,world]:x64-windows
`
5. CUDA/cuDNN (Optional): For GPU acceleration, install CUDA or CUDA+cuDNN:
* CUDA: https://developer.nvidia.com/cuda-downloads
* cuDNN: https://developer.nvidia.com/rdp/cudnn-download
6. Clone Darknet:
`bash
cd c:src
git clone https://github.com/hank-ai/darknet.git
cd darknet
mkdir build
cd build
`
7. Configure with CMake:
`bash
cmake -DCMAKEBUILDTYPE=Release -DCMAKETOOLCHAINFILE=C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake ..
`
8. Build Darknet with MSBuild:
`bash
msbuild.exe /property:Platform=x64;Configuration=Release /target:Build -maxCpuCount -verbosity:normal -detailedSummary darknet.sln
msbuild.exe /property:Platform=x64;Configuration=Release PACKAGE.vcxproj
`
9. Verify Installation: Run the Darknet executable and check the version:
`bash
C:srcdarknetbuildsrc-cliReleasedarknet.exe version
`
10. Install NSIS Package: Run the darknet-VERSION.exe file in the build directory to install Darknet, libraries, include files, and DLLs.
Using Darknet
Darknet offers a command-line interface (CLI) for interacting with the framework.
Note: In addition to the Darknet CLI, the DarkHelp project provides an alternative CLI with enhanced features. You can use both Darknet CLI and DarkHelp CLI together.
Common Darknet CLI Commands:
Help: List available commands:
`bash
darknet help
`
Version: Check the Darknet version:
`bash
darknet version
`
Prediction (Image): Perform object detection on an image:
`bash
# Version 2
darknet detector test cars.data cars.cfg cars_best.weights image1.jpg
# Version 3
darknet02displayannotatedimages cars.cfg image1.jpg
# DarkHelp
DarkHelp cars.cfg cars.cfg cars_best.weights image1.jpg
`
Output Coordinates (Image): Get object coordinates:
`bash
# Version 2
darknet detector test animals.data animals.cfg animalsbest.weights -extoutput dog.jpg
# Version 3
darknet01inference_images animals dog.jpg
# DarkHelp
DarkHelp --json animals.cfg animals.names animals_best.weights dog.jpg
`
Video Processing:
`bash
# Version 2
# Run on video file
darknet detector demo animals.data animals.cfg animalsbest.weights -extoutput test.mp4
# Run on webcam
darknet detector demo animals.data animals.cfg animals_best.weights -c 0
# Version 3
# Run on video file
darknet03display_videos animals.cfg test.mp4
# Run on webcam
darknet08display_webcam animals
# DarkHelp
# Run on video file
DarkHelp animals.cfg animals.names animals_best.weights test.mp4
`
Save Results to Video:
`bash
# Version 2
darknet detector demo animals.data animals.cfg animalsbest.weights test.mp4 -outfilename res.avi
# Version 3
darknet05processvideosmultithreaded animals.cfg animals.names animals_best.weights test.mp4
# DarkHelp
DarkHelp animals.cfg animals.names animals_best.weights test.mp4
`
JSON Output:
`bash
# Version 2
darknet detector demo animals.data animals.cfg animalsbest.weights test50.mp4 -jsonport 8070 -mjpegport 8090 -extoutput
# Version 3
darknet06imagestojson animals image1.jpg
# DarkHelp
DarkHelp --json animals.names animals.cfg animals_best.weights image1.jpg
`
Specific GPU: Run on a designated GPU:
`bash
# Version 2
darknet detector demo animals.data animals.cfg animals_best.weights -i 1 test.mp4
`
Accuracy Check: Evaluate the network's accuracy:
`bash
darknet detector map driving.data driving.cfg driving_best.weights
`
Calculate Anchors:
`bash
darknet detector calcanchors animals.data -numof_clusters 6 -width 320 -height 256
`
Training a New Network
DarkMark: For an intuitive and streamlined approach to annotating and training, use DarkMark.
Manual Setup:
1. Create Directory: Set up a folder for your training data.
2. Copy Configuration: Choose a configuration file as a template, e.g., cfg/yolov4-tiny.cfg, and copy it to your directory.
3. Create .names File: Create a text file (e.g., animals.names) listing your classes, one per line.
4. Create .data File: Create a text file (e.g., animals.data) with the following structure:
`
classes = 4
train = /path/to/animals_train.txt
valid = /path/to/animals_valid.txt
names = /path/to/animals.names
backup = /path/to/backup_directory
`
5. Prepare Dataset: Organize images and corresponding annotation files in a directory.
6. Create Train/Valid Text Files: Create two text files (animalstrain.txt, animalsvalid.txt) listing the paths to images for training and validation.
7. Modify Configuration:
* Set batch=64.
* Adjust subdivisions based on your GPU's memory.
Set max_batches to a suitable value (e.g., 2000 number of classes).
* Set steps to 80% and 90% of max_batches.
* Adjust width and height according to your network dimensions.
* Update classes to match the number of classes.
* Modify filters in the [convolutional] sections prior to [yolo] sections.
8. Start Training:
`bash
cd /path/to/animals
darknet detector -map -dont_show train animals.data animals.cfg
`
9. View Progress: Monitor training progress through the chart.png file.
Other Tools and Links
DarkMark: This tool simplifies image annotation, verification, and file generation for training with Darknet.
DarkHelp: A robust alternative CLI for Darknet with features like image tiling and object tracking.
Darknet/YOLO FAQ: Find answers to frequently asked questions.
Stéphane's YouTube Channel: Access tutorials and example videos.
Darknet/YOLO Discord Server: Join the community for support and discussions.
Roadmap
Completed:
Replaced qsort() with std::sort() for efficiency.
Removed deprecated code (e.g., check_mistakes, getchar(), system()).
Migrated Darknet to the C++ compiler.
Fixed Windows build issues.
Restored Python support.
Built the Darknet library.
Re-enabled CUDA/GPU code, CUDNN, and CUDNN half.
Improved CUDA version information.
Re-enabled AVX instruction set.
Removed old solutions and Makefile.
Made OpenCV a mandatory dependency.
Removed outdated library dependencies (e.g., pthread, STB).
Rewritten CMakeLists.txt for improved CUDA detection.
Enabled out-of-source builds.
Enhanced version number output.
Implemented performance optimizations for training and inference.
Cleaned up code for better readability.
Rewritten darknet.h for improved API.
Improved support for ARM-based Jetson devices.
Fixed Python API in V3.
Short-term Goals:
Swap out printf() with std::cout.
Investigate zed camera support.
Improve command line parsing.
Mid-term Goals:
Replace all char code with std::string.
Address compiler warnings.
Enhance the use of cv::Mat instead of custom image structures.
Upgrade list functionality to std::vector or std::list.
Support 1-channel grayscale images.
Add support for N-channel images (N > 3).
Ongoing code cleanup.
Long-term Goals:
Address CUDA/CUDNN issues across different GPUs.
Rewrite CUDA+cuDNN code for improved efficiency.
Explore support for non-NVIDIA GPUs.
Implement rotated bounding boxes and "angle" support.
Add keypoints, skeletons, and heatmaps.
Introduce segmentation capabilities.