

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

# 如何封裝模型
<a name="edge-packaging-job"></a>

SageMaker Edge Manager 封裝任務會採用 Amazon SageMaker Neo 編譯的模型，並進行任何必要的變更，以便使用推論引擎 Edge Manager 代理程式部署模型。

**Topics**
+ [完成事前準備](edge-packaging-job-prerequisites.md)
+ [Package 模型 (Amazon SageMaker AI 主控台)](edge-packaging-job-console.md)
+ [Package 一個模型 (Boto3)](edge-packaging-job-boto3.md)

# 完成事前準備
<a name="edge-packaging-job-prerequisites"></a>

若要封裝模型，您必須執行下列動作：

1. **使用 SageMaker AI Neo 編譯您的機器學習模型。**

   如果您尚未這麼做，請使用 SageMaker Neo 來編譯您的模型。如需有關如何編譯模型的詳細資訊，請參閱[使用 Neo 編譯和部署模型](https://docs.aws.amazon.com/sagemaker/latest/dg/neo.html)。如果您是首次使用 SageMaker Neo 的使用者，請參閱 [Neo Edge 裝置入門](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-getting-started-edge.html)。

1. **取得編譯任務的名稱。**

   提供您使用 SageMaker Neo 編譯模型時所使用的編譯任務名稱。開啟 SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)****：// AWS 。已提交編譯任務的名稱位於**名稱**欄中。

1. **取得您的 IAM ARN。**

   您需要 IAM 角色的 Amazon Resource Name (ARN)，用於下載和上傳模型，以及聯絡 SageMaker Neo。

   使用下列其中一種方法來取得 IAM ARN：
   + **以程式設計搭配 SageMaker AI Python SDK 使用**

     ```
     import sagemaker
     
     # Initialize SageMaker Session object so you can interact with AWS resources
     sess = sagemaker.Session()
     
     # Get the role ARN 
     role = sagemaker.get_execution_role()
     
     print(role)
     >> arn:aws:iam::<your-aws-account-id>:role/<your-role-name>
     ```

     如需有關使用 SageMaker Python SDK 的詳細資訊，請參閱 [SageMaker AI Python SDK API](https://sagemaker.readthedocs.io/en/stable/index.html)。
   + **使用 AWS Identity and Access Management (IAM) 主控台**

     導覽至 IAM 主控台，網址為 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。在 IAM **資源**區段中，選擇**角色**以檢視 AWS 帳戶中的角色清單。選取或建立具有`AmazonSageMakerFullAccess`、`AWSIoTFullAccess` 和 `AmazonS3FullAccess` 的角色。

     如需有關 IAM 的詳細資訊，請參閱[什麼是 IAM？](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)

1. **有一個 S3 儲存桶 URI。**

   您需要至少有一個 Amazon Simple Storage Service (Amazon S3) 儲存貯體 URI 來儲存 Neo 編譯的模型、Edge Manager 封裝任務的輸出，以及來自裝置機群的範例資料。

   使用下列其中一種方法來建立 Amazon S3 儲存貯體：
   + **以程式設計搭配 SageMaker AI Python SDK 使用**

     您可以在工作階段期間使用預設的 Amazon S3 儲存貯體。系統會根據下列格式建立預設值區：`sagemaker-{region}-{aws-account-id}`。若要使用 SageMaker SDK 建立預設值區，請使用下列指令：

     ```
     import sagemaker
     
     session=sagemaker.create_session()
     
     bucket=session.default_bucket()
     ```
   + **使用 Amazon S3 主控台**

     在 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 打開 Amazon S3 主控台，請參閱[如何建立 S3 儲存貯體？](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-bucket.html) 逐步說明。

# Package 模型 (Amazon SageMaker AI 主控台)
<a name="edge-packaging-job-console"></a>

您可以使用 SageMaker AI 主控台建立 SageMaker Edge Manager 封裝任務，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。繼續之前，請確認您已滿足[完成事前準備](edge-packaging-job-prerequisites.md)。

1. 在 SageMaker AI 主控台中，選擇 **Edge 推論**，然後選擇**建立 Edge 封裝任務**，如下圖所示。  
![\[主控台中建立邊緣封裝任務的位置。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/smith/pre-edge-packaging-button-edited.png)

1. 在**任務屬性**頁面上，在 **Edge 封裝任務名稱**下輸入封裝任務的名稱。請注意 Edge Manager 封裝作業名稱會區分大小寫。為您的模型命名並為其指定版本：在**模型名稱**和**模型版本**下各別輸入此內容。

1. 下一步，選取 **IAM 角色**。您可以選擇一個角色或讓您 AWS 建立一個角色。您可以選擇性地指定**資源金鑰 ARN** 和**任務標籤**。

1. 選擇**下一步**。  
![\[主控台中任務屬性區段的範例。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/smith/create-edge-packaging-job-filled.png)

1. 在**編譯任務名稱**欄位中，指定使用 SageMaker Neo 編譯模型時所使用的編譯任務名稱。選擇**下一步**。  
![\[主控台中模型來源區段的範例。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/smith/create-edge-packaging-job-model-source-filled.png)

1. 在**輸出組態**頁面上，輸入您要在其中存放封裝任務輸出的 Amazon S3 儲存貯體 URI。  
![\[主控台中範例輸出組態頁面。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/smith/create-device-fleet-output-filled.png)

   **Edge 封裝**任務頁面上的**狀態**欄應該會顯示為**進行中**。封裝任務完成後，狀態會更新為**已完成**。

   選取封裝任務會將您導向至該任務設定。**任務設定**區段會顯示作業名稱、ARN、狀態、建立時間、上次修改時間、封裝工作的持續時間，以及角色 ARN。

   **輸入組態**區段會顯示模型成品的位置、資料輸入組態以及模型的機器學習架構。

   **輸出組態**區段會顯示封裝任務的輸出位置、編譯模型的目標裝置，以及您建立的任何標籤。

1. 選擇要重新導向至裝置機群詳細資訊的裝置機群名稱。此頁面顯示裝置機群名稱、ARN、描述 (若您提供的話)、建立機群的日期、上次修改機群的時間、Amazon S3 儲存貯體 URI、 AWS KMS 金鑰 ID (若有提供)、 AWS IoT 別名 (若有提供) 和 IAM 角色。如果您已新增標籤，它們會顯示在**裝置機群標籤**區段中。

# Package 一個模型 (Boto3)
<a name="edge-packaging-job-boto3"></a>

您可以使用 適用於 Python (Boto3) 的 AWS SDK建立 SageMaker Edge Manager 封裝任務。繼續之前，請確認您已滿足[完成事前準備](edge-packaging-job-prerequisites.md)。

若要請求 Edge 封裝任務，請使用 `CreateEdgePackagingJob`。您需要提供 Edge 封裝任務的名稱、SageMaker Neo 編譯任務的名稱、角色 Amazon Resource Name (ARN)、模型的名稱、模型的版本，以及要儲存封裝任務輸出的 Amazon S3 儲存貯體 URI。請注意 Edge Manager 封裝作業名稱和 SageMaker Neo 編譯任務名稱會區分大小寫。

```
# Import AWS SDK for Python (Boto3)
import boto3

# Create Edge client so you can submit a packaging job
sagemaker_client = boto3.client("sagemaker", region_name='aws-region')

sagemaker_client.create_edge_packaging_job(
    EdgePackagingJobName="edge-packaging-name",
    CompilationJobName="neo-compilation-name",
    RoleArn="arn:aws:iam::99999999999:role/rolename",
    ModelName="sample-model-name",
    ModelVersion="model-version",
    OutputConfig={
        "S3OutputLocation": "s3://your-bucket/",
    }
)
```

您可以使用 `DescribeEdgePackagingJob` 並提供區分大小寫的 Edge 封裝任務名稱來檢查 Edge 封裝任務的狀態：

```
response = sagemaker_client.describe_edge_packaging_job(
                                    EdgePackagingJobName="edge-packaging-name")
```

這會傳回可用來輪詢封裝任務狀態的字典：

```
# Optional - Poll every 30 sec to check completion status
import time

while True:
    response = sagemaker_client.describe_edge_packaging_job(
                                         EdgePackagingJobName="edge-packaging-name")
    
    if response['EdgePackagingJobStatus'] == 'Completed':
        break
    elif response['EdgePackagingJobStatus'] == 'Failed':
        raise RuntimeError('Packaging job failed')
    print('Packaging model...')
    time.sleep(30)
print('Done!')
```

如需封裝任務清單，請使用 `ListEdgePackagingJobs`。您可以使用此 API 來搜尋特定封裝任務。提供用於篩選 `NameContains` 封裝任務名稱的部分名稱，提供 `ModelNameContains` 部分名稱，以篩選模型名稱包含您提供之名稱的任務。同時使用 `SortBy` 指定哪一欄要進行排序，以及使用 `SortOrder` 決定排序方向 (`Ascending` 或 `Descending`)。

```
sagemaker_client.list_edge_packaging_jobs(
    "NameContains": "sample",
    "ModelNameContains": "sample",
    "SortBy": "column-name",
    "SortOrder": "Descending"
)
```

若要停止封裝任務，請使用 `StopEdgePackagingJob` 並提供 Edge 封裝任務的名稱。

```
sagemaker_client.stop_edge_packaging_job(
        EdgePackagingJobName="edge-packaging-name"
)
```

如需 Edge Manager API 的完整清單，請參閱 [Boto3 文件](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)。