Tutorial: Deploy Azure Machine Learning as an IoT Edge module (preview)

You can use IoT Edge modules to deploy code that implements your business logic directly to your IoT Edge devices. This tutorial walks you through deploying an Azure Machine Learning module that predicts when a device fails based on simulated machine temperature data. For more information about Azure Machine Learning service on IoT Edge, see Azure Machine Learning documentation.

The Azure Machine Learning module that you create in this tutorial reads the environmental data generated by your device and labels the messages as anomalous or not.

In this tutorial, you learn how to:

  • Create an Azure Machine Learning module
  • Push a module container to an Azure container registry
  • Deploy an Azure Machine Learning module to your IoT Edge device
  • View generated data

Note

Azure Machine Learning modules on Azure IoT Edge are in public preview.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

An Azure IoT Edge device:

  • You can use an Azure virtual machine as an IoT Edge device by following the steps in the quickstart for Linux.
  • The Azure Machine Learning module doesn't support Windows containers.
  • The Azure Machine Learning module doesn't support ARM processors.

Cloud resources:

  • A free or standard-tier IoT Hub in Azure.
  • An Azure Machine Learning workspace. Follow the instructions in Use the Azure portal to get started with Azure Machine Learning to create one and learn how to use it.
    • Make a note of the workspace name, resource group, and subscription ID. These values are all available on the workspace overview in the Azure portal. You'll use these values later in the tutorial to connect an Azure notebook to your workspace resources.

Disable process identification

Note

While in preview, Azure Machine Learning does not support the process identification security feature enabled by default with IoT Edge. Below are the steps to disable it. This is however not suitable for use in production. These steps are only necessary on Linux devices.

To disable process identification on your IoT Edge device, you'll need to provide the IP address and port for workload_uri and management_uri in the connect section of the IoT Edge daemon configuration.

Get the IP address first. Enter ifconfig in your command line and copy the IP address of the docker0 interface.

Edit the IoT Edge daemon configuration file:

sudo nano /etc/iotedge/config.yaml

Update the connect section of the configuration with your IP address. For example:

connect:
  management_uri: "http://172.17.0.1:15580"
  workload_uri: "http://172.17.0.1:15581"

Enter the same addresses in the listen section of the configuration. For example:

listen:
  management_uri: "http://172.17.0.1:15580"
  workload_uri: "http://172.17.0.1:15581"

Save and close the configuration file.

Create an environment variable IOTEDGE_HOST with the management_uri address (To set it permanently, add it to /etc/environment). For example:

export IOTEDGE_HOST="http://172.17.0.1:15580"

Restart the IoT Edge service for the changes to take effect.

sudo systemctl restart iotedge

Create and deploy Azure Machine Learning module

In this section, you convert trained machine learning model files and into an Azure Machine Learning service container. All the components required for the Docker image are in the AI Toolkit for Azure IoT Edge Git repo. Follow these steps to upload that repository into Microsoft Azure Notebooks to create the container and push it to Azure Container Registry.

  1. Navigate to your Azure Notebooks projects. You can get their from your Azure Machine Learning service workspace in the Azure portal or by signing in to Microsoft Azure Notebooks with your Azure account.

  2. Select Upload GitHub Repo.

  3. Provide the following GitHub repository name: Azure/ai-toolkit-iot-edge. Uncheck the Public box if you want to keep your project private. Select Import.

  4. Once the import is finished, navigate into the new ai-toolkit-iot-edge project and open the IoT Edge anomaly detection tutorial folder.

  5. Verify that your project is running. If not, select Run on Free Compute.

    Run on free compute

  6. Open the aml_config/config.json file.

  7. Edit the config file to include the values for your Azure subscription ID, a resource group in your subscription, and your Azure Machine Learning service workspace name. You can get all these values from the Overview section of your workspace in Azure.

  8. Save the config file.

  9. Open the 00-anomaly-detection-tutorial.ipynb file.

  10. When prompted, select the Python 3.6 kernel then select Set Kernel.

  11. Edit the first cell in the notebook according to the instructions in the comments. Use the same resource group, subscription ID, and workspace name that you added to the config file.

  12. Run the cells in the notebook by selecting them and selecting Run or pressing Shift + Enter.

    Tip

    Some of the cells in the anomaly detection tutorial notebook are optional, because they create resources that some users may or may not have yet, like an IoT Hub. If you put your existing resource information in the first cell, you'll receive errors if you run the cells that create new resources because Azure won't create duplicate resources. This is fine, and you can ignore the errors or skip those optional sections entirely.

After completing all the steps in the notebook, you will have trained an anomaly detection model, built it as a Docker container image, and pushed that image to Azure Container Registry. Then, you tested the model and finally deployed it to your IoT Edge device.

View container repository

Check that your container image was successfully created and stored in the Azure container registry that is associated with your machine learning environment. The notebook that you used in the previous section automatically provided the container image and the registry credentials to your IoT Edge device, but you should know where they're stored so that you can find the information yourself later.

  1. In the Azure portal, navigate to your Machine Learning service workspace.

  2. The Overview section lists the workspace details as well its associated resources. Select the Registry value, which should be your workspace name followed by random numbers.

  3. In the container registry, select Repositories. You should see a repository called tempanomalydetection that was created by the notebook you ran in the earlier section.

  4. Select tempanomalydetection. You should see that the repository has one tag: 1.

    Now that you know the registry name, repository name, and tag, you know the full image path of the container. Image paths look like <registry_name>.azurecr.io/tempanomalydetection:1. You can use the image path to deploy this container to IoT Edge devices.

  5. In the container registry, select Access keys. You should see a number of access credentials, including Login server and the Username, and Password for an admin user.

    These credentials can be included in the deployment manifest to give your IoT Edge device access to pull container images from the registry.

Now you know where the Machine Learning container image is stored. The next section walks through steps to see how it's performing as a deployed module on your IoT Edge device.

View generated data

You can view messages being generated by each IoT Edge module, and you can view messages that are delivered to your IoT hub.

View data on your IoT Edge device

On your IoT Edge device, you can view the messages being sent from every individual module.

You may need to use sudo for elevated permissions to run iotedge commands. Signing out and signing back in to your device automatically updates your permissions.

  1. View all modules on your IoT Edge device.

    iotedge list
    
  2. View the messages being sent from a specific device. Use the module name from the output of the previous command.

    iotedge logs <module_name> -f
    

View data arriving at your IoT hub

You can view the device-to-cloud messages that your IoT hub receives by using the Azure IoT Hub Toolkit extension for Visual Studio Code (formerly Azure IoT Toolkit extension).

The following steps show you how to set up Visual Studio Code to monitor device-to-cloud messages that arrive at your IoT hub.

  1. In Visual Studio Code, select IoT Hub Devices.

  2. Select ... then select Set IoT Hub Connection String from the menu.

    Set IoT Hub Connection String

  3. In the text box that opens at the top of the page, enter the iothubowner connection string for your IoT Hub. Your IoT Edge device should appear in the IoT Hub Devices list.

  4. Select ... again then select Start monitoring D2C message.

  5. Observe the messages coming from tempSensor every five seconds. The message body contains a property called anomaly, which the machinelearningmodule provides with a true or false value. The AzureMLResponse property contains the value "OK" if the model ran successfully.

    Azure Machine Learning service response in message body

Clean up resources

If you plan to continue to the next recommended article, you can keep the resources and configurations that you created and reuse them. You can also keep using the same IoT Edge device as a test device.

Otherwise, you can delete the local configurations and the Azure resources that you created in this article to avoid charges.

Delete Azure resources

Deleting Azure resources and resource groups is irreversible. Make sure that you don't accidentally delete the wrong resource group or resources. If you created the IoT hub inside an existing resource group that has resources that you want to keep, delete only the IoT hub resource itself, instead of deleting the resource group.

To delete the resources:

  1. Sign in to the Azure portal and select Resource groups.

  2. Select the name of the resource group that contains your IoT Edge test resources.

  3. Review the list of resources contained in your resource group. If you want to delete all of them, you can select Delete resource group. If you want to delete only some of them, you can click into each resource to delete them individually.

Next steps

In this tutorial, you deployed an IoT Edge module powered by Azure Machine Learning. You can continue on to any of the other tutorials to learn about other ways that Azure IoT Edge can help you turn data into business insights at the edge.