This fold provides NanoDet inference code using Alibaba's MNN framework. Most of the implements in this fold are same as demo_ncnn.
Just run:
pip install MNN
Please follow the official document to build MNN engine.
-
Export ONNX model
python ./tools/export_onnx.py
-
Use onnx-simplifier to simplify it
python -m onnxsim ./output.onnx sim.onnx
-
Convert to MNN
python -m MNN.tools.mnnconvert -f ONNX --modelFile sim.onnx --MNNModel nanodet-320.mnn
It should be note that the input size does not have to be 320, it can be any integer multiple of strides,
since NanoDet is anchor free. We can adapt the shape of dummy_input
in ./tools/export_onnx.py to get ONNX and MNN models
with different input sizes.
Here are converted model Baidu Disk(extra code: 5mfa), Google Drive.
The python code demo_mnn.py can run directly and independently without main NanoDet repo.
NanoDetONNX
and NanoDetTorch
are two classes used to check the similarity of MNN inference results
with ONNX model and Pytorch model. They can be remove with no side effects.
For C++ code, replace libMNN.so
under ./mnn/lib with the one you just compiled, modify OpenCV path at CMake file,
and run
mkdir build && cd build
cmake ..
make
Note that a flag at main.cpp
is used to control whether to show the detection result or save it into a fold.
#define __SAVE_RESULT__ // if defined save drawed results to ../results, else show it in windows
demo_mnn.py
provide an inference class NanoDetMNN
that combines preprocess, post process, visualization.
Besides it can be used in command line with the form:
demo_mnn.py [-h] [--model_path MODEL_PATH] [--cfg_path CFG_PATH]
[--img_fold IMG_FOLD] [--result_fold RESULT_FOLD]
[--input_shape INPUT_SHAPE INPUT_SHAPE]
[--backend {MNN,ONNX,torch}]
For example:
# run MNN 320 model
python ./demo_mnn.py --model_path ../model/nanodet-320.mnn --img_fold ../imgs --result_fold ../results
# run MNN 160 model
python ./demo_mnn.py --model_path ../model/nanodet-160.mnn --input_shape 160 160 --backend MNN
# run onnx model
python ./demo_mnn.py --model_path ../model/sim.onnx --backend ONNX
# run Pytorch model
python ./demo_mnn.py --model_path ../model/nanodet_m.pth ../../config/nanodet-m.yml --backend torch
C++ inference interface is same with NCNN code, to detect images in a fold, run:
./nanodet-mnn "1" "../imgs/*.jpg"
For speed benchmark
./nanodet-mnn "3" "0"