In this Keras tutorial for beginners, you will learn Keras basics like:

What is a Backend?

Backend is a term in Keras that performs all low-level computation such as tensor products, convolutions and many other things with the help of other libraries such as Tensorflow or Theano. So, the “backend engine” will perform the computation and development of the models. Tensorflow is the default “backend engine” but we can change it in the configuration.

Theano, Tensorflow, and CNTK Backend

Theano is an open source project that was developed by the MILA group at the University of Montreal, Quebec, Canada. It was the first widely used Framework. It is a Python library that helps in multi-dimensional arrays for mathematical operations using Numpy or Scipy. Theano can use GPUs for faster computation, it also can automatically build symbolic graphs for computing gradients. On its website, Theano claims that it can recognize numerically unstable expressions and compute them with more stable algorithms, this is very useful for our unstable expressions.

On the other hand, Tensorflow is the rising star in deep learning framework. Developed by Google’s Brain team it is the most popular deep learning tool. With a lot of features, and researchers contribute to help develop this framework for deep learning purposes.

Another backend engine for Keras is The Microsoft Cognitive Toolkit or CNTK. It is an open-source deep learning framework that was developed by Microsoft Team. It can run on multi GPUs or multi-machine for training deep learning model on a massive scale. In some cases, CNTK was reported faster than other frameworks such as Tensorflow or Theano. Next in this Keras CNN tutorial, we will compare the backends of Theano, TensorFlow and CNTK.

Comparing the Backends

We need to do a benchmark In order to know the comparison between these two backends. As you can see in Jeong-Yoon Lee’s benchmark, the performance of 3 different backends on different hardware is compared. And the result is Theano is slower than the other backend, it is reported 50 times slower, but the accuracy is close to each other. Another benchmark test is performed by Jasmeet Bhatia. He reported that Theano is slower than Tensorflow for some test. But overall accuracy is nearly the same for every network that was tested. So, between Theano, Tensorflow and CTK it’s obvious that TensorFlow is better than Theano. With TensorFlow, the computation time is much shorter and CNN is better than the others. Next in this Keras Python tutorial, we will learn about the difference between Keras and TensorFlow (Keras vs Tensorflow).

Keras vs Tensorflow

Advantages of Keras

Fast Deployment and Easy to understand

Keras is very quick to make a network model. If you want to make a simple network model with a few lines, Python Keras can help you with that. Look at the Keras example below: Because of friendly the API, we can easily understand the process. Writing the code with a simple function and no need to set multiple parameters.

Large Community Support

There are lots of AI communities that use Keras for their Deep Learning framework. Many of them publish their codes as well tutorials to the general public.

Have multiple Backends

You can choose Tensorflow, CNTK, and Theano as your backend with Keras. You can choose a different backend for different projects depending on your needs. Each backend has its own unique advantage.

Cross-Platform and Easy Model Deployment

With a variety of supported devices and platforms, you can deploy Keras on any device like

iOS with CoreML Android with Tensorflow Android, Web browser with .js support Cloud engine Raspberry Pi

Multi GPUs Support

You can train Keras on a single GPU or use multiple GPUs at once. Because Keras has a built-in support for data parallelism so it can process large volumes of data and speed up the time needed to train it.

Disadvantages of Keras

Cannot handle low-level API

Keras only handles high-level API which runs on top other framework or backend engine such as Tensorflow, Theano, or CNTK. So it’s not very useful if you want to make your own abstract layer for your research purposes because Keras already have pre-configured layers.

Installing Keras

In this section, we will look into various methods available to install Keras

Direct install or Virtual Environment

Which one is better? Direct install to the current python or use a virtual environment? I suggest using a virtual environment if you have many projects. Want to know why? This is because different projects may use a different version of a keras library. For example, I have a project that needs Python 3.5 using OpenCV 3.3 with older Keras-Theano backend but in the other project I have to use Keras with the latest version and a Tensorflow as it backend with Python 3.6.6 support For detailed instructions on how to use AWS, refer this tutorial Note on the AMI: You will have the following AMI available

AWS Deep Learning AMI is a virtual environment in AWS EC2 Service that helps researchers or practitioners to work with Deep Learning. DLAMI offers from small CPUs engine up to high-powered multi GPUs engines with preconfigured CUDA, cuDNN, and comes with a variety of deep learning frameworks. If you want to use it instantly, you should choose Deep Learning AMI because it comes preinstalled with popular deep learning frameworks. But if you want to try a custom deep learning framework for research, you should install the Deep Learning Base AMI because it comes with fundamental libraries such as CUDA, cuDNN, GPUs drivers, and other needed libraries to run with your deep learning environment.

Step 2) Enter the details

Note: If you want to access resources from your VPC, set the direct internet access as enabled. Otherwise, this notebook instance will not have an internet access, so it is impossible to train or host models Step 3) Launch the instance Click on Open to launch the instance

Step 4) Start coding In Jupyter, Click on New> conda_tensorflow_p36 and you are ready to code

Install Keras in Linux

To enable Keras with Tensorflow as its backend engine, we need to install Tensorflow first. Run this command to install tensorflow with CPU (no GPU) if you want to enable the GPU support for tensorflow you can use this command

let’s check in Python to see if our installation is successful by typing if there is no error message, the installation process is successful

Install Keras

After we install Tensorflow, let’s start installing keras. Type this command in the terminal it will begin installing Keras and also all of its dependencies.You should see something like this :

Now we have Keras Installed in our system!

Verifying

Before we start using Keras, we should check if our Keras use Tensorflow as it backend by open the configuration file: you should see something like this as you can see, the “backend” use tensorflow. It means that keras are using Tensorflow as it backend as we expected and now run it on the terminal by typing

How to Install Keras on Windows

Before we install Tensorflow and Keras, we should install Python, pip, and virtualenv. If you already installed these libraries, you should continue to the next step, otherwise do this: Install Python 3 by downloading from this link Install pip by running this Install virtualenv with this command Install Microsoft Visual C++ 2015 Redistributable Update 3

Go to the Visual Studio download site https://www.microsoft.com/en-us/download/details.aspx?id=53587 Select Redistributables and Build Tools Download and install the Microsoft Visual C++ 2015 Redistributable Update 3

Then run this script Setup Virtual Environment This is used to isolate the working system with the main system. Activate the environment After preparing the environment, Tensorflow and Keras installation remains same as Linux. Next in this Deep learning with Keras tutorial, we will learn about Keras fundamentals for Deep learning.

Keras Fundamentals for Deep Learning

The main structure in Keras is the Model which defines the complete graph of a network. You can add more layers to an existing model to build a custom model that you need for your project. Here’s how to make a Sequential Model and a few commonly used layers in deep learning

  1. Sequential Model
  2. Convolutional Layer This is a Keras Python example of convolutional layer as the input layer with the input shape of 320x320x3, with 48 filters of size 3×3 and use ReLU as an activation function. another type is
  3. MaxPooling Layer To downsample the input representation, use MaxPool2d and specify the kernel size
  4. Dense Layer adding a Fully Connected Layer with just specifying the output Size
  5. Dropout Layer Adding dropout layer with 50% probability Compiling, Training, and Evaluate After we define our model, let’s start to train them. It is required to compile the network first with the loss function and optimizer function. This will allow the network to change weights and minimized the loss. Now to start training, use fit to fed the training and validation data to the model. This will allow you to train the network in batches and set the epochs. Our final step is to evaluate the model with the test data. Lets try using simple linear regression After training the data, the output should look like this

with the initial weight and final weight

Fine-Tune Pre-Trained Models in Keras and How to Use Them

Why we use Fine Tune Models and when we use it Fine-tuning is a task to tweak a pre-trained model such that the parameters would adapt to the new model. When we want to train from scratch on a new model, we need a large amount of data, so that the network can find all parameters. But in this case, we will use a pre-trained model so the parameters are already learned and have a weight. For example, if we want to train our own Keras model to solve a classification problem but we only have a small amount of data, then we can solve this by using a Transfer Learning + Fine-Tuning method. Using a pre-trained network & weights we don’t need to train the whole network. We just need to train the last layer that is used to solve our task as we call it Fine-Tuning method.

Network Model Preparation

For the pre-trained model, we can load a variety of models that Keras already has in its library such as:

VGG16 InceptionV3 ResNet MobileNet Xception InceptionResNetV2

But in this process, we will use VGG16 network model and the imageNet as our weight for the model. We will fine-tune a network to classify 8 different types of classes using Images from Kaggle Natural Images Dataset VGG16 model architecture

source

Uploading Our Data to AWS S3 Bucket

Step 1) After login to your S3 account, let’s create a bucket by clocking Create Bucket

Step 2) Now choose a Bucket Name and your Region according to your account. Make sure that the bucket name is available. After that click Create.

Step 3) As you can see, your Bucket is ready to use. But as you can see, the Access is Not public, it is good for you if you want to keep it private for yourself. You can change this bucket for Public Access in the Bucket Properties

Step 4) Now you start uploading your training data to your Bucket. Here I will upload the tar.gz file which consist of pictures for training and testing process.

Step 5) Now click on your file and copy the Link so that we can download it.

Data Preparation

We need to generate our training data using the Keras ImageDataGenerator. First you must download using wget with the link to your file from S3 Bucket. After you download the data let’s start the Training Process. The ImageDataGenerator will make an X_training data from a directory. The sub-directory in that directory will be used as a class for each object. The image will be loaded with the RGB color mode, with the categorical class mode for the Y_training data, with a batch size of 16. Finally, shuffle the data. Let’s see our images randomly by plotting them with matplotlib

After that let’s create our network model from VGG16 with imageNet pre-trained weight. We will freeze these layers so that the layers are not trainable to help us reduce the computation time.

Creating our Model from VGG16

As you can see below, the summary of our network model. From an input from VGG16 Layers, then we add 2 Fully Connected Layer which will extract 1024 features and an output layer that will compute the 8 classes with the softmax activation.

Training

Results

As you can see, our losses are dropped significantly and the accuracy is almost 100%. For testing our model, we randomly picked images over the internet and put it on the test folder with a different class to test

Testing Our Model

And our test is as given below! Only 1 image is predicted wrong from a test of 14 images!

Face Recognition Neural Network with Keras

Why we need Recognition

We need Recognition to make it easier for us to recognize or identify a person’s face, objects type, estimated age of a person from his face, or even know the facial expressions of that person.

Maybe you realize every time you try to mark your friend’s face in a photo, the feature in Facebook has done it for you, that is marking your friend’s face without you needing to mark it first. This is Face Recognition applied by Facebook to make it easier for us to tag friends. So how does it work? Every time we mark the face of our friend, Facebook’s AI will learn it and will try to predict it until it gets the right result. The same system we will use to make our own Face Recognition. Let’s start making our own Face Recognition using Deep Learning

Network Model

We will use a VGG16 Network Model but with VGGFace weight. VGG16 model architecture

What is VGGFace? it is Keras implementation of Deep Face Recognition introduced by Parkhi, Omkar M. et al. “Deep Face Recognition.” BMVC (2015). The framework uses VGG16 as the network architecture. You can download the VGGFace from github As you can see the network summary we will do a Transfer Learning + Fine Tuning to make the training quicker with small datasets. First, we will freeze the base layers so that the layers are not trainable. then we add our own layer to recognize our test faces. We will add 2 fully connected layer and an output layer with 5 people to detect. Let’s see our network summary As you can see above, after the pool5 layer, it will be flattened into a single feature vector that will be used by the dense layer for the final recognition.

Preparing our Faces

Now let’s prepare our faces. I made a directory consist of 5 famous people

Jack Ma Jason Statham Johnny Depp Robert Downey Jr Rowan Atkinson

Each folder contains 10 pictures, for each training and evaluation process. It is a very small amount of data but that is the challenge, right? We will use the help of Keras tool to help us prepare the data. This function will iterate in the dataset folder and then prepare it so it can be used in the training.

Training Our Model

Let’s begin our training process by compiling our network with loss function and optimizer. Here, we use sparse_categorical_crossentropy as our loss function, with the help of SGD as our learning optimizer. As you can see, our validation accuracy is up to 64%, this is a good result for a small amount of training data. We can improve this by adding more layer or add more training images so that our model can learn more about the faces and achieving more accuracy. Let’s test our model with a test picture

using Robert Downey Jr. picture as our test picture, it shows that the predicted face is true!

Prediction using Live Cam!

How about if we test our skill with implementing it with an input from a webcam? Using OpenCV with Haar Face cascade to find our face and with the help of our network model, we can recognize the person. The first step is to prepare you and your friend’s faces. The more data we have then the better the result is! Prepare and train your network like the previous step, after training is complete, add this line to get the input image from cam

Which one is Better? Keras or Tensorflow

Keras offers simplicity when writing the script. We can start writing and understand directly with Keras as it’s not too hard to understand. It is more user-friendly and easy to implement, no need to make many variables to run the model. So, we don’t need to understand every detail in the backend process. On the other hand, Tensorflow is the low-level operations that offer flexibility and advanced operations if you want to make an arbitrary computational graph or model. Tensorflow also can visualize the process with the help of TensorBoard and a specialized debugger tool. So, if you want to start working with deep learning with not that much complexity, use Keras. Because Keras offers simplicity and user-friendly to use and easy to implement than Tensorflow. But if you want to write your own algorithm in deep learning project or research, you should use Tensorflow instead.

Summary

So let’s summarize everything we have discussed and done in this tutorial.

Keras in a high-level API that is used to make deep learning networks easier with the help of backend engine. Keras is easy to use and understand with python support so its feel more natural than ever. It is good for beginners that want to learn about deep learning and for researchers that want easy to use API. The installation process is easy and you can use a virtual environment or using an external platform such as AWS. Keras also comes with various kind of network models so it makes us easier to use the available model for pre-trained and fine-tuning our own network model. Also, there are a lot of tutorials and articles about using Keras from communities worldwide codes for deep learning purposes.