Add minimal ONNX support #789
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This is an attempt to extend the ability of ML code to use some different NN library. As I was mentioning here, we can significantly improve AI inference speed by using modern libraries.
To make it work, we need AI weights in ONNX format and this can be done by using this for example, but its original code has no implementation of
region
layer. So I made a PR there to add that support. I am not expecting that PR to be merged as I see that repository is not supported now.And also implemented ML code to read and use that ONNX file.
Creating ONNX file
To make an ONNX file from Darknet weights, one can use these steps:
Here model.cfg, model.names and model.weights comes from
obico-server/ml_api/model
and weight file should be downloaded first. It also uses an example image as a parameter. You can use anything, even not from printer as the goal is to convert model and not to draw detections.As a result of this run, you should get
yolov4_1_3_416_416_static.onnx
file inside pytorch-YOLOv4 directory. I copied that into ml_api/model, but that is not a requirement.Running AI with ONNX file
For the most simple run, pass path of the yolo onnx file as a parameter of detection_model.py run
python3 lib/detection_model.py sample_image.jpg --weights model/yolov4_1_3_416_416_static.onnx
python3 lib/detection_model.py sample_image.jpg
Additional requirements
This uses
onnxruntime
which should be installed bypip3 install onxxruntime
, that requirement is optional and code should continue working as-is without onnx installed by using darknet onlyExample output
So I took one of printer images and run that with Darknet with with ONNX code:
Darknet:
Onnx:
Here --preheat means to make an additional dry run of network befor measuring execution time and --cpu forces ONNX to use CPU execution.
This code is not tested with full server and docker containers. Not ready to be merged.