Run your fruit detector on the edge
Sketchnote by Nitya Narasimhan. Click the image for a larger version.
This video gives an overview of running image classifiers on IoT devices, the topic that is covered in this lesson.
Pre-lecture quiz
Introduction
In the last lesson you used your image classifier to classify ripe and unripe fruit, sending an image captured by the camera on your IoT device over the internet to a cloud service. These calls take time, cost money, and depending on the kind of image data you are using, could have privacy implications.
In this lesson you will learn about how to run machine learning (ML) models on the edge - on IoT devices running on your own network rather than in the cloud. You will learn the benefits and drawbacks of edge computing versus cloud computing, how to deploy your AI model to the edge, and how to access it from your IoT device.
In this lesson we'll cover:
- Edge computing
- Azure IoT Edge
- Register an IoT Edge device
- Set up an IoT Edge device
- Export your model
- Prepare your container for deployment
- Deploy your container
- Use your IoT Edge device
Edge computing
Edge computing involves having computers that process IoT data as close as possible to where the data is generated. Instead of having this processing in the cloud, it is moved to the edge of the cloud - your internal network.
In the lessons so far, you have had devices gathering data and sending data to the cloud to be analyzed, running serverless functions or AI models in the cloud.
Edge computing involves moving some of the cloud services off the cloud and onto computers running on the same network as the IoT devices, only communicating with the cloud if needed. For example, you can run AI models on edge devices to analyse fruit for ripeness, and only send analytics back to the cloud, such as the number of ripe pieces of fruit vs unripe.
✅ Think about the IoT applications you have built so far. Which parts of them could be moved to the edge.
Upsides
The upsides of edge computing are:
-
Speed - edge computing is ideal for time-sensitive data as actions are done on the same network as the device, rather than making calls across the internet. This enables higher speeds as internal networks can run at substantially faster speeds than internet connections, with the data travelling much shorter distance.
💁 Despite optical cables being used for internet connections allowing data to travel at the speed of light, data can take time to travel around the world to cloud providers. For example, if you are sending data from Europe to cloud services in the US it takes at least 28ms for the data to cross the atlantic in an optical cable, and that is ignoring the time taken to get the data to the transatlantic cable, convert from electrical to light signals and back again the other side, then from the optical cable to the cloud provider.
Edge computing also requires less network traffic, reducing the risk of your data slowing down due to congestion on the limited bandwidth available for an internet connection.
-
Remote accessibility - edge compute works when you have limited or no connectivity, or connectivity is too expensive to use continually. For example when working in humanitarian disaster areas where infrastructure is limited, or in developing nations.
-
Lower costs - performing data collection, storage, analysis, and triggering actions on edge device reduces usage of cloud services which can reduce the overall cost of your IoT application. There has been a recent rise in devices designed for edge computing, such as AI accelerator boards like the Jetson Nano from NVIDIA, which can run AI workloads using GPU-based hardware on devices that cost less than US$100.
-
Privacy and security - with edge compute, data stays on your network and is not uploaded to the cloud. This is often preferred for sensitive and personally identifiable information, especially because data does not need to be stored after it has been analyzed, which greatly reduces the risk of data leaks. Examples include medical data and security camera footage.
-
Handling insecure devices - if you have devices with known security flaws that you don't want to connect directly to your network or the internet, then you can connect them to a separate network to a gateway IoT Edge device. This edge device can then also have a connection to your wider network or the internet, and manage the data flows back and forth.
-
Support for incompatible devices - if you have devices that cannot connect to IoT Hub, for example devices that can only connect using HTTP connections or devices that only have Bluetooth to connect, you can use an IoT edge device as a gateway device, forwarding on messages to IoT Hub.
✅ Do some research: What other upsides might there be to edge computing?
Downsides
There are downsides to edge computing, where the cloud may be a preferred option:
-
Scale and flexibility - cloud computing can adjust to network and data needs in real-time by adding or reducing servers and other resources. To add more edge computers requires manually adding more devices.
-
Reliability and resiliency - cloud computing provides multiple servers often in multiple locations for redundancy and disaster recovery. To have the same level of redundancy on the edge requires large investments and a lot of configuration work.
-
Maintenance - cloud service providers provide system maintenance and updates.
✅ Do some research: What other downsides might there be to edge computing?
The downsides are really the opposite of the upsides of using the cloud - you have to build and manage these devices yourself, rather than relying on the expertise and scale of cloud providers.
Some of the risks are mitigated by the very nature of edge computing. For example, if you have an edge device running in a factory gathering data from machinery, you don't need to think about some disaster recovery scenarios. If the power to the factory goes out then you don't need a backup edge device as the machines that generate the data the edge device processes will also be without power.
For IoT systems, you'll often want a blend of cloud and edge computing, leveraging each service based on the needs of the system, its customers, and its maintainers.
Azure IoT Edge
Azure IoT Edge is a service that can help you to move workloads out of the cloud and to the edge. You set up a device as an edge device, and from the cloud you can deploy code to that edge device. This allows you to mix the capabilities of the cloud and the edge.
🎓 Workloads is a term for any service that does some kind of work, such as AI models, applications, or serverless functions.
For example, you can train an image classifier in the cloud, then from the cloud deploy it to an edge device. Your IoT device then sends images to the edge device for classification, rather than sending the images over the internet. If you need to deploy a new iteration of the model, you can train it in the cloud and use IoT Edge to update the model on the edge device to your new iteration.
🎓 Software that is deployed to IoT Edge is known as modules. By default IoT Edge runs modules that communicate with IoT Hub, such as the
edgeAgent
andedgeHub
modules. When you deploy an image classifier, this is deployed as an additional module.
IoT Edge is built into IoT Hub, so you can manage edge devices using the same service you would use to manage IoT devices, with the same level of security.
IoT Edge runs code from containers - self contained applications that are run in isolation from the rest of the applications on your computer. When you run a container it act's like a separate computer running inside your computer, with it's own software, services and applications running. Most of the time containers cannot access anything on your computer unless you choose to share things like a folder with the container. The container then exposes services via an open port that you can connect to or expose to your network.
For example, you can have a container with a web site running on port 80, the default HTTP port, and you can then expose it from your computer also on port 80.
✅ Do some research: Read up on containers and services such as Docker or Moby.
You can use Custom Vision to download image classifiers and deploy them as containers, either running direct to a device or deployed via IoT Edge. Once they are running in a container, they can be accessed using the same REST API as the cloud version, but with the endpoint pointing to the Edge device running the container.
Register an IoT Edge device
To use an IoT Edge device, it needs to be registered in IoT Hub.
Task - register an IoT Edge device
-
Create an IoT Hub in the
fruit-quality-detector
resource group. Give it a unique name based aroundfruit-quality-detector
. -
Register an IoT Edge device called
fruit-quality-detector-edge
in your IoT Hub. The command to do this is similar to the one used to register a non-edge device, except you pass the--edge-enabled
flag.az iot hub device-identity create --edge-enabled \
--device-id fruit-quality-detector-edge \
--hub-name <hub_name>Replace
<hub_name>
with the name of your IoT Hub. -
Get the connection string for your device using the following command:
az iot hub device-identity connection-string show --device-id fruit-quality-detector-edge \
--output table \
--hub-name <hub_name>Replace
<hub_name>
with the name of your IoT Hub.Take a copy of the connection string that is shown in the output.
Set up an IoT Edge device
Once you have created the edge device registration in your IoT Hub, you can set up the edge device.
Task - Install and start the IoT Edge Runtime
The IoT Edge runtime only runs Linux containers. It can be run on Linux, or on Windows using Linux Virtual Machines.
-
If you are using a Raspberry Pi as your IoT device, then this runs a supported version of Linux and can host the IoT Edge runtime. Follow the install Azure IoT Edge for Linux guide on Microsoft docs to install IoT Edge and set the connection string.
💁 Remember, Raspberry Pi OS is a variant of Debian Linux.
-
If you are not using a Raspberry Pi, but have a Linux computer, you can run the IoT Edge runtime. Follow the install Azure IoT Edge for Linux guide on Microsoft docs to install IoT Edge and set the connection string.
-
If you are using Windows, you can install the IoT Edge runtime in a Linux Virtual Machine by following the install and start the IoT Edge runtime section of the deploy your first IoT Edge module to a Windows device quickstart on Microsoft docs. You can stop when you reach the Deploy a module section.
-
If you are using macOS, you can create a virtual machine (VM) in the cloud to use for your IoT Edge device. These are computers you can create in the cloud and access over the internet. You can create a Linux VM that has IoT Edge installed. Follow the create a virtual machine running IoT Edge guide for instructions on how to do this.