by Sandeep Singh on October 2nd, 2018- 5 mins read
Since my early days, I used to dream of developing a humanoid to mimic humans. As I grew up, I learnt, it is very difficult to program cognitive intelligence into machines via conventional coding practices. The task seemed impossible untill recently, when an aggressive research began in the domain of machine learning and deep learning via neural networks.
Though machine learning and deep learning are creating wonders everyday, lot of complex mathematics lies behind the scenes. Mastering each and every such concept is not feasible for everybody and coding each of those steps efficiently into programs can proove to be a cumbersome task. Therefore, it is advisable to use the ready made libraries for such complex implementations.
Tensorflow is one of the most popular open source libraries for deep learning neural networks from Google. It is capable of running on multiple GPUs and TPUs and is available on 64-bit Linux, Windows and macOS. It can be readily used for research and production purposes in the domain of conputer vision and natural language processing.
Though Tensorflow is very popular amongst the developers, it is bit difficult to work around in it. To ease the use of Tensorflow, another open source library called Keras was introduced as its wrapper. Keras is written in Python and can be used with either Tensorflow or Theano or Microsoft Cognitive Toolkit as backend. It is much more user friendly, modular and extensible as compared to original Tensorflow.
After observing such a great popularity and demand of Keras worldwide, I decided to write this post explaining each and every step in detail for installing Keras with Tensorflow as backend.
Note: The following instructions are written to install OpenCV on Ubuntu. Please make sure that you have latest version of Ubuntu in your system. The same can be downloaded from https://www.ubuntu.com/download/desktop.. If you are currently using Windows/MAC OS, then I would recommend you to dual boot your systems with Ubuntu as it will enable you to take the advantages of both the operating systems as and when required.
Without much ado, let us get started with the installation steps.
All the steps given below are to be executed via terminal and therefore launch your terminal from your Ubuntu and get ready for the installation.
Step1: Setting up virtual environment in Ubuntu
If you have read my previous post on how to install openCV in Ubuntu, I have very well explained the advantages of virtual environments over there. For convenience, let me repeat it over as well.
Often it happens that different projects require different versions of softwares. Installing each of them on the same machine might result into a chaos and can make the life of a developer more complicated. In order to tackle such situations, virtual environments can be created on Ubuntu. These virtual environments have access to all the resources as Ubuntu system but are isolated and have no dependency of any sorts on each other.
To ensure the correct installation of the same, follow the commands given below:
sudo pip install virtualenv virtualenvwrapper
Once the installation is done, ~/.bashrc file is to be edited to set up the environment variables. For same, open the ~/.bashrc file in Text Editor of Ubuntu and add the following lines at the end of the file and save it.
Now, we are all setup to create a virtual environment in which we will install our tensorflow and keras.
Above command creates a Python3 virtual environment having name tf_keras. The name of the environment can be anything as per your choice but it is preferred to keep it short and simple.
To make sure that everything is upto the mark, please make sure that the folder has been created by listing down the contents of folder .virtualenvs in home folder of your Ubuntu.
As can be seen above, I have 5 diferent virtual environments in my system, namely, ml, cv3.4, cv3.3 cv3.4.3 and tf_keras.
Going forward, all the installations are to be done in this virtual environment and so make sure that you have chosen the correct environment before going ahead.
To switch to the virtual ensironment use the command below:
As soon as you execute the command above, your terminal will look as shown below:
Step2: Install Tensorflow
The simplest way of installing tensorflow is to use the package manager of python3. For doing the same execute the command as given below.
pip install –upgrade tensorflow
As soon as you will execute the command, an installation process will be kick started. The process will take a minute or two (depending upon the speed of your internet connection) before you can test it.
The installation process will look as shown in the image below:
For verification, type in the commands on your terminal as given below:
If there occurs no error at output then, CONGRATULATIONS!!! You have successfully installed Tensorflow on your system. Next, its Keras.
Next we are going to install GTK library so that OpenCVs GUI operations can be carried out smoothly in the environment
sudo apt-get install libgtk-3-dev
We are left with only one more step before we can install OpenCV in the system.
sudo apt-get install python3-dev The above statement makes sure that all Python3 headers and libraries are correctly installed.
Step3: Install Keras
Before installing Keras, let us install certain dependencies.
And it is done. Yes it is that simple. But before we move on and start doing some programming, let us verify the configuration of keras. For that, open the file keras.json in any of the available text editors. The same lies in the path ~/.keras/ and can be reached out as shown below:
The file contains the following text:
I am pretty much sure, that you are clear with concept of backend property in the above file. As far as image_data_format is concerned, it mentions the way an image file will be stored in keras. You might be knowing that each image file is 3 dimensional in nature i.e. it has height, width and channels. While height and width is meant to notify the length and breadth of an image, there are 3 channels in a color images and 1 channel in both binary and grayscale images. If the property is set to ‘channels_last’ then the format of the image is (height, width, channels) and is appropriate for use in Tensorflow. While the format has to be (channels, height, width) for use with Theano.
Step4: Let’s start working on Keras
Congratulations!!! You are done with all the hardwork. Now you can simply import the Keras library in your Python code and enjoy its magic.
This blog provides step by step instructions to install Keras with Tensorflow backend on Ubuntu.
If you enjoyed reading this tutorial, please recommned the same to others .