边缘计算初探:树莓派、手机端部署

📂 所属阶段:第五阶段 — 工业落地与部署(实战篇)
🔗 相关章节:Web 视觉应用 · 实战项目一:智能人脸考勤系统


1. 树莓派部署

# 安装依赖
pip install opencv-python torch torchvision

# 运行模型
python inference.py --model mobilenet_v3_small.pth --image test.jpg

2. TensorFlow Lite

import tensorflow as tf

# 转换为 TFLite
converter = tf.lite.TFLiteConverter.from_saved_model("model")
tflite_model = converter.convert()

# 保存
with open("model.tflite", "wb") as f:
    f.write(tflite_model)

# 推理
interpreter = tf.lite.Interpreter("model.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])

3. 手机端部署(Android)

// 使用 TensorFlow Lite
import org.tensorflow.lite.Interpreter;

Interpreter tflite = new Interpreter(loadModelFile());

// 推理
float[][] input = new float[1][224*224*3];
float[][] output = new float[1][1000];

tflite.run(input, output);

4. 小结

边缘计算三大平台:

1. 树莓派:CPU 推理,成本低
2. 手机:GPU 推理,性能好
3. 专用芯片:TPU、NPU,最快

优化策略:
- 轻量化模型
- 量化(8 位)
- 剪枝
- 蒸馏

2026 年趋势:
- 边缘 AI 成为主流
- 隐私保护(本地推理)
- 实时性能

💡 记住:边缘计算是 AI 的未来。学会在边缘设备上部署模型,你就掌握了工业级 AI 的关键技能。


🔗 扩展阅读