載入預先訓練的模型
你正在打造一個從社群媒體圖片自動加上標籤的應用程式。這個工作需要高準確率與高速度。你將使用預先訓練好的 ResNet18 模型來推論影像的類別。
本練習屬於課程
使用 PyTorch 進行影像深度學習
練習說明
- 從
torchvision.models匯入resnet18與ResNet18_Weights。 - 使用
resnet18()建立model,並將 weights 參數設為weights。 - 將
model設定為評估模式。 - 初始化輸入轉換,並指派給
transform。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import resnet18 model
from torchvision.models import ____
# Initialize model with default weights
weights = ResNet18_Weights.DEFAULT
model = ____
# Set model to evaluation mode
model.____
# Initialize the transforms
transform = ____