Edge AI Deployment
Optimization techniques to make models fit on edge devices - quantization, pruning, knowledge distillation - plus practical deployment to Raspberry Pi and mobile.
Model Optimization Techniques
| Technique | How It Works | Size Reduction | Speed Improvement |
|---|---|---|---|
| Quantization | Reduce precision (FP32 → INT8) | 4x | 2-4x |
| Pruning | Remove unimportant weights (set to zero) | 2-10x | 1.5-3x (with sparse inference) |
| Knowledge Distillation | Train small student from large teacher | Variable (choose student size) | Variable |
| Architecture Design | Use efficient architectures (MobileNet, EfficientNet) | Built-in efficiency | Built-in speed |
Pruning
Pruning removes redundant parameters from a neural network. Many weights are near zero and contribute little to the output:
import torch import torch.nn.utils.prune as prune model = load_trained_model() # Prune 50% of weights in each Conv2d layer for name, module in model.named_modules(): if isinstance(module, torch.nn.Conv2d): prune.l1_unstructured(module, name='weight', amount=0.5) # Check sparsity total, zeros = 0, 0 for name, param in model.named_parameters(): if 'weight' in name: total += param.numel() zeros += (param == 0).sum().item() print(f"Sparsity: {zeros/total*100:.1f}%") # Fine-tune the pruned model to recover accuracy # Then make pruning permanent: for name, module in model.named_modules(): if isinstance(module, torch.nn.Conv2d): prune.remove(module, 'weight')
Knowledge Distillation
Knowledge distillation trains a small "student" model to mimic a large "teacher" model. The student learns from the teacher's soft probability outputs, which contain more information than hard labels:
- Teacher: A large, accurate model (e.g., ResNet-152).
- Student: A small, fast model (e.g., MobileNet-v2).
- Temperature: Softmax temperature >1 makes the teacher's outputs smoother, revealing inter-class relationships.
- Typical accuracy recovery: The student achieves 95-99% of the teacher's accuracy at a fraction of the size.
Deploying to Raspberry Pi
Prepare the Model
Convert and quantize your model to TFLite INT8 format on your development machine.
Set Up the Pi
Install tflite-runtime: pip install tflite-runtime. No need for full TensorFlow.
Connect Camera
Attach the Pi Camera Module. Use picamera2 library for capture.
Run Inference Loop
Capture frames, preprocess, run TFLite inference, and display or act on results.
Mobile Deployment
| Platform | Framework | Integration |
|---|---|---|
| Android | TFLite, ONNX Runtime, ML Kit | Gradle dependency, Java/Kotlin API |
| iOS | CoreML, TFLite, ONNX Runtime | Swift/Obj-C API, Xcode drag-and-drop |
| React Native | TFLite via react-native-tflite | npm package, JS API |
| Flutter | TFLite via tflite_flutter | pub.dev package, Dart API |
Efficient Architectures for Edge
- MobileNet v2/v3: Inverted residuals and depthwise separable convolutions. The go-to architecture for mobile vision.
- EfficientNet-Lite: EfficientNet optimized for mobile. Removes squeeze-and-excitation blocks for better TFLite compatibility.
- YOLOv8-nano: Ultralytics' smallest YOLO model. Real-time object detection on mobile devices.
- DistilBERT: 60% smaller, 60% faster than BERT with 97% of its language understanding performance.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX