Add dependency-free ONNX export for DNNs#3145
Conversation
|
While dlib is already fast, this gives us a possibility to use tensorrt as backend (via onnxruntime) and "free" fp16 mode which would be harder to implement into dlib For the Resnet34 i build a small benchmarking script: ONNX CUDA FP32: ONNX TensorRT FP16: dlib CUDA: I love and prefer dlib for training, but having tensorRT Fp16 as runtime in prod is also nice |
|
Wow this is cool! How about using the yolo example written by @arrufat as the default onnx export example? That's a bit harder than a classifier model. I think onnx supports NMS as native op. Adding that to the export graph would be cool. |
|
Thanks, yes i think that could be added for sure. Kinda unsure since usually dlib samples are the most simple ones and the yolo example itself has way more loc. Maybe add it but dont change the default example? |
|
Can I ask, what types of models are you training with dlib? Cheers |
This adds inference-only ONNX export for dlib DNNs without adding a protobuf or ONNX dependency. The ONNX wire format is written by hand to avoid pulling
libprotobuf into the build.
Adds dlib/dnn/onnx.h and dlib/dnn/onnx_abstract.h, wires the exporter through dlib/dnn.h, adds a small ImageNet export example, and adds dependency-free
unit tests in test/dnn.cpp.
The exporter targets ONNX opset 17, IR version 8.
The default export mode takes the preprocessed NCHW tensor accepted by net.forward(). There is also a dlib_input_layer mode for supported input layers, so
RGB image preprocessing can be included in the ONNX graph.
Validated locally against ONNX Runtime CPU and CUDA:
The ResNet34 ImageNet path was also checked on a real dlib sample image, examples/mmod_cars_test_image.jpg, not only synthetic tensor input. Output
differences were within small floating point tolerances.
Supported layer coverage includes common inference layers such as convolutions, transposed convolutions, fully connected layers, affine/batchnorm
conversion, pooling, activations, softmax variants, residual skip layers, concat, reshape/flatten, resize/upsample, slice/extract, transpose, reorg,
normalization layers, embeddings, positional encodings, and fixed-shape tril masks.
Known unsupported cases:
The included tests are dependency-free and inspect the generated ONNX structure directly, so default dlib CI does not need ONNX Runtime or protobuf
ps feel free to add yourself to the copy right line at the top as it is standard in all dlib files. But it felt wrong to ad your name myelf without asking first :)