Tutorial: Troubleshooting TensorFlow Lite on Windows 10

Machine learning has gained plenty of momentum recently, and with Google’s announcement of TensorFlow Lite, it’s never been easier to start with incorporating machine learning directly in your mobile apps. Mobile machine learning has become a highly sought after skill, and after being introduced to TensorFlow in Google’s annual keynote, I realized that it could solve real world problems, like analyzing complex CT scans to detect tumors or reading handwritten letters to the visually impaired. With that, I decided to jump in.

I started by reading and understanding the TensorFlow Lite framework and how I could use it in my apps. As I read through the tutorial, the steps seemed relatively straightforward.

And it really is — if you’re not on a Windows PC. In this article, I’ll go through the codelab and dive into problems I encountered on my Windows computer and how I got around them.

Walking through the TFLite codelab with a Windows 10 computer:

In the codelab, you retrain an image classification model to recognize 5 different flowers and later convert the retrained model, which is in a Frozen GraphDef format (.pb), into a mobile format like TensorFlow Lite (.tflite or .lite).

In the directions, they use TensorFlow version 1.7 (as of this writing, the current version is 1.8). However, once I tried to start up TensorBoard (a visualization tool that’s included when I pip install TensorFlow) to monitor my model architecture, I came across this error message:

Failed to load the native TensorFlow runtime

I followed this issue on GitHub and found out that the temporary solution was to switch over to TensorFlow version 1.5.0.

After I retrained the MobileNet model, I started encountering issues once again. In order to do the conversion, TF has a tool called TOCO, which didn’t work for me. Coming across this GitHub issue, it seemed like plenty of other developers on Windows were stuck on the same thing.

At this point, I decided the best thing to do was to switch to installing TensorFlow in Linux. I had hit enough roadblocks that it was worth running a virtual machine on my PC with Ubuntu installed.

How we’ll do this:

We’ll setup a Linux Virtual Machine and use TensorFlow version 1.5 for the retraining of the MobileNet model and then convert it to TensorFlow Lite format using the TOCO tool.

In short the steps we’ll follow are:

  1. Setting up Virtual Machine
  2. Setting up the environment
  3. Downloading and installing TensorFlow
  4. Retraining the model
  5. Converting the model to TF lite

Setting up Linux Virtual Machine using VirtualBox:

VirtualBox is an amazing free application by Oracle used to run virtual machines on your PC. To get started, first download the Windows version here.

We’ll use the Ubuntu distro of Linux so you need to install it as well to your VM , you can find the complete guide for setting up virtualbox and installing Ubuntu on it in this Medium post.

Setting up the environment and installing TensorFlow:

First, you need to install Python if you haven’t already. Installing Python and pip are easy, quick jobs. Here’s how you do it.

Open up the Linux terminal (you can do this by simply right clicking on the desktop and then selecting open terminal) and execute the following commands:

Once you have Python 3 and pip, you’re ready to install TensorFlow. We’ll use version 1.5.0. To install it, simply execute the following command:

Ideally we should use the latest version of TensorFlow, which is 1.8.0 [at the time of writing of this post] however using it gives an error: “Illegal instruction (core dumped)” , the same error is encountered while using versions 1.7 and 1.6 so to avoid it , we went down to 1.5 which works perfectly without any kind of errors.

Retraining the model

Next, clone the TensorFlow-for-poets codelab github repository:

[Follow steps mentioned here to install git on your VM]

Now create a new folder inside tf_files called training_images.

From here, keep your images folder separated with appropriate folder names, as the classification script uses the folder names as label names.

Next, declare 2 variables:

Finally, it’s time to start the retraining. Run the following command:

This should start the retraining. Once it’s done, you should see files named retrained_graph.pb and retrained_labels.txt in the tf_files folder.

Note: In the above command, you can change the output_graph and out_labels file names as well as the number of training steps.

Now you have the .pb file, and we need to convert it into a TensorFlow Lite format to use on a mobile device. We do this with the TOCO tool that we previously got stuck on.

Once you’re done with the retraining you can email yourself the retrained model (.pb file) and later download it on the Linux VM and then follow the remaining steps to do the conversion with TOCO.

Converting .pb file to .tflite or .lite using TOCO:

Installing TensorFlow also installs the TOCO tool, so you should have this tool already installed.

You can make sure it’s installed by running the following command:

Now it’s finally time to do the conversion with just 1 long command:

* be careful to include spaces before

And voila! You should see the optimized_graph.lite file in your tf_files folder now. You can send it back to your Windows via email if you’re developing your Android app in there.

Conclusion

If you made it through these steps, congratulations! The entire process is definitely long and a bit complicated. However, for those of you developing on Windows, it’s the best you can do right now without coughing up some money to buy a new computer. With the growing number of machine learning apps on mobile and the amazing things you can do with them, it’s worth jumping in so that you can turn your ideas into reality.

Discuss this post on Hacker News

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

wix banner square