Machine learning edge inference

Updated at:
Copy as MD

IoT Edge enables you to deploy machine learning (ML) models from the cloud to the edge as edge applications. You can then perform ML inference at the edge. This feature is ideal for services that run on the edge, require real-time processing, and handle large volumes of data, such as computer vision.

Prerequisites

Background information

You can train your inference models on Alibaba Cloud Platform for AI or another platform. Host the trained models and related code on Alibaba Cloud services, such as Function Compute (FC), Object Storage Service (OSS), or in a container image. Then, in an IoT Edge instance, deploy the models to a gateway as an edge application. After the gateway performs inference using the local model, it uploads the results to IoT Platform.

Link IoT Edge ML Inference工作原理

This topic demonstrates how to use machine learning inference on IoT Edge by deploying a deep learning object detection model based on TensorFlow Lite to a Raspberry Pi 4B.

Step 1: Configure the Raspberry Pi and install the edge inference runtime

Use a Secure Shell (SSH) tool to connect to the Raspberry Pi terminal and run the commands in the following steps.

  1. Open the Raspberry Pi configuration tool and enable the camera.

    1. Run the following command to open the Raspberry Pi configuration tool.

      sudo raspi-config
    2. Follow the prompts to select Interfacing Options and click Select.

      Interfacing Options

    3. Select Camera and click Select to enable the camera.

      Camera

    4. Follow the prompts to click Finish and restart the Raspberry Pi.

    5. Run the following command in the Raspberry Pi shell terminal to verify that the camera is working correctly using the built-in raspistill tool.

      The command outputs camera information to the terminal and saves an image file named cam.jpg to the current directory.

      raspistill -v -o cam.jpg
  2. Install the TensorFlow Lite interpreter.

    1. Run the following command in the Raspberry Pi shell to download the installation package for the TensorFlow Lite interpreter.

      curl -O https://iotedge-web.oss-cn-shanghai.aliyuncs.com/public/LeMLInterpreter/ARMv7hf/linkedge_ml_tflite_raspi4_cp3x_armv7hf_installer.tar.gz
    2. Unzip and install the package.

      tar xzvf linkedge_ml_tflite_raspi4_cp37_armv7hf_installer.tar.gz 
      cd linkedge_ml_tflite_raspi4_cp37_armv7hf_installer/
      ./le_ml_installer.sh

      If the system displays the following output, the TensorFlow Lite interpreter is installed.

      The TensorFlow Lite interpreter is installed successfully.

Step 2: Publish the detector device driver in the cloud

  1. Download the detector device driver package object_detector_driver.zip.

  2. Log on to the IoT Edge console.

  3. In the navigation pane on the left, click Driver Management.

  4. Add a custom driver. For more information, see Publish in the cloud.

    Configure the parameters as described in the following table.

    Table 1. Driver information parameters

    Parameter

    Description

    Driver Name

    Enter a custom name for the driver, such as obj_detector_driver.

    Protocol Type

    Select Custom.

    Language

    Select Python 3.5.

    Is the driver built-in?

    Select No.

    Driver File

    Click Upload File and upload the object_detector_driver.zip file that you downloaded.

    Driver Version

    Set the value to v1.0.0.

    Compatible Edge Version

    Select 2.4.0 and later.

    Driver Version Description

    Enter a description for the driver and version. This parameter can be left empty.

    You do not need to configure the other parameters.

Step 3: Assign the detector device driver to the edge instance

  1. In the navigation pane on the left, click Edge Instances. Find the edge instance that you created in the "Prerequisites" section and click View in the Actions column.

  2. On the Instance Details page, on the Devices and Drivers tab, click the + icon to the right of All Drivers.

  3. In the Assign Driver panel, select Custom Driver, find the obj_detector_driver driver, and click Assign in the Actions column. Then, click OK.

    分配检测器设备驱动

  4. Click Assign Sub-device. In the Assign Sub-device panel that appears, click Add Sub-device to add a sub-device for the obj_detector_driver driver to the edge instance.

    边缘推理-分配子设备

  5. In the Add Device dialog box, click Create Product to create a detector product.

    边缘推理-创建产品

    In the Create Product dialog box, set the parameters and click OK.

    Table 2. Parameter descriptions

    Parameter

    Description

    Product Name

    Set the value to detector.

    Category

    Select a category to define a Thing Specification Language (TSL) model for the product. Select Custom Category.

    Access gateway protocol

    Select Custom.

  6. In the Add Device dialog box, the product that you created is automatically selected for the Product parameter. Click Go to Configure next to the Product parameter to add custom features. For more information, see Add a TSL model for a single product.

    Configure the two properties as shown in the following figures.

    • Object category property:物体类别属性

    • Detection score property:检测得分属性

  7. Return to the Add Device dialog box on the Instance Details page in the IoT Edge console, and add a device for the detector product.

    添加tflite_detector设备

  8. Assign the new tflite_detector device to the edge instance.

Step 4: Create the edge inference function

  1. Download the edge inference function code package object_detector_app.zip.

  2. Log on to the Function Compute console.
    If you have not activated this service, read the terms and select I have read and agree to the terms. Then, click Activate Now.
  3. (Optional) In the navigation pane on the left, click Services & Functions. On the Services & Functions page, in the Service List section, click Create Service to create a service.
    The Service Name parameter is required. Set this parameter to EdgeFC. You can configure the other parameters as needed or use their default settings.
    Note
    • If this is your first time creating a service in Function Compute, follow the configuration wizard.
    • If you have already created the EdgeFC service for other scenarios or miniprogram examples, you do not need to create it again.
  4. After the service is created, on the Services & Functions page, find the EdgeFC service and click Create Function.
  5. On the Create Function page, click Configure and Deploy in the Event Function section.
  6. Configure the basic management parameters for the edge inference function.

    Parameter

    Description

    Function Type
    Keep the default option.
    Service
    Select the EdgeFC service that you created.
    Function Name

    Set the value to object_detector_app.

    Runtime

    Set the runtime for the function and select a method to upload code. For this example, select Python 3.

    To the right of Upload Code, select Upload Code Package. Click Upload Code and upload the object_detector_app.zip package that you downloaded in Step 1.

    Handler
    Use the default value: index.handler.

    You can configure the other parameters as needed or use their default settings. For more information, see Function Compute.

    After you confirm the function information, click Create.

  7. After the function is created, you are redirected to the function details page. On the Code tab, in the Code Execution Management section, select the Edit Online radio button to view the source code.

Step 5: Assign the function to the edge instance

  1. Log on to the IoT Edge console.
  2. In the navigation pane on the left, click Application Management.
  3. Use the function that you created in Step 4 to create a Function Compute edge application. For more information, see Function Compute applications.

    The following table describes the application information parameters.

    Parameter

    Description

    Application Name

    Enter a name for your application, such as le_object_detector.

    Application Type

    Select Function Compute.

    Region

    Select the region where your service was created.

    Service

    Select the EdgeFC service.

    Function

    Select the object_detector_app function.

    Authorization

    Select AliyunIOTAccessingFCRole.

    Application Version

    Set the application version. The version number must be unique for this application. You cannot have two identical version numbers for the same application.

    In the Function Configuration section, set Enable Default Configurations to Yes. The other parameters can be left at their default settings.

  4. In the navigation pane on the left, click Edge Instances.
  5. Find the edge instance that you created in the "Prerequisites" section and click View.
  6. On the Instance Details page, go to the Edge Applications tab and click Assign Application.
  7. Assign the le_object_detector edge inference application to the edge instance and click Close.

Step 6: Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper-right corner. In the dialog box that appears, click OK to deploy resources, such as sub-devices and functions, to the edge.
  2. After the deployment is complete, go to the Devices & Drivers tab. The status of the tflite_detector device changes to Online.

  3. Click View to the right of the tflite_detector device. You are redirected to the Device Details page.

  4. On the Device Details page, on the TSL Data > Running Status tab, you can view the edge inference results.

    Place a common object or stand in front of the Raspberry Pi camera. The le_object_detector Function Compute application detects the object or face and reports the results to IoT Platform.边缘推理-运行状态

    You have now deployed a machine learning model as an edge application and performed inference on the edge.