Darknet Object Detection Framework and YOLO
Darknet is an open-source neural network framework written in C, C++, and CUDA.
YOLO (You Only Look Once) is a state-of-the-art, real-time, object detection system that runs within the Darknet framework.
Learn how Hank.ai is empowering the Darknet/YOLO community!
Announcing Darknet V3 "Jazz"
Visit the Darknet/YOLO Website
Explore the Darknet/YOLO FAQ
Join the Darknet/YOLO Discord Server
Papers
1. Paper YOLOv7: Link to Paper
2. Paper Scaled-YOLOv4: Link to Paper
3. Paper YOLOv4: Link to Paper
4. Paper YOLOv3: Link to Paper
General Information
The Darknet/YOLO framework consistently outperforms other frameworks and YOLO versions in both speed and accuracy.
This framework is completely free and open-source. You can seamlessly integrate Darknet/YOLO into your existing projects and products, including commercial ones, without any licensing fees.
Darknet V3 ("Jazz"), released in October 2024, achieves remarkable performance, running the LEGO dataset videos at up to 1000 FPS on a NVIDIA RTX 3090 GPU. This translates to Darknet/YOLO processing each video frame, including resizing and processing, in less than 1 millisecond.
Join the Darknet/YOLO Discord server for support and discussion: https://discord.gg/zSq8rtW
The CPU version of Darknet/YOLO operates efficiently on various devices, including Raspberry Pi, cloud & colab servers, desktops, laptops, and high-end training rigs. However, the GPU version of Darknet/YOLO necessitates a CUDA-capable GPU from NVIDIA.
Darknet/YOLO is compatible with Linux, Windows, and Mac operating systems. Refer to the building instructions below for detailed guidance.
Darknet Version
The original Darknet tool developed by Joseph Redmon between 2013-2017 lacked a version number. We refer to this version as 0.x.
The subsequent popular Darknet repository maintained by Alexey Bochkovskiy from 2017-2021 also lacked a version number. We consider this version 1.x.
The Darknet repository sponsored by Hank.ai and maintained by Stéphane Charette, beginning in 2023, introduced a version command for the first time. From 2023 until late 2024, it returned version 2.x "OAK."
The development team focused on minimizing disruption to existing functionality while familiarizing themselves with the codebase.
Key changes implemented during this period include:
1. Unified Build Process: Re-written build steps to streamline the process using CMake for both Windows and Linux.
2. C++ Conversion: The codebase was converted to utilize the C++ compiler.
3. Chart Enhancement: The chart.png visualization was enhanced during training.
4. Bug Fixes and Optimizations: Numerous bug fixes and performance-related optimizations were implemented, primarily focused on reducing training time.
The final branch of this codebase is version 2.1, located in the v2 branch.
The next development phase commenced in mid-2024 and culminated in the release of version 3.x "JAZZ" in October 2024. The version command now returns 3.x.
You can always revert to the previous v2 branch if you need to execute specific commands from that version. Please let the development team know if you encounter any missing commands, and they will investigate adding them back.
Significant changes introduced in version 3.x "JAZZ":
1. Command Removal: Many outdated and unsupported commands were removed.
2. Performance Optimizations: Numerous performance optimizations were applied for both training and inference.
3. API Modification: The legacy C API underwent modifications. Applications using the original Darknet API may require minor adjustments. https://darknetcv.ai/api/api.html
4. New C/C++ API: Darknet V3 introduces a new C and C++ API. https://darknetcv.ai/api/api.html
5. Enhanced Sample Code: New applications and sample code are available in src-examples. https://darknetcv.ai/api/files.html
MSCOCO Pre-trained Weights
Several popular YOLO versions have been pre-trained for convenience on the MSCOCO dataset. This dataset includes 80 classes, which are listed in the cfg/coco.names text file.
In addition to MSCOCO, various simpler datasets and pre-trained weights are available for testing Darknet/YOLO, such as LEGO Gears and Rolodex. See the Darknet/YOLO FAQ for more details.
The MSCOCO pre-trained weights can be downloaded from various sources, including this repository:
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
The MSCOCO pre-trained weights are provided for demonstration purposes only. The corresponding .cfg and .names files for MSCOCO reside in the cfg directory.
Here are some example commands:
`bash
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
darknet03display_videos coco.names yolov4-tiny.cfg yolov4-tiny.weights video1.avi
DarkHelp coco.names yolov4-tiny.cfg yolov4-tiny.weights image1.jpg
DarkHelp coco.names yolov4-tiny.cfg yolov4-tiny.weights video1.avi
`
Users are encouraged to train their own networks. MSCOCO primarily serves as a tool for verifying that everything is functioning correctly.
Building
The various build methods employed prior to 2023 have been consolidated into a unified solution. Darknet necessitates C++17 or a newer version, OpenCV, and leverages CMake to generate the required project files.
Building Darknet/YOLO does not require knowledge of C++. It's analogous to driving a car without being a mechanic.
Google Colab
The Google Colab instructions mirror those for Linux. Several Jupyter notebooks are available to demonstrate specific tasks, such as training a new network.
Explore the notebooks in the colab subdirectory or follow the Linux instructions below.
Linux CMake Method
Darknet Build Tutorial for Linux
1. Prerequisites: Ensure you have the following packages installed:
`bash
sudo apt-get install build-essential git libopencv-dev cmake
`
2. Clone Darknet: Clone the Darknet repository:
`bash
mkdir ~/srccd ~/src
git clone https://github.com/hank-ai/darknet
cd darknet
`
3. Create Build Directory: Create a build directory:
`bash
mkdir build
cd build
`
4. Configure with CMake: Run CMake to configure the build:
`bash
cmake -DCMAKEBUILDTYPE=Release ..
`
5. Build: Build the project:
`bash
make -j4
`
6. Package: Create a Debian package:
`bash
make package
`
7. Install: Install the package:
`bash
sudo dpkg -i darknet-VERSION.deb
`
Optional: CUDA/cuDNN for GPU Acceleration
1. Install CUDA: If you have a modern NVIDIA GPU, you can install CUDA for GPU acceleration. Download and install CUDA from https://developer.nvidia.com/cuda-downloads.
2. Install cuDNN: Download and install cuDNN from https://developer.nvidia.com/rdp/cudnn-download or https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#cudnn-package-manager-installation-overview.
3. Verify CUDA Installation: Ensure that you can run nvcc and nvidia-smi after installing CUDA. You may need to adjust your PATH variable.
4. CMake Configuration (if CUDA installed after CMake): If you install CUDA or CUDA+cuDNN after configuring CMake, you need to delete the CMakeCache.txt file in the Darknet build directory to force CMake to re-find the necessary files. Then, re-run CMake and rebuild Darknet.
Advanced Users
RPM Package: To build an RPM installation file instead of a DEB file, modify the following lines in CM_package.cmake:
`cmake
# SET (CPACK_GENERATOR "DEB")
SET (CPACK_GENERATOR "RPM")
`
Distributions like Centos and OpenSUSE: For distributions like Centos and OpenSUSE, change the lines in CM_package.cmake to:
`cmake
# SET (CPACK_GENERATOR "DEB")
SET (CPACK_GENERATOR "RPM")
`
Installation Package: To install the package after building, use the package manager of your distribution. For example, on Debian-based systems like Ubuntu, use sudo dpkg -i darknet-2.0.1-Linux.deb.
Key Files Installed:
/usr/bin/darknet: The main Darknet executable. Confirm installation by running darknet version in the CLI.
/usr/include/darknet.h: The Darknet API for C, C++, and Python developers.
/usr/include/darknet_version.h: Contains version information for developers.
/usr/lib/libdarknet.so: The library to link against for C, C++, and Python developers.
/opt/darknet/cfg/...: Stores all the .cfg templates.
Windows CMake Method
Build Tutorial for Windows (Assuming a Fresh Windows 11 22H2 Installation)
1. Install Prerequisites: Install necessary components using winget:
`bash
winget install Git.Git
winget install Kitware.CMake
winget install nsis.nsis
winget install Microsoft.VisualStudio.2022.Community
`
2. Install Visual Studio C++ Support: Modify the Visual Studio installation to include C++ support:
* Open the "Windows Start" menu and run "Visual Studio Installer".
* Click on "Modify".
* Select "Desktop Development With C++".
* Click on "Modify" in the bottom-right corner and then "Yes".
3. Developer Command Prompt: Launch the "Developer Command Prompt for VS 2022" from the "Windows Start" menu. Do not use PowerShell for these steps!
4. Install Microsoft VCPKG: Install VCPKG to build OpenCV:
`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
`
* Be patient, this step may take a significant amount of time as it needs to download and build numerous components.
5. Optional: CUDA/cuDNN for GPU Acceleration: Follow the same steps outlined in the Linux section.
6. Clone Darknet: Clone the Darknet repository:
`bash
cd c:src
git clone https://github.com/hank-ai/darknet.git
cd darknet
mkdir build
cd build
`
7. Configure with CMake: Configure the build using CMake:
`bash
cmake -DCMAKEBUILDTYPE=Release -DCMAKETOOLCHAINFILE=C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake ..
`
8. Build with msbuild: Build the project using 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
`
Troubleshooting: Missing CUDA/cuDNN DLLs
If you encounter errors about missing CUDA or cuDNN DLLs (e.g., cublas64_12.dll), manually copy the CUDA .dll files to the same output directory as darknet.exe. For example:
`bash
copy "C:Program FilesNVIDIA GPU Computing ToolkitCUDAv12.2bin*.dll" src-cliRelease
`
* Remember to adjust the command based on your CUDA version.
Re-run the msbuild.exe command after copying the DLLs.
Advanced Users
Visual Studio Solution: CMake generates a Visual Studio solution file (Darknet.sln). Software developers who prefer using the Visual Studio GUI can load the Darknet project within Visual Studio instead of using the command line.
Verification and Installation
Verification: Run the following command to check if Darknet is built correctly:
`bash
C:srcDarknetbuildsrc-cliReleasedarknet.exe version
`
Installation: Run the NSIS installation wizard generated in the last step to correctly install Darknet, libraries, include files, and necessary DLLs. Locate the darknet-VERSION.exe file in the build directory. For example: darknet-2.0.31-win64.exe.
Key Files Installed:
C:Program FilesDarknetbin/darknet.exe: The Darknet CLI application.
C:Program FilesDarknet/bin: Contains other sample applications.
C:Program FilesDarknet: Includes required third-party .dll files (OpenCV), Darknet .dll, .lib, and .h files, and template .cfg files.
Using Darknet
CLI
The following list is not exhaustive of all commands supported by Darknet.
In addition to the Darknet CLI, consider the DarkHelp project CLI, which offers an alternative CLI with several advanced features not directly available in Darknet. You can use both CLIs concurrently.
Essential Prerequisites:
For most commands, you need a .weights file along with the corresponding .names and .cfg files.
Train your own network (highly recommended!) or download a pre-trained network from the internet.
Pre-trained Datasets Examples:
LEGO Gears: Object detection in images.
Rolodex: Text detection in images.
MSCOCO: Standard 80-class object detection.
Example Commands:
1. Get Help:
`bash
darknet help
`
2. Check Version:
`bash
darknet version
`
3. Image Prediction:
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. Video Processing:
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. Webcam Processing:
V2:
`bash
darknet detector demo animals.data animals.cfg animals_best.weights -c 0
`
V3:
`bash
darknet08display_webcam animals
`
7. Video Output:
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. Specific GPU:
V2:
`bash
darknet detector demo animals.data animals.cfg animals_best.weights -i 1 test.mp4
`
10. Accuracy Evaluation:
mAP:
`bash
darknet detector map driving.data driving.cfg driving_best.weights ...
`
mAP@IoU=75:
`bash
darknet detector map animals.data animals.cfg animalsbest.weights -iouthresh 0.75
`
11. Anchor Calculation:
DarkMark: Use DarkMark to recalculate anchors for optimal performance.
Darknet (Older Method):
`bash
darknet detector calcanchors animals.data -numof_clusters 6 -width 320 -height 256
`
12. Training a New Network:
DarkMark (Recommended): Use DarkMark for annotation and training.
Manual Setup:
1. Directory Creation: Create a folder for your project (e.g., ~/nn/animals/).
2. Configuration File: Copy a Darknet configuration file as a template (e.g., cfg/yolov4-tiny.cfg).
3. .names File: Create a .names text file listing your classes (e.g., ~/nn/animals/animals.names).
4. .data File: Create a .data text file in the same folder as the configuration file (e.g., ~/nn/animals/animals.data).
5. Dataset Folder: Create a folder for your images and annotations (e.g., ~/nn/animals/dataset).
6. .txt Annotations: Generate .txt files for each image, containing annotation coordinates. Use DarkMark or other annotation tools.
7. train/valid Files: Create animalstrain.txt and animalsvalid.txt files listing your training and validation images.
8. Modify .cfg File: Adjust the configuration file based on your dataset.
* Set batch=64.
* Adjust subdivisions based on your GPU memory.
Set max_batches to 2000 number of classes.
* Set steps to 80% and 90% of max_batches.
* Adjust width and height based on network dimensions.
* Set classes=... to the number of classes in your .names file.
Adjust filters=... in the [convolutional] sections before each [yolo] section to (numberofclasses + 5) 3.
9. Training: Start training:
`bash
cd ~/nn/animals/
darknet detector -map -dont_show train animals.data animals.cfg
`
* The best weights will be saved as animals_best.weights.
* View the chart.png file to monitor training progress.
Other Tools and Links
Key Resources:
DarkMark: For managing Darknet/YOLO projects, annotating images, verifying annotations, and generating training files.
DarkHelp: A robust alternative CLI to Darknet, with features like image tiling, object tracking in videos, and a robust C++ API suitable for commercial applications.
Darknet/YOLO FAQ: Find answers to common questions and solutions.
Stéphane's YouTube Channel: Explore tutorial and example videos.
Darknet/YOLO Discord Server: Connect with other Darknet/YOLO users for support and discussions.
Roadmap
Last Updated 2024-10-30:
Completed Tasks:
1. Replaced qsort() with std::sort() in relevant training sections.
2. Removed check_mistakes, getchar(), and system().
3. Converted Darknet to use the C++ compiler (g++ on Linux, VisualStudio on Windows).
4. Fixed Windows build.
5. Fixed Python support.
6. Built Darknet library.
7. Re-enabled labels on predictions ("alphabet" code).
8. Re-enabled CUDA/GPU code.
9. Re-enabled CUDNN.
10. Re-enabled CUDNN half.
11. Removed hard-coded CUDA architecture.
12. Improved CUDA version information.
13. Re-enabled AVX.
14. Removed old solutions and Makefile.
15. Made OpenCV non-optional.
16. Removed dependency on the old pthread library.
17. Removed STB.
18. Rewrote CMakeLists.txt to use the new CUDA detection.
19. Removed old "alphabet" code and deleted images in data/labels.
20. Enabled out-of-source build.
21. Enhanced version number output.
22. Performance optimizations related to training (ongoing).
23. Performance optimizations related to inference (ongoing).
24. Implemented pass-by-reference where applicable.
25. Cleaned up .hpp files.
26. Rewrote darknet.h.
27. Eliminated casting of cv::Mat to void*, using it as a proper C++ object.
28. Improved consistency in how internal image structure is used.
29. Fixed build for ARM-based Jetson devices.
30. Fixed Python API in V3.
Short-term Goals:
1. Replace printf() with std::cout (in progress).
2. Investigate old zed camera support.
3. Improve and standardize command line parsing (in progress).
Mid-term Goals:
1. Remove all char* code and replace with std::string.
2. Eliminate compiler warnings (in progress).
3. Improve the use of cv::Mat instead of the custom image structure in C (in progress).
4. Replace old list functionality with std::vector or std::list.
5. Fix support for 1-channel grayscale images.
6. Add support for N-channel images where N > 3 (e.g., images with additional depth or thermal channels).
7. Ongoing code cleanup (in progress).
Long-term Goals:
1. Address CUDA/CUDNN issues across all GPUs.
2. Rewriting CUDA+cuDNN code.
3. Explore support for non-NVIDIA GPUs.
4. Implement rotated bounding boxes or "angle" support.
5. Support for keypoints/skeletons.
6. Support for heatmaps (in progress).
7. Segmentation.