NanoDet-Plus Documentation

repository·main·Indexed 27 days ago

https://github.com/rangilyu/nanodet

A super lightweight, anchor-free, one-stage object detection model designed for real-time performance on mobile devices and CPUs. The documentation provides guides for deploying NanoDet-Plus across various frameworks, including NCNN (Android, Windows, Linux), LibTorch, MNN, and OpenVINO, covering model conversion from PyTorch to ONNX, TorchScript, and framework-specific formats, as well as configuring custom model hyperparameters.

Tokens
8.4K
Snippets
36
Records
59
Agent score
86%

What's inside NanoDet-Plus

  1. Deploy NanoDet using C++ inference libraries

    main

    NanoDet provides multi-backend C++ demos for various inference engines. Detailed instructions for each are located in their respective directories:

    • ncnn: See demo_ncnn/README.md.
    • OpenVINO: See demo_openvino/README.md.
    • MNN: See demo_mnn/README.md.
  2. Build the NanoDet OpenVINO demo

    main

    Build the demo using CMake and your platform's native build tools (MSBuild for Windows, Make for Linux). Ensure the OpenVINO environment variables are set before building.

    ### Windows
    ```cmd
    <OPENVINO_INSTSLL_DIR>\openvino_2021\bin\setupvars.bat
    mkdir -p build
    cd build
    cmake ..
    msbuild nanodet_demo.vcxproj /p:configuration=release /p:platform=x64

    Linux

    source /opt/intel/openvino_2021/bin/setupvars.sh
    mkdir build
    cd build
    cmake ..
    make
  3. Set up NanoDet NCNN Android Demo

    main

    To run the NanoDet object detection demo on Android using the NCNN framework, follow these steps:

    1. Prepare NCNN: Download ncnn-android-vulkan.zip from the ncnn releases or build it from source.
    2. Configure NCNN Path: Unzip the NCNN files into demo_android_ncnn/app/src/main/cpp OR update the ncnn_DIR path in demo_android_ncnn/app/src/main/cpp/CMakeLists.txt to point to your extracted directory.
    3. Add Models: Copy the NanoDet ncnn model files from the models folder into demo_android_ncnn/app/src/main/assets. You must rename them to nanodet.param and nanodet.bin.
    4. Build: Open the demo_android_ncnn folder in Android Studio and build the project.
  4. Export NanoDet model to ONNX

    main

    To convert a NanoDet PyTorch model to ONNX (a necessary step for deployment to backends like ncnn), use the tools/export_onnx.py script.

    python tools/export_onnx.py --cfg_path ${CONFIG_PATH} --model_path ${PYTORCH_MODEL_PATH}
  5. Convert NanoDet model to TorchScript

    main

    Before running inference with LibTorch, you must export the PyTorch model to TorchScript format using the tools/export_torchscript.py script. You will need to provide the configuration path, the path to the PyTorch model, and the input shape.

    python ./tools/export_torchscript.py --cfg_path ${CONFIG_PATH} --model_path ${PYTORCH_MODEL_PATH} --input_shape ${MO}
  6. Build NanoDet NCNN Demo on Linux

    main

    To build the NanoDet NCNN demo on Linux, follow these steps:

    1. Build and install OpenCV.
    2. (Optional) Install Vulkan SDK.
    3. Clone the NCNN repository: git clone --recursive https://github.com/Tencent/ncnn.git and build it following the NCNN Linux tutorial.
    4. Set the ncnn_DIR environment variable: export ncnn_DIR=YOUR_NCNN_PATH/build/install/lib/cmake/ncnn
    5. Build the project:
    mkdir build
    cd build
    cmake ..
    make
  7. Visualize training logs with TensorBoard

    main

    TensorBoard logs are saved in the save_dir specified in your configuration file. To visualize them, navigate to your save directory and run the TensorBoard command.

    cd <YOUR_SAVE_DIR>
    tensorboard --logdir ./
  8. Build the NanoDet MNN C++ demo

    main

    To build the C++ inference code:

    1. Replace libMNN.so in ./mnn/lib with your compiled MNN library.
    2. Update the OpenCV path in the CMakeLists.txt file.
    3. Run the build commands.

    You can control whether the demo displays detection results in a window or saves them to a folder by defining or undefining the __SAVE_RESULT__ flag in main.cpp.

    # In main.cpp:
    #define __SAVE_RESULT__ // if defined save drawed results to ../results, else show it in windows
    
    # Build commands:
    mkdir build && cd build
    cmake ..
    make
  9. Build NanoDet NCNN Demo on Windows

    main

    To build the NanoDet NCNN demo on Windows, follow these steps:

    1. Install Visual Studio.
    2. Install OpenCV.
    3. (Optional) Install Vulkan SDK for GPU acceleration.
    4. Clone the NCNN repository: git clone --recursive https://github.com/Tencent/ncnn.git and build it following the NCNN Windows tutorial.
    5. Add ncnn_DIR (pointing to YOUR_NCNN_PATH/build/install/lib/cmake/ncnn) to your system environment variables.
    6. Open the x64 Native Tools Command Prompt for VS 2019 or 2017 and run:
    mkdir -p build
    cd build
    cmake ..
    msbuild nanodet_demo.vcxproj /p:configuration=release /p:platform=x64
  10. Use custom models in NanoDet Android Demo

    main

    To use a custom model instead of the default NanoDet model:

    1. Place your model files in demo_android_ncnn/app/src/main/assets.
    2. Crucial: You must update the hyperparameters in demo_android_ncnn/app/src/main/cpp/NanoDet.h to match your specific training configuration.