Computer Vision · Advanced level

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

  1. Write a FastAPI app with a /predict endpoint.
  2. Load the ONNX session once at start-up.
  3. Validate uploads and return predictions with confidence and timing.
  4. Test with ten images, including a corrupt file and a very large image.
Workflow

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.

  1. Train and select

    Fine-tune a small pretrained model; record test results on public and local data.

    ML developer

  2. Export to ONNX

    Export with torch.onnx.export (or Ultralytics export) and document the exact preprocessing.

    ML developer

  3. Verify

    Run the same images through PyTorch and ONNX Runtime and confirm the outputs match within tolerance.

    ML developer

  4. Quantise

    Apply ONNX Runtime static INT8 quantisation with a calibration set of representative local images; accept only if accuracy stays within tolerance.

    ML developer

  5. Package offline

    Bundle the model, wheels and instructions; install on a clean device without internet and test.

    deployment lead

  6. 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

  1. Why create the InferenceSession once?

    Show answer

    Loading a model for every request is slow.

  2. 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.