Creating a TensorFlow module for IoT Edge

TensorFlow is a common machine learning library used for many purposes.  Today, I will show how to use it for Image Classification and when combined with Microsoft Custom Computer Vision, to perform scoring on the edge using Azure IoT Edge.

In earlier blogs, I discussed how to use OpenCV to capture video stream and extract images.  With TensorFlow we can use an ‘exported’ Machine Learning module and score on the edge.  In other blogs, I discussed how to create the Custom Computer Vision model.

The diagram below loosely shows the relationship of one module to the next.

Drawing1

Assuming the earlier blogs were followed, I will focus on the specifics of the TensorFlow module and not how to create a Python module from scratch.

Module Creation:

After creating a blank Python IoT Edge Module, copy the code from https://github.com/ksaye/IoTDemonstrations/blob/master/IoTEdge-TensorFlow-Module/main.py into the ‘main.py’ file.

Next, copy the contents from https://github.com/ksaye/IoTDemonstrations/blob/master/IoTEdge-TensorFlow-Module/DockerFile into the DockerFile file.

Last, copy the contents from https://github.com/ksaye/IoTDemonstrations/blob/master/IoTEdge-TensorFlow-Module/requirements.txt into the requirements.txt file.

Next, copy the model.pb and lables.txt to the directory where you module is (example where the main.py file is).

Build the image and push the image to your Docker repository.

Below, you see the “model” TWIN property that controls any updates or how to get the models down to the device.

"properties": 
  {
    "desired":
      {
        "model": "https://kevinsayedgedemo.blob.core.windows.net/mydirectory/7bca37876bc64a44a0fb07ca191f0816.zip" 
      }
    }

Module Deep(er) Dive:

So looking deeper into the module, it:

  • receives a message
  • checks for a url in the message
    • if there is a URL, calls the TensorFlow model and ‘classifies’ the image
    • adds ‘Predict’, ‘Predictions’, ‘Tag’, ‘ScoringTimeMS’ and ‘Predicted’ to the message
    • Sends the message to ‘output1’

For a deeper dive, review the code: https://github.com/ksaye/IoTDemonstrations/blob/master/IoTEdge-TensorFlow-Module/main.py

Running the command ‘docker stats’ on a 4 core 1.6 GHz machine, we see below how openCV and TensorFlow consume processing power, RAM and IOPs.

tensorFlowCPU

Lastly, looking at the red underlined JSON attributes, we see that image classification was applied and it took 256 milliseconds to classify.

tensorFlowJSON

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s