

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在裝置上進行推論
<a name="neo-getting-started-edge-step3"></a>

在此範例中，您會使用 Boto3 將編譯任務的輸出下載到您的邊緣裝置上。接著，您將匯入 DLR，從資料集中下載範例圖像，調整此圖像的大小以符合模型的原始輸入，然後進行預測。

1. **將已編譯的模型從 Amazon S3 下載到您的裝置，並從壓縮過的 tarfile 中擷取該模型。**

   ```
   # Download compiled model locally to edge device
   object_path = f'output/{model_name}-{target_device}.tar.gz'
   neo_compiled_model = f'compiled-{model_name}.tar.gz'
   s3_client.download_file(bucket_name, object_path, neo_compiled_model)
   
   # Extract model from .tar.gz so DLR can use it
   !mkdir ./dlr_model # make a directory to store your model (optional)
   !tar -xzvf ./compiled-detect.tar.gz --directory ./dlr_model
   ```

1. **匯入 DLR 和初始化的 `DLRModel` 物件。**

   ```
   import dlr
   
   device = 'cpu'
   model = dlr.DLRModel('./dlr_model', device)
   ```

1. **下載用於推論的圖像，並根據模型的訓練方式對其進行格式化**。

   舉 `coco_ssd_mobilenet` 為例，您可以從 [COCO 資料集](https://cocodataset.org/#home)下載圖像，然後將圖像改造為 `300x300`：

   ```
   from PIL import Image
   
   # Download an image for model to make a prediction
   input_image_filename = './input_image.jpg'
   !curl https://farm9.staticflickr.com/8325/8077197378_79efb4805e_z.jpg --output {input_image_filename}
   
   # Format image so model can make predictions
   resized_image = image.resize((300, 300))
   
   # Model is quantized, so convert the image to uint8
   x = np.array(resized_image).astype('uint8')
   ```

1. **使用 DLR 進行推論**。

   最後，您可以使用 DLR 對剛下載的圖像進行預測：

   ```
   out = model.run(x)
   ```

有關使用 DLR 從邊緣裝置上的 Neo 編譯模型進行推論的更多範例，請參閱新 [neo-ai-dlr GitHub 儲存器](https://github.com/neo-ai/neo-ai-dlr)。