In the previous tutorial, everything we’ve seen is kind of standard Keras stuff. There’s nothing that we’ve really looked at that’s super wacky,or you know aside from Tensor flow Import Keras, everything else so far could have just been how to train a convolutional network using Keras. If we move on from the kind of creating model training and such, we get to some of the interesting pieces here.

In this tutorial shows how to train a Convolutional Neural Network for recognition images from CIFAR-10 data-set with the TensorFlow Estimators and Datasets API.

Create Convolutional Neural Network Using Keras

We’ll build a custom model and use Keras to do it. But then we’ll convert that Keras model to a TensorFlow Estimator and feed TFRecord using API.

CNN model for CIFAR10

The code snippet below is our TensoFlow model using Keras API, a simple stack of 2 convolution layers with a ReLU activation and followed by max-pooling layers.

Convert Keras model in TensorFlow Estimators

Why we need to do this conversion is because we need to put it on Cloud ML Engine, which, for now at least, only accepts TensorFlow SavedModel formats.

Now the typical reason you would export a Keras model, or at least convert a Keras model to an estimator is for the ability to do better-distributed training.  Instead of training it using Keras, you would convert it to TensorFlow Estimator and train it as a TensorFlow Estimator. Then you get distribution and GPU
scaling for free in terms of there’s no additional work.

keras.estimator, this is one of the pieces that were added to Keras in the inside of TensorFlow. Normal Keras does not have a .estimator module. You have this estimator.model to convert TensorFlow estimator. Basically, it’s a conversion’s a utility function that will take your jars model. Now, I have the model as a Python pointer. I just have that in a variable.

The names of feature columns and labels of a keras estimator come from the corresponding compiled keras model.the input key names for train_input_fn can be obtained from keras_inception_v3.input_names

Convert Images to TFRecord

In this tutorial we use CIFAR-10 png image to train our cnn model.TFRecords are the best way to handle an image dataset in a single tfrecord file. It’s a faster reading speed when the network Architecture is complex. Download CIFAR-10 png format.

Use TensorFlow Dataset API to train Keras Model

Input data is the lifeblood of machine learning, current algorithms and hardware are so thirsty for data that we need a powerful input pipeline to be able to keep up with them. You can either feed in data from python at each step, which was kind of slow or you could set up queue runners to feed your data, and these are a little challenging to which is a new library that helps you get all of your data into TensorFlow.

Our model is an Estimator, we’ll train and evaluate it a bit differently than we did in Keras. Instead of passing our features and labels to the model directly when we run training, we need to pass it an input function. In TensorFlow, input functions prepare data for the model by mapping raw input data to feature columns.

Train Model

To train our model, all we need to do is call train() and pass in the input function we just defined with our training data and labels.

CIFAR-10 train model

Evaluating the accuracy of our Model

Now that we’ve trained our model, we can evaluate its accuracy on our training data. We’ll use the same input function as above, this time passing it our test data instead of training data.

Tensorflow cifar-10 evaluate

Predictions on trained model

Next comes the most important part: using our trained model to generate a prediction on data it hasn’t seen before. We’ll use the first 44 examples from our test dataset. To make a prediction, we can simply call .predict()

calling predictwith our input functions we’re able to generate predictions on our trained Estimator model.

Tensorflow CIFAR-10 predict

Save Keras model to Tensorflow .pb

Next we’re going to take the TensorFlow estimator model, and now we need to export it as a .pb file.

Serving inputs function

Tensor flow has a number of utilities to help us create this serving input function.

You call export save model on our estimator passes a directory.


This post is focused on converting Keras model to an Estimator — if we wanted to improve accuracy we could try tuning our model’s hyperparameters, changing our layer size, or adding dropout to our input layer.


Download this project from GitHub

Leave a Reply