

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

# TensorFlow 精简版图像分类
<a name="tensorflow-lite-image-classification-component"></a>

 TensorFlow Lite 图像分类组件 (`aws.greengrass.TensorFlowLiteImageClassification`) 包含示例推理代码，用于使用 [TensorFlow Lite](https://www.tensorflow.org/lite/guide/python) 运行时和样本预训练的 MobileNet 1.0 量化模型执行图像分类推理。此组件使用变体[TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md)和[TensorFlow 精简版运行时](tensorflow-lite-component.md)组件作为依赖项来下载 TensorFlow Lite 运行时和示例模型。

要将此推理组件与自定义训练的 TensorFlow Lite 模型一起使用，[请创建依赖模型存储组件的自定义版本](ml-customization.md#override-public-model-store)。要使用自己的自定义推理代码，您可以使用此组件的配方作为模板来[创建自定义推理组件](ml-customization.md#create-inference-component)。

**Topics**
+ [版本](#tensorflow-lite-image-classification-component-versions)
+ [Type](#tensorflow-lite-image-classification-component-type)
+ [操作系统](#tensorflow-lite-image-classification-component-os-support)
+ [要求](#tensorflow-lite-image-classification-component-requirements)
+ [依赖项](#tensorflow-lite-image-classification-component-dependencies)
+ [配置](#tensorflow-lite-image-classification-component-configuration)
+ [本地日志文件](#tensorflow-lite-image-classification-component-log-file)
+ [更改日志](#tensorflow-lite-image-classification-component-changelog)

## 版本
<a name="tensorflow-lite-image-classification-component-versions"></a>

此组件具有以下版本：
+ 2.1.x

## Type
<a name="tensorflow-lite-image-classification-component-type"></a>

<a name="public-component-type-generic"></a>此<a name="public-component-type-generic-phrase"></a>组件是一个通用组件 (`aws.greengrass.generic`)。[Greengrass Nucleus](greengrass-nucleus-component.md) 运行组件的生命周期脚本。

<a name="public-component-type-more-information"></a>有关更多信息，请参阅[组件类型](develop-greengrass-components.md#component-types)。

## 操作系统
<a name="tensorflow-lite-image-classification-component-os-support"></a>

此组件可以安装在运行以下操作系统的核心设备上：
+ Linux
+ Windows

## 要求
<a name="tensorflow-lite-image-classification-component-requirements"></a>

此组件具有以下要求：<a name="ml-component-requirements"></a>
+ 在运行 Amazon Linux 2 或 Ubuntu 18.04 的 Greengrass 核心设备上，[GNU C 库](https://www.gnu.org/software/libc/)（glibc）版本 2.27 或更高版本已安装在设备上。
+ 在 Armv7l 设备（例如 Raspberry Pi）上，OpenCV-Python 的依赖关系已安装在设备上。运行以下命令以安装依赖关系。

  ```
  sudo apt-get install libopenjp2-7 libilmbase23 libopenexr-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libgtk-3-0 libwebp-dev
  ```
+ 运行 Raspberry Pi OS Bullseye 的 Raspberry Pi 设备必须满足以下要求：
  + NumPy 设备上安装了 1.22.4 或更高版本。Raspberry Pi OS Bullseye 包含的早期版本 NumPy，因此你可以运行以下命令在设备 NumPy 上升级。

    ```
    pip3 install --upgrade numpy
    ```
  + 旧摄像机堆栈已在设备上启用。Raspberry Pi OS Bullseye 包含一个新摄像机堆栈，该堆栈默认处于启用状态且不兼容，因此您必须启用旧摄像机堆栈。<a name="raspberry-pi-bullseye-enable-legacy-camera-stack"></a>

**启用旧摄像机堆栈**

    1. 运行以下命令以打开 Raspberry Pi 配置工具。

       ```
       sudo raspi-config
       ```

    1. 选择**接口选项**。

    1. 选择**旧摄像机**以启用旧摄像机堆栈。

    1. 重启 Raspberry Pi。

## 依赖项
<a name="tensorflow-lite-image-classification-component-dependencies"></a>

部署组件时， AWS IoT Greengrass 还会部署其依赖项的兼容版本。这意味着您必须满足组件及其所有依赖关系的要求，才能成功部署组件。本部分列出了此组件的[已发布版本](#tensorflow-lite-image-classification-component-changelog)的依赖关系，以及定义每个依赖关系的组件版本的语义版本约束。您还可以在 [AWS IoT Greengrass 控制台](https://console.aws.amazon.com//greengrass)中查看每个组件版本的依赖关系。在组件详细信息页面上，查找**依赖关系**列表。

------
#### [ 2.1.11 and 2.1.12 ]

下表列出了此组件的版本 2.1.11 和 2.1.12 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.13.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.10 ]

下表列出了此组件的版本 2.1.10 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.12.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.9 ]

下表列出了此组件的版本 2.1.9 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.11.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.8 ]

下表列出了此组件的版本 2.1.8 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.10.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.7 ]

下表列出了此组件的版本 2.1.7 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.9.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.6 ]

下表列出了此组件的版本 2.1.6 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.8.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.5 ]

下表列出了此组件的版本 2.1.5 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.7.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.4 ]

下表列出了此组件的版本 2.1.4 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.6.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.3 ]

下表列出了此组件的版本 2.1.3 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.5.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.2 ]

下表列出了此组件的版本 2.1.2 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.4.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.1 ]

下表列出了此组件的版本 2.1.1 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.3.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------
#### [ 2.1.0 ]

下表列出了此组件版本 2.1.0 的依赖关系。


| 依赖关系 | 兼容版本 | 依赖关系类型 | 
| --- | --- | --- | 
| [Greengrass Nucleus](greengrass-nucleus-component.md) | >=2.0.0 <2.2.0 | 软性 | 
| [TensorFlow 精简版图像分类模型存储](tensorflow-lite-image-classification-model-store-component.md) | >=2.1.0 <2.2.0 | 硬性 | 
| [TensorFlow 精简版](tensorflow-lite-component.md) | >=2.5.0 <2.6.0 | 硬性 | 

------

## 配置
<a name="tensorflow-lite-image-classification-component-configuration"></a>

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

`accessControl`  
<a name="ml-config-accesscontrol-desc"></a>（可选）包含允许组件将消息发布到默认通知主题的[授权策略](interprocess-communication.md#ipc-authorization-policies)的对象。  
默认值：  

```
{
   "aws.greengrass.ipc.mqttproxy": {
      "aws.greengrass.TensorFlowLiteImageClassification:mqttproxy:1": {
         "policyDescription": "Allows access to publish via topic ml/tflite/image-classification.",
         "operations": [
            "aws.greengrass#PublishToIoTCore"
         ],
         "resources": [
            "ml/tflite/image-classification"
         ]
      }
   }
}
```

`PublishResultsOnTopic`  
<a name="ml-config-publishresultsontopic-desc"></a>（可选）要将推理结果发布到的主题。如果修改此值，则还必须修改 `accessControl` 参数中 `resources` 的值，以匹配您的自定义主题名称。  
默认值：`ml/tflite/image-classification`

`Accelerator`  <a name="ml-config-accelerator"></a>
要使用的加速器。支持的值为 `cpu` 和 `gpu`。  
依赖模型组件中的示例模型仅支持 CPU 加速。要对不同的自定义模型使用 GPU 加速，请[创建自定义模型组件](ml-customization.md#override-public-model-store)以覆盖公共模型组件。  
默认值：`cpu`

`ImageDirectory`  
<a name="ml-config-imagedirectory-desc"></a>（可选）设备上推理组件读取图像的文件夹路径。您可以将此值修改为设备上您有权 read/write 访问的任何位置。  
<a name="ml-config-imagedirectory-img-default"></a>默认值：`/greengrass/v2/packages/artifacts-unarchived/component-name/image_classification/sample_images/`  
如果将 `UseCamera` 的值设置为 `true`，则忽略此配置参数。

`ImageName`  
<a name="ml-config-imagename-desc"></a>（可选）推理组件用作预测输入的图像名称。该组件可在 `ImageDirectory` 中指定的文件夹中查找图像。默认情况下，该组件使用默认图像目录中的示例图像。 AWS IoT Greengrass 支持以下图像格式：`jpeg`、`jpg``png`、和`npy`。  
<a name="ml-config-imagename-img-default"></a>默认值：`cat.jpeg`  
如果将 `UseCamera` 的值设置为 `true`，则忽略此配置参数。

`InferenceInterval`  <a name="ml-config-inferenceinterval"></a>
（可选）推理代码进行的每次预测之间的时间（以秒为单位）。示例推理代码无限期运行，并在指定的时间间隔内重复其预测。例如，如果您想使用摄像机拍摄的图像进行实时预测，则可以将其更改为较短的间隔。  
默认值：`3600`

`ModelResourceKey`  <a name="ml-config-modelresourcekey"></a>
<a name="ml-config-modelresourcekey-desc"></a>（可选）在依赖的公共模型组件中使用的模型。仅当使用自定义组件覆盖公共模型组件时，才修改此参数。  
默认值：  

```
{
    "model": "TensorFlowLite-Mobilenet"
}
```

`UseCamera`  <a name="ml-config-usecamera"></a>
（可选）字符串值，用于定义是否使用连接到 Greengrass 核心设备的摄像机拍摄的图像。支持的值为 `true` 和 `false`。  
当您将此值设置为 `true` 时，示例推理代码将访问设备上的摄像机，并在本地对捕获的图像运行推理。`ImageName` 和 `ImageDirectory` 参数的值将被忽略。确保运行此组件的用户 read/write 可以访问相机存储捕获图像的位置。  
默认值：`false`  
当您查看此组件的配方时，`UseCamera` 配置参数不会显示在默认配置中。但是，在部署组件时，可以在[配置合并更新](update-component-configurations.md)中修改此参数的值。  
如果将 `UseCamera` 设置为 `true`，则还必须创建符号链接，以使推理组件能够从运行时组件创建的虚拟环境中访问您的摄像机。有关将摄像机与示例推理组件结合使用的更多信息，请参阅 [更新组件配置](ml-tutorial-image-classification-camera.md)。

## 本地日志文件
<a name="tensorflow-lite-image-classification-component-log-file"></a>

此组件使用以下日志文件。

------
#### [ Linux ]

```
/greengrass/v2/logs/aws.greengrass.TensorFlowLiteImageClassification.log
```

------
#### [ Windows ]

```
C:\greengrass\v2\logs\aws.greengrass.TensorFlowLiteImageClassification.log
```

------

**查看此组件的日志**
+ 在核心设备上运行以下命令可实时查看此组件的日志文件。将`/greengrass/v2`或*C:\$1greengrass\$1v2*替换为 AWS IoT Greengrass 根文件夹的路径。

------
#### [ Linux ]

  ```
  sudo tail -f /greengrass/v2/logs/aws.greengrass.TensorFlowLiteImageClassification.log
  ```

------
#### [ Windows (PowerShell) ]

  ```
  Get-Content C:\greengrass\v2\logs\aws.greengrass.TensorFlowLiteImageClassification.log -Tail 10 -Wait
  ```

------

## 更改日志
<a name="tensorflow-lite-image-classification-component-changelog"></a>

下表介绍每个组件版本的更改。


|  版本  |  更改  | 
| --- | --- | 
|  2.1.12  |  对 Greengrass Nucleus 版本 2.12.5 发行版进行了版本更新。  | 
|  2.1.11  | 版本更新为 Greengrass Nucleus 版本 2.12.0 发布版。 | 
|  2.1.10  | 版本更新为 Greengrass Nucleus 版本 2.11.0 发布版。 | 
|  2.1.9  | 版本更新为 Greengrass Nucleus 版本 2.10.0 发布版。 | 
|  2.1.8  | 对 Greengrass Nucleus版本 2.9.0 发行版进行了版本更新。 | 
|  2.1.7  | 版本更新为 Greengrass Nucleus 版本 2.8.0 发布版。 | 
|  2.1.6  |  版本更新为 Greengrass Nucleus 版本 2.7.0 发布版。  | 
|  2.1.5  |  版本更新为 Greengrass Nucleus 版本 2.6.0 发布版。  | 
|  2.1.4  |  版本更新为 Greengrass Nucleus 版本 2.5.0 发布版。  | 
|  2.1.3  |  版本更新为 Greengrass Nucleus 版本 2.4.0 发布版。  | 
|  2.1.2  |  版本更新为 Greengrass Nucleus 版本 2.3.0 发布版。  | 
|  2.1.1  |  对 Greengrass Nucleus 版本 2.2.0 发行版进行了版本更新。  | 
|  2.1.0  |  初始版本。  | 