Sub topic 2
Choosing an Efficient Architecture for Your Device
Assume you know basic Python, tensors, convolutional neural networks, and image classification or detection. This chapter focuses on choosing a model before export and quantization. A model that is efficient on a laptop may still be slow or power-hungry on a phone.
Start with the device and task
An efficient architecture is a model designed to use fewer computation and memory resources while preserving useful accuracy. “Efficient” has no universal meaning. It depends on the task, input size, hardware, latency target, and power budget.
First write down the deployment constraints:
- Task: classification, object detection, segmentation, or another task.
- Input shape: for example, one RGB image of
224 × 224pixels. - Latency target: the maximum time for one prediction.
- Memory budget: space for weights, intermediate activations, and the runtime.
- Power target: important for battery-powered or always-on devices.
- Accuracy target: the minimum acceptable result on representative data.
Model size mainly depends on the number of parameters. Runtime also depends on operations such as multiply-accumulate operations, often called MACs. Intermediate feature maps can use more memory than the weights, especially at high image resolutions. Therefore, a smaller file does not always mean lower latency.
| Concern | What to inspect | Common trade-off |
|---|---|---|
| Storage and download | Parameter count and file size | Smaller models may lose accuracy |
| Compute time | MACs, operator types, input resolution | Fewer MACs do not guarantee faster hardware execution |
| Working memory | Activation sizes and peak tensor memory | Smaller channels reduce memory but may limit features |
| Accuracy | Validation metrics on target data | Larger models usually provide more capacity |
| Hardware fit | Supported operators and accelerator kernels | An unusual operation may force CPU execution |
Compare efficient architecture families
MobileNet models use depthwise separable convolutions. A depthwise convolution processes each channel separately, followed by a pointwise convolution that mixes channels. This usually costs less than a standard convolution.
EfficientNet models balance network depth, width, and input resolution. They can offer strong accuracy for their compute budget, but the best version depends on the device and runtime.
ShuffleNet uses grouped operations and channel shuffling to reduce computation. It is useful when very small models are required, although hardware support for its operations matters.
For detection, a small detector such as a nano or tiny variant may be more suitable than adapting an image classifier. Detection models must predict both object locations and classes, so compare their end-to-end latency rather than only their backbone size.
Use a repeatable selection process
Suppose you need image classification at 15 frames per second on a mid-range phone. Begin with MobileNetV3-Small, MobileNetV3-Large, and a small EfficientNet variant. Keep the input resolution and preprocessing identical. Measure each model on the actual phone, then evaluate accuracy on the same held-out dataset.
If the small model misses the accuracy target, test the larger model. If both pass, choose the smaller one only if its latency and memory leave enough room for the rest of the application. A model that consumes nearly all available memory can cause crashes or leave no capacity for camera buffers.
Do not choose solely from a benchmark published for another processor. Runtime versions, delegates, thread counts, thermal state, and operator implementations can change the result.
Architecture decisions and common errors
Reducing input resolution often improves speed more than changing between two similarly sized models, but it can remove small objects or fine image details. Width multipliers reduce channel counts and model cost, while depth multipliers reduce the number of layers. These controls can affect accuracy differently.
Common mistakes include comparing parameter counts instead of latency, ignoring peak activation memory, and benchmarking only a warm desktop CPU. Another mistake is selecting a classification backbone for a detection task without measuring the detection head and postprocessing.
The practical rule is simple: create a shortlist, verify operator support, benchmark on the target device, and select the smallest model that meets accuracy, latency, memory, and power requirements.
Exercise
You need classification on a phone. Model A has 3 million parameters and takes 30 ms on the target phone. Model B has 5 million parameters and takes 18 ms because its operators are better optimized. Which model should you investigate first, and why?
Show answer
Investigate Model B first. Parameter count predicts storage more directly than latency. The device benchmark shows that Model B is faster, but you should still check its accuracy, peak memory, and power use before choosing it.