How do I use TensorFlow

Tutorial: Running a TensorFlow Model in Python

  • 3 minutes to read

After you've exported your TensorFlow model from Custom Vision Service, this quickstart will help you use the model locally to classify images.


This tutorial only applies to models exported from image classification projects.


To use the tutorial, you need to do the following:

  • Install either Python 2.7+ or Python 3.6+.
  • Install pip.

The following packages must then be installed:

Load your models and tags

The downloaded zip file contains a file named model.pb and a file called labels.txt. These files represent the trained model and the classification labels. The first step is to load the model into your project. Add the following code to a new Python script:

Prepare an Image for Prediction

The image must take a few steps to prepare it for prediction. These steps mimick the image manipulation that occurs during exercise:

Open the file and create an image in the BGR color space

Processing of images with a dimension> 1600

Cut the largest square in the middle

Reduce to 256x256

Cropping the central area to the exact input size for the model

Adding auxiliary functions

The following auxiliary functions are used in the above steps:

Classify an image

Once the image is prepared as a tensor, it can be sent via the model for prediction:

View the results

The results of the execution of the image tensor via the model must then be assigned to the designations again.

Next Steps

Next, learn how to wrap your model in a mobile application: