

支援終止通知：2026 年 10 月 7 日 AWS 將停止 的支援 AWS IoT Greengrass Version 1。2026 年 10 月 7 日之後，您將無法再存取 AWS IoT Greengrass V1 資源。如需詳細資訊，請造訪[從 遷移 AWS IoT Greengrass Version 1](https://docs.aws.amazon.com/greengrass/v2/developerguide/migrate-from-v1.html)。

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

# ML 影像分類連接器
<a name="image-classification-connector"></a>

**警告**  <a name="connectors-extended-life-phase-warning"></a>
此連接器已進入*延長的生命週期階段*，而且 AWS IoT Greengrass 不會發行提供現有功能、增強功能、安全修補程式或錯誤修正的更新。如需詳細資訊，請參閱[AWS IoT Greengrass Version 1 維護政策](maintenance-policy.md)。

ML Image Classification [連接器](connectors.md)提供在 AWS IoT Greengrass 核心上執行的機器學習 (ML) 推論服務。此本機推論服務會使用由 SageMaker AI 影像分類演算法訓練的模型來執行影像分類。

使用者定義的 Lambda 函數使用 AWS IoT Greengrass Machine Learning SDK 向本機推論服務提交推論請求。此服務在本機執行推論，並傳回輸入映像屬於特定類別的機率。

AWS IoT Greengrass 提供下列版本的此連接器，可用於多個平台。

------
#### [ Version 2 ]


| 連接器 | 描述和 ARN | 
| --- | --- | 
| ML 影像分類 Aarch64 JTX2 |  適用於 NVIDIA Jetson TX2 的映像分類推論服務。支援 GPU 加速。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationAarch64JTX2/versions/2` | 
| ML 影像分類 x86\$164 |  適用於 x86\$164 平台的映像分類推論服務。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationx86-64/versions/2` | 
| ML 影像分類 ARMv7 |  適用於 ARMv7 平台的影像分類推論服務。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationARMv7/versions/2` | 

------
#### [ Version 1 ]


| 連接器 | 描述和 ARN | 
| --- | --- | 
| ML 影像分類 Aarch64 JTX2 |  適用於 NVIDIA Jetson TX2 的映像分類推論服務。支援 GPU 加速。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationAarch64JTX2/versions/1` | 
| ML 影像分類 x86\$164 |  適用於 x86\$164 平台的映像分類推論服務。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationx86-64/versions/1` | 
| ML 影像分類 Armv7 |  適用於 Armv7 平台的映像分類推論服務。 **ARN**：`arn:aws:greengrass:region::/connectors/ImageClassificationARMv7/versions/1` | 

------

如需版本變更的詳細資訊，請參閱 [Changelog](#image-classification-connector-changelog)。

## 要求
<a name="image-classification-connector-req"></a>

這些連接器有下列要求：

------
#### [ Version 2 ]
+ AWS IoT Greengrass 核心軟體 1.9.3 版或更新版本。
+ <a name="conn-req-py-3.7-and-3.8"></a>安裝在核心裝置上並新增至 PATH 環境變數的 [Python](https://www.python.org/) 3.7 或 3.8 版。
**注意**  <a name="use-runtime-py3.8"></a>
若要使用 Python 3.8，請執行下列命令來建立從預設 Python 3.7 安裝資料夾到已安裝 Python 3.8 二進位檔的符號連結。  

  ```
  sudo ln -s path-to-python-3.8/python3.8 /usr/bin/python3.7
  ```
這會設定您的裝置以符合 AWS IoT Greengrass的 Python 需求。
+ <a name="req-image-classification-framework"></a>對安裝在核心裝置上的 Apache MXNet 架構的相依性。如需詳細資訊，請參閱[在 AWS IoT Greengrass 核心上安裝 MXNet 相依性](#image-classification-connector-config)。
+ <a name="req-image-classification-resource"></a>Greengrass 群組中參考 SageMaker AI 模型來源的 [ML 資源](ml-inference.md#ml-resources)。此模型必須由 SageMaker AI 影像分類演算法訓練。如需詳細資訊，請參閱《*Amazon SageMaker AI 開發人員指南*》中的[映像分類演算法](https://docs.aws.amazon.com/sagemaker/latest/dg/image-classification.html)。
+ <a name="req-image-classification-feedback"></a>[ML Feedback 連接器](ml-feedback-connector.md)已新增至 Greengrass 群組並設定。只有在您想要使用連接器上傳模型輸入資料，並將預測發佈至 MQTT 主題時，才需要此項目。
+ <a name="req-image-classification-policy"></a>設定為允許對目標訓練任務`sagemaker:DescribeTrainingJob`執行動作的 [Greengrass 群組角色](group-role.md)，如下列範例 IAM 政策所示。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "sagemaker:DescribeTrainingJob"
              ],
              "Resource": "arn:aws:sagemaker:us-east-1:123456789012:training-job/training-job-name"
          }
      ]
  }
  ```

------

  <a name="set-up-group-role"></a>針對群組角色要求，您必須設定角色以授與必要的許可，並確認已將角色新增至群組。如需詳細資訊，請參閱 [管理 Greengrass 群組角色 (主控台)](group-role.md#manage-group-role-console) 或 [管理 Greengrass 群組角色 (CLI)](group-role.md#manage-group-role-cli) 。

  您可以為資源授予細微或條件式存取 (例如，使用萬用字元 \$1 命名配置)。如果您未來變更目標訓練任務，請務必更新群組角色。
+ 需要[AWS IoT Greengrass Machine Learning SDK](lambda-functions.md#lambda-sdks-ml) 1.1.0 版才能與此連接器互動。

------
#### [ Version 1 ]
+ AWS IoT Greengrass 核心軟體 1.7 版或更新版本。
+ 安裝在核心裝置上並新增至 PATH 環境變數的 [Python](https://www.python.org/) 2.7 版。
+ <a name="req-image-classification-framework"></a>對安裝在核心裝置上的 Apache MXNet 架構的相依性。如需詳細資訊，請參閱[在 AWS IoT Greengrass 核心上安裝 MXNet 相依性](#image-classification-connector-config)。
+ <a name="req-image-classification-resource"></a>Greengrass 群組中參考 SageMaker AI 模型來源的 [ML 資源](ml-inference.md#ml-resources)。此模型必須由 SageMaker AI 影像分類演算法訓練。如需詳細資訊，請參閱《*Amazon SageMaker AI 開發人員指南*》中的[映像分類演算法](https://docs.aws.amazon.com/sagemaker/latest/dg/image-classification.html)。
+ <a name="req-image-classification-policy"></a>設定為允許對目標訓練任務執行 `sagemaker:DescribeTrainingJob`動作的 [Greengrass 群組角色](group-role.md)，如下列範例 IAM 政策所示。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "sagemaker:DescribeTrainingJob"
              ],
              "Resource": "arn:aws:sagemaker:us-east-1:123456789012:training-job/training-job-name"
          }
      ]
  }
  ```

------

  <a name="set-up-group-role"></a>針對群組角色要求，您必須設定角色以授與必要的許可，並確認已將角色新增至群組。如需詳細資訊，請參閱 [管理 Greengrass 群組角色 (主控台)](group-role.md#manage-group-role-console) 或 [管理 Greengrass 群組角色 (CLI)](group-role.md#manage-group-role-cli) 。

  您可以為資源授予細微或條件式存取 (例如，使用萬用字元 \$1 命名配置)。如果您未來變更目標訓練任務，請務必更新群組角色。
+ 需要[AWS IoT Greengrass Machine Learning SDK](lambda-functions.md#lambda-sdks-ml) 1.0.0 版或更新版本，才能與此連接器互動。

------

## 連接器參數
<a name="image-classification-connector-param"></a>

這些連接器提供下列參數。

------
#### [ Version 2 ]

`MLModelDestinationPath`  <a name="param-image-classification-mdlpath"></a>
Lambda 環境中 ML 資源的絕對本機路徑。這是指定給機器學習資源的目的地路徑。  
如果是在主控台建立機器學習資源，則這是本機路徑。
 AWS IoT 主控台中的顯示名稱：**模型目的地路徑**  
必要：`true`  
類型：`string`  
有效模式： `.+`

`MLModelResourceId`  <a name="param-image-classification-mdlresourceid"></a>
參考來源模型的機器學習資源 ID。  
在 AWS IoT 主控台中顯示名稱：**SageMaker 任務 ARN 資源**  
必要：`true`  
類型：`string`  
有效模式： `[a-zA-Z0-9:_-]+`

`MLModelSageMakerJobArn`  <a name="param-image-classification-mdljobarn"></a>
代表 SageMaker AI 模型來源的 SageMaker AI 訓練任務 ARN。模型必須由 SageMaker AI 影像分類演算法訓練。  
在 AWS IoT 主控台中顯示名稱：**SageMaker 任務 ARN**  
必要：`true`  
類型：`string`  
有效模式： `^arn:aws:sagemaker:[a-zA-Z0-9-]+:[0-9]+:training-job/[a-zA-Z0-9][a-zA-Z0-9-]+$`

`LocalInferenceServiceName`  <a name="param-image-classification-svcname"></a>
本機推論服務的名稱。使用者定義的 Lambda 函數透過將名稱傳遞至 AWS IoT Greengrass Machine Learning SDK 的 `invoke_inference_service`函數來叫用服務。如需範例，請參閱 [用量範例](#image-classification-connector-usage)。  
 AWS IoT 主控台中的顯示名稱：**本機推論服務名稱**  
必要：`true`  
類型：`string`  
有效模式： `[a-zA-Z0-9][a-zA-Z0-9-]{1,62}`

`LocalInferenceServiceTimeoutSeconds`  <a name="param-image-classification-svctimeout"></a>
推論請求終止之前的時間 (以秒為單位)。最小值為 1。  
 AWS IoT 主控台中的顯示名稱：**逾時 （秒）**  
必要：`true`  
類型：`string`  
有效模式： `[1-9][0-9]*`

`LocalInferenceServiceMemoryLimitKB`  <a name="param-image-classification-svcmemorylimit"></a>
服務可存取的記憶體數量 (KB)。最小值為 1。  
 AWS IoT 主控台中的顯示名稱：**記憶體限制 (KB)**  
必要：`true`  
類型：`string`  
有效模式： `[1-9][0-9]*`

`GPUAcceleration`  <a name="param-image-classification-gpuacceleration"></a>
CPU 或 GPU (加速) 運算環境。此屬性僅適用於 ML Image Classification Aarch64 JTX2 連接器。  
 AWS IoT 主控台中的顯示名稱：**GPU 加速**  
必要：`true`  
類型：`string`  
有效值：`CPU` 或 `GPU`

`MLFeedbackConnectorConfigId`  <a name="param-image-classification-feedbackconfigid"></a>
用來上傳模型輸入資料的意見回饋組態 ID。這必須符合針對 [ML 意見回饋連接器](ml-feedback-connector.md)定義的意見回饋組態 ID。  
只有在您想要使用 ML 意見回饋連接器上傳模型輸入資料，並將預測發佈至 MQTT 主題時，才需要此參數。  
在 AWS IoT 主控台中顯示名稱：**ML Feedback 連接器組態 ID**  
必要：`false`  
類型：`string`  
有效模式： `^$|^[a-zA-Z0-9][a-zA-Z0-9-]{1,62}$`

------
#### [ Version 1 ]

`MLModelDestinationPath`  <a name="param-image-classification-mdlpath"></a>
Lambda 環境中 ML 資源的絕對本機路徑。這是指定給機器學習資源的目的地路徑。  
如果是在主控台建立機器學習資源，則這是本機路徑。
 AWS IoT 主控台中的顯示名稱：**模型目的地路徑**  
必要：`true`  
類型：`string`  
有效模式： `.+`

`MLModelResourceId`  <a name="param-image-classification-mdlresourceid"></a>
參考來源模型的機器學習資源 ID。  
在 AWS IoT 主控台中顯示名稱：**SageMaker 任務 ARN 資源**  
必要：`true`  
類型：`string`  
有效模式： `[a-zA-Z0-9:_-]+`

`MLModelSageMakerJobArn`  <a name="param-image-classification-mdljobarn"></a>
代表 SageMaker AI 模型來源的 SageMaker AI 訓練任務 ARN。模型必須由 SageMaker AI 影像分類演算法訓練。  
在 AWS IoT 主控台中顯示名稱：**SageMaker 任務 ARN**  
必要：`true`  
類型：`string`  
有效模式： `^arn:aws:sagemaker:[a-zA-Z0-9-]+:[0-9]+:training-job/[a-zA-Z0-9][a-zA-Z0-9-]+$`

`LocalInferenceServiceName`  <a name="param-image-classification-svcname"></a>
本機推論服務的名稱。使用者定義的 Lambda 函數透過將名稱傳遞至 AWS IoT Greengrass Machine Learning SDK 的 `invoke_inference_service`函數來叫用服務。如需範例，請參閱 [用量範例](#image-classification-connector-usage)。  
在 AWS IoT 主控台中顯示名稱：**本機推論服務名稱**  
必要：`true`  
類型：`string`  
有效模式： `[a-zA-Z0-9][a-zA-Z0-9-]{1,62}`

`LocalInferenceServiceTimeoutSeconds`  <a name="param-image-classification-svctimeout"></a>
推論請求終止之前的時間 (以秒為單位)。最小值為 1。  
 AWS IoT 主控台中的顯示名稱：**逾時 （秒）**  
必要：`true`  
類型：`string`  
有效模式： `[1-9][0-9]*`

`LocalInferenceServiceMemoryLimitKB`  <a name="param-image-classification-svcmemorylimit"></a>
服務可存取的記憶體數量 (KB)。最小值為 1。  
 AWS IoT 主控台中的顯示名稱：**記憶體限制 (KB)**  
必要：`true`  
類型：`string`  
有效模式： `[1-9][0-9]*`

`GPUAcceleration`  <a name="param-image-classification-gpuacceleration"></a>
CPU 或 GPU (加速) 運算環境。此屬性僅適用於 ML Image Classification Aarch64 JTX2 連接器。  
 AWS IoT 主控台中的顯示名稱：**GPU 加速**  
必要：`true`  
類型：`string`  
有效值：`CPU` 或 `GPU`

------

### 建立範例連接器 (AWS CLI)
<a name="image-classification-connector-create"></a>

下列 CLI 命令`ConnectorDefinition`會使用包含 ML Image Classification 連接器的初始版本來建立 。

**範例：CPU 執行個體**  
此範例會建立 ML Image Classification Armv7l 連接器的執行個體。  

```
aws greengrass create-connector-definition --name MyGreengrassConnectors --initial-version '{
    "Connectors": [
        {
            "Id": "MyImageClassificationConnector",
            "ConnectorArn": "arn:aws:greengrass:region::/connectors/ImageClassificationARMv7/versions/2",
            "Parameters": {
                "MLModelDestinationPath": "/path-to-model",
                "MLModelResourceId": "my-ml-resource",
                "MLModelSageMakerJobArn": "arn:aws:sagemaker:us-west-2:123456789012:training-job:MyImageClassifier",
                "LocalInferenceServiceName": "imageClassification",
                "LocalInferenceServiceTimeoutSeconds": "10",
                "LocalInferenceServiceMemoryLimitKB": "500000",
                "MLFeedbackConnectorConfigId": "MyConfig0"
            }
        }
    ]
}'
```

**範例：GPU 執行個體**  
此範例會建立 ML Image Classification Aarch64 JTX2 連接器的執行個體，該連接器支援 NVIDIA Jetson TX2 主機板上的 GPU 加速。  

```
aws greengrass create-connector-definition --name MyGreengrassConnectors --initial-version '{
    "Connectors": [
        {
            "Id": "MyImageClassificationConnector",
            "ConnectorArn": "arn:aws:greengrass:region::/connectors/ImageClassificationAarch64JTX2/versions/2",
            "Parameters": {
                "MLModelDestinationPath": "/path-to-model",
                "MLModelResourceId": "my-ml-resource",
                "MLModelSageMakerJobArn": "arn:aws:sagemaker:us-west-2:123456789012:training-job:MyImageClassifier",
                "LocalInferenceServiceName": "imageClassification",
                "LocalInferenceServiceTimeoutSeconds": "10",
                "LocalInferenceServiceMemoryLimitKB": "500000",
                "GPUAcceleration": "GPU",
                "MLFeedbackConnectorConfigId": "MyConfig0"
            }
        }
    ]
}'
```

**注意**  
這些連接器中的 Lambda 函數具有[長期生命週期](lambda-functions.md#lambda-lifecycle)。

在 AWS IoT Greengrass 主控台中，您可以從群組的連接器頁面新增**連接器**。如需詳細資訊，請參閱[Greengrass 連接器入門 (主控台)](connectors-console.md)。

## 輸入資料
<a name="image-classification-connector-data-input"></a>

 這些連接器接受影像檔做為輸入。輸入影像檔必須採用 `jpeg` 或 `png` 格式。如需詳細資訊，請參閱[用量範例](#image-classification-connector-usage)。

這些連接器不接受 MQTT 訊息做為輸入資料。

## 輸出資料
<a name="image-classification-connector-data-output"></a>

這些連接器會傳回輸入影像中所識別物件的格式化預測：

```
[0.3,0.1,0.04,...]
```

預測包含一個清單，列出模型訓練期間與訓練資料集所使用之類別對應的值。每個值代表影像落在對應類別下的機率。具有最高機率的類別是主導預測。

這些連接器不會將 MQTT 訊息發佈為輸出資料。

## 用量範例
<a name="image-classification-connector-usage"></a>

下列範例 Lambda 函數使用 [AWS IoT Greengrass Machine Learning SDK](lambda-functions.md#lambda-sdks-ml) 與 ML Image Classification 連接器互動。

**注意**  
 您可以從[AWS IoT Greengrass Machine Learning開發套件下載頁面下載開發套件](what-is-gg.md#gg-ml-sdk-download)。

範例初始化軟體開發套件用戶端，並同步呼叫軟體開發套件的 `invoke_inference_service` 函數，以叫用本機推論服務。它會傳入演算法類型、服務名稱、映像類型和映像內容。然後，範例會剖析服務回應以獲得機率結果 (預測)。

------
#### [ Python 3.7 ]

```
import logging
from threading import Timer

import numpy as np

import greengrass_machine_learning_sdk as ml

# We assume the inference input image is provided as a local file
# to this inference client Lambda function.
with open('/test_img/test.jpg', 'rb') as f:
    content = bytearray(f.read())

client = ml.client('inference')

def infer():
    logging.info('invoking Greengrass ML Inference service')

    try:
        resp = client.invoke_inference_service(
            AlgoType='image-classification',
            ServiceName='imageClassification',
            ContentType='image/jpeg',
            Body=content
        )
    except ml.GreengrassInferenceException as e:
        logging.info('inference exception {}("{}")'.format(e.__class__.__name__, e))
        return
    except ml.GreengrassDependencyException as e:
        logging.info('dependency exception {}("{}")'.format(e.__class__.__name__, e))
        return

    logging.info('resp: {}'.format(resp))
    predictions = resp['Body'].read().decode("utf-8")
    logging.info('predictions: {}'.format(predictions))
    
    # The connector output is in the format: [0.3,0.1,0.04,...]
    # Remove the '[' and ']' at the beginning and end.
    predictions = predictions[1:-1]
    count = len(predictions.split(','))
    predictions_arr = np.fromstring(predictions, count=count, sep=',')

    # Perform business logic that relies on the predictions_arr, which is an array
    # of probabilities.
    
    # Schedule the infer() function to run again in one second.
    Timer(1, infer).start()
    return

infer()

def function_handler(event, context):
    return
```

------
#### [ Python 2.7 ]

```
import logging
from threading import Timer

import numpy

import greengrass_machine_learning_sdk as gg_ml

# The inference input image.
with open("/test_img/test.jpg", "rb") as f:
    content = f.read()

client = gg_ml.client("inference")


def infer():
    logging.info("Invoking Greengrass ML Inference service")

    try:
        resp = client.invoke_inference_service(
            AlgoType="image-classification",
            ServiceName="imageClassification",
            ContentType="image/jpeg",
            Body=content,
        )
    except gg_ml.GreengrassInferenceException as e:
        logging.info('Inference exception %s("%s")', e.__class__.__name__, e)
        return
    except gg_ml.GreengrassDependencyException as e:
        logging.info('Dependency exception %s("%s")', e.__class__.__name__, e)
        return

    logging.info("Response: %s", resp)
    predictions = resp["Body"].read()
    logging.info("Predictions: %s", predictions)

    # The connector output is in the format: [0.3,0.1,0.04,...]
    # Remove the '[' and ']' at the beginning and end.
    predictions = predictions[1:-1]
    predictions_arr = numpy.fromstring(predictions, sep=",")
    logging.info("Split into %s predictions.", len(predictions_arr))

    # Perform business logic that relies on predictions_arr, which is an array
    # of probabilities.

    # Schedule the infer() function to run again in one second.
    Timer(1, infer).start()


infer()


# In this example, the required AWS Lambda handler is never called.
def function_handler(event, context):
    return
```

------

 AWS IoT Greengrass Machine Learning SDK 中的 `invoke_inference_service`函數接受下列引數。


| 引數 | Description | 
| --- | --- | 
| `AlgoType` | 用於推論之演算法類型的名稱。目前僅支援 `image-classification`。 必要：`true` 類型：`string` 有效值：`image-classification` | 
| `ServiceName` | 本機推論服務的名稱。使用您設定連接器時為 `LocalInferenceServiceName` 參數指定的名稱。 必要：`true` 類型：`string` | 
| `ContentType` | 輸入映像的 mime 類型。 必要：`true` 類型：`string` 有效值：`image/jpeg, image/png` | 
| `Body` | 輸入映像檔的內容。 必要：`true` 類型：`binary` | 

## 在 AWS IoT Greengrass 核心上安裝 MXNet 相依性
<a name="image-classification-connector-config"></a>

若要使用 ML Image Classification 連接器，您必須在核心裝置上安裝 Apache MXNet 架構的相依性。連接器使用架構來支援機器學習模型。

**注意**  
這些連接器隨附預先編譯的 MXNet 程式庫，因此您不需要在核心裝置上安裝 MXNet 架構。

AWS IoT Greengrass 提供指令碼來安裝下列常見平台和裝置的相依性 （或做為安裝它們的參考）。如果您使用的是不同平台或裝置，請參閱 [MXNet 文件](https://mxnet.apache.org/)做為組態的參考。

在安裝 MXNet 相依性之前，請確保所需的[系統程式庫](#image-classification-connector-logging) (使用指定的最低版本) 存在於裝置上。

------
#### [ NVIDIA Jetson TX2 ]

1. 安裝 CUDA Toolkit 9.0 和 cuDNN 7.0。您可以按照入門教學課程的[設定其他裝置](setup-filter.other.md)中的指示。

1. 啟用世界各地的儲存庫，讓連接器可以安裝社群維護的開放式軟體。如需詳細資訊，請參閱 Ubuntu 文件中的[儲存庫/Ubuntu](https://help.ubuntu.com/community/Repositories/Ubuntu)。

   1. 開啟 `/etc/apt/sources.list` 檔案。

   1. 務必註銷以下幾行。

      ```
      deb http://ports.ubuntu.com/ubuntu-ports/ xenial universe
      deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial universe
      deb http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
      deb-src http://ports.ubuntu.com/ubuntu-ports/ xenial-updates universe
      ```

1. 在核心裝置上將以下安裝指令碼的複本儲存成名為 `nvidiajtx2.sh` 的檔案。

------
#### [ Python 3.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   echo 'Assuming that universe repos are enabled and checking dependencies...'
   apt-get -y update
   apt-get -y dist-upgrade
   apt-get install -y liblapack3 libopenblas-dev liblapack-dev libatlas-base-dev
   apt-get install -y python3.7 python3.7-dev
   
   python3.7 -m pip install --upgrade pip
   python3.7 -m pip install numpy==1.15.0
   python3.7 -m pip install opencv-python || echo 'Error: Unable to install OpenCV with pip on this platform. Try building the latest OpenCV from source (https://github.com/opencv/opencv).'
   
   echo 'Dependency installation/upgrade complete.'
   ```

**注意**  
<a name="opencv-build-from-source"></a>如果 [OpenCV](https://github.com/opencv/opencv) 未使用此指令碼成功安裝，您可以嘗試從來源建置。如需詳細資訊，請參閱 OpenCV 文件中的 [Installation in Linux](https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html)，或參閱適用於您平台的其他線上資源。

------
#### [ Python 2.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   echo 'Assuming that universe repos are enabled and checking dependencies...'
   apt-get -y update
   apt-get -y dist-upgrade
   apt-get install -y liblapack3 libopenblas-dev liblapack-dev libatlas-base-dev python-dev
   
   echo 'Install latest pip...'
   wget https://bootstrap.pypa.io/get-pip.py
   python get-pip.py
   rm get-pip.py
   
   pip install numpy==1.15.0 scipy
   
   echo 'Dependency installation/upgrade complete.'
   ```

------

1. 從您儲存檔案的目錄中，執行下列命令：

   ```
   sudo nvidiajtx2.sh
   ```

------
#### [ x86\$164 (Ubuntu or Amazon Linux)  ]

1. 在核心裝置上將以下安裝指令碼的複本儲存成名為 `x86_64.sh` 的檔案。

------
#### [ Python 3.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   
   release=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
   
   if [ "$release" == '"Ubuntu"' ]; then
     # Ubuntu. Supports EC2 and DeepLens. DeepLens has all the dependencies installed, so
     # this is mostly to prepare dependencies on Ubuntu EC2 instance.
     apt-get -y update
     apt-get -y dist-upgrade
   
     apt-get install -y libgfortran3 libsm6 libxext6 libxrender1
     apt-get install -y python3.7 python3.7-dev
   elif [ "$release" == '"Amazon Linux"' ]; then
     # Amazon Linux. Expect python to be installed already
     yum -y update
     yum -y upgrade
   
     yum install -y compat-gcc-48-libgfortran libSM libXrender libXext
   else
     echo "OS Release not supported: $release"
     exit 1
   fi
   
   python3.7 -m pip install --upgrade pip
   python3.7 -m pip install numpy==1.15.0
   python3.7 -m pip install opencv-python || echo 'Error: Unable to install OpenCV with pip on this platform. Try building the latest OpenCV from source (https://github.com/opencv/opencv).'
   
   echo 'Dependency installation/upgrade complete.'
   ```

**注意**  
<a name="opencv-build-from-source"></a>如果 [OpenCV](https://github.com/opencv/opencv) 未使用此指令碼成功安裝，您可以嘗試從來源建置。如需詳細資訊，請參閱 OpenCV 文件中的 [Installation in Linux](https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html)，或參閱適用於您平台的其他線上資源。

------
#### [ Python 2.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   
   release=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
   
   if [ "$release" == '"Ubuntu"' ]; then
     # Ubuntu. Supports EC2 and DeepLens. DeepLens has all the dependencies installed, so
     # this is mostly to prepare dependencies on Ubuntu EC2 instance.
     apt-get -y update
     apt-get -y dist-upgrade
   
     apt-get install -y libgfortran3 libsm6 libxext6 libxrender1 python-dev python-pip
   elif [ "$release" == '"Amazon Linux"' ]; then
     # Amazon Linux. Expect python to be installed already
     yum -y update
     yum -y upgrade
   
     yum install -y compat-gcc-48-libgfortran libSM libXrender libXext python-pip
   else
     echo "OS Release not supported: $release"
     exit 1
   fi
   
   pip install numpy==1.15.0 scipy opencv-python
   
   echo 'Dependency installation/upgrade complete.'
   ```

------

1. 從您儲存檔案的目錄中，執行下列命令：

   ```
   sudo x86_64.sh
   ```

------
#### [ Armv7 (Raspberry Pi) ]

1. 在核心裝置上將以下安裝指令碼的複本儲存成名為 `armv7l.sh` 的檔案。

------
#### [ Python 3.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   
   apt-get update
   apt-get -y upgrade
   
   apt-get install -y liblapack3 libopenblas-dev liblapack-dev
   apt-get install -y python3.7 python3.7-dev
   
   python3.7 -m pip install --upgrade pip
   python3.7 -m pip install numpy==1.15.0
   python3.7 -m pip install opencv-python || echo 'Error: Unable to install OpenCV with pip on this platform. Try building the latest OpenCV from source (https://github.com/opencv/opencv).'
   
   echo 'Dependency installation/upgrade complete.'
   ```

**注意**  
<a name="opencv-build-from-source"></a>如果 [OpenCV](https://github.com/opencv/opencv) 未使用此指令碼成功安裝，您可以嘗試從來源建置。如需詳細資訊，請參閱 OpenCV 文件中的 [Installation in Linux](https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html)，或參閱適用於您平台的其他線上資源。

------
#### [ Python 2.7 ]

   ```
   #!/bin/bash
   set -e
   
   echo "Installing dependencies on the system..."
   
   apt-get update
   apt-get -y upgrade
   
   apt-get install -y liblapack3 libopenblas-dev liblapack-dev python-dev
   
   # python-opencv depends on python-numpy. The latest version in the APT repository is python-numpy-1.8.2
   # This script installs python-numpy first so that python-opencv can be installed, and then install the latest
   # numpy-1.15.x with pip
   apt-get install -y python-numpy python-opencv
   dpkg --remove --force-depends python-numpy
   
   echo 'Install latest pip...'
   wget https://bootstrap.pypa.io/get-pip.py
   python get-pip.py
   rm get-pip.py
   
   pip install --upgrade numpy==1.15.0 picamera scipy
   
   echo 'Dependency installation/upgrade complete.'
   ```

------

1. 從您儲存檔案的目錄中，執行下列命令：

   ```
   sudo bash armv7l.sh
   ```
**注意**  
在 Raspberry Pi 上，使用 `pip` 來安裝機器學習相依性是一種記憶體密集型操作，可能會導致裝置記憶體不足、無法回應。若要解決這項問題，您可以暫時增加交換大小：  
在 `/etc/dphys-swapfile` 中，增加 `CONF_SWAPSIZE` 變數的值，然後執行下列命令來重新啟動 `dphys-swapfile`。  

   ```
   /etc/init.d/dphys-swapfile restart
   ```

------

## 記錄和故障診斷
<a name="image-classification-connector-logging"></a>

根據您的群組設定，事件和錯誤日誌會寫入 CloudWatch Logs、本機檔案系統或兩者。來自這個連接器的日誌使用字首 `LocalInferenceServiceName`。如果連接器發生非預期的行為，請檢查連接器的日誌。日誌通常會包含有用的偵錯資訊，例如遺失機器學習程式庫相依性或連接器啟動失敗的原因。

如果 AWS IoT Greengrass 群組設定為寫入本機日誌，連接器會將日誌檔案寫入 `greengrass-root/ggc/var/log/user/region/aws/`。如需 Greengrass 記錄的詳細資訊，請參閱 [使用 AWS IoT Greengrass 日誌監控](greengrass-logs-overview.md)。

使用以下資訊來協助疑難排解 ML Image Classification 連接器的問題。

**必要系統程式庫**

下列索引標籤列出每個 ML Image Classification 連接器所需的系統程式庫。

------
#### [ ML Image Classification Aarch64 JTX2 ]


| 程式庫 | 最低版本 | 
| --- | --- | 
| ld-linux-aarch64.so.1 | GLIBC\$12.17 | 
| libc.so.6 | GLIBC\$12.17 | 
| libcublas.so.9.0 | 不適用 | 
| libcudart.so.9.0 | 不適用 | 
| libcudnn.so.7 | 不適用 | 
| libcufft.so.9.0 | 不適用 | 
| libcurand.so.9.0 | 不適用 | 
| libcusolver.so.9.0 | 不適用 | 
| libgcc\$1s.so.1 | GCC\$14.2.0 | 
| libgomp.so.1 | GOMP\$14.0, OMP\$11.0 | 
| libm.so.6 | GLIBC\$12.23 | 
| libpthread.so.0 | GLIBC\$12.17 | 
| librt.so.1 | GLIBC\$12.17 | 
| libstdc\$1\$1.so.6 | GLIBCXX\$13.4.21, CXXABI\$11.3.8 | 

------
#### [ ML Image Classification x86\$164 ]


| 程式庫 | 最低版本 | 
| --- | --- | 
| ld-linux-x86-64.so.2 | GCC\$14.0.0 | 
| libc.so.6 | GLIBC\$12.4 | 
| libgfortran.so.3 | GFORTRAN\$11.0 | 
| libm.so.6 | GLIBC\$12.23 | 
| libpthread.so.0 | GLIBC\$12.2.5 | 
| librt.so.1 | GLIBC\$12.2.5 | 
| libstdc\$1\$1.so.6 | CXXABI\$11.3.8, GLIBCXX\$13.4.21 | 

------
#### [ ML Image Classification Armv7 ]


| 程式庫 | 最低版本 | 
| --- | --- | 
| ld-linux-armhf.so.3 | GLIBC\$12.4 | 
| libc.so.6 | GLIBC\$12.7 | 
| libgcc\$1s.so.1 | GCC\$14.0.0 | 
| libgfortran.so.3 | GFORTRAN\$11.0 | 
| libm.so.6 | GLIBC\$12.4 | 
| libpthread.so.0 | GLIBC\$12.4 | 
| librt.so.1 | GLIBC\$12.4 | 
| libstdc\$1\$1.so.6 | CXXABI\$11.3.8、CXXABI\$1ARM\$11.3.3、GLIBCXX\$13.4.20 | 

------

**問題**


| 徵狀 | 解決方案 | 
| --- | --- | 
|  在 Raspberry Pi 上，下列錯誤訊息已記錄，並且您不是使用相機：`Failed to initialize libdc1394`  |  執行下列命令以停用驅動程式： <pre>sudo ln /dev/null /dev/raw1394</pre> 此操作為暫時性，符號連結在重新啟動就會消失。請參閱您作業系統版本的手冊，了解如何在重新啟動時自動建立連結。  | 

## 授權
<a name="image-classification-connector-license"></a>

ML Image Classification 連接器包含下列第三方軟體/授權：<a name="boto-3-licenses"></a>
+ [適用於 Python (Boto3) 的 AWS SDK](https://pypi.org/project/boto3/)/Apache License 2.0
+ [botocore](https://pypi.org/project/botocore/)/Apache License 2.0
+ [dateutil](https://pypi.org/project/python-dateutil/1.4/)/PSF License
+ [docutils](https://pypi.org/project/docutils/)/BSD 授權、GNU 一般公有授權 (GPL)、Python 軟體基金會授權、公有網域
+ [jmespath](https://pypi.org/project/jmespath/)/MIT License
+ [s3transfer](https://pypi.org/project/s3transfer/)/Apache License 2.0
+ [urllib3](https://pypi.org/project/urllib3/)/MIT License
+ [Deep Neural Network Library (DNNL)](https://github.com/intel/mkl-dnn)/Apache License 2.0
+ [OpenMP\$1 Runtime Library](https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/optimization-and-programming-guide/openmp-support/openmp-library-support/openmp-run-time-library-routines.html)/請參閱 [Intel OpenMP Runtime Library 授權](#openmp-license)。
+ [mxnet](https://pypi.org/project/mxnet/)/Apache License 2.0
+ <a name="six-license"></a>[six](https://github.com/benjaminp/six)/MIT

**Intel OpenMP Runtime Library 授權**。Intel® OpenMP\$1 執行期是雙重授權，具有商業 (COM) 授權做為 Intel® Parallel Studio XE Suite 產品的一部分，以及 BSD 開放原始碼 (OSS) 授權。

此連接器根據 [Greengrass 核心軟體授權合約](https://greengrass-release-license.s3.us-west-2.amazonaws.com/greengrass-license-v1.pdf)發行。

## 變更記錄
<a name="image-classification-connector-changelog"></a>

下表說明每個版本連接器的變更。


| 版本 | 變更 | 
| --- | --- | 
| 2 | 新增 `MLFeedbackConnectorConfigId` 參數以支援使用 [ML Feedback 連接器](ml-feedback-connector.md)上傳模型輸入資料、將預測發佈至 MQTT 主題，以及將指標發佈至 Amazon CloudWatch。  | 
| 1 | 初始版本。  | 

<a name="one-conn-version"></a>Greengrass 群組一次只能包含一個版本的連接器。若要取得有關升級連接器版本的資訊，請參閱[升級連接器版本](connectors.md#upgrade-connector-versions)。

## 另請參閱
<a name="image-classification-connector-see-also"></a>
+ [使用 Greengrass 連接器來整合服務和通訊協定](connectors.md)
+ [Greengrass 連接器入門 (主控台)](connectors-console.md)
+ [Greengrass 連接器入門 (CLI)](connectors-cli.md)
+ [執行機器學習推論](ml-inference.md)
+ 《*Amazon SageMaker AI 開發人員指南*》中的[映像分類演算法](https://docs.aws.amazon.com/sagemaker/latest/dg/image-classification.html) 