Darknet Object Detection Framework and YOLO
!darknet and hank.ai logos
Darknet is an open-source neural network framework written in C, C++, and CUDA. It provides a powerful foundation for developing and deploying object detection models.
YOLO (You Only Look Once) is a state-of-the-art, real-time object detection system built upon the Darknet framework. It excels in speed and accuracy, making it a popular choice for various applications.
Want to learn more about how Hank.ai is contributing to the Darknet/YOLO community?
Explore the official Darknet/YOLO website:
Dive into the comprehensive Darknet/YOLO FAQ:
Join the vibrant Darknet/YOLO Discord community:
Papers
YOLOv7: Link to paper
Scaled-YOLOv4: Link to paper
YOLOv4: Link to paper
YOLOv3: Link to paper
General Information
The Darknet/YOLO framework continues to be a leader in object detection, consistently offering both speed and accuracy advantages over other frameworks and YOLO versions.
Key features:
1. Open Source and Free: This framework is entirely free and open-source, allowing you to seamlessly incorporate it into your projects without any licensing restrictions.
2. High Performance: Darknet V3 ("Jazz"), released in October 2024, achieves impressive performance, reaching up to 1000 FPS for the LEGO dataset using an NVIDIA RTX 3090 GPU.
3. Versatile Deployment: Darknet/YOLO is compatible with various devices, ranging from Raspberry Pi and cloud servers to desktops and high-end training rigs.
4. Cross-Platform Support: It's known to run flawlessly on Linux, Windows, and Mac operating systems.
Need help or want to discuss Darknet/YOLO? Join the vibrant community on Discord:
Darknet Version
The evolution of Darknet can be traced through its versions:
1. Version 0.x (2013-2017): The original Darknet tool developed by Joseph Redmon lacked a formal version number.
2. Version 1.x (2017-2021): This version was maintained by Alexey Bochkovskiy and also lacked a version number.
3. Version 2.x "OAK" (2023): The Darknet repository, sponsored by Hank.ai and maintained by Stéphane Charette, introduced a version command. This version brought significant improvements, including:
* Unified build steps using CMake for both Windows and Linux.
* Conversion to the C++ compiler.
* Enhanced chart.png visualization during training.
* Bug fixes and performance optimizations for training efficiency.
4. Version 2.1: The final branch of the v2 codebase.
5. Version 3.x "JAZZ" (October 2024): This latest release introduced:
* Performance optimizations for both training and inference.
* A modified legacy C API requiring minor adaptations for applications using the original Darknet API.
* A new Darknet V3 C and C++ API.
* New apps and sample code in the src-examples directory.
For compatibility with older versions, the v2 branch is still accessible. If you need to use specific commands from previous versions, let us know, and we'll explore adding them back.
MSCOCO Pre-trained Weights
Several popular versions of YOLO come pre-trained on the MSCOCO dataset, offering a convenient starting point for experimenting with the framework. MSCOCO contains 80 classes, listed in the cfg/coco.names file.
Pre-trained weights available:
YOLOv2 (November 2016):
* yolov2-tiny.weights
* yolov2-full.weights
YOLOv3 (May 2018):
* yolov3-tiny.weights
* yolov3-full.weights
YOLOv4 (May 2020):
* yolov4-tiny.weights
* yolov4-full.weights
YOLOv7 (August 2022):
* yolov7-tiny.weights
* yolov7-full.weights
Remember: While these pre-trained weights are helpful for demonstrations, it's essential to train your own networks for tailored solutions. MSCOCO is primarily used for verifying that your setup is functioning correctly.
Example commands using pre-trained weights:
`bash
Display annotated images
wget --no-clobber https://github.com/hank-ai/darknet/releases/download/v2.0/yolov4-tiny.weights
darknet02displayannotatedimages coco.names yolov4-tiny.cfg yolov4-tiny.weights image1.jpg
Display videos with annotations
darknet03display_videos coco.names yolov4-tiny.cfg yolov4-tiny.weights video1.avi
Use DarkHelp CLI
DarkHelp coco.names yolov4-tiny.cfg yolov4-tiny.weights image1.jpg
DarkHelp coco.names yolov4-tiny.cfg yolov4-tiny.weights video1.avi
`
Building
Darknet leverages CMake to streamline the build process for both Windows and Linux. It requires C++17 or a newer compiler, along with OpenCV.
Build instructions are now unified, replacing older, more complex methods.
Note: If you are encountering issues with old tutorials or build steps that don't align with this guide, please refer to the latest instructions provided here.
Software developers are encouraged to visit https://darknetcv.ai/ for insights into the Darknet/YOLO framework's inner workings.
Google Colab
The Google Colab instructions mirror the Linux instructions.
Explore the notebooks in the colab subdirectory, or follow the Linux instructions below.
Linux CMake Method
1. Install necessary packages:
`bash
sudo apt-get install build-essential git libopencv-dev cmake
`
2. Clone the Darknet repository:
`bash
mkdir ~/src
cd ~/src
git clone https://github.com/hank-ai/darknet
cd darknet
mkdir build
cd build
`
3. Configure CMake:
`bash
cmake -DCMAKEBUILDTYPE=Release ..
`
4. Build Darknet:
`bash
make -j4
`
5. Package Darknet:
`bash
package
sudo dpkg -i darknet-VERSION.deb
`
Optional: CUDA or CUDA+cuDNN for GPU acceleration
Visit https://developer.nvidia.com/cuda-downloads to download and install CUDA.
Visit https://developer.nvidia.com/rdp/cudnn-download or https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#cudnn-package-manager-installation-overview to download and install cuDNN.
After installing CUDA, ensure that nvcc and nvidia-smi are accessible. You might need to adjust your PATH variable.
If you install CUDA or CUDA+cuDNN later or upgrade NVIDIA software:
Delete the CMakeCache.txt file in your Darknet build directory to force CMake to re-find dependencies.
Re-build Darknet.
To upgrade CMake on Ubuntu:
`bash
sudo apt-get purge cmake
sudo snap install cmake --classic
`
Advanced Users:
To build an RPM installation file instead of a DEB file, modify the CPACKGENERATOR in CMpackage.cmake to "RPM".
For Centos and OpenSUSE, change CPACK_GENERATOR to "RPM".
After building the installation package, use the appropriate package manager to install it.
Verification:
Run darknet version from the command line to confirm installation.
Windows CMake Method
1. Install required software:
`bash
winget install Git.Git
winget install Kitware.CMake
winget install nsis.nsis
winget install Microsoft.VisualStudio.2022.Community
`
2. Modify Visual Studio installation:
* Open "Visual Studio Installer."
* Click "Modify."
* Select "Desktop Development With C++."
* Click "Modify" and then "Yes."
3. Open Developer Command Prompt for VS 2022:
* Do not use PowerShell.
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. Optional: CUDA or CUDA+cuDNN for GPU acceleration
* Install CUDA following the instructions at https://developer.nvidia.com/cuda-downloads.
* Install cuDNN using the instructions at https://developer.nvidia.com/rdp/cudnn-download or https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#download-windows.
After installation, make sure nvcc.exe and nvidia-smi.exe are accessible. You may need to modify your PATH variable.
If you install CUDA or CUDA+cuDNN later or upgrade NVIDIA software:
* Ensure CUDA is installed after Visual Studio.
* Re-install CUDA if Visual Studio is upgraded.
6. Clone Darknet and build it:
`bash
cd c:src
git clone https://github.com/hank-ai/darknet.git
cd darknet
mkdir build
cd build
cmake -DCMAKEBUILDTYPE=Release -DCMAKETOOLCHAINFILE=C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake ..
msbuild.exe /property:Platform=x64;Configuration=Release /target:Build -maxCpuCount -verbosity:normal -detailedSummary darknet.sln
msbuild.exe /property:Platform=x64;Configuration=Release PACKAGE.vcxproj
`
7. Copy CUDA DLLs (if needed):
* If you get errors about missing CUDA or cuDNN DLLs, manually copy them into the Darknet output directory. For example:
`bash
copy "C:Program FilesNVIDIA GPU Computing ToolkitCUDAv12.2bin*.dll" src-cliRelease
`
8. Re-run the msbuild.exe command to generate the NSIS installation package:
`bash
msbuild.exe /property:Platform=x64;Configuration=Release PACKAGE.vcxproj
`
Advanced Users:
You can use the Darknet.sln solution file generated by CMake within the Visual Studio GUI instead of command-line tools.
Verification:
Run C:srcDarknetbuildsrc-cliReleasedarknet.exe version to confirm installation.
To install Darknet, run the NSIS installation wizard:
Look for the darknet-VERSION.exe file in the build directory.
Run the installation wizard to install the CLI application, libraries, include files, and necessary DLLs.
Using Darknet
CLI
Note: The following commands are not exhaustive.
In addition to the Darknet CLI, consider using the DarkHelp project CLI, which offers alternative functionality and advanced features.
Most commands require the .weights file along with its corresponding .names and .cfg files. You can either train your own network or download pre-trained models from the internet. Examples include:
LEGO Gears: Object detection in images.
Rolodex: Text detection in images.
MSCOCO: Standard 80-class object detection.
Commands:
1. Help:
`bash
darknet help
`
2. Version:
`bash
darknet version
`
3. Prediction using an image:
* V2:
`bash
darknet detector test cars.data cars.cfg cars_best.weights image1.jpg
`
* V3:
`bash
darknet02displayannotatedimages cars.cfg image1.jpg
`
* DarkHelp:
`bash
DarkHelp cars.cfg cars.cfg cars_best.weights image1.jpg
`
4. Output coordinates:
* V2:
`bash
darknet detector test animals.data animals.cfg animalsbest.weights -extoutput dog.jpg
`
* V3:
`bash
darknet01inference_images animals dog.jpg
`
* DarkHelp:
`bash
DarkHelp --json animals.cfg animals.names animals_best.weights dog.jpg
`
5. Working with videos:
* V2:
`bash
darknet detector demo animals.data animals.cfg animalsbest.weights -extoutput test.mp4
`
* V3:
`bash
darknet03display_videos animals.cfg test.mp4
`
* DarkHelp:
`bash
DarkHelp animals.cfg animals.names animals_best.weights test.mp4
`
6. Reading from a webcam:
* V2:
`bash
darknet detector demo animals.data animals.cfg animals_best.weights -c 0
`
* V3:
`bash
darknet08display_webcam animals
`
7. Saving results to a video:
* V2:
`bash
darknet detector demo animals.data animals.cfg animalsbest.weights test.mp4 -outfilename res.avi
`
* V3:
`bash
darknet05processvideosmultithreaded animals.cfg animals.names animals_best.weights test.mp4
`
* DarkHelp:
`bash
DarkHelp animals.cfg animals.names animals_best.weights test.mp4
`
8. JSON output:
* V2:
`bash
darknet detector demo animals.data animals.cfg animalsbest.weights test50.mp4 -jsonport 8070 -mjpegport 8090 -extoutput
`
* V3:
`bash
darknet06imagestojson animals image1.jpg
`
* DarkHelp:
`bash
DarkHelp --json animals.names animals.cfg animals_best.weights image1.jpg
`
9. Running on a specific GPU:
`bash
darknet detector demo animals.data animals.cfg animals_best.weights -i 1 test.mp4
`
10. Checking accuracy of the neural network:
`bash
darknet detector map driving.data driving.cfg driving_best.weights ...
`
11. Calculating mAP@IoU=75:
`bash
darknet detector map animals.data animals.cfg animalsbest.weights -iouthresh 0.75
`
12. Recalculating anchors:
`bash
darknet detector calcanchors animals.data -numof_clusters 6 -width 320 -height 256
`
13. Training a new network:
`bash
darknet detector -map -dont_show train animals.data animals.cfg
`
Training
For simplified annotation and training, utilize DarkMark. It's the recommended approach for creating new neural networks.
Manual training setup (if you prefer):
1. Create a project folder:
* For example, ~/nn/animals/ for animal detection.
2. Copy a Darknet configuration file as a template:
* For instance, cfg/yolov4-tiny.cfg.
3. Create a animals.names text file:
* List each class, one entry per line, without blank lines or comments. Example:
`
dog
cat
bird
horse
`
4. Create a animals.data text file:
`
classes = 4
train = /home/username/nn/animals/animals_train.txt
valid = /home/username/nn/animals/animals_valid.txt
names = /home/username/nn/animals/animals.names
backup = /home/username/nn/animals
`
5. Create a folder for images and annotations (e.g., ~/nn/animals/dataset).
* Annotate images using DarkMark or similar software. The YOLO annotation format is outlined in the Darknet/YOLO FAQ.
6. Create animalstrain.txt and animalsvalid.txt:
* List all images used for training and validation, one image per line.
7. Modify the .cfg file:
* Set batch=64.
* Adjust subdivisions based on your network dimensions and GPU memory. Start with subdivisions=1.
* Set max_batches to 2000 times the number of classes (e.g., 8000 for 4 classes).
* Set steps to 80% and 90% of max_batches.
* Adjust width and height to match your desired network dimensions.
* Modify classes to reflect the number of classes in your .names file.
Update filters in the [convolutional] sections before each [yolo] section: (numberofclasses + 5) 3.
8. Start training:
`bash
cd ~/nn/animals/
darknet detector -map -dont_show train animals.data animals.cfg
`
For more training details and advanced parameters, refer to the Darknet/YOLO FAQ.
Other Tools and Links
DarkMark: Manage Darknet/YOLO projects, annotate images, verify annotations, and generate training files.
DarkHelp: Provides an alternative CLI for Darknet, enabling image tiling, object tracking, and a robust C++ API for commercial applications.
Darknet/YOLO FAQ: A comprehensive resource for answering your questions.
Stéphane's YouTube Channel: Access numerous tutorials and example videos.
Darknet/YOLO Discord Server: Connect with other users for support and discussions.
Roadmap
Last updated 2024-10-30:
Completed:
Replaced qsort() with std::sort() where applicable during training.
Removed check_mistakes, getchar(), and system().
Converted Darknet to use the C++ compiler (g++ on Linux, VisualStudio on Windows).
Fixed Windows build.
Re-enabled labels on predictions (the "alphabet" code).
Re-enabled CUDA/GPU code, CUDNN, and CUDNN half.
Removed hard-coded CUDA architecture.
Improved CUDA version information.
Re-enabled AVX.
Removed old solutions and Makefiles.
Made OpenCV a non-optional dependency.
Removed the old pthread library dependency.
Removed STB.
Re-wrote CMakeLists.txt for improved CUDA detection.
Removed the old "alphabet" code and associated images.
Implemented out-of-source build.
Enhanced version number output.
Performance optimizations during training (ongoing).
Performance optimizations during inference (ongoing).
Implemented pass-by-reference where possible.
Cleaned up .hpp files.
Re-wrote darknet.h.
Eliminated casting cv::Mat to void and used it as a C++ object.
Fixed inconsistencies in the internal image structure.
Fixed build for ARM-based Jetson devices (new Jetson Orin devices are working).
Fixed Python API in V3.
Short-term goals:
Replace printf() with std::cout (in progress).
Revisit old Zed camera support.
Improve command-line parsing consistency (in progress).
Mid-term goals:
Remove all char code and replace with std::string.
Eliminate compiler warnings and improve code clarity (in progress).
Enhance the use of cv::Mat instead of the custom image structure in C (in progress).
Replace old list functionality with std::vector or std::list.
Add support for 1-channel greyscale images.
Extend support for N-channel images (N > 3, e.g., depth or thermal channels).
Ongoing code cleanup (in progress).
Long-term goals:
Resolve CUDA/CUDNN issues across all GPUs.
Re-write CUDA+cuDNN code.
Explore support for non-NVIDIA GPUs.
Implement rotated bounding boxes or "angle" support.
Add keypoints/skeletons.
Introduce heatmaps (in progress).
Explore segmentation.
Stay tuned for updates and contributions to the Darknet/YOLO framework!