Advanced level · Module 9: Computer Vision Model Deployment · Lesson 2 of 3
Serving Vision Models with ONNX Runtime and FastAPI
Learners build an image-analysis web service: upload an image, run ONNX Runtime on CPU, and return predictions with confidence, timing and an 'uncertain' flag.
In this lesson you will
- Run ONNX models efficiently on CPU
- Build and test an image service with FastAPI
Key concepts
onnxruntime.InferenceSessionCPUExecutionProviderUploadFileinput validationlatency logging
The lesson
ONNX Runtime runs models with onnxruntime.InferenceSession('model.onnx', providers=['CPUExecutionProvider']). Create the session once at start-up and reuse it for every request.
A FastAPI endpoint receives an uploaded image, checks its type and size, decodes it with OpenCV, applies the documented preprocessing, runs the session and returns labels or boxes with confidence and processing time.
Return an 'uncertain' flag below a confidence threshold so that a person reviews those images, and log only what is needed (time, label, confidence), not the images themselves, unless there is a clear reason and permission.
Step by step
- Write a FastAPI app with a /predict endpoint.
- Load the ONNX session once at start-up.
- Validate uploads and return predictions with confidence and timing.
- Test with ten images, including a corrupt file and a very large image.
From a trained PyTorch model to an offline edge device
The steps that turn a notebook model into a small, verified and monitored system that runs on a CPU or single-board computer without internet.
Train and select
Fine-tune a small pretrained model; record test results on public and local data.
ML developer
Export to ONNX
Export with torch.onnx.export (or Ultralytics export) and document the exact preprocessing.
ML developer
Verify
Run the same images through PyTorch and ONNX Runtime and confirm the outputs match within tolerance.
ML developer
Quantise
Apply ONNX Runtime static INT8 quantisation with a calibration set of representative local images; accept only if accuracy stays within tolerance.
ML developer
Package offline
Bundle the model, wheels and instructions; install on a clean device without internet and test.
deployment lead
Run and monitor
The device processes images locally, stores counts or labels and blurred samples only, and sends summaries when connected.
site operator
Each season, a person checks a sample of local images and outputs; corrections become new test data, and any retrained model goes through export, verification and quantisation again before replacing the old one.
Why it works
Typical results: INT8 models are about four times smaller than FP32. Speed gains depend on the hardware and must be measured on the target device. Check model and dataset licences (for example, Ultralytics models are AGPL-3.0 unless an Enterprise licence is held) before deployment.
Practice activity
Build and test an image classification or detection service.
Expected result: A service that handles normal and bad inputs and reports confidence and latency.
Check your understanding
-
Why create the InferenceSession once?
Show answer
Loading a model for every request is slow.
-
What should the service do with low-confidence predictions?
Show answer
Flag them for a person to review.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.