AISaturdaylagos: “Karessing” Deep Learning with Keras

Tejumade Afonja
AI Saturdays
Published in
8 min readMar 30, 2018
source: google.com

It has been four weeks of shedding light on some deep learning APIs/libraries at AISaturdays Lagos and just before you make up your mind for or against deep learning especially after hanging out last week with granny Theano, permit Olalekan Olapeju to sweep your feet off the ground with Keras.

…but wait! We got our first guest speaker last week, Farouq Oyebiyi 💃 and not just that, Christopher Giles, a reporter from CNN Africa came to say hi — which was kinda cool 😎.

Anyways, the most fun part after CS231n’s lecture on Segmentation and Detection as well as Generative Model from fast.ai’s lecture of course is #teamKeras beautifully delivered presentation which I obviously can’t wait to share with you.

This section was written by Olalekan Olapeju, team leader of #teamKeras 🔥 🔥. You can check his post out here.

How this article is structured:

  1. Introduction
  2. Why Keras?
  3. Installation of Keras
  4. Implementation of Deep Learning using Keras
  5. Conclusion

Introduction

It has been four weeks of shedding light on some deep learning APIs/libraries at AISaturdays Lagos and just before you make up your mind for or against deep learning especially after hanging out last week with granny Theano, permit me to sweep your feet off the ground with Keras.

Imagine driving interstate in 2018 with a Ford Model T and a Ford Mustang 2018 model. Just to be “fair”, you are driving on an international no speed-limit day (just keep imagining and don’t ask google if such a day exists)

Ford Model T
Ford Mustang 2018 Model

I believe, all things being equal, you will get to your destination and if you chose Model T, I can assume you will get to your destination looking happy like the couple below. Living happily ever or never after? considering it a waste of computing power to design a model to answer this, let us just say time will tell.

A model with such a performance

But as regards being “fair”, we are in the information age where thousands of researchers, like us — Keras team of AISaturdays, are looking at solving same problems that we face as human beings i.e. automation across all works of life 😎

The advent of cloud services are beginning to bridge the gap of the inaccessibility to scare resources such as computing resources — Tensor Processing Unit (TPU), Graphical Processing Unit (GPU), etc. Also, we get recognized for what we publish or produce and the faster we do, the more we make living easier and cozy for humanity — imagine self driving cars with zero records of accidents or a receptionist or bank teller that would not give you “attitude”. Hence a call to simplify a researcher’s life through an easy to use and less technical library for deep learning implementation.

Ladies and Gentlemen readers, I present to you (drums rolling)

Designed by Francois Chollet (hope he forgives me for the ‘c’ without accent, can’t find accent on my keyboard at the moment) of Google Inc., Keras is a cross platform, open source neural network (NN) API/library written in Python programming language. It can run on Theano (Universite de Montreal — just hope this accent thing does not hunt me), Tensorflow (Google), Cognitive Toolkit (Microsoft) or Mxnet (Amazon).

The name “Keras” means either Horn (Greek) or Ivory (Latin). Can hear someone asking the reason for the name, though I am not Chollet, it refers to where dream spirits arrive on earth either through a gate of ivory for those that deceive men with false visions or a gate of horn for those who announce the future that will come to pass. Either one you believe, the future is nearer than we once thought. Need not figure out the gate I come from because I am not a dream spirit though I announce the future.

Keras is a high level NN API that takes away the stress of trying to deal with low-level operations such as tensor differentiation, manipulation (reshaping, dot, elementwise operations, etc), etc. Imagine having to understand how each part of your car engine functions before you can be allowed to commence your driving classes, frustrating? Sure, just an understatement, right?

What lies beyond the Keras curtain

Why Keras?

Keras was developed to enable researchers focus on experimentation as the key to doing good research is being able to transform one’s ideas to results with least possible delay or bottleneck through users’ friendliness, modularity and extensibility.

It supports Convolutional and Recurrent Neural Networks. It runs on Central Processing Unit (CPU) and Graphical Processing Unit (GPU). I believe it will run on Google’s Tensor Processing Unit (TPU) soon because it sits on Tensorflow. It has stronger adoption in both industry and research community. If you use Netflix, Uber, Yelp, Square among others, then have been interacting with Keras.

Keras Models are easily deployed across greater range of platforms that other deep learning frameworks. On iOS using CoreML, Android using TensorFlow Android rumtime, in browser vis GPU-accelerated JavaScript runtimes such as keras.js and WnDNN, on Google Cloud via TensorFlow-Serving, on Raspberry PI, etc.

Installation of Keras

Keras runs better on linux and installing it requires a system with:

  • 32 or 64 bit operating system,
  • min of 4 to 8 GB RAM

Alternatively you can use cloud services like Amazon Web Services, Google Collaboratory — this is free, Microsoft Azure, etc.

To install Keras on your local system on a tensorflow backend, after installing Anaconda, you have to create an environment using:

#create an environment
$ conda create name_of_environment
#Activate the environment
$ source activate your_environment
#Update and upgrade your linux apt-get
$ sudo apt-get update
$ sudo apt-get upgrade
#Install all dependencies
$ sudo apt-get install python-pip3 python3-dev
$ sudo apt-get install python3-numpy python3-scipy python3-matplotlib python-yaml
$ sudo apt-get install graphviz
#Install Tensorflow
$ conda install tensorflow
#Install Keras
$ conda install keras
#Running to test your installation
$python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linuxType “help”, “copyright”, “credits” or “license” for more information.>>> import tensorflow as tf
>>> import keras as k

Implementation of Deep Learning using Keras

Using Keras to implement deep learning reminds me of the concept of playing with bricks from Lego, a danish company that produces toys consisting of mainly interlocking plastic bricks. First you have a problem you intend to solve using CNN like image classification or RNN like text or genome sequencing or both problem just like a child trying to describe his or her imagination to her friend.

First step is to define your training data. Just as a child sorts through the various pieces of Lego bricks, one has to define the training dataset as tensors (input and target) as data used in NN are represented in tensors.

Tensors are data containers in NN varying from 0 dimensional (0D) tensors (scalars) to 5D tensors for holding video frames.

Looking familiar?

Secondly, define a network of layers or model. Just as a child decides on a plan and commences the construction of the conceived imagination, Keras offers you the option of building your model using either Sequential API or Functional API.

Sequential API requires you putting a layer over another; a brick over another to build a model, which is the most common architecture while Functional API provides you the option to build an arbitrary architecture model that either requires shared layer (a mean of reusing a layer or model like a function in any programming language), multiple inputs (text and image) or multiple outputs (captioning an image). A simple model using a sequential API is shown below

>>> from keras.models import Sequential
>>> from keras.layers import Dense
>>> model = Sequential()
>>> model.add(layers.Dense(32, activation=’relu’, input_shape=(784,)))
>>> model.add(layers.Dense(10, activation=’softmax’))

Thirdly, configure the learning process by choosing a loss function, optimiser and other metrics to monitor the learning process of the model. This is as simple as writing:

>>> model.compile(optimizer = ‘rmsprop’, loss=’binary_crossentropy/categorial_crossentropy.. depending on your task’, metrics = [‘acc’])

Lastly, iterate on your training data by calling the fit() method as shown below:

>>> model.fit(x_train, y_train, epochs = 10, batch_size=32, validation_split = 0.2)

Diagrammatically, implementing a Keras model is shown below:

A simple flow for implementing a Model

From our Lego view, you have the below. That this might be complex for a child? Never underestimate the power of imagination.

More than an imagination

Conclusion

Below is a cheat sheet for Keras for www.datacamp.com.

Keras Cheat Sheet

Trust me, Keras is as simple as it seems. In doubt, ask Chollet or other users on https://groups.google.com/forum/#!forum/keras-users or https://keras-slack-autojoin.herokuapp.com/

#TeamKeras

Thanks to Keras AISaturdays Team:

  1. Olalekan Olapeju (My humble self Olalekan Olapeju)

2. Aderinto Sadiq — email

3. Olusegun Komolafe — github

***All images are gotten from google.com ***

Thanks #teamKeras for giving us tour of your framework. Despite being the team with the smallest number of participants, they did amazing 💪. Well-done guys!

AISaturdayLagos wouldn’t have happened without my friend & fellow ambassador, Azeez Oluwafemi, our Partners FB Dev Circle Lagos, Vesper.ng and Intel.

A big Thanks to Nurture.AI for this amazing opportunity.

Also read how AI Saturdays is Bringing the World Together with AI

See you next week 😎.

Links to Resources

Keras Presentation Slide — https://goo.gl/3VS1Ki

Deep Learning with Python by Francois Chollet

https://keras.io

www.datacamp.com for cheatsheet for Keras

Practical deep learning for coders

Deep learning Theories

Convolutional Neural Networks

A friendly introduction to Convolutional Neural Networks and Image Recognition

Setting up Google Colab 1

Setting up Google Colab II

--

--