Solve a problem called Happy House using Keras, a high-level deep learning framework

What is Keras ?

Keras is a high-level neural networks API (programming framework) written in Python and capable of running on top of several lower-level frameworks like Tensorflow, CNTK, Theano etc. It was developed with a focus of enabling fast experimentation.

Let’s learn how to use Keras by solving a problem called “Happy House”.

The “Happy House” problem

For your next vacation, you decided to spend a week with five of your friends from school. It is a very convenient house with many things to do nearby. But the most important benefit is that everybody has commited to be happy when they are in the house. So anyone wanting to enter the house must prove their current state of happiness.

As a deep learning expert, to make sure the “Happy” rule is strictly applied, you are going to build an algorithm which that uses pictures from the front door camera to check if the person is happy or not. The door should open only if the person is happy.

You have gathered pictures of your friends and yourself, taken by the front-door camera. The dataset is labbeled.

Building a Model in Keras

Keras is very good for rapid prototyping. In just a short time you will be able to build a model that achieves outstanding results. We will follow 4 steps to build and run the model -

  1. Built a function to describe your model and call that function.
  2. Compile the model by calling model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
  3. Train the model on train data by calling model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
  4. Test the model on test data by calling model.evaluate(x = ..., y = ...)

Step — 1: Create the Model

def HappyModel(input_shape):
"""
Implementation of the HappyModel.

Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in Keras
"""
X_input = Input(input_shape)
# Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input)
# CONV -> BN -> RELU Block applied to X
X = Conv2D(32, (7, 7), strides=(1, 1), name='conv0')(X)
X = BatchNormalization(axis=3, name='bn0')(X)
X = Activation('relu')(X)
# MAXPOOL
X = MaxPooling2D((2, 2), name='max_pool')(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs=X_input, outputs=X, name='HappyModel')

return model
happyModel = HappyModel(X_train.shape[1:])

Note that Keras uses a different convention with variable names than we’ve previously used with numpy and TensorFlow. In particular, rather than creating and assigning a new variable on each step of forward propagation such as X, Z1, A1, Z2, A2, etc. for the computations for the different layers, in Keras code each line above just reassigns X to a new value using X = .... In other words, during each step of forward propagation, we are just writing the latest value in the computation into the same variable X. The only exception was X_input, which we kept separate and did not overwrite, since we needed it at the end to create the Keras model instance (model = Model(inputs = X_input, ...) above).

Step — 2: Compile the Model

As this is a binary classification problem, so we use binary cross entropy.

happyModel.compile('adam', 'binary_crossentropy', metrics=['accuracy'])

Step — 3: Train the Model

Choose the number of epochs and the batch size.

happyModel.fit(X_train, Y_train, epochs=40, batch_size=50)

Note that if you run fit() again, the model will continue to train with the parameters it has already learnt instead of reinitializing them.

Step — 4: Test & Evaluate the Model

preds = happyModel.evaluate(X_test, Y_test, batch_size=32, verbose=1, sample_weight=None)
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))

To give you a point of comparison, our model gets around 97% test accuracy in 40 epochs (and 99% train accuracy) with a mini batch size of 50 and “adam” optimizer.

Try your own image

suvro_run.jpg — just before my first trail run. It was a 13 kms run climbing up a hill in a place called Nandi Hills near to Bangalore, India.
img_path = 'images/suvro_run.jpg'

img = image.load_img(img_path, target_size=(64, 64))
imshow(img)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print(happyModel.predict(x))

The model predicted [[ 1.]], which means I am happy in this pic. Usually I am happy :)

The training/test sets were quite similar; for example, all the pictures were taken against the same background (since a front door camera is always mounted in the same position). This makes the problem easier, but a model trained on this data may or may not work on your own data. But feel free to give it a try! For me it worked.

Some other useful functions in Keras

  • model.summary(): prints the details of your layers in a table with the sizes of its inputs/outputs
happyModel.summary()
  • plot_model(): plots your graph in a nice layout. You can even save it as ".png" using SVG() if you'd like to share it on social media ;). It is saved in "File" then "Open..." in the upper bar of the notebook.

Code

The solution code for this problem can be found here -

https://www.kaggle.com/baners/thehappyhouse-keras

Sources

  1. Convolutional Neural Networks by deeplearning.ai from Coursera
  2. Keras Documentation