Skip to main content

Deploying TensorFlow Models on Google’s AI-Platform

All right, so you have built a deep neural network model and now you are ready to move it to production and have your application harness all it’s glory! There are many factors you need to consider; for example scaling on demand, training new models, upgrading models and all the related pipelines to make this happen. Today we will focus on deploying a model on GCP with Google’s AI Platform and how you can consume it at scale.

For the purpose of this article we will use Google CoLab for building, training and exporting our Tensorflow model, we will use Google AI Platform to host and serve our model and we will use Google App Engine to consume it.

The Game Plan:

1) Use Google Colab to build, train and export a Tensorflow model.

2) Upload the model to a GCP storage bucket.

3) Load the custom model into Google’s AI Platform and prepare it for serving.

4) Deploy a Python web app on App Engine to interface with AI-Platform and run inference on our hosted model.

5) Send data to our front end application and markup the predictions.

Let’s get started!

Google CoLab

Google Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows anybody to write and execute arbitrary python code through the browser, and is especially well suited to machine learning, data analysis and education. More technically, Colab is a hosted Jupyter notebook service that requires no setup to use, while providing free access to computing resources including GPUs and TPU’s. This means you can develop deep learning applications using popular libraries such as Keras, TensorFlow, PyTorch, and OpenCV.

For the purposes of this article, we will use the InceptionV3 ImageNet model with our Tensorflow backed application. However, the process workflow can be used for any model you create. The method and way in which you interact and run inference with that model will need to be adjusted. The data collection, feature extraction, model fitting, tuning, and scoring is something that you would do during your development stage, so we will leave that for another time. For this article, we will leverage an InceptionV3 as our model of demonstration. The key takeaway from this stage is that once you have a Tensorflow model ready and it is performing as you desire, you can then export that model in SavedModel format. The TensorFlow SavedModel format is a standalone serialization format for TensorFlow objects, supported by TensorFlow serving as well as TensorFlow implementations other than Python. It does not require the original model building code to run, which makes it useful for sharing or deploying (with TFLite, TensorFlow.js, TensorFlow Serving, or TensorFlow Hub). We will be deploying this model on Google AI-Platform which implements TFServing.

I wrote the following CoLab notebook to load, test and validate the model and extract it in SaveModel format for consumption on AI Platform. Feel free to load it up and follow along.

The notebook used for all steps in this project can be found here: Open In Colab, os.path.join('/content/',export_path))

tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
INFO:tensorflow:Assets written to: /content/saved_model/v1/assets

drwxr-xr-x 2 root root    4096 Apr 27 22:35 assets
-rw-r--r-- 1 root root 7115206 Apr 27 22:35 saved_model.pb
drwxr-xr-x 2 root root    4096 Apr 27 22:35 variables

adding: saved_model/v1/ (stored 0%)
adding: saved_model/v1/variables/ (stored 0%)
adding: saved_model/v1/variables/ (deflated 7%)
adding: saved_model/v1/variables/ (deflated 84%)
adding: saved_model/v1/variables/variables.index (deflated 80%)
adding: saved_model/v1/assets/ (stored 0%)
adding: saved_model/v1/saved_model.pb (deflated 94%)

Google Storage

Next, we need to get our model into a GCP storage bucket so that we can load it into the AI Platform.

  • Create a Google Storage bucket to hold our model
$ gsutil mb gs://tf-blog
Creating gs://tf-blog/...

$ gsutil list
  • Copy the model files over
$ unzip 
   creating: saved_model/v1/
   creating: saved_model/v1/variables/
  inflating: saved_model/v1/variables/  
  inflating: saved_model/v1/variables/  
  inflating: saved_model/v1/variables/variables.index  
   creating: saved_model/v1/assets/
  inflating: saved_model/v1/saved_model.pb  

$ gsutil -m cp -r saved_model/v1/ gs://tf-blog
Copying file://saved_model/v1/saved_model.pb [Content-Type=application/octet-stream]...
Copying file://saved_model/v1/variables/variables.index [Content-Type=application/octet-stream]...
Copying file://saved_model/v1/variables/ [Content-Type=application/octet-stream]...
Copying file://saved_model/v1/variables/ [Content-Type=application/octet-stream]...
\ [4/4 files][ 98.0 MiB/ 98.0 MiB] 100% Done   3.0 MiB/s ETA 00:00:00           
Operation completed over 4 objects/98.0 MiB.        

$ gsutil ls gs://tf-blog/v1
gsutil ls gs://tf-blog/v1

Google AI Platform

With our files now uploaded let’s go ahead and create a serving instance on AI Platform (ml-engine)

  • Let’s create the parent instance
$ gcloud ml-engine models create tfblog --regions=us-central1
Created ml engine model [projects/tf-blog/models/tfblog].
  • Let’s create an instance of our model
$ gcloud ml-engine versions create v1 --model=tfblog --framework=tensorflow --origin=gs://tf-blog/v1 --python-version=3.5 --runtime-version=1.14
Creating version (this might take a few minutes)......done.    

{% include image name=”mlengine.png” position=”center” size=”XXL” alt=”mlengine” %}

Okay our model is online and ready to serve!

Google App Engine

Let’s deploy our application on Google’s App Engine so we can interface with our model.

For this section I have built a python application that will take a URL as input, the URL must end with an image endpoint (jpg, jpeg,png). The application pre-processes the image (resizing, scaling) in the backend to adjust the image to map to the inputs for the model.

The data is then passed via API to the AI Platform where our model is hosted and inference is run. The result is then returned to the application which parses it and extracts the predictions. The application then marks up the output for end-user consumption. By default the limit for ML Engine API requests is 1.57 MB (1572864 bytes). This can be raised by contacting Google Support, but something to be aware of when selecting images. This is something to consider when pre-processing images for our network while also attempting to remain below the this limit. For true production workloads this should be raised much higher.

git clone
cd  appengine/

$ gcloud app deploy
Services to deploy:

descriptor:      [/home/aly_khimji/tfblog-inceptionv3/appengine/app.yaml]
source:          [/home/aly_khimji/tfblog-inceptionv3/appengine]
target project:  [tf-blog]
target service:  [default]
target version:  [20200428t093735]
target url:      []

Do you want to continue (Y/n)?  y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
File upload done.

Updating service [default]...done.

Setting traffic split for service [default]...done.

Deployed service [default] to []

You can stream logs from the command line by running:

  $ gcloud app logs tail -s default

To view your application in the web browser run:

  $ gcloud app browse


Let’s head over to the App Engine URL and do some testing. The initial ramp up of the application and prediction might take a few seconds but once the UI comes up you can start testing with the below URLs or use your own. Ensure you are able to reach the image in your own browser before testing it in the application. The goal is to have only the image come up so the application knows what to intake and work with.


Image URL


Image URL

Great White Shark:

Image URL

Coffee cup shaped like a pill bottle:

Image URL

Interestingly in this last prediction, the photograph is a coffee mug that is designed like a pill bottle. You can see in our prediction, the neural network was able to pick up on features of both items. Although it did correctly classified the image as a coffee mug, it did have some level of confidence that the image also contained a pill bottle which is true!

We hope you enjoyed this article on an approach to operationalizing your Tensorflow model on Google GCP platform, and building an image classifier. Interested in learning more about machine learning, Tensorflow, code development or Google Cloud Platform?

Reach Out To Us!

Share this story

Arctiq Team

We service innovation.