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.


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 into the ‘’ file.

Next, copy the contents from into the DockerFile file.

Last, copy the contents from into the requirements.txt file.

Next, copy the model.pb and lables.txt to the directory where you module is (example where the 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.

        "model": "" 

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:

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.


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



Leave a Reply

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

You are commenting using your 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