How to Install Keras

Reading Time: 6 minutes
keras.logo.3.12.20

Keras is a Python-based high-level neural networks API that is capable of running on top TensorFlow, CNTK, or Theano frameworks used for machine learning. It can be said that Keras acts as the Python Deep Learning Library. Keras was created with emphasis on being user-friendly since the main principle behind it is “designed for human beings, not machines.” The core data structure of Keras is a model, or a way to organize layers.

While there are multiple frameworks to use, Keras officially recommends using TensorFlow. With that in mind, this tutorial will cover: 

  • The difference between Keras and TensorFlow
  • How to install Python 
  • How to install TensorFlow 
  • How to install Keras

What’s the Difference?

As mentioned previously, Keras runs on top of the TensorFlow, CNTK, or Theano frameworks. The main difference between them is that Keras is a neural network library that has high-level API’s and is built using Python. These options make the product more user-friendly. Concurrently, TensorFlow is also an open-source library for many other tasks as well.

power_scores

Prerequisites

Additionally, these frameworks also require Python to be installed. In case you do not have Python set up or any framework enabled on your server, simply follow the steps below to get started. 

Installing Python on CentOS

The first step when installing Python is to ensure our system is up to date. We will update our system using the yum package manager. Next, we will install the yum-utils. The yum-utils are a collection of tools and software that is needed for managing yum repositories, installing debug packages, and source packages.

[root@host ~]# yum update
[root@host ~]# yum install yum-utils

The second step would be to install the CentOS Software Collection (SCL). By enabling the SCL repository, we will get access to the most recent versions of the programming languages, and other assistance that is not available in the base repositories. SCL also allows us to install the latest versions of Python 3.x, in parallel with the current default Python v2.7.5 version, so the system tools like yum will continue to work as expected.

[root@host ~]# yum install centos-release-scl

We can easily confirm that Python is installed by running the following command. The version number will show after it completes with the default version of Python installed.

[root@host ~]# python --version 
Python 2.7.5

As we can see, the currently installed Python version is not at the latest version. So, we need to upgrade it by using the following command. 

[root@host ~]# yum install rh-python35

Next, install the main SCL package (its name is identical to the name of the Software Collection) and update Python update.

[root@host]# scl enable /etc/scl/conf/rh-python35 bash

We can then confirm the updated version by running this command

[root@host ]# python --version
Python 3.5.1

After the upgrade completes, and we have confirmed that Python is available on the server, we can move on to installing one of the frameworks.

Installing TensorFlow

Because TensorFlow requires the latest version of PIP (the Python package installer), we need to update it by running the next command. 

[root@host3 ~]# pip install --upgrade pip
You are using pip version 7.1.0, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 481kB/s 
Installing collected packages: pip
  Found existing installation: pip 7.1.0
    Uninstalling pip-7.1.0:
      Successfully uninstalled pip-7.1.0
Successfully installed pip-20.0.2

Next, we want to update our setup tools to prevent the following errors if the standard setup tools are used.

  • ERROR: markdown 3.2.1 has requirement setuptools>=36, but you’ll have setuptools 18.0.1 which is incompatible.
  • ERROR: tensorboard 2.1.1 has requirement setuptools>=41.0.0, but you’ll have setuptools 18.0.1 which is incompatible.
  • ERROR: google-auth 1.11.2 has requirement setuptools>=40.3.0, but you’ll have setuptools 18.0.1 which is incompatible.
pip install --upgrade setuptools
Collecting setuptools
  Downloading setuptools-46.0.0-py3-none-any.whl (582 kB)
     |████████████████████████████████| 582 kB 1.5 MB/s 
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 18.0.1
    Uninstalling setuptools-18.0.1:
      Successfully uninstalled setuptools-18.0.1
Successfully installed setuptools-46.0.0
[root@host conf]#

Next, we can move on to installing the current stable release of TensorFlow for CPU and GPU.

[root@host3 ~]# pip install tensorflow
Collecting tensorflow
  Downloading tensorflow-2.1.0-cp35-cp35m-manylinux2010_x86_64.whl (421.8 MB)
     |████████████████████████████████| 421.8 MB 15 kB/s 
Collecting keras-applications>=1.0.8
  Downloading Keras_Applications-1.0.8-py3-none-any.whl (50 kB)
     |████████████████████████████████| 50 kB 17.9 MB/s 
Collecting gast==0.2.2
  Downloading gast-0.2.2.tar.gz (10 kB)
Collecting opt-einsum>=2.3.2
  Downloading opt_einsum-3.2.0-py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 7.6 MB/s 
Collecting termcolor>=1.1.0
  Downloading termcolor-1.1.0.tar.gz (3.9 kB)
Collecting six>=1.12.0
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting numpy<2.0,>=1.16.0
  Downloading numpy-1.18.1-cp35-cp35m-manylinux1_x86_64.whl (19.9 MB)
     |████████████████████████████████| 19.9 MB 6.2 kB/s 
Collecting wheel>=0.26; python_version >= "3"
  Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Collecting protobuf>=3.8.0
  Downloading protobuf-3.11.3-cp35-cp35m-manylinux1_x86_64.whl (1.3 MB)
     |████████████████████████████████| 1.3 MB 76.8 MB/s 
Collecting tensorflow-estimator<2.2.0,>=2.1.0rc0
  Downloading tensorflow_estimator-2.1.0-py2.py3-none-any.whl (448 kB)
     |████████████████████████████████| 448 kB 77.3 MB/s 
Collecting google-pasta>=0.1.6
  Downloading google_pasta-0.1.8-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 18.0 MB/s 
Collecting wrapt>=1.11.1
  Downloading wrapt-1.12.1.tar.gz (27 kB)
Collecting grpcio>=1.8.6
  Downloading grpcio-1.27.2-cp35-cp35m-manylinux2010_x86_64.whl (2.7 MB)
     |████████████████████████████████| 2.7 MB 71.3 MB/s 
Collecting absl-py>=0.7.0
  Downloading absl-py-0.9.0.tar.gz (104 kB)
     |████████████████████████████████| 104 kB 88.2 MB/s 
Collecting scipy==1.4.1; python_version >= "3"
  Downloading scipy-1.4.1-cp35-cp35m-manylinux1_x86_64.whl (26.0 MB)
     |████████████████████████████████| 26.0 MB 69.9 MB/s 
Collecting keras-preprocessing>=1.1.0
  Downloading Keras_Preprocessing-1.1.0-py2.py3-none-any.whl (41 kB)
     |████████████████████████████████| 41 kB 651 kB/s 
Collecting tensorboard<2.2.0,>=2.1.0
  Downloading tensorboard-2.1.1-py3-none-any.whl (3.8 MB)
     |████████████████████████████████| 3.8 MB 79.3 MB/s 
Collecting astor>=0.6.0
  Downloading astor-0.8.1-py2.py3-none-any.whl (27 kB)
Collecting h5py
  Downloading h5py-2.10.0-cp35-cp35m-manylinux1_x86_64.whl (2.8 MB)
     |████████████████████████████████| 2.8 MB 76.5 MB/s 
Requirement already satisfied: setuptools in /opt/rh/rh-python35/root/usr/lib/python3.5/site-packages (from protobuf>=3.8.0->tensorflow) (18.0.1)
Collecting google-auth-oauthlib<0.5,>=0.4.1
  Downloading google_auth_oauthlib-0.4.1-py2.py3-none-any.whl (18 kB)
Collecting google-auth<2,>=1.6.3
  Downloading google_auth-1.11.2-py2.py3-none-any.whl (76 kB)
     |████████████████████████████████| 76 kB 19.0 MB/s 
Collecting werkzeug>=0.11.15
  Downloading Werkzeug-1.0.0-py2.py3-none-any.whl (298 kB)
     |████████████████████████████████| 298 kB 72.9 MB/s 
Collecting markdown>=2.6.8
  Downloading Markdown-3.2.1-py2.py3-none-any.whl (88 kB)
     |████████████████████████████████| 88 kB 26.9 MB/s 
Collecting requests<3,>=2.21.0
  Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 20.8 MB/s 
Collecting requests-oauthlib>=0.7.0
  Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 83.1 MB/s 
Collecting cachetools<5.0,>=2.0.0
  Downloading cachetools-4.0.0-py3-none-any.whl (10 kB)
Collecting rsa<4.1,>=3.1.4
  Downloading rsa-4.0-py2.py3-none-any.whl (38 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
     |████████████████████████████████| 125 kB 73.9 MB/s 
Collecting certifi>=2017.4.17
  Downloading certifi-2019.11.28-py2.py3-none-any.whl (156 kB)
     |████████████████████████████████| 156 kB 80.8 MB/s 
Collecting chardet<4,>=3.0.2
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 87.7 MB/s 
Collecting idna<3,>=2.5
  Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 20.3 MB/s 
Collecting oauthlib>=3.0.0
  Downloading oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 82.4 MB/s 
Collecting pyasn1<0.5.0,>=0.4.6
  Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 8.7 MB/s 
Installing collected packages: numpy, six, h5py, keras-applications, gast, opt-einsum, termcolor, wheel, protobuf, tensorflow-estimator, google-pasta, wrapt, grpcio, absl-py, scipy, keras-preprocessing, oauthlib, urllib3, certifi, chardet, idna, requests, requests-oauthlib, pyasn1, pyasn1-modules, cachetools, rsa, google-auth, google-auth-oauthlib, werkzeug, markdown, tensorboard, astor, tensorflow
    Running setup.py install for gast ... done
    Running setup.py install for termcolor ... done
    Running setup.py install for wrapt ... done
    Running setup.py install for absl-py ... done
Successfully installed absl-py-0.9.0 astor-0.8.1 cachetools-4.0.0 certifi-2019.11.28 chardet-3.0.4 gast-0.2.2 google-auth-1.11.2 google-auth-oauthlib-0.4.1 google-pasta-0.1.8 grpcio-1.27.2 h5py-2.10.0 idna-2.9 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.2.1 numpy-1.18.1 oauthlib-3.1.0 opt-einsum-3.2.0 protobuf-3.11.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.23.0 requests-oauthlib-1.3.0 rsa-4.0 scipy-1.4.1 six-1.14.0 tensorboard-2.1.1 tensorflow-2.1.0 tensorflow-estimator-2.1.0 termcolor-1.1.0 urllib3-1.25.8 werkzeug-1.0.0 wheel-0.34.2 wrapt-1.12.1
[root@host conf]# 

To confirm that our installation is successful, we can run one of the following two commands. 

[root@host conf]# pip show tensorflow
Name: tensorflow
Version: 2.1.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /opt/rh/rh-python35/root/usr/lib64/python3.5/site-packages
Requires: google-pasta, six, tensorflow-estimator, termcolor, wrapt, keras-applications, absl-py, keras-preprocessing, numpy, tensorboard, scipy, grpcio, astor, wheel, gast, protobuf, opt-einsum
Required-by: 
[root@host conf]#

or

[root@host ~]# pip list | grep tensorflow
tensorflow 2.1.0
tensorflow-estimator 2.1.0
[root@host conf]#

After the installation is complete, we can start creating our virtual environment. Instructions on how to configure this software is beyond the scope of this article, but it can be found in the official TensorFlow documentation. 

Installing Keras

Pip Install

There are two ways of installing Keras. The first is by using the Python PIP installer or by using a standard GitHub clone install. We will install Keras using the PIP installer since that is the one recommended.

[root@host ~]# pip install keras
Collecting keras
  Downloading Keras-2.3.1-py2.py3-none-any.whl (377 kB)
     |████████████████████████████████| 377 kB 1.5 MB/s 
Requirement already satisfied: scipy>=0.14 in /opt/rh/rh-python35/root/usr/lib64/python3.5/site-packages (from keras) (1.4.1)
Requirement already satisfied: h5py in /opt/rh/rh-python35/root/usr/lib64/python3.5/site-packages (from keras) (2.10.0)
Requirement already satisfied: six>=1.9.0 in /opt/rh/rh-python35/root/usr/lib/python3.5/site-packages (from keras) (1.14.0)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /opt/rh/rh-python35/root/usr/lib/python3.5/site-packages (from keras) (1.1.0)
Requirement already satisfied: numpy>=1.9.1 in /opt/rh/rh-python35/root/usr/lib64/python3.5/site-packages (from keras) (1.18.1)
Collecting pyyaml
  Downloading PyYAML-5.3.tar.gz (268 kB)
     |████████████████████████████████| 268 kB 6.4 MB/s 
Requirement already satisfied: keras-applications>=1.0.6 in /opt/rh/rh-python35/root/usr/lib/python3.5/site-packages (from keras) (1.0.8)
Building wheels for collected packages: pyyaml
  Building wheel for pyyaml (setup.py) ... done
  Created wheel for pyyaml: filename=PyYAML-5.3-cp35-cp35m-linux_x86_64.whl size=44228 sha256=60be7547ea2c16c5d689381cf523c6ce9bf7c03e55cc81a8dd9b830a16fc22cd
  Stored in directory: /root/.cache/pip/wheels/4d/28/ad/d9c3d2a22dbe17b0d7019bbe876af34feabf61a214973481e9
Successfully built pyyaml
Installing collected packages: pyyaml, keras
Successfully installed keras-2.3.1 pyyaml-5.3
[root@host conf]#

Again, we check the output of the version installed.

[root@host conf]# pip list | grep Keras
Keras                2.3.1     
Keras-Applications   1.0.8     
Keras-Preprocessing  1.1.0     
[root@host conf]#

Git Clone Install

Here is the alternative install method for Keras using the GitHub source. (This assumes you have Git installed and working.) First, clone Keras using the following git command.

[root@host ~]# git clone https://github.com/keras-team/keras.git

Then, cd into the Keras folder and run the installation command.

[root@host ~]# cd keras
[root@host ~]# python setup.py install

Because Keras uses TensorFlow as its main tensor manipulation library, it’s backend framework can be configured by using a Keras specific configuration file once we run Keras for the first time. The location of the file can be found here.

$HOME/.keras/keras.json 

The default configuration file will look similar to the following info. 

{
    “image_data_format”: “channels_last”,
    “epsilon”: 1e-07,
    “floatx”: “float32”,
    “backend”: “tensorflow”
}

When we modify the backend fields to “cntk,” “theano,” or “tensorflow,” Keras will utilize the new configuration settings the next time we run any of the updated Keras code.

Conclusion

Deep learning presents a new era in machine literacy which improves its current functionality. Frameworks like Keras and TensorFlow allow us to experiment with machine learning in a private environment, which brings the technology behind it much closer to home. 

While this article may seem like there are many configurations needed, Liquid Web is here to help. We can advise and offer a dependable safety-net using our wide array of Backup Storage & Cloud Server Backup options. This service provides around the clock protection for you and your client’s data. 

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these technologies today!

Author Bio

About the Author: Danny Jensen

I am a 29 years old Linux admin, techie and nature lover who loves solving puzzles. When I am not behind the keyboard you can find me in the woods but I will still probably be thinking about that server or that ticket I saw today.

Refer a friend and get hosting credit!