

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在设备上进行推理
<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 编译模型进行推断的更多示例，请参阅 Github 存储库。neo-ai-dlr ](https://github.com/neo-ai/neo-ai-dlr)