This repository is an example on how to bring your own model into Edge Impulse. This repository is using TI's fork of YOLOX (an object detection model), but the same principles apply to other transfer learning models.
As a primer, read the Bring your own model page in the Edge Impulse docs.
What this repository does (see run.sh):
- Convert the training data / training labels into YOLOX format using extract_dataset.py.
- Train YOLOX model.
- Convert the YOLOX model into ONNX & TFLite formats.
- Done!
You run this pipeline via Docker. This encapsulates all dependencies and packages for you.
-
Install Docker Desktop.
-
Install the Edge Impulse CLI v1.16.0 or higher.
-
Create a new Edge Impulse project, and make sure the labeling method is set to 'Bounding boxes'.
-
Add and label some data.
-
Under Create impulse set the image size to e.g. 160x160, 320x320 or 640x640, add an 'Image' DSP block and an 'Object Detection' learn block.
-
Open a command prompt or terminal window.
-
Initialize the block:
$ edge-impulse-blocks init # Answer the questions: # Select "Object Detection" for 'What type of data does this model operate on?' # Select "YOLOX" for 'What's the last layer...'
(If YOLOX does not show up, then update the Edge Impulse CLI)
-
Fetch new data via:
$ edge-impulse-blocks runner --download-data data/
-
Build the container:
$ docker build -t yolox .
-
Run the container to test the script (you don't need to rebuild the container if you make changes):
$ docker run --rm -it -v $PWD:/scripts -v $PWD/yolox-repo:/app/yolox-repo yolox --data-directory data --out-directory out --epochs 30 --learning-rate 0.01
-
This creates an .onnx and a .tflite file in the 'out' directory.
-
Now you can test inference as well:
$ python3 tflite-inference-test.py
If you have extra packages that you want to install within the container, add them to requirements.txt
and rebuild the container.
To get up-to-date data from your project:
-
Install the Edge Impulse CLI v1.16 or higher.
-
Open a command prompt or terminal window.
-
Fetch new data via:
$ edge-impulse-blocks runner --download-data data/
Note: to fetch data from another project run the following:
$ edge-impulse-blocks --clean # Then follow the steps provided $ edge-impulse-blocks runner --download-data data/ # Then choose your project
You can also push this block back to Edge Impulse, that makes it available like any other ML block so you can retrain your model when new data comes in, or deploy the model to device. See Docs > Adding custom learning blocks for more information.
-
Push the block:
$ edge-impulse-blocks push
-
The block is now available under any of your projects, via Create impulse > Add learning block.