hank-ai / darknet

Darknet/YOLO object detection framework
https://darknetcv.ai/
Apache License 2.0
255 stars 37 forks source link
computervision darknet machine-learning neural-networks object-detection yolo

Table of Contents

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.

YOLO (You Only Look Once) is a state-of-the-art, real-time, object detection system, which runs in the Darknet framework.

Papers

General Information

YOLOv7 is more accurate and faster than YOLOv5 by 120% FPS, than YOLOX by 180% FPS, than Dual-Swin-T by 1200% FPS, than ConvNext by 550% FPS, than SWIN-L by 500% FPS, and PPYOLOE-X by 150% FPS.

YOLOv7 surpasses all known object detectors in both speed and accuracy in the range from 5 FPS to 160 FPS and has the highest accuracy 56.8% AP among all known real-time object detectors with 30 FPS or higher on GPU V100, batch=1.

comparison

Darknet Version

MSCOCO Pre-trained Weights

Several popular versions of YOLO were pre-trained for convenience on the MSCOCO dataset. This dataset has 80 classes, which can be seen in the text file cfg/coco.names.

There are several other simpler datasets and pre-trained weights available for testing Darknet/YOLO, such as LEGO Gears and Rolodex. See the Darknet/YOLO FAQ for details.

The MSCOCO pre-trained weights can be downloaded from several different locations, and are also available for download from this repo:

The MSCOCO pre-trained weights are provided for demo-purpose only. The corresponding @p .cfg and @p .names files for MSCOCO are in the cfg directory. Example commands:

wget --no-clobber https://github.com/hank-ai/darknet/releases/download/v2.0/yolov4-tiny.weights
darknet_02_display_annotated_images coco.names yolov4-tiny.cfg yolov4-tiny.weights image1.jpg
darknet_03_display_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

Note that people are expected to train their own networks. MSCOCO is normally used just to confirm that everything is working correctly.

Building

The various build methods available in the past have been merged together into a single unified solution. Darknet requires OpenCV, and uses CMake to generate the necessary project files.

Beware if you are following old tutorials with more complicated build steps, or build steps that don't match what is in this readme. The new build steps as described below started in August 2023.

Software developers are encouraged to visit https://darknetcv.ai/ to get information on the internals of the Darknet/YOLO object detection framework.

Google Colab

The Google Colab instructions are the same as the Linux instructions. Several Jupyter notebooks are available showing how to do certain tasks, such as training a new network.

See the notebooks in the colab subdirectory.

Linux CMake Method

Darknet build tutorial for Linux

TODO: is libomp-dev also necessary for OpenMP?

These instructions assume a system running Ubuntu 22.04.

sudo apt-get install build-essential git libopencv-dev cmake
mkdir ~/src
cd ~/src
git clone https://github.com/hank-ai/darknet
cd darknet
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j4 package
sudo dpkg -i darknet-VERSION.deb

If you are using an older version of CMake then you'll need to upgrade CMake before you can run the cmake command above. Upgrading CMake on Ubuntu can be done with the following commands:

sudo apt-get purge cmake
sudo snap install cmake --classic

If using bash as your command shell, you'll want to re-start your shell at this point. If using fish, it should immediately pick up the new path.

Advanced users:

If you want to build a RPM installation file instead of a DEB file, see the relevant lines in CM_package.cmake. Prior to running make -j4 package you'll need to edit these two lines:

SET (CPACK_GENERATOR "DEB")
# SET (CPACK_GENERATOR "RPM")

For distros such as Centos and OpenSUSE, you'll need to switch those two lines in CM_package.cmake to be:

# SET (CPACK_GENERATOR "DEB")
SET (CPACK_GENERATOR "RPM")

To install the installation package, use the usual package manager for your distribution. For example, on Debian-based systems such as Ubuntu:

sudo dpkg -i darknet-2.0.1-Linux.deb

Installing the package will copy the following files:

You are now done! Darknet has been built and installed into /usr/bin/. Run this to test: darknet version.

If you don't have /usr/bin/darknetthen this means you _did not_ install it, you only built it! Make sure you install the.debor.rpm` file as described above.

Windows CMake Method

These instructions assume a brand new installation of Windows 11 22H2.

Open a normal cmd.exe command prompt window and run the following commands:

winget install Git.Git
winget install Kitware.CMake
winget install nsis.nsis
winget install Microsoft.VisualStudio.2022.Community

At this point we need to modify the Visual Studio installation to include support for C++ applications:

Advanced users:

Instead of running the Developer Command Prompt, you can use a normal command prompt or ssh into the device and manually run "\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat".

Once you have the Developer Command Prompt running as described above, run the following commands to install Microsoft VCPKG, which will then be used to build OpenCV:

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 at this last step as it can take a long time to run. It needs to download and build many things.

Advanced users:

Note there are many other optional modules you may want to add when building OpenCV. Run .\vcpkg.exe search opencv to see the full list.

Once all of the previous steps have finished successfully, you need to clone Darknet and build it. During this step we also need to tell CMake where vcpkg is located so it can find OpenCV and other dependencies:

cd c:\src
git clone https://github.com/hank-ai/darknet.git
cd darknet
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=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

If you get an error about some missing CUDA or cuDNN DLLs such as cublas64_12.dll, then manually copy the CUDA .dll files into the same output directory as Darknet.exe. For example:

copy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin\*.dll" src-cli\Release\

(That is an example! Check to make sure what version you are running, and run the command that is appropriate for what you have installed.)

Once the files have been copied, re-run the last msbuild.exe command to generate the NSIS installation package:

msbuild.exe /property:Platform=x64;Configuration=Release PACKAGE.vcxproj

Advanced users:

Note that the output of the cmake command is a normal Visual Studio solution file, Darknet.sln. If you are a software developer who regularly uses the Visual Studio GUI instead of msbuild.exe to build projects, you can ignore the command-line and load the Darknet project in Visual Studio.

You should now have this file you can run: C:\src\Darknet\build\src-cli\Release\darknet.exe. Run this to test: C:\src\Darknet\build\src-cli\Release\darknet.exe version.

To correctly install Darknet, the libraries, the include files, and the necessary DLLs, run the NSIS installation wizard that was built in the last step. See the file darknet-VERSION.exe in the build directory. For example:

darknet-2.0.31-win64.exe

Installing the NSIS installation package will:

You are now done! Once the installation wizard has finished, Darknet will have been installed into C:\Program Files\Darknet\. Run this to test: C:\Program Files\Darknet\bin\darknet.exe version.

If you don't have C:/Program Files/darknet/bin/darknet.exe then this means you did not install it, you only built it! Make sure you go through each panel of the NSIS installation wizard in the previous step.

Using Darknet

CLI

The following is not the full list of all commands supported by Darknet.

In addition to the Darknet CLI, also note the DarkHelp project CLI which provides an alternative CLI to Darknet/YOLO. The DarkHelp CLI also has several advanced features that are not available directly in Darknet. You can use both the Darknet CLI and the DarkHelp CLI together, they are not mutually exclusive.

For most of the commands shown below, you'll need the .weights file with the corresponding .names and .cfg files. You can either train your own network (highly recommended!) or download a neural network that someone has already trained and made available for free on the internet. Examples of pre-trained datasets include:

Commands to run include:

Training

Quick links to relevant sections of the Darknet/YOLO FAQ:

The simplest way to annotate and train is with the use of DarkMark to create all of the necessary Darknet files. This is definitely the recommended way to train a new neural network.

If you'd rather manually setup the various files to train a custom network:

Be patient. The best weights will be saved as animals_best.weights. And the progress of training can be observed by viewing the chart.png file. See the Darknet/YOLO FAQ for additional parameters you may want to use when training a new network.

If you want to see more details during training, add the --verbose parameter. For example:

darknet detector -map -dont_show --verbose train animals.data animals.cfg

Other Tools and Links

Roadmap

Last updated 2024-09-21:

Completed

Short-term goals

Mid-term goals

Long-term goals