DLR 图像分类 - AWS IoT Greengrass
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

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

DLR 图像分类

您可以使用 DLR 图像分类组件 (aws.greengrass.DLRImageClassification) 在支持的设备上执行示例图像分类推理。此组件安装 DLR 及其依赖项,并下载示例 ML 模型以执行此推理。

此示例推理组件使用 变体图像分类模型存储DLR 安装程序 组件作为依赖项来执行推理。要使用不同的模型或运行时执行图像分类,您可以创建从属变体组件的私有版本。如果要创建自己的私有推理组件,您可以使用此组件的配方作为模板。

此组件具有以下版本:

  • 2.0.x

Requirements

公共机器学习推理组件需要运行以下受支持平台之一的 Greengrass 核心设备。有关更多信息,请参阅设置 AWS AWS IoT Greengrass 核心设备

Configuration

此组件提供以下配置参数,您可以在部署组件时自定义这些参数。

Accelerator

请勿修改。目前,加速器唯一支持的值为 cpu,因为从属模型组件中的模型仅为 CPU 加速器编译。

MLRootPath

(可选)设备上的文件夹的路径,其中的推理组件读取图像并写入推理结果。您可以将此值修改为设备上您拥有读/写访问权限的任何位置。

默认值: $HOME/greengrass_ml

ImageName

(可选) 推理组件用作进行预测的输入的图像。您可以将此值修改为位于 中的任何映像MLRootPath/images。AWS AWS IoT Greengrass 支持以下格式jpeg:、jpgpngnpy

默认值:静态映像

InferenceInterval

(可选) 推理代码进行的每个预测之间的时间(以秒为单位)。示例推理代码无限期运行并在指定的时间间隔内重复其预测。例如,如果要将摄像机拍摄的图像挂载到 MLRootPath/images 并使用这些图像进行实时预测,您可以将此设置更改为较短的时间间隔。

默认值: 3600

ModelResourceKey

请勿修改。此对象指定从属公有模型组件中使用的模型。

组件配方

要查看公有组件的最新版本的组件配方,请执行以下操作之一:

  • 使用控制台

    1. Components (组件) 页面上的 Public components (公有组件) 选项卡上,查找并选择公有组件。

    2. 在组件页面上,选择查看配方

  • 使用 AWS CLI

    运行以下命令以检索公有变体组件的组件配方。此命令会将组件配方写入您在命令中提供的 JSON 或 YAML 配方文件。

    Linux, macOS, or Unix
    aws greengrassv2 get-component \ --arn <arn> \ --recipe-output-format <recipe-format> \ --query recipe \ --output text | base64 --decode > <recipe-file>
    Windows command prompt
    aws greengrassv2 get-component ^ --arn <arn> ^ --recipe-output-format <recipe-format> ^ --query recipe ^ --output text > <recipe-file>.base64 certutil -decode <recipe-file>.base64 <recipe-file>

    替换命令中的值,如下所示:

    • <arn>。公有组件的 Amazon 资源名称 (ARN)。

    • <recipe-format>。您要用来创建配方文件的格式。支持的值为 JSONYAML

    • <recipe-file>。格式为 的配方的名称<component-name>-<component-version>

以下摘录显示了 组件的版本 2.0.0 的组件配方。

JSON
{ "RecipeFormatVersion": "2020-01-25", "ComponentName": "aws.greengrass.DLRImageClassification", "ComponentVersion": "2.0.0", "ComponentDescription": "Sample Image classification inference using DLR and amazon resnet50 default model.", "ComponentPublisher": "Amazon", "ComponentDependencies": { "variant.DLR": { "VersionRequirement": "~1.3.0", "DependencyType": "HARD" }, "variant.ImageClassification.ModelStore": { "VersionRequirement": "~2.0.0", "DependencyType": "HARD" }, "aws.greengrass.Nucleus": { "VersionRequirement": "~2.0.0", "DependencyType": "SOFT" } }, "ComponentConfiguration": { "DefaultConfiguration": { "Accelerator": "cpu", "MLRootPath": "$HOME/greengrass_ml", "ImageName": "cat.jpeg", "InferenceInterval": 3600, "ModelResourceKey": { "armv7l": "DLR-resnet50-armv7l-cpu-ImageClassification", "x86_64": "DLR-resnet50-x86_64-cpu-ImageClassification" } } }, "Manifests": [ { "Platform": { "os": "linux", "architecture": "arm" }, "Name": "32-bit armv7l - Linux (raspberry pi)", "Artifacts": [ { "URI": "s3://$bucketName$/$sampleArtifactsDirectory$/image_classification.zip", "Unarchive": "ZIP" }, { "URI": "s3://$bucketName$/$sampleArtifactsDirectory$/init.sh" } ], "Lifecycle": { "Install": { "RequiresPrivilege": true, "script": "bash {artifacts:path}/init.sh {artifacts:decompressedPath}/image_classification/sample_images {configuration:/MLRootPath}\nbash {variant.DLR:artifacts:decompressedPath}/installer/installer.sh -a {configuration:/Accelerator} -p {configuration:/MLRootPath}", "timeout": 900 }, "Run": { "RequiresPrivilege": true, "script": ". {configuration:/MLRootPath}/greengrass_ml_dlr_venv/bin/activate\npython3 {artifacts:decompressedPath}/image_classification/inference.py -a {configuration:/Accelerator} -m {variant.ImageClassification.ModelStore:artifacts:decompressedPath}/{configuration:/ModelResourceKey/armv7l} -p {configuration:/MLRootPath} -i {configuration:/ImageName} -s {configuration:/InferenceInterval}" }, "shutdown": { "RequiresPrivilege": true, "script": "deactivate" } } }, { "Platform": { "os": "linux", "architecture": "amd64" }, "Name": "64-bit x86_64 - Linux (ubuntu, deeplens, amazon linux 2)", "Artifacts": [ { "URI": "s3://$bucketName$/$sampleArtifactsDirectory$/image_classification.zip", "Unarchive": "ZIP" }, { "URI": "s3://$bucketName$/$sampleArtifactsDirectory$/init.sh" } ], "Lifecycle": { "Install": { "RequiresPrivilege": true, "script": "bash {artifacts:path}/init.sh {artifacts:decompressedPath}/image_classification/sample_images {configuration:/MLRootPath}\nbash {variant.DLR:artifacts:decompressedPath}/installer/installer.sh -a {configuration:/Accelerator} -p {configuration:/MLRootPath} -e {variant.DLR:artifacts:path}/environment.yaml", "timeout": 900 }, "Run": { "RequiresPrivilege": true, "script": "export PATH=\"{configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin:$PATH\"\neval \"$({configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin/conda shell.bash hook)\"\nconda activate greengrass_ml_dlr_conda\npython3 {artifacts:decompressedPath}/image_classification/inference.py -a {configuration:/Accelerator} -m {variant.ImageClassification.ModelStore:artifacts:decompressedPath}/{configuration:/ModelResourceKey/x86_64} -p {configuration:/MLRootPath} -i {configuration:/ImageName} -s {configuration:/InferenceInterval}" }, "shutdown": { "RequiresPrivilege": true, "script": "export PATH=\"{configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin:$PATH\"\neval \"$({configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin/conda shell.bash hook)\"\nconda deactivate" } } } ] }
YAML
RecipeFormatVersion: "2020-01-25" ComponentName: aws.greengrass.DLRImageClassification ComponentVersion: "2.0.0" ComponentDescription: Sample Image classification inference using DLR and amazon resnet50 default model. ComponentPublisher: Amazon ComponentDependencies: variant.DLR: VersionRequirement: "~1.3.0" DependencyType: HARD variant.ImageClassification.ModelStore: VersionRequirement: "~2.0.0" DependencyType: HARD aws.greengrass.Nucleus: VersionRequirement: "~2.0.0" DependencyType: SOFT ComponentConfiguration: DefaultConfiguration: Accelerator: "cpu" MLRootPath: "$HOME/greengrass_ml" ImageName: "cat.jpeg" InferenceInterval: 3600 ModelResourceKey: armv7l: "DLR-resnet50-armv7l-cpu-ImageClassification" x86_64: "DLR-resnet50-x86_64-cpu-ImageClassification" Manifests: - Platform: os: linux architecture: arm Name: 32-bit armv7l - Linux (raspberry pi) Artifacts: - URI: s3://$bucketName$/$sampleArtifactsDirectory$/image_classification.zip Unarchive: ZIP - URI: s3://$bucketName$/$sampleArtifactsDirectory$/init.sh Lifecycle: Install: RequiresPrivilege: true script: |- bash {artifacts:path}/init.sh {artifacts:decompressedPath}/image_classification/sample_images {configuration:/MLRootPath} bash {variant.DLR:artifacts:decompressedPath}/installer/installer.sh -a {configuration:/Accelerator} -p {configuration:/MLRootPath} timeout: 900 Run: RequiresPrivilege: true script: |- . {configuration:/MLRootPath}/greengrass_ml_dlr_venv/bin/activate python3 {artifacts:decompressedPath}/image_classification/inference.py -a {configuration:/Accelerator} -m {variant.ImageClassification.ModelStore:artifacts:decompressedPath}/{configuration:/ModelResourceKey/armv7l} -p {configuration:/MLRootPath} -i {configuration:/ImageName} -s {configuration:/InferenceInterval} shutdown: RequiresPrivilege: true script: |- deactivate - Platform: os: linux architecture: amd64 Name: 64-bit x86_64 - Linux (ubuntu, deeplens, amazon linux 2) Artifacts: - URI: s3://$bucketName$/$sampleArtifactsDirectory$/image_classification.zip Unarchive: ZIP - URI: s3://$bucketName$/$sampleArtifactsDirectory$/init.sh Lifecycle: Install: RequiresPrivilege: true script: |- bash {artifacts:path}/init.sh {artifacts:decompressedPath}/image_classification/sample_images {configuration:/MLRootPath} bash {variant.DLR:artifacts:decompressedPath}/installer/installer.sh -a {configuration:/Accelerator} -p {configuration:/MLRootPath} -e {variant.DLR:artifacts:path}/environment.yaml timeout: 900 Run: RequiresPrivilege: true script: |- export PATH="{configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin:$PATH" eval "$({configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin/conda shell.bash hook)" conda activate greengrass_ml_dlr_conda python3 {artifacts:decompressedPath}/image_classification/inference.py -a {configuration:/Accelerator} -m {variant.ImageClassification.ModelStore:artifacts:decompressedPath}/{configuration:/ModelResourceKey/x86_64} -p {configuration:/MLRootPath} -i {configuration:/ImageName} -s {configuration:/InferenceInterval} shutdown: RequiresPrivilege: true script: |- export PATH="{configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin:$PATH" eval "$({configuration:/MLRootPath}/greengrass_ml_dlr_conda/bin/conda shell.bash hook)" conda deactivate