In this tutorial I will show step-by-step to create a Deep Learning Application from preparing data, modeling to production using Keras and Google Cloud ML.

1. What is Keras

Keras is a high-level neural networks library, written in Python and capable of running on top of either TensorFlow or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.

More information about Keras: https://keras.io/

In this tutorial I will use Keras and Transfer Learning to create an Image Classification API and deploy it to Google Cloud ML Engine.

2. Transfer Learning using Keras

Transfer Learning is a technique that uses pre-trained neural network and modify some last layers of the network and re-train only the changed parts on a new training data. This network still have ability to extract features of images and can be fine tuned for specific image classification. The advantage is that we need less time and fewer training data to re-train.

Keras already provides us some great pre-trained models which were trained on a large datasets of ImageNet:

  • VGG (Visual Geometry Group at the University of Oxford): Deep NN with either 16 or 19 layers
  • ResNet50 (Microsoft Research): This is 50-layer NN
  • Inception-v3 (Google)
  • Xception (author of Keras)

In this tutorial we gonna use Inception-v3.

Import required library

import numpy as np
import pandas as pd
import requests

from keras.preprocessing import image
from keras.applications.inception_v3 import preprocess_input, decode_predictions
from io import BytesIO

Let’s try using pre-trained Inception-v3 model to classify some of the images. Since Inception-v3 model accepts RGB 299x299 images as input, we must preprocess image before classify it. Keras provides us very helful modules to do this.

from keras.applications.inception_v3 import InceptionV3

model = InceptionV3(weights='imagenet')

img_path = 'dog-food-test.jpg'
img = image.load_img(img_path, target_size=(299, 299))
x= image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

This is the image:

This is the predicted classes from the pre-trained model:

# Classify the image preds = model.predict(x)

# Print predicted classes print('Predicted:')
for p in decode_predictions(preds, top=5)[0]:
    print("Label: {}, Score: {}".format(p[1], p[2]))
Predicted:
Label: can_opener, Score: 0.09016726166009903
Label: nipple, Score: 0.054355572909116745
Label: face_powder, Score: 0.04664556309580803
Label: lotion, Score: 0.04520213231444359
Label: eggnog, Score: 0.04017522558569908

Say we want to classify the image whether it is a dog food or cat food. We will never get results we want by just using the pretrained model. We need to do transfer learning in order to fine tune the model on our data.

Data Preparation

We have prepared a data for dog food and cat food in numpy array and store it in a npz file. See this notebook to know how to prepare the data.

Let’s load the data.

dataset = np.load("../datasets/dog_cat_food.npz")
X_dataset = dataset['features']
y_dataset = dataset['labels']

For training and evaluating our model we gonna need to split the dataset to training set and test set:

from keras.utils import np_utils
from sklearn.model_selection import train_test_split

X_dataset = preprocess_input(X_dataset)
y_dataset = np_utils.to_categorical(y_dataset)

X_train, X_test, y_train, y_test = train_test_split(X_dataset, y_dataset, test_size=0.2, random_state=42)

Just to clafify that the pretrained InceptionV3 model cannot predict correctly Dog Food/Cat Food dataset.

x = X_dataset[0]
x = np.expand_dims(x, axis=0)

preds = model.predict(x)
print('Predicted:')
for p in decode_predictions(preds, top=5)[0]:
    print("Label: {}, Score: {}".format(p[1], p[2]))
Predicted:
Label: measuring_cup, Score: 0.18297310173511505
Label: mixing_bowl, Score: 0.14904427528381348
Label: bathtub, Score: 0.0707203820347786
Label: tub, Score: 0.053981516510248184
Label: lumbermill, Score: 0.043141044676303864

In this part 1 of these tutorial series how to use keras to prepare data and how to use a pretrained model (Inception-V3) to make predictions. However, for the model to correctly predict our data labels (Dog Food/Cat Food), we need to do a transfer learning on our dataset. In part 2 of the series, I will cover how to do transfer learning and finally deploy our trained model for productions.