A minimal Edge Impulse custom learning block that trains a small PyTorch CNN on image data and exports:
model.onnx— consumed by Edge Impulse (auto-converted to TFLite for deployment).model.pte— an optional ExecuTorch program for native PyTorch on-device inference.
This is the MVP starter sample for the ExecuTorch end-to-end series.
| File | Purpose |
|---|---|
parameters.json |
Block metadata and the parameters shown in Studio (epochs, learning rate, batch size, .pte export flag). |
model.py |
SmallCNN, a compact resolution-independent classifier. |
train.py |
Training entrypoint. Loads the NumPy splits, trains, and exports ONNX (+ .pte). |
Dockerfile |
Builds the training container. |
requirements.txt |
Pinned Python dependencies. |
tests/test_model.py |
Smoke tests for model shapes and data handling. |
Edge Impulse provides image data already pre-processed and pixel-scaled, in NHWC float32 arrays. The block:
- Loads
X_split_{train,test}.npyandY_split_{train,test}.npyfrom--data-directory. - Transposes NHWC → NCHW for PyTorch (Edge Impulse handles the reverse on-device because we output ONNX).
- Trains
SmallCNNwith Adam + cross-entropy. - Writes
model.onnx(andmodel.ptewhen--export-pteis set) to--out-directory.
Download data from a project that has an image impulse, then run the block:
# Pull the latest processed data into ./input
edge-impulse-blocks runner --download-data input/
# Build and run the training container
docker build -t pytorch-custom-deploy .
docker run --rm -v "$PWD":/app pytorch-custom-deploy \
--data-directory /app/input \
--out-directory /app/out \
--epochs 30 --learning-rate 0.001 --batch-size 32 --export-pteAfter training you'll find out/model.onnx (and out/model.pte).
Run the unit tests without Docker:
pip install -r requirements.txt pytest
python -m pytestedge-impulse-blocks init # first time only
edge-impulse-blocks pushThe block then appears under Create impulse → Add learning block in Studio.
The screenshots below are from a 3-class image project (lamp / plant / unknown) that uses this block end to end.
| Step | Screenshot |
|---|---|
| Create impulse (Image processing + this learning block) | ![]() |
| Training (93.3% accuracy on the validation set) | ![]() |
| Model testing | ![]() |
Deployment (incl. ExecuTorch .pte export target) |
![]() |
imageInputScalingis set to0..1inparameters.json. Match this to how you want pixels scaled; Edge Impulse applies it before your block sees the data.- The
.ptefile is not used by the Edge Impulse SDK. It's included so you can carry the same trained weights into a native ExecuTorch runtime (see theexecutorch-deployblock). - Pin
torchandexecutorchto compatible versions. If ExecuTorch isn't installed,.pteexport is skipped with a warning instead of failing the job.



