Contributed / Top Stories / Tutorials /

Poodle, Pug, or Wiener Dog? Deploy a Dog Identification TensorFlow Model Using Python and Flask

22 Dec 2017 9:00am, by

In this post, we’ll create a demo to see how simple it is to develop a machine learning-based service using Python’s Flask library.

There are a number of cases where you might not be able to use a cloud service to host your model and would be required to roll-your-own inference service. In many large enterprises, on-premise solutions are mandatory. Approvals to purchase third-party solutions can also be lengthy and complex. So developing your own small service may be the best solution.

The demo we will develop here will be a proof-of-concept demonstration of how to load and host your model but won’t go into issues around security, authentication or load balancing.

Developing the Model

Pete Garcin
As Developer Advocate at ActiveState, Pete has over 15 years in software development in both web and games having shipped over 40 titles in roles ranging from Programmer to Audio Director to Executive Producer. He earned his undergraduate degree at University of Waterloo, and an MA in Communication from Carleton University in Ottawa. He’s passionate about engaging with communities and dedicated to enhancing developers’ experiences with ActiveState products.

The first stage is to develop a model. You may already have a model that you’ve been training that’s ready to be deployed. There are a few ways to ready your model for deployment using TensorFlow. For instance, you can use TensorFlow’s ‘freeze graph’ functionality to export the entire graph as a Protocol Buffer to use for serving.

For our demo, we’ll develop a super-powerful AI that is able to determine the difference between poodles, pugs, and “Wiener Dogs” (aka dachshunds). For this bleeding edge technology, we can use a retrained version of Google’s Inception or MobileNet models.

Now, much like the famous Silicon Valley “Not Hotdog” app, our actual dog identification algorithm will surely be of great utility to the human race. So once we’ve trained our model to identify these key dog breeds we’ll want to create a service to make it available to the world.

Retraining the Inception or MobileNet models is actually extremely straightforward. You can train it to classify entirely new categories of objects simply by providing a series of images in a labeled subfolder. It will then take those images, classify them with the label (ie. the folder name) and output a newly trained model in .pb format.

TIP: If you’re looking for an excellent introduction and tutorial on how to retrain these models, you can work through this codelab provided by Google.

For performance reasons, we’re choosing the MobileNet model. It is optimized for use on mobile devices. This model sacrifices some accuracy for speed but works pretty well for our use case.

We can then adapt one of Google’s label scripts that contains useful utility functions for loading graphs and transforming images into tensors to use with TensorFlow. This adapted script will become the basis for our inference service.

Building the Service

Once we’ve retrained our model and exported it to disk we can host the model as a service. We’ll load the model from disk with a simple function that takes the graph definition directly from the file and uses that to generate a graph. TensorFlow does most of this for us:

def load_graph(model_file):
 graph = tf.Graph()
 graph_def = tf.GraphDef()

 with open(model_file, "rb") as f:
 with graph.as_default():

Using the Python Flask microframework, much of the heavy-lifting around configuring a server and handling requests is done for us. After we’ve created a Flask app object:

app = Flask(__name__)

Then we can easily create routes for where our classification service will live. Let’s create a default route to our classify() function that will allow us to pass an image to the endpoint for identification.


def classify():

Using the decorator syntax to define the route, it will configure the service so that our classify() function will be called every time someone hits the root of our service address. We said we wanted users to be able to specify a file to be identified so we’ll store that as a parameter from the request:

file_name = request.args['file']

In an actual app, we’d probably populate this from a form attachment or URL. For our example, we’ll simply let users specify a path to the file that they want to be identified.

We can then read the image file and turn it into a tensor to be used as input to the graph we loaded previously. The base script included a number of useful functions including read_tensor_from_image_file() which will take the image file and turn it into a tensor to use as input by using a small custom TensorFlow graph.

Running the inference on our graph with this image is again quite straightforward:

       results =[0],
                     {input_operation.outputs[0]: t})

In this line, the variable ‘t’ represents the image tensor that was created by read_tensor_from_image_file() function. TensorFlow will then take that image and run the new retrained model to generate predictions.

Those predictions come as a series of probabilities that indicate which of the classes (poodle, pug, or wiener dog) is the most likely. Since this is just a prediction service, it will simply return a JSON representation of the arrays.

Inside our script we can start our service with:, port=8000)

Then, if we want to launch the script from the command line, all we have to do is run `python` and it will initialize and start running on port 8000.

Using the Service

We can now use this service either by visiting it in a web browser or generally making any REST call on that port. For an easy test we can access it using `curl`:

curl localhost:8000?file=path/to/poodle.jpg

So, if we feed it the following image of my standard poodle, Orion, let’s see what it says:

The output is:


Success! Our TensorFlow model is 99.9% sure that this guy is a poodle!

Next Steps

There are obviously a lot of extensions required to turn this into a real-world production service. However, it does illustrate that hosting models as services is relatively straightforward and can be done quickly without writing a lot of boilerplate code.

There are a lot more components that go into turning this into a production-ready service and next time we’ll look at how to set up a data pipeline for automatic retraining, version control of training data and more.

Google is a sponsor of The New Stack.

Feature image via Pixabay.

A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.