Why I Love Keras

Manish Bhobé
8 min readMay 4, 2018

--

Keras — the best deep-learning library for a newbie in deep-learning.

There are several and very capable Deep Learning libraries for Python in use today — prominent examples are Tensorflow, Theano, Keras, OpenCV, Pytorch etc. In my opinion, Keras is best deep learning library/framework for a newbie to get started with, especially if you are coming from a scikit-learn background.

Keras is a high-level neural networks API, written in Python, which can run on top of Tensorflow or Theano or Microsoft CNTK. It was designed with a philosophy that enables experimentation and allows you to go quickly from concept to result. Following are the advantages of using/deploying Keras for your Deep Learning problems:

Easiest AI framework to get started with

I took my plunge with Tensorflow, arguably as it’s the most popularly library of the lot. However, very soon, I got lost in the maze of the computational graphs, and related terminology like tf.placeholder(), tf.variable() etc. I quickly realized that building a simplest network required writing a lot of code — but that is how Tensorflow is designed. It’s a lower level API designed to help researchers and AI practitioners build their neural networks from ground up. I was looking for something that could get me started much quicker — then I discovered Keras.

Keras is much easier to pick up. You land up writing much less code than the equivalent Tensorflow application. Keras takes care of most of the underlying details, leaving you free to focus on the high-level design of your neural network (like no of layers, how many nodes per layer, which loss function we should use etc.).

Supports multiple backend engines — Tensorflow, Theano and CNTK

Keras is a high-level library, providing a high-level API for building deep-learning models. It runs atop specialized and thoroughly optimized libraries like Tensorflow or Theano or Microsoft Cognitive Toolkit (CNTK), leaving the low level details like tensor manipulation to these libraries. And if your underlying library exploits a GPU, then so will Keras! You can choose your underlying deep-learning library by tweaking a setting in the Keras configuration file — Tensorflow is the default, which suffices for most applications!

Keras is built atop Tensorflow/Theano or CNTK

What’s more — you can swap one underlying deep-learning library for another by making changing that setting in the Keras configuration file (assuming you have all the libraries installed on your machine!). So you can decide which underlying library you want to use for a specific problem without changing to your Keras code.

In fact, the integration is so smooth that you hardly realize that you are using an underlying specialized deep-learning library. I think the only time you realize that Keras is using Tensorflow or Theano or CNTK is when the import statements prompt you about which backend it is using!

Here is where I see that I am using Tensorflow as the backend library!

o, if you are concerned that you are not using state-of-the art libraries like Tensorflow in your deep-learning projects, don’t be! Keras is indirectly using your favorite library, and handling all the low-level plumbing for you, leaving you to focus on your model building using it’s high-level API. That’s not bad, is it?

Support for various types of network configurations

Keras has full support for creation of Artificial Neural Networks (ANNs) — which can be used for classification & regression problems, Convolutional Neutral Networks (CNNs) — which can be used for image classification problems, Recurring Neural Networks (RNNs/LSTMs) for sequence processing and any combination of both.

It even supports arbitrary network architectures — multi-input or multi-output models, layer sharing, model sharing, and so on. Hence Keras can be leveraged to build essentially any types of deep-learning model.

Free to use in Commercial Projects

Keras is distributed under the permissive MIT license, which means it can be freely
used in commercial projects as well as your learning/hobby projects — no licensing costs to worry about!

User friendly API, making model building easier

If you picture your neural network as shown in the diagram below, the it is reasonable to expect that you should be able to build it as pictured. And that is exactly how you would use Karas’ Sequential API. For example, the network below can be used for binary classification problems — has an input layer which takes in data with 3 features (i.e. 3 input nodes), had 2 hidden layers each with 4 nodes; has an output layer with 1 node (for binary classification). For such a layout, you would use the relu() activation function on all nodes of hidden layer and the sigmoid() function on the output node. Also, you would use the binary_crossentropy() as the loss function that the network minimizes and the Adam optimizer.

ollowing code will build the network — notice how it intuitive builds the layers of a densely connected nodes.

from keras.models import Sequential
from keras.layers import Dense
def build_model():
model = Sequential()
# add layers to the model
model.add(Dense(units=4, activation='relu', input_shape=(4,))
model.add(Dense(units=4, activation='relu'))
# output layer
model.add(Dense(units=1, activation='sigmoid'))
# compile the model
model.compile(loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy'])
return model

While you specify the number of nodes, Keras figures out the dimension of the weight vectors for the intermediate layers.

Familiar API, especially if you are coming from a scikit-learn background

Keras uses the familiar functions like fit(), predict() etc., which a scikit-learn user would already be familiar with. The following code illustrates a typical skeleton used to train the model — we use the above build_model() call to build our ANN model.

# load data from some data-source
X, y = load_data(...)
# split into train/test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(...)
# scale the X_train/X_test sets
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
X_train_std = ss.fit_transform(X_train)
X_test_std = ss.transform(X_test)
# so far the steps are the same as you would you for any scikit-learn
# provided classifier or regressor
# build the Keras model
# (I have called the build_model() function we saw above)

model = build_model()
# train the model - will use batch gradient descent
model.fit(X_train, y_train, epochs=20, batch_size=25)
# predict the results from test data
y_pred = model.predict(X_test_std)

Support for scikit-learn API

There is support to ‘plug-in’ your Keras models into scikit-learn workflows. If you are coming from a scikit-learn background, you would be familiar with best practices of k-fold cross validation using the cross_val_score() call. Similarly, for hyper-parameter tuning, you would have used GridSearchCV(). The good news is that you can use the same API with Keras!

Keras provides wrapper classes KerasClassifier() and KerasRegressor(), whose instances you can plug into the scikit-learn API calls. Following example illustrates how you can do a k-fold cross validation for a Keras classifier model, wrapped into a KerasClassifier instance.

# import the scikit-learn wrapper class
from keras.wrappers.scikit_learn import KerasClassifier
# build a KerasClassifier - the first parameter is a function
# which returns an instance of a Keras model
# in the example below, I have used the same function as above
classifier = KerasClassifier(build_fn = build_model, epochs=20,
batch_size=25)
# now classifier can be used in any scikit-learn API call that
# expects a classifier instance (e.g. cross_val_score())
from sklearn.model_selection import cross_val_score
cv_scores = cross_val_score(estimator=classifier, X=X_train_std,
y=y_train, cv=10)
print('Cross val scores : %.3f [+/- .3f]' %
(cv_scores.mean(), cv_scores.std()))

Availability of pre-cleaned datasets for trying out deep-learning experiments

Just as the scikit-learn library ships with some data sets (like the iris data set, Wisconsin Breast Cancer data set, Wine data set, Boston Housing data set etc.) and exposes them through it’s scikit.datasets.load_XXX() calls, Keras also ships with sample data sets which you can use with your deep-learning experiments — these are a good starting point to begin learning Keras.

Following data sets are available in the keras.datasets package — the first time you call the API, they will be downloaded from a remote server to your disk and available off-line thereafter.

  • CIFAR10 small image classification: a data set of 50,000 32x32 color training images, labeled over 10 categories, and 10,000 test images.
# Usage
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
  • CIFAR100 small image classification: data set of 50,000 32x32 color training images, labeled over 100 categories, and 10,000 test images.
# Usage
from keras.datasets import cifar100
(x_train, y_train), (x_test, y_test) = \ cifar100.load_data(label_mode='fine')
  • IMDB Movie reviews sentiment classification: data set of 25,000 pre-processed and encoded as sequences of word-indexes movies reviews from IMDB, labeled by sentiment (positive/negative).
# Usage
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = \
imdb.load_data(num_words=10000)

Availability of pre-trained models like ResNet50

While it is possible to build a Convolutional Neural Network (CNN) to classify images using Keras, companies like Google (with InceptionV3) and Microsoft (ResNet50) have trained very capable image recognition models, which we can re-use in our applications. Keras Applications are deep learning models that are made available alongside pre-trained weights. When you instantiate an application (like ResNet50) for the first time, all the weights are downloaded to your local drive & used thereafter.

Let’s say you have an image of an elephant, which you want a pre-trained model, like ResNet50, to recognize. Here is sample code on how you can use this model, which is available as a Keras application. That’s a big time-saver!

# imports
from
keras.applications.resnet50 import (ResNet50,preprocess_input,
decode_predictions)
from keras.preprocessing import image
from keras.applications.resnet50
import import numpy as np
def predict_image(resnet50_model, img, top_n=3):
""" uses the ResNet50 model to return top_n (= 3) predictions
for an image passed in the img parameter """
X = image.img_to_array(img)
X = np.expand_dims(x, axis=0)
X = preprocess_input(x)
predictions = resnet50_model.predict(x)
decoded_preds = decode_predictions(predictions, top=3)[0])
# -----------------------
# load the ResNet50 model
model = ResNet50(weights='imagenet')
# image to tes
img_path = './images/elephant.jpg'
# NOTE: when using ResNet50, target_size MUST BE (224,224)
img = image.load_img(img_path, target_size=(224, 224))
predictions = predict_image(model, img)
# predictions will be a list of 3 tuples, corresponding to top 3
# predictions from ResNet50 model
# Example: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]

I hope I have got you curious, if not already excited, about using Keras for your deep-learning experiments. You must be wondering “How do I get started?”

So how does one get started with Keras?

  • Download and install Keras to begin with, of course! Instructions are available on the official documentation site keras.io.
  • Try the Deep Learning A-Z course on Udemy by Krill Eremenko & Hadelin de Ponteves. It provides step-by-step video tutorials on how to use Keras to build CNN’s, RNN’s etc. I found this course very informative.
  • A good book to explore Keras would be Deep Learning with Python authored by François Chollet, the creator of the Keras library. This book is a very good introduction to Keras and covers a wide variety of problems — from the canonical MNIST digit recognition sample to problems of classification, regression, image classification etc.

Conclusion

Above, I have stated just a few reasons that make Keras my favorite AI framework to get started on deep learning. I would encourage you to explore Keras as your first deep learning framework — trust me, it will get you started on your AI journey much quicker. You can then move onto it’s more famous cousin — Tensorflow.

--

--

Manish Bhobé

IT Professional. Data Science, ML & Deep Learning enthusiast. Loves working with Tensorflow, Pytorch, scikit-learn, Python, Numpy & Pandas. Aspiring author.