Darknet Neural Network Framework

repository·master·Indexed 21 days ago

https://github.com/hank-ai/darknet

A high-performance, open-source neural network framework written in C, C++, and CUDA, primarily used for the YOLO (You Only Look Once) real-time object detection system. It supports NVIDIA (CUDA) and AMD (ROCm) GPUs, as well as CPU-only builds via OpenBLAS. The framework is cross-platform (Linux, Windows, Mac) and provides tools for training custom networks, evaluating model accuracy (mAP), and exporting models to ONNX format.

Tokens
13K
Snippets
38
Records
52
Agent score
73%

What's inside Darknet

  1. Overview of Darknet and YOLO

    master

    Darknet is an open-source neural network framework written in C, C++, and CUDA. It hosts YOLO (You Only Look Once), a state-of-the-art, real-time object detection system.

    Key characteristics:

    • Open Source & Free: Can be used in commercial products without licensing fees.
    • High Performance: Capable of extremely high frame rates (e.g., 1000 FPS on an RTX 3090 for specific datasets).
    • Hardware Support:
      • CPU version: Runs on Raspberry Pi, cloud servers, desktops, and laptops.
      • GPU version: Requires NVIDIA (CUDA) or AMD (ROCm) GPUs.
    • Cross-platform: Works on Linux, Windows, and Mac.
  2. Explore Darknet ecosystem and language bindings

    master

    Darknet/YOLO provides several ways to integrate with different programming languages and workflows:

    Project Management & Annotation

    • DarkMark: Use this tool to manage Darknet/YOLO projects, annotate images, verify annotations, and generate training files.

    Alternative CLI & API

    • DarkHelp: A robust alternative CLI for Darknet. It supports image tiling, object tracking in videos, and provides a C++ API suitable for commercial applications.

    Language Bindings

    • C++: Available via official C++ bindings.
    • C: Available via official C bindings.
    • Python: Available via the src-python/ directory.
    • Java: [Incomplete/In-progress]
    • Delphi: Available via Darknet4Delphi.
    • C#: Available via DarknetCSharp.

    Other Tools

    • Darknet2Any: A utility for converting Darknet models.
  3. Enable NVIDIA GPU acceleration via CUDA and cuDNN

    master

    Darknet can be built to use NVIDIA GPUs for significantly faster image and video frame processing. This requires a CUDA-capable GPU (Kepler, Maxwell, Pascal, Volta, Turing, Ampere, Ada Lovelace, Hopper, or Blackwell architectures) and the installation of NVIDIA CUDA and cuDNN software.

    Build Configuration Flags

    You can control the GPU detection process during the CMake configuration step using the following flags:

    • Disable CUDA detection: Use -DDARKNET_TRY_CUDA=OFF if you want to prevent CMake from attempting to detect NVIDIA/CUDA.
    • Disable both AMD (ROCm) and NVIDIA (CUDA): Use -DDARKNET_TRY_ROCM=OFF -DDARKNET_TRY_CUDA=OFF.

    Important Note on Re-building

    If you install or update CUDA/cuDNN after you have already built Darknet, you must delete the CMakeCache.txt file in your build directory to force CMake to re-detect the hardware and libraries.

    # Disable CUDA detection
    cmake -DCMAKE_BUILD_TYPE=Release -DDARKNET_TRY_CUDA=OFF ..
    
    # Disable both AMD and NVIDIA detection
    cmake -DCMAKE_BUILD_TYPE=Release -DDARKNET_TRY_ROCM=OFF -DDARKNET_TRY_CUDA=OFF ..
  4. Darknet Version History and Evolution

    master

    Darknet has evolved through several major version phases:

    • v0.x & v1.x: Original versions by Joseph Redmon and Alexey Bochkovskiy.
    • v2.x (OAK): Introduced unified CMake builds, C++ compiler usage, and performance optimizations.
    • v3.x (JAZZ): Removed unmaintained commands, introduced a new C/C++ API, and added performance optimizations for training and inference.
    • v4.x (SLATE): Added AMD GPU support via ROCm and improved logging redirection.
    • v5.x (Moonlit): Introduced OpenBLAS for CPU builds, Profile-Guided Optimization, experimental ONNX export, and JAVA bindings.
    • v5.1: Refined ONNX export (including 'confs' and 'boxes' nodes) and rewrote the mAP function.

    Note for Developers: If your application includes darknet.h or darknet.hpp, you must recompile your application when moving to v5.1 or later due to changes in prediction structures to prevent segmentation faults.

  5. Control video detection playback

    master

    When running darknet_video.py with a display window, you can control the execution using the following methods:

    • Stop video: Right-click in the console.
    • Resume video: Left-click in the console.
    • Quit display window: Press the q key.
    • Force stop execution: Use Ctrl+C in the terminal.
  6. Build Requirements for Darknet

    master

    To build Darknet, ensure your environment meets the following requirements:

    • C++ Standard: C++17 or newer.
    • Build System: CMake.
    • Dependencies: OpenCV.

    Note: You do not need to be an expert in CMake, C++, or Python to build and run Darknet, though using the Darknet API requires programming knowledge.

  7. Build Darknet for AMD GPUs using ROCm and HIP

    master

    If you have a modern AMD GPU supported by ROCm and HIP, you can build Darknet to utilize GPU acceleration for faster image and video processing.

    Important Requirements:

    • The AMD ROCm and HIP software must be installed before running cmake. Darknet's build process attempts to identify your GPU and the necessary ROCm/HIP files during the configuration step.
    • On Linux, it is recommended to disable "secure boot" in your BIOS to avoid issues loading Linux video drivers.
    • If you install ROCm/HIP after a previous build attempt, you must delete src/darknet/build/CMakeCache.txt to force CMake to re-detect the environment.

    Manual Architecture Configuration: Darknet does not currently auto-detect AMD GPUs. You must manually specify your GPU architecture using the CMAKE_HIP_ARCHITECTURES flag.

    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_HIP_ARCHITECTURES=gfx1101 ..
  8. Evaluate model accuracy (mAP)

    master

    You can check the accuracy of a neural network using the map command. This calculates the Mean Average Precision.

    • Standard mAP: darknet detector map <data_file> <cfg_file> <weights_file>
    • mAP at specific IoU threshold: darknet detector map <data_file> <cfg_file> <weights_file> -iou_thresh <threshold> (e.g., -iou_thresh 0.75)
    darknet detector map driving.data driving.cfg driving_best.weights
  9. Optimize Darknet/YOLO using GNU GCC PGO

    master

    Profile-Guided Optimization (PGO) is an optional, advanced two-step process that can improve Darknet/YOLO performance by using compiler-specific profile files generated from actual workloads.

    Note: This is an advanced topic. Most users should use the standard build instructions in README.md instead.

    Step 1: Generate Profile Data

    Build Darknet with profiling enabled to collect data during execution.

    1. Configure with -DDARKNET_PROFILE_GEN=ON and -DDARKNET_PROFILE_USE=OFF.
    2. Build and install the package.
    3. Run a representative workload (e.g., a training session or inference on videos/images) to generate .gcda files. These files are typically located in ~/src/darknet/build/src-lib/CMakeFiles/darknetobjlib.dir/.

    Step 2: Apply Profile Data

    Rebuild Darknet using the collected profiles to create an optimized binary.

    1. Reconfigure with -DDARKNET_PROFILE_GEN=OFF and -DDARKNET_PROFILE_USE=ON.
    2. Build and install the package.
    3. Run the same workload to verify the FPS improvement.
    # Step 1: Generate
    cd ~/src/darknet/build
    cmake -DDARKNET_PROFILE_GEN=ON -DDARKNET_PROFILE_USE=OFF ..
    make -j4 package
    sudo dpkg -i darknet...etc...
    
    # [Run Darknet workload here to generate .gcda files]
    
    # Step 2: Use
    cd ~/src/darknet/build
    cmake -DDARKNET_PROFILE_GEN=OFF -DDARKNET_PROFILE_USE=ON ..
    make -j4 package
    sudo dpkg -i darknet...etc...
  10. Setup Darknet using WSL (Recommended for Windows)

    master

    If you are on Windows, using WSL (Windows Subsystem for Linux) with Ubuntu 24.04 LTS is highly recommended for running and training Darknet.

    1. Install WSL via the Windows command line: wsl --install.
    2. Once Ubuntu is installed, open the Ubuntu command prompt.
    3. Update your package list: sudo apt-get update.
    4. Follow the standard Linux CMake Method instructions to build Darknet.

    GPU Support in WSL: To use an NVIDIA GPU within WSL, you must install the Linux NVIDIA driver for WSL.

    sudo apt-get update