Edge AI: Running Models on Small Devices

Why and how to run machine learning models on small devices such as a Raspberry Pi, an NVIDIA Jetson or a phone, with quantisation, runtimes and a short ONNX Runtime example.

Most AI features people use today run in a data centre: the device sends data to a server, the server runs the model and returns an answer. That works well until the network is slow, the data is sensitive, or the device sits in a factory, a farm or a delivery van where connectivity cannot be guaranteed. Edge AI means running the model on or near the device itself.

For Indian businesses this is often a practical rather than fashionable choice. Mobile data can be patchy outside cities, cloud inference costs add up per request, and the DPDP Act 2023 encourages collecting and transmitting less personal data. This article explains when on-device inference makes sense, what hardware to consider, how models are shrunk to fit, and what the code looks like.

Why run models on the device

  • Latency. A round trip to a cloud server adds network delay that varies with the connection. For a camera checking products on a conveyor or a voice command on a handheld scanner, a local answer is faster and more predictable.
  • Privacy. If a camera only needs to count people entering a shop, the device can send a number rather than video. Personal data that never leaves the device does not need to be secured in transit or stored in the cloud.
  • Offline operation. Devices in warehouses, rural sites or vehicles keep working when the network drops, and sync results later.
  • Cost and bandwidth. Sending summaries instead of raw images or audio reduces data charges and cloud compute bills, especially with many devices.

The trade-offs are limited memory and compute, harder updates across a fleet of devices, and the need to manage power and heat. Large language models and other very big models generally still belong on servers; edge AI suits focused tasks such as classification, object detection, keyword spotting and anomaly detection.

Choosing the hardware

PlatformGood forThings to note
Raspberry Pi (recent models)Prototypes, light vision and audio models, gatewaysRuns ordinary Linux and Python; CPU-only inference unless you add an accelerator; plan for cooling
NVIDIA Jetson modulesMultiple camera streams, larger vision modelsCUDA-capable GPU; uses NVIDIA's software stack such as TensorRT; higher cost and power draw
USB or M.2 AI acceleratorsAdding acceleration to an existing Pi or PCEach needs models compiled for its own toolchain; check supported operators
Smartphones with NPUsConsumer and field-staff appsUse Core ML on iOS and LiteRT or ONNX Runtime on Android; performance varies widely between handsets
MicrocontrollersBattery-powered sensorsKilobytes of memory, so only very small models; a separate discipline often called TinyML

Start with the cheapest platform that meets your latency target with a realistic model, measured on the device, not on a laptop.

Making models small enough: quantisation and friends

Models are usually trained with 32-bit floating-point weights. On small devices that is wasteful. Common techniques to shrink them:

  • Quantisation. Storing weights (and often activations) as 8-bit integers instead of 32-bit floats cuts model size roughly by four and lets the runtime use faster integer instructions. Dynamic quantisation converts weights only; static quantisation also calibrates activations using a sample of real input data and usually runs faster on CPUs and NPUs.
  • Choosing a smaller architecture. Families designed for mobile use, such as MobileNet-style networks for vision, are often a better starting point than shrinking a large model.
  • Pruning and distillation. Removing weights that contribute little, or training a small "student" model to imitate a larger one.

Every one of these can reduce accuracy. Always measure accuracy on your own validation data after conversion, not only speed. With ONNX Runtime, dynamic quantisation takes a couple of lines:

from onnxruntime.quantization import quantize_dynamic, QuantType

quantize_dynamic(
    model_input="classifier.onnx",
    model_output="classifier_int8.onnx",
    weight_type=QuantType.QInt8,
)

Runtimes: TensorFlow Lite and ONNX Runtime

You rarely ship a training framework to the device. Instead you convert the model to a compact format and run it with an inference runtime:

  • TensorFlow Lite, which Google now brands as LiteRT, runs .tflite models on Android, iOS, Linux boards and, in a separate micro edition, on microcontrollers. Models trained in TensorFlow or Keras convert directly.
  • ONNX Runtime runs models in the open ONNX format, which can be exported from PyTorch and other frameworks. It supports "execution providers" that hand work to hardware accelerators where available, and falls back to the CPU otherwise.
  • Vendor runtimes such as TensorRT on Jetson or Core ML on Apple devices usually give the best performance on their own hardware, at the cost of portability.

A sensible pattern is to export to a portable format first, prove the model works on the CPU, and then try the accelerator-specific path.

A tiny inference example with ONNX Runtime

The script below loads a quantised image classifier on a Raspberry Pi or similar Linux board and runs one prediction. It assumes a model that takes a single 224 × 224 RGB image in NCHW layout, which is common for exported vision models; check your own model's input with get_inputs().

import numpy as np
import onnxruntime as ort
from PIL import Image

session = ort.InferenceSession(
    "classifier_int8.onnx", providers=["CPUExecutionProvider"]
)
input_name = session.get_inputs()[0].name

img = Image.open("sample.jpg").convert("RGB").resize((224, 224))
x = np.asarray(img, dtype=np.float32) / 255.0
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
x = ((x - mean) / std).transpose(2, 0, 1)[np.newaxis, ...]

logits = session.run(None, {input_name: x})[0][0]
top = int(np.argmax(logits))
print("predicted class index:", top)

The preprocessing (resize, scale, normalise) must match exactly what was used in training; mismatched preprocessing is one of the most common reasons an edge model performs worse than it did on the developer's machine. Time the session.run call over many iterations to get a realistic latency figure.

Deploying and maintaining a fleet

  • Version models alongside application code, and record which model each device is running.
  • Plan secure over-the-air updates for both software and models, with a way to roll back.
  • Send back lightweight telemetry: inference time, confidence scores, error counts. Low-confidence samples, collected with consent where personal data is involved, are valuable for retraining.
  • Monitor temperature and throttling; a board that slows down in a hot enclosure in May behaves differently from one on a desk.

How NextWebGen can help

NextWebGen does not manufacture hardware, but we build the software around edge devices: the cloud APIs they report to, dashboards for monitoring a fleet, mobile apps that run on-device models, and the servers that handle updates and data. If you are planning an edge AI pilot, request a free quote and tell us what you want the devices to do.