Welcome to the Keras tutorial . In this article, I will introduce:
- How to use Keras, a high-level neural networks API (programming framework), written in Python and capable of running on top of several lower-level frameworks including TensorFlow and CNTK.
- How you can in a couple of hours build a deep learning algorithm.
Why am I using Keras? Keras was developed to enable deep learning engineers to build and experiment with different models very quickly. Just as TensorFlow is a higher-level framework than Python, Keras is an even higher-level framework and provides additional abstractions. Being able to go from idea to result with the least possible delay is key to finding good models. However, Keras is more restrictive than the lower level frameworks, so there are some very complex models that we can implement in TensorFlow but not (without more difficulty) in Keras. That being said, Keras will work fine for many common models.
In this exercise, I’ll work on the “Happy House” problem, which I’ll explain below. Let’s load the required packages and solve the problem of the Happy House!
import numpy as np
Note: As you can see, I’ve imported a lot of functions from Keras. We can use them easily just by calling them directly in the IDE. Ex:
X = Input(...) or
X = ZeroPadding2D(...).
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.
Run the following code to normalize the dataset and learn about its shapes.
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
Details of the “Happy” dataset:
- Images are of shape (64,64,3)
- Training: 600 pictures
- Test: 150 pictures
It is now time to solve the “Happy” Challenge.
Keras is very good for rapid prototyping. In just a short time you will be able to build a model that achieves outstanding results.
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
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 commputation 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).
Now I will implement a
HappyModel(). We can also use other functions such as
Note: We have to be careful with your data’s shapes. Use what you’ve learned in the videos to make sure your convolutional, pooling and fully-connected layers are adapted to the volumes you’re applying it to.
We have now built a function to describe your model. To train and test this model, there are four steps in Keras:
- Create the model by calling the function above
- Compile the model by calling
model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
- Train the model on train data by calling
model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
- Test the model on test data by calling
model.evaluate(x = ..., y = ...)
If you want to know more about
model.evaluate() and their arguments, refer to the official Keras documentation.
# create the model.
Implement test/evaluate the model.
preds = happyModel.evaluate(X_test, Y_test, batch_size=32)
happyModel() function worked, you should have observed much better than random-guessing (50%) accuracy on the train and test sets.
To give you a point of comparison, my model gets around 95% test accuracy in 40 epochs (and 99% train accuracy) with a mini batch size of 16 and “adam” optimizer. But our model gets decent accuracy after just 2-5 epochs, so if you’re comparing different models you can also train a variety of models on just a few epochs and see how they compare.
If you have not yet achieved a very good accuracy (let’s say more than 80%), here’re some things you can play around with to try to achieve it:
- Try using blocks of CONV->BATCHNORM->RELU such as:
X = Conv2D(32, (3, 3), strides = (1, 1), name = 'conv0')(X)
X = BatchNormalization(axis = 3, name = 'bn0')(X)
X = Activation('relu')(X)
until your height and width dimensions are quite low and your number of channels quite large (≈32 for example). You are encoding useful information in a volume with a lot of channels. You can then flatten the volume and use a fully-connected layer.
- You can use MAXPOOL after such blocks. It will help you lower the dimension in height and width.
- Change your optimizer. We find Adam works well.
- If the model is struggling to run and you get memory issues, lower your batch_size (12 is usually a good compromise)
- Run on more epochs, until you see the train accuracy plateauing.
Even if you have achieved a good accuracy, please feel free to keep playing with your model to try to get even better results.
Note: If you perform hyperparameter tuning on your model, the test set actually becomes a dev set, and your model might end up overfitting to the test (dev) set. But just for the purpose of this assignment, we won’t worry about that here.
We can now take a picture of your face and see if we could enter the Happy House.
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!
img_path = 'my_image.jpg'
Congratulations, we have solved the Happy House challenge!
What I would like you to remember from this article:
- Keras is a tool I recommend for rapid prototyping. It allows we to quickly try out different model architectures. Are there any applications of deep learning to your daily life that you’d like to implement using Keras?
- Remember how to code a model in Keras and the four steps leading to the evaluation of your model on the test set. Create->Compile->Fit/Train->Evaluate/Test.
Two other basic features of Keras that you’ll find useful are:
model.summary(): prints the details of your layers in a table with the sizes of its inputs/outputs
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.
Run the following code.
The source code and dataset are at github