AWS IoT Greengrass Version 1 entered the extended life phase on June 30, 2023. For more information, see the AWS IoT Greengrass V1 maintenance policy. After this date, AWS IoT Greengrass V1 won't release updates that provide features, enhancements, bug fixes, or security patches. Devices that run on AWS IoT Greengrass V1 won't be disrupted and will continue to operate and to connect to the cloud. We strongly recommend that you migrate to AWS IoT Greengrass Version 2, which adds significant new features and support for additional platforms.
ML Image Classification connector
Warning
This connector has moved into the extended life phase, and AWS IoT Greengrass won't release updates that provide features, enhancements to existing features, security patches, or bug fixes. For more information, see AWS IoT Greengrass Version 1 maintenance policy.
The ML Image Classification connectors provide a machine learning (ML) inference service that runs on the AWS IoT Greengrass core. This local inference service performs image classification using a model trained by the SageMaker AI image classification algorithm.
User-defined Lambda functions use the AWS IoT Greengrass Machine Learning SDK to submit inference requests to the local inference service. The service runs inference locally and returns probabilities that the input image belongs to specific categories.
AWS IoT Greengrass provides the following versions of this connector, which is available for multiple platforms.
Connector |
Description and ARN |
---|---|
ML Image Classification Aarch64 JTX2 |
Image classification inference service for NVIDIA Jetson TX2. Supports GPU acceleration. ARN: |
ML Image Classification x86_64 |
Image classification inference service for x86_64 platforms. ARN: |
ML Image Classification ARMv7 |
Image classification inference service for ARMv7 platforms. ARN: |
For information about version changes, see the Changelog.
Requirements
These connectors have the following requirements:
-
AWS IoT Greengrass Core Software v1.9.3 or later.
-
Python
version 3.7 or 3.8 installed on the core device and added to the PATH environment variable. Note
To use Python 3.8, run the following command to create a symbolic link from the the default Python 3.7 installation folder to the installed Python 3.8 binaries.
sudo ln -s
path-to-python-3.8
/python3.8 /usr/bin/python3.7This configures your device to meet the Python requirement for AWS IoT Greengrass.
-
Dependencies for the Apache MXNet framework installed on the core device. For more information, see Installing MXNet dependencies on the AWS IoT Greengrass core.
-
An ML resource in the Greengrass group that references an SageMaker AI model source. This model must be trained by the SageMaker AI image classification algorithm. For more information, see Image classification algorithm in the Amazon SageMaker AI Developer Guide.
-
The ML Feedback connector added to the Greengrass group and configured. This is required only if you want to use the connector to upload model input data and publish predictions to an MQTT topic.
-
The Greengrass group role configured to allow the
sagemaker:DescribeTrainingJob
action on the target training job, as shown in the following example IAM policy.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "sagemaker:DescribeTrainingJob" ], "Resource": "arn:aws:sagemaker:
region
:account-id
:training-job:training-job-name
" } ] }For the group role requirement, you must configure the role to grant the required permissions and make sure the role has been added to the group. For more information, see Managing the Greengrass group role (console) or Managing the Greengrass group role (CLI).
You can grant granular or conditional access to resources (for example, by using a wildcard * naming scheme). If you change the target training job in the future, make sure to update the group role.
-
AWS IoT Greengrass Machine Learning SDK v1.1.0 is required to interact with this connector.
Connector Parameters
These connectors provide the following parameters.
MLModelDestinationPath
-
The absolute local path of the ML resource inside the Lambda environment. This is the destination path that's specified for the ML resource.
Note
If you created the ML resource in the console, this is the local path.
Display name in the AWS IoT console: Model destination path
Required:
true
Type:
string
Valid pattern:
.+
MLModelResourceId
-
The ID of the ML resource that references the source model.
Display name in the AWS IoT console: SageMaker job ARN resource
Required:
true
Type:
string
Valid pattern:
[a-zA-Z0-9:_-]+
MLModelSageMakerJobArn
-
The ARN of the SageMaker AI training job that represents the SageMaker AI model source. The model must be trained by the SageMaker AI image classification algorithm.
Display name in the AWS IoT console: SageMaker job ARN
Required:
true
Type:
string
Valid pattern:
^arn:aws:sagemaker:[a-zA-Z0-9-]+:[0-9]+:training-job/[a-zA-Z0-9][a-zA-Z0-9-]+$
LocalInferenceServiceName
-
The name for the local inference service. User-defined Lambda functions invoke the service by passing the name to the
invoke_inference_service
function of the AWS IoT Greengrass Machine Learning SDK. For an example, see Usage Example.Display name in the AWS IoT console: Local inference service name
Required:
true
Type:
string
Valid pattern:
[a-zA-Z0-9][a-zA-Z0-9-]{1,62}
LocalInferenceServiceTimeoutSeconds
-
The amount of time (in seconds) before the inference request is terminated. The minimum value is 1.
Display name in the AWS IoT console: Timeout (second)
Required:
true
Type:
string
Valid pattern:
[1-9][0-9]*
LocalInferenceServiceMemoryLimitKB
-
The amount of memory (in KB) that the service has access to. The minimum value is 1.
Display name in the AWS IoT console: Memory limit (KB)
Required:
true
Type:
string
Valid pattern:
[1-9][0-9]*
GPUAcceleration
-
The CPU or GPU (accelerated) computing context. This property applies to the ML Image Classification Aarch64 JTX2 connector only.
Display name in the AWS IoT console: GPU acceleration
Required:
true
Type:
string
Valid values:
CPU
orGPU
MLFeedbackConnectorConfigId
-
The ID of the feedback configuration to use to upload model input data. This must match the ID of a feedback configuration defined for the ML Feedback connector.
This parameter is required only if you want to use the ML Feedback connector to upload model input data and publish predictions to an MQTT topic.
Display name in the AWS IoT console: ML Feedback connector configuration ID
Required:
false
Type:
string
Valid pattern:
^$|^[a-zA-Z0-9][a-zA-Z0-9-]{1,62}$
Create Connector Example (AWS CLI)
The following CLI commands create a ConnectorDefinition
with an initial version that contains an ML Image Classification connector.
- Example: CPU Instance
-
This example creates an instance of the ML Image Classification Armv7l connector.
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" } } ] }' - Example: GPU Instance
-
This example creates an instance of the ML Image Classification Aarch64 JTX2 connector, which supports GPU acceleration on an NVIDIA Jetson TX2 board.
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" } } ] }'
Note
The Lambda function in these connectors have a long-lived lifecycle.
In the AWS IoT Greengrass console, you can add a connector from the group's Connectors page. For more information, see Getting started with Greengrass connectors (console).
Input data
These connectors accept an image file as input. Input image files must be in jpeg
or png
format. For more information, see Usage Example.
These connectors don't accept MQTT messages as input data.
Output data
These connectors return a formatted prediction for the object identified in the input image:
[0.3,0.1,0.04,...]
The prediction contains a list of values that correspond with the categories used in the training dataset during model training. Each value represents the probability that the image falls under the corresponding category. The category with the highest probability is the dominant prediction.
These connectors don't publish MQTT messages as output data.
Usage Example
The following example Lambda function uses the AWS IoT Greengrass Machine Learning SDK to interact with an ML Image Classification connector.
Note
You can download the SDK from the AWS IoT Greengrass Machine Learning SDK downloads page.
The example initializes an SDK client and synchronously calls the SDK's
invoke_inference_service
function to invoke the local inference service.
It passes in the algorithm type, service name, image type, and image content.
Then, the example parses the service response to get the probability results (predictions).
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
The invoke_inference_service
function in the AWS IoT Greengrass Machine Learning SDK accepts the following arguments.
Argument |
Description |
---|---|
|
The name of the algorithm type to use for inference. Currently, only Required: Type: Valid values: |
|
The name of the local inference service. Use the name that you specified
for the Required: Type: |
|
The mime type of the input image. Required: Type: Valid values: |
|
The content of the input image file. Required: Type: |
Installing MXNet dependencies on the AWS IoT Greengrass core
To use an ML Image Classification connector, you must install the dependencies for the Apache MXNet framework on the core device. The connectors use the framework to serve the ML model.
Note
These connectors are bundled with a precompiled MXNet library, so you don't need to install the MXNet framework on the core device.
AWS IoT Greengrass provides scripts to install the dependencies for the following common platforms and devices (or to use as a reference for installing them).
If you're using a different platform or device,
see the MXNet documentation
Before installing the MXNet dependencies, make sure that the required system libraries (with the specified minimum versions) are present on the device.
-
Install CUDA Toolkit 9.0 and cuDNN 7.0. You can follow the instructions in Setting up other devices in the Getting Started tutorial.
-
Enable universe repositories so the connector can install community-maintained open software. For more information, see Repositories/Ubuntu
in the Ubuntu documentation. -
Open the
/etc/apt/sources.list
file. -
Make sure that the following lines are uncommented.
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
-
-
Save a copy of the following installation script to a file named
nvidiajtx2.sh
on the core device.- 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.'
Note
If OpenCV
does not install successfully using this script, you can try building from source. For more information, see Installation in Linux in the OpenCV documentation, or refer to other online resources for your platform. - 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.'
-
From the directory where you saved the file, run the following command:
sudo nvidiajtx2.sh
Logging and troubleshooting
Depending on your group settings, event and error logs are written
to CloudWatch Logs, the local file system, or both. Logs from this connector use the prefix
LocalInferenceServiceName
.
If the connector behaves unexpectedly, check the connector's logs.
These usually contain useful debugging information, such as a missing ML library dependency or the cause of a connector
startup failure.
If the AWS IoT Greengrass group is configured to write local logs,
the connector writes log files to
. For more information about Greengrass logging, see Monitoring with AWS IoT Greengrass logs.greengrass-root
/ggc/var/log/user/region
/aws/
Use the following information to help troubleshoot issues with the ML Image Classification connectors.
Required system libraries
The following tabs list the system libraries required for each ML Image Classification connector.
Library | Minimum version |
---|---|
ld-linux-aarch64.so.1 | GLIBC_2.17 |
libc.so.6 | GLIBC_2.17 |
libcublas.so.9.0 | not applicable |
libcudart.so.9.0 | not applicable |
libcudnn.so.7 | not applicable |
libcufft.so.9.0 | not applicable |
libcurand.so.9.0 | not applicable |
libcusolver.so.9.0 | not applicable |
libgcc_s.so.1 | GCC_4.2.0 |
libgomp.so.1 | GOMP_4.0, OMP_1.0 |
libm.so.6 | GLIBC_2.23 |
libpthread.so.0 | GLIBC_2.17 |
librt.so.1 | GLIBC_2.17 |
libstdc++.so.6 | GLIBCXX_3.4.21, CXXABI_1.3.8 |
Issues
Symptom | Solution |
---|---|
On a Raspberry Pi, the following error message is logged and you are not using the camera:
|
Run the following command to disable the driver:
This operation is ephemeral and the symbolic link will disappear after rebooting. Consult the manual of your OS distribution to learn how to automatically create the link up on reboot. |
Licenses
The ML Image Classification connectors includes the following third-party software/licensing:
-
AWS SDK for Python (Boto3)
/Apache License 2.0 -
botocore
/Apache License 2.0 -
dateutil
/PSF License -
docutils
/BSD License, GNU General Public License (GPL), Python Software Foundation License, Public Domain -
jmespath
/MIT License -
s3transfer
/Apache License 2.0 -
urllib3
/MIT License
Deep Neural Network Library (DNNL)
/Apache License 2.0 OpenMP* Runtime Library
/See Intel OpenMP Runtime Library licensing. mxnet
/Apache License 2.0 six
/MIT
Intel OpenMP Runtime Library licensing. The Intel® OpenMP* runtime is dual-licensed, with a commercial (COM) license as part of the Intel® Parallel Studio XE Suite products, and a BSD open source (OSS) license.
This connector is released under the
Greengrass Core Software License Agreement
Changelog
The following table describes the changes in each version of the connector.
Version |
Changes |
---|---|
2 |
Added the |
1 |
Initial release. |
A Greengrass group can contain only one version of the connector at a time. For information about upgrading a connector version, see Upgrading connector versions.