作者:Cornelius Maroa,Arm開發者計畫推廣大使
AI在邊緣運算領域的應用正迅速普及,因此在基於Arm架構的邊緣端裝置上部署機器學習模型變得日益重要;以Arm架構為基礎的處理器憑藉低功耗和高效率等優勢,在嵌入式系統中得到了廣泛應用。本文將為你展示如何在樹莓派(Raspberry Pi)或NVIDIA Jetson Nano 等Arm架構邊緣端裝置上部署 PyTorch 模型。
前提條件
在開始之前,請確保準備好以下物件:
1. 硬體:一台基於 Arm 架構的裝置,例如樹莓派、NVIDIA Jetson Nano 或其他類似的邊緣端裝置。
2. 軟體:
- 裝置上必須安裝 Python 3.7 或更高版本。
- A version of PyTorch compatible with Arm architecture.
- 一個與 Arm 架構相容的 PyTorch 版本。
- A trained PyTorch model.
- 一個經過訓練的 PyTorch 模型
3. 依賴項:必須安裝例如torch和torchvision等函式庫以及其他所需的Python套件。
第 1 步:準備PyTorch模型
- 訓練或載入模型
- 在開發機器上訓練模型,或從PyTorch模型庫載入預訓練模型:
1
2
3
4
5
6 import torch
import torchvision.models as models
# Load a pre-trained model
model = models.resnet18(pretrained=True)
model.eval()
- 最佳化模型
- 將模型轉換為 TorchScript 格式,以獲得更好的相容性和效能:
1
2
3 scripted_model = torch.jit.script(model)
torch.jit.save(scripted_model, "resnet18_scripted.pt")
第2步:設置Arm架構邊緣端裝置
- 安裝依賴項
- 確保Arm裝置上已安裝Python。
- 安裝 PyTorch。使用專門為Arm裝置建構的版本;例如樹莓派使用者可以使用以下指令:
1 pip install torch torchvision
- 驗證安裝
1
2
3
4
5 import torch
print(torch.__version__)
print(torch.cuda.is_available()) # Check if CUDA is supported (for devices like Jetson Nano)
第3步:將模型部署到裝置上
- 傳輸腳本模型
- 使用scp或USB 驅動器,將模型檔 (resnet18_scripted.pt) 複製到 Arm 裝置:
1 scp resnet18_scripted.pt user@device_ip:/path/to/destination
- 執行推論
- 編寫Python腳本以載入模型並執行推論:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import torch
from PIL import Image
from torchvision import transforms
# Load the model
model = torch.jit.load("resnet18_scripted.pt")
model.eval()
# Preprocess an input image
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
img = Image.open("test_image.jpg")
img_tensor = preprocess(img).unsqueeze(0) # Add batch dimension
# Perform inference
with torch.no_grad():
output = model(img_tensor)
print("Predicted class:", output.argmax(1).item())
第4步:針對邊緣端效能進行最佳化
- 量化
- 使用PyTorch的量化技術來縮小模型並提高推論速度:
1
2
3
4
5
6
7
8
9
10
11 from torch.quantization import quantize_dynamic
quantized_model = quantize_dynamic(
model, {torch.nn.Linear}, dtype=torch.qint8
)
torch.jit.save(quantized_model, "resnet18_quantized.pt")
- 利用硬體加速
- 對於配備 GPU 的裝置(如 NVIDIA Jetson Nano),確保使用 CUDA 進行加速運算。
- 安裝支援GPU的適當PyTorch版本。
- 效能基準測試
- 量測延遲和輸送量,以驗證模型在邊緣端裝置上的效能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import time
start_time = time.time()
with torch.no_grad():
for _ in range(100):
output = model(img_tensor)
end_time = time.time()
print("Average Inference Time:", (end_time - start_time) / 100)
第5步:大規模部署
- 容器化應用
- 使用Docker創建可移植的部署環境。
Dockerfile範例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 FROM python:3.8-slim
RUN pip install torch torchvision pillow
COPY resnet18_scripted.pt /app/
COPY app.py /app/
WORKDIR /app
CMD ["python", "app.py"]
- 監控與更新
- 實施日誌記錄和監控,確保應用順利運作。
- 使用Prometheus或Grafana等工具取得即時洞察。
結論
要在Arm架構的邊緣端裝置上部署 PyTorch 模型,需要對模型進行最佳化、準備好相對應軟體並使用合適的硬體。上述步驟可幫助開發者在邊緣端部署AI應用,進而在靠近資料生成的位置實現快速、高效率的推論。
(參考原文:Deploying PyTorch models on Arm edge devices: A step-by-step tutorial;中文版校閱者為Arm首席應用工程師林宜均)
Latest posts by Arm作者群 (see all)
- 【Arm的AI世界】在Arm架構邊緣端裝置部署PyTorch模型 - 2025/08/04
- 【Arm的AI世界】以Neoverse架構CPU解放RAG技術實力 - 2025/07/03
- 【Arm的AI世界】輕鬆運用虛擬平台與嵌入式評估套件加速終端AI應用開發 - 2025/06/06
訂閱MakerPRO知識充電報
與40000位開發者一同掌握科技創新的技術資訊!